IHHotelPickerManager.swift 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //
  2. // IHHotelPickerManager.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/3/11.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. class IHHotelPickerManager: NSObject ,IHViewManagerProtocolDelegate{
  10. var provincelist:[DropDownData]?
  11. /// 懒加载 创建
  12. lazy var hotelPicker: IHCountryPicker = {
  13. let picker = IHCountryPicker()
  14. picker.modalSize = (width: .full, height: .custom(size: 500))
  15. picker.modalPosition = .bottomCenter
  16. picker.cornerRadius = 32
  17. /// 该回调方法可以在本类任意地方写
  18. // picker.selectedAreaCompleted = { [weak self] (province, city, district) in
  19. // self?.addressLabel.text = province + " " + city + " " + district
  20. // }
  21. return picker
  22. }()
  23. //创建主view容器
  24. lazy var mainView: IHHotelPickerView = {
  25. let mainView = IHHotelPickerView()
  26. mainView.delegate = self
  27. return mainView
  28. }()
  29. private var searchFiled:UITextField!
  30. //私有变量,传递进来的绑定控制器
  31. private weak var vc : UIViewController?
  32. override init() {
  33. super.init()
  34. notificationRegister()
  35. }
  36. //绑定控制器,主要是 绑定viewdidload这个函数
  37. func bindController(_ vc: UIViewController) {
  38. self.vc = vc
  39. createUI()
  40. requestData()
  41. }
  42. //布局
  43. func createUI() {
  44. mainView.frame = CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT - KNavBarHeight )
  45. mainView.hotelName = Intermediate.hotelName
  46. mainView.countryName = Intermediate.countryName
  47. mainView.countryId = Intermediate.countryId
  48. self.getProvinceList(Intermediate.countryId)
  49. getCountriesList()
  50. self.vc?.view.addSubview(mainView)
  51. setNavgationBar()
  52. }
  53. @objc func tapMainView() {
  54. searchFiled.resignFirstResponder()
  55. }
  56. //请求数据
  57. func requestData() {
  58. }
  59. deinit {
  60. NotificationCenter.default.removeObserver(self)
  61. }
  62. func notificationRegister() {
  63. }
  64. @objc func itemNotify(_ notify:Notification) {
  65. }
  66. //获取国家列表
  67. func getCountriesList() {
  68. IHAddressService.share.getContryList(requestSuccess: { (countries) in
  69. self.mainView.countryList = countries
  70. }) {
  71. }
  72. }
  73. //获取省份列表
  74. func getProvinceList(_ countryId:String) {
  75. IHAddressService.share.getProvinceList(countryId, requestSuccess: { (provinces) in
  76. self.mainView.provicelist = provinces
  77. self.provincelist = provinces
  78. }) {
  79. }
  80. }
  81. func setNavgationBar() {
  82. let searchBtn = UIBarButtonItem(image: UIImage(named: "搜索"), style: .done, target: self, action: #selector(serachAction))
  83. self.vc?.navigationItem.rightBarButtonItem = searchBtn
  84. searchFiled = UITextField(frame: CGRect(x: 0, y: 0, width: 300, height: 44))
  85. searchFiled.placeholder = "请输入省份名"
  86. searchFiled.font = UIFont(name: Alibaba_PuHuiTi_Regular, size: 14)
  87. searchFiled.textColor = UIColor(hexString: "#333333")
  88. searchFiled.delegate = self
  89. self.vc?.navigationItem.titleView = searchFiled
  90. }
  91. @objc func serachAction(){
  92. //
  93. self.mainView.searchText = searchFiled.text
  94. searchFiled.resignFirstResponder()//注销第一响应者
  95. }
  96. }
  97. extension IHHotelPickerManager:IHHotelPickerViewDelegate{
  98. func headerPickerHistroyHotel(_ hotel: DropDownData) {
  99. let picker = IHHistroyHotelPicker()
  100. picker.modalSize = (width: .full, height: .custom(size: 300))
  101. picker.modalPosition = .bottomCenter
  102. picker.cornerRadius = 32
  103. /// 该回调方法可以在本类任意地方写
  104. // picker.selectedAreaCompleted = { [weak self] (province, city, district) in
  105. // self?.addressLabel.text = province + " " + city + " " + district
  106. // }
  107. picker.hotel = hotel
  108. picker.completion = {
  109. self.vc?.navigationController?.popViewController(animated: false)
  110. }
  111. self.vc?.present(picker, animated: true, completion: nil)
  112. }
  113. func headerPikcerCountry(_ countryId: String) {
  114. self.getProvinceList(countryId)
  115. }
  116. func headerPickHotel() {
  117. IHAreaService.share.getBuildNavData(hotelId: Intermediate.hotelId, requestSuccess: { (buildList) in
  118. //添加一个all
  119. let allBuild = DropDownData()
  120. allBuild.id = ""
  121. allBuild.name = "所有"
  122. var newlist = [DropDownData]()
  123. newlist.append(allBuild)
  124. for build in buildList{
  125. newlist.append(build)
  126. }
  127. let picker = IHFloorPicker()
  128. let pickerHeight = self.culPickerViewHeight(newlist.count)
  129. picker.modalSize = (width: .full, height: .custom(size: pickerHeight))
  130. picker.modalPosition = .bottomCenter
  131. picker.cornerRadius = 32
  132. picker.dismissOnTap = false
  133. self.vc?.present(picker, animated: true, completion: nil)
  134. picker.buildList = newlist
  135. picker.hotelName = Intermediate.hotelName
  136. picker.completion = {(floor) in
  137. self.vc?.navigationController?.popViewController(animated: false)
  138. NotificationCenter.default.post(name: NSNotification.Name(kNotificationIHAddressControllerReloadAddress), object: nil)
  139. }
  140. }) {
  141. }
  142. }
  143. func selectedProvince(_ provinceId: String) {
  144. IHAddressService.share.getCityeList(provinceId, requestSuccess: { (cityList) in
  145. let picker = IHCountryPicker()
  146. picker.modalSize = (width: .full, height: .custom(size: 500))
  147. picker.modalPosition = .bottomCenter
  148. picker.cornerRadius = 32
  149. if cityList?.count == 0{
  150. picker.cityId = provinceId
  151. }
  152. picker.cityList = cityList
  153. picker.completion = {
  154. self.vc?.navigationController?.popViewController(animated: false)
  155. }
  156. self.vc?.present(picker, animated: true, completion: nil)
  157. }) {
  158. }
  159. }
  160. func selectedHotel(_ indexPath: IndexPath) {
  161. }
  162. func culPickerViewHeight(_ floorCount: Int) ->Float {
  163. if floorCount == 0 {
  164. return 76 + 117
  165. }else{
  166. //计算行数
  167. let row : Int = floorCount / 4
  168. let contentViewHight = 30 * (row + 1) + 10 + 10 + 10 * row
  169. return 76 + 117 + Float(contentViewHight)
  170. }
  171. }
  172. }
  173. extension IHHotelPickerManager:UITextFieldDelegate{
  174. func textFieldDidBeginEditing(_ textField: UITextField) {
  175. //开始编辑
  176. }
  177. func textFieldDidEndEditing(_ textField: UITextField) {
  178. //结束编辑
  179. }
  180. func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
  181. let currentText = searchFiled.text ?? ""
  182. let newText = (currentText as NSString).replacingCharacters(in: range, with: string)
  183. self.mainView.searchText = newText
  184. log.debug("搜索内容 - \(newText)")
  185. if newText == "" {
  186. }
  187. return true
  188. }
  189. }
  190. extension UILocalizedIndexedCollation{
  191. func partitionObjects(array:[AnyObject], collationStringSelector:Selector) -> ([AnyObject], [String]) {
  192. var unsortedSections = [[AnyObject]]()
  193. //1. Create a array to hold the data for each section
  194. for _ in self.sectionTitles {
  195. unsortedSections.append([]) //appending an empty array
  196. }
  197. //2. Put each objects into a section
  198. for item in array {
  199. let index:Int = self.section(for: item, collationStringSelector:collationStringSelector)
  200. unsortedSections[index].append(item)
  201. }
  202. //3. sorting the array of each sections
  203. var sectionTitles = [String]()
  204. var sections = [AnyObject]()
  205. for index in 0 ..< unsortedSections.count { if unsortedSections[index].count > 0 {
  206. sectionTitles.append(self.sectionTitles[index])
  207. sections.append(self.sortedArray(from: unsortedSections[index], collationStringSelector: collationStringSelector) as AnyObject)
  208. }
  209. }
  210. return (sections, sectionTitles)
  211. }
  212. }