IHCountryPicker.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. //
  2. // IHCountryPicker.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/3/13.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import IBAnimatable
  10. import PKHUD
  11. class IHCountryPicker: AnimatableModalViewController {
  12. var isClickBuild:Bool = false //判断是否已经选择了楼房 这个很关键
  13. var buildArr:[(hotelName: String,
  14. hotelId:String,
  15. buildList:[DropDownData],
  16. isExpand:Bool)]? = [(String,String,[DropDownData],Bool)]()
  17. var cityList: [DropDownData]?{
  18. didSet{
  19. if let list = self.cityList {
  20. if list.count > 0 {
  21. self.tableView.reloadSections([0], with: .automatic)
  22. self.buildArr?.removeAll() //重新请求
  23. //获取第一个城市
  24. let firstCity = list.first
  25. getHotelList(firstCity?.id, city: firstCity)
  26. Intermediate.cityId = firstCity!.id!
  27. Intermediate.cityName = firstCity!.name!
  28. }
  29. }
  30. }
  31. }
  32. var historyCity:DropDownData?//保存搜索过的城市
  33. var historyHotel:DropDownData? //保存搜索过的酒店
  34. var completion:(()->Void)?
  35. //用来存储这五个值
  36. var selectedBuid:DropDownData?
  37. var selectedHotel:(hotelName: String,
  38. hotelId:String,
  39. buildList:[DropDownData],
  40. isExpand:Bool)?
  41. var cityId:String?{
  42. didSet{ //如果没有则用provinceId province也没有就用countryId
  43. if let cityId = self.cityId {
  44. getHotelList(cityId, city: nil)
  45. }
  46. }
  47. }
  48. @IBOutlet weak var contentView: UIView!
  49. lazy var tableView: UITableView = {
  50. let tableView = UITableView(frame: CGRect.zero, style: .plain)
  51. tableView.delegate = self
  52. tableView.dataSource = self
  53. tableView.backgroundColor = .white
  54. tableView.separatorStyle = .none
  55. tableView.register(UINib(nibName: "IHTPickerAreaCell", bundle: nil), forCellReuseIdentifier: "areaCell")
  56. tableView.register(UINib(nibName: "IHPickFloorCell", bundle: nil), forCellReuseIdentifier: "floorCell")
  57. return tableView
  58. }()
  59. override func viewDidLoad() {
  60. super.viewDidLoad()
  61. //buildArr?.removeAll()
  62. self.contentView.addSubview(tableView)
  63. }
  64. @IBAction func close(_ sender: Any) {
  65. self.dismiss(animated: true, completion: nil)
  66. }
  67. @IBAction func save(_ sender: Any) {
  68. if self.selectedBuid == nil || isClickBuild == false {
  69. //Please selected building
  70. g_showHUD("请选择区域")
  71. return
  72. }
  73. if self.selectedHotel == nil {
  74. //Please selecte hotel
  75. g_showHUD("请选择酒店/学校")
  76. return
  77. }
  78. log.debug("-- 选择了 - \(self.selectedBuid?.id) -buildname-- \(self.selectedBuid?.name)")
  79. // let hotel = IHHistoryHotelModel(id: selectedHotel!.hotelId, name: selectedHotel!.hotelName)
  80. let hotel = IHHistoryHotelModel(id: selectedHotel!.hotelId, name: selectedHotel!.hotelName, countryId: Intermediate.countryId, countryName: Intermediate.countryName, provinceId: Intermediate.provinceId, provinceName: Intermediate.provinceName, cityId: Intermediate.cityId, cityName: Intermediate.cityName, buildId: Intermediate.buildId, buildName: Intermediate.buildName)
  81. IHHistroyPlist.share.addHotel(hotel)
  82. Intermediate.hotelId = selectedHotel!.hotelId
  83. Intermediate.hotelName = selectedHotel!.hotelName
  84. Intermediate.buildId = selectedBuid!.id!
  85. Intermediate.buildName = selectedBuid!.name!
  86. let info = StorageInfo(countryId: Intermediate.countryId, countryName: Intermediate.countryName, provinceId: Intermediate.provinceId, provinceName: Intermediate.provinceName, cityId: Intermediate.cityId, cityName: Intermediate.cityName, hotelId: Intermediate.hotelId, hotelName: Intermediate.hotelName, buildId: Intermediate.buildId, buildName: Intermediate.buildName)
  87. IHHotelStorage.shareInstance.storageHotelInfo(info)
  88. NotificationCenter.default.post(name: NSNotification.Name(kNotificationIHAddressControllerReloadAddress), object: nil)
  89. NotificationCenter.default.post(name: NSNotification.Name(kNotifactionIHTabBarControllerUpdateBadgeNumber), object: Intermediate.hotelId)
  90. self.dismiss(animated: true, completion: nil)
  91. if let block = self.completion {
  92. NotificationCenter.default.post(name: NSNotification.Name(kNotifactionIHHotelPickerViewGetHistory), object: nil)
  93. block()
  94. isClickBuild = false
  95. }
  96. }
  97. override func viewDidLayoutSubviews() {
  98. super.viewDidLayoutSubviews()
  99. tableView.frame = self.contentView.bounds
  100. }
  101. }
  102. extension IHCountryPicker:UITableViewDelegate,UITableViewDataSource{
  103. func numberOfSections(in tableView: UITableView) -> Int {
  104. return 2
  105. }
  106. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  107. if section == 0 {
  108. return 1
  109. }else{
  110. return self.buildArr?.count ?? 0
  111. }
  112. }
  113. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  114. if indexPath.section == 0 {
  115. return culAreaCellHeight(cityList?.count ?? 0)
  116. }else{
  117. let build = self.buildArr![indexPath.row]
  118. if build.isExpand == true{
  119. return culAreaCellHeight(build.buildList.count)
  120. }
  121. return 40
  122. }
  123. }
  124. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  125. if section == 0 {
  126. return 0
  127. }else{
  128. return 40
  129. }
  130. }
  131. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  132. if section == 1 {
  133. let header = UIView()
  134. header.backgroundColor = .white
  135. let label = UILabel(frame: CGRect(x: 20, y: 0, width: KSCREENWIDTH - 40, height: 40))
  136. label.font = UIFont(name: Alibaba_PuHuiTi_Medium, size: 14)
  137. label.textColor = UIColor(hexString: "#333333")
  138. label.text = "列表"
  139. header.addSubview(label)
  140. return header
  141. }else{
  142. return UIView()
  143. }
  144. }
  145. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  146. if indexPath.section == 0 {
  147. let cell = tableView.dequeueReusableCell(withIdentifier: "areaCell") as! IHTPickerAreaCell
  148. cell.selectionStyle = .none
  149. cell.cityList = self.cityList
  150. cell.cityCallback = { [unowned self] (city) in
  151. self.buildArr?.removeAll()
  152. self.getHotelList(city.id,city: city)
  153. }
  154. return cell
  155. }else{
  156. let build = self.buildArr![indexPath.row]
  157. if build.isExpand == false {
  158. var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
  159. if cell == nil {
  160. cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
  161. }
  162. cell?.selectionStyle = .none
  163. cell?.textLabel?.text = build.hotelName
  164. cell?.textLabel?.font = UIFont(name: PingFangSC_Regular, size: 13)
  165. cell?.textLabel?.textColor = UIColor(hexString: "#333333")
  166. return cell!
  167. }else{
  168. let cell = tableView.dequeueReusableCell(withIdentifier: "floorCell") as! IHPickFloorCell
  169. cell.selectionStyle = .none
  170. let build = self.buildArr?[indexPath.row]
  171. cell.buildList = build?.buildList
  172. cell.completion = {[unowned self] (build) in
  173. // log.debug("self.buildArr - \(self.buildArr)")
  174. if self.buildArr?.count == 0 {
  175. g_showHUD("重试") //出现数组为空的情况 表示没请求完
  176. }else{
  177. self.selectedBuid = build
  178. self.selectedHotel = self.buildArr![indexPath.row - 1]//前一组
  179. }
  180. }
  181. return cell
  182. }
  183. }
  184. }
  185. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  186. if indexPath.section == 1 {
  187. let build = self.buildArr![indexPath.row]
  188. //只有关闭的才可以点击
  189. if build.isExpand == false { //点击展开
  190. log.debug("点击展开")
  191. self.isClickBuild = true
  192. self.selectedHotel = build
  193. if build.buildList.count > 1 {
  194. self.selectedBuid = build.buildList[1]
  195. }else{
  196. self.selectedBuid = build.buildList.first
  197. }
  198. var newBuild = build
  199. //这里要注意是不是最后一组,最后一组 注意越界问题
  200. //判断下一行是否是可以展开的,如果是可以站看的,就可以添加,否则 删除
  201. if indexPath.row < self.buildArr!.count - 1 {//判断是不是最后一组
  202. let nextBuild = self.buildArr![indexPath.row + 1]
  203. if nextBuild.isExpand == false{
  204. newBuild.isExpand = true
  205. self.buildArr?.insert(newBuild, at: indexPath.row + 1)//插入下一行
  206. removeOtherExpand(indexPath.row + 1)
  207. self.tableView.reloadSections([1], with: .fade)
  208. }else{
  209. self.buildArr?.remove(at: indexPath.row + 1)
  210. self.tableView.reloadSections([1], with: .fade)
  211. }
  212. }else{
  213. let nextbuild = self.buildArr![indexPath.row]//处理最后一组
  214. if nextbuild.isExpand == false {
  215. newBuild.isExpand = true
  216. self.buildArr?.insert(newBuild, at: indexPath.row + 1)//插入下一行
  217. removeOtherExpand(indexPath.row + 1)
  218. self.tableView.reloadSections([1], with: .fade)
  219. }
  220. }
  221. }
  222. }
  223. }
  224. }
  225. extension IHCountryPicker{
  226. //删除其他展开数组
  227. func removeOtherExpand(_ index:Int) {
  228. var expandArr :[Int] = [Int]()//搜集展开的数组
  229. ///获取么展开情况
  230. for i in 0..<(self.buildArr?.count)! {
  231. let build = self.buildArr![i]
  232. if build.isExpand == true{
  233. expandArr.append(i)//添加数组里边
  234. }
  235. }
  236. //删除其他内容
  237. if expandArr.count > 1 {
  238. for j in expandArr {
  239. if j != index {
  240. self.buildArr?.remove(at: j)
  241. }
  242. }
  243. }
  244. }
  245. //计算区域的行高
  246. func culAreaCellHeight(_ cityCount: Int) ->CGFloat {
  247. if cityCount == 0 {
  248. return 0
  249. }else{
  250. //计算行数
  251. var row : Int = cityCount / 4
  252. if row > 5 { //大于5行 只取5行
  253. row = 5
  254. }
  255. let contentViewHight = 30 * (row + 1) + 10 + 10 + 10 * row
  256. return CGFloat(contentViewHight)
  257. }
  258. }
  259. //计算楼层的行高
  260. func culFloorCellHeight(_ cityCount: Int) ->CGFloat {
  261. if cityCount == 0 {
  262. return 0
  263. }else{
  264. //计算行数
  265. let row : Int = cityCount / 4
  266. let contentViewHight = 30 * (row + 1) + 10 + 10 + 10 * row
  267. return CGFloat(contentViewHight)
  268. }
  269. }
  270. //获取酒店
  271. //id 酒店id或者省份id
  272. //city 有city则保存起来起来
  273. func getHotelList(_ id:String?,city:DropDownData?) {
  274. // self.historyCity = city
  275. HUD.show(.progress)
  276. HUD.hide(afterDelay: 5)
  277. IHAddressService.share.getHotelWithBuildlist(id, requestSuccess: { (hotelList) in
  278. HUD.hide()
  279. self.buildArr?.removeAll()
  280. if hotelList.count > 0{
  281. for hotel in hotelList{
  282. if let buildList = hotel.buildList {
  283. // let allBuild = DropDownData()
  284. // allBuild.id = ""
  285. // allBuild.name = "All"
  286. var newlist = [DropDownData]()
  287. //newlist.append(allBuild)
  288. if buildList.count > 0{
  289. for build in buildList {
  290. newlist.append(build)
  291. }
  292. }
  293. self.buildArr?.append((hotel.name!,hotel.id!,newlist,false))
  294. self.tableView.reloadSections([1], with: .automatic)
  295. // log.debug("检查楼房数组 -- \(self.buildArr)")
  296. }
  297. }
  298. }
  299. }) {
  300. }
  301. // IHAddressService.share.getHotelList(id, requestSuccess: { (hotelList) in
  302. // self.buildArr?.removeAll()
  303. // if hotelList.count > 0{
  304. // let queue = DispatchQueue(label: "com.welcome.Inhealth.IhCountryPiker")
  305. // for hotel in hotelList{
  306. // queue.async {
  307. // self.getBuildList(hotel.id!) { (buildlist) in
  308. // HUD.hide()
  309. // self.buildArr?.append((hotel.name!,hotel.id!,buildlist,false))
  310. // self.tableView.reloadSections([1], with: .automatic)
  311. // }
  312. // }
  313. // }
  314. // }
  315. //
  316. // }) { }
  317. }
  318. //获取楼房
  319. func getBuildList(_ hotelId :String,completion: @escaping (([DropDownData])->Void)) {
  320. IHAreaService.share.getBuildNavData(hotelId: hotelId, requestSuccess: { (buildList) in
  321. let allBuild = DropDownData()
  322. allBuild.id = ""
  323. allBuild.name = "All"
  324. var newlist = [DropDownData]()
  325. newlist.append(allBuild)
  326. if buildList.count == 0{
  327. completion(newlist)
  328. }else{
  329. for build in buildList {
  330. newlist.append(build)
  331. }
  332. completion(newlist)
  333. }
  334. }) {
  335. }
  336. }
  337. }