Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.33 KB | None | 0 0
  1. import UIKit
  2. import RxSwift
  3. import RxCocoa
  4. import MapKit
  5. import CoreLocation
  6.  
  7. class HomeViewController: UIViewController, MKMapViewDelegate,
  8. CLLocationManagerDelegate{
  9.  
  10. let disposeBag = DisposeBag()
  11. let keyChainService = KeychainService()
  12.  
  13. @IBOutlet weak var hotelCollectionView: UICollectionView!
  14. @IBOutlet weak var backgroundImageView: UIImageView!
  15. @IBOutlet weak var shadow: UIView!
  16. @IBOutlet weak var searchBar: UIView!
  17. @IBOutlet weak var citySearch: UITextField!
  18. let hotelRepo = HotelsRepo()
  19. @IBOutlet weak var map: MKMapView!
  20. var locationManager: CLLocationManager!
  21.  
  22.  
  23. var homeViewModel: HomeViewModel!
  24. var user : User?
  25.  
  26.  
  27. override func viewDidLoad() {
  28. super.viewDidLoad()
  29. homeViewModel = HomeViewModel()
  30. map.delegate = self
  31. getCurrentLocation()
  32. createBindings()
  33. createObservers()
  34. initOverlay()
  35. hotelCollectionView.register(UINib(nibName:
  36. "AlbumsCollectionViewCell", bundle: nil),
  37. forCellWithReuseIdentifier: String(describing:
  38. AlbumsCollectionViewCell.self))
  39.  
  40.  
  41.  
  42. let gestureRecognizer = UITapGestureRecognizer(target:self,
  43. action: #selector(handleTap(gestureReconizer:)))
  44. map.addGestureRecognizer(gestureRecognizer)
  45. }
  46.  
  47. @objc func handleTap(gestureReconizer:
  48. UILongPressGestureRecognizer) {
  49.  
  50. let location = gestureReconizer.location(in: map)
  51. let coordinate = map.convert(location,toCoordinateFrom:
  52. map)
  53. print("coord: (coordinate)")
  54.  
  55. }
  56.  
  57. override func viewDidDisappear(_ animated: Bool) {
  58. print("did")
  59. }
  60.  
  61. func createBindings(){
  62. homeViewModel.context = self
  63. homeViewModel
  64. .hotels
  65. .observeOn(MainScheduler.instance)
  66. .bind(to: hotelCollectionView.rx.items(cellIdentifier:
  67. "AlbumsCollectionViewCell", cellType:
  68. AlbumsCollectionViewCell.self)) { (row,hotel,cell) in
  69. cell.cellHotel = hotel
  70. self.homeViewModel.selectedHotel = hotel
  71. cell.visitButton.rx.tap
  72. .bind(onNext: self.homeViewModel.hotelAction)
  73. .disposed(by: cell.disposeBag)
  74. }.disposed(by: disposeBag)
  75.  
  76.  
  77. hotelCollectionView.rx
  78. .modelSelected(Hotel.self)
  79. .subscribe(onNext: {
  80. hotel in
  81. self.homeViewModel.selectedHotel = hotel
  82. })
  83. .disposed(by: disposeBag)
  84. hotelCollectionView.rx
  85. .itemSelected
  86. .subscribe(onNext:{ indexPath in
  87. let cell = self.hotelCollectionView.cellForItem(at:
  88. indexPath) as! AlbumsCollectionViewCell
  89. if cell.selection == 0 {
  90. let otherCells =
  91. self.hotelCollectionView.indexPathsForVisibleItems
  92. otherCells.forEach { index in
  93. if (index == indexPath) {
  94. cell.selection = 1
  95. }else {
  96. let cell =
  97. self.hotelCollectionView.cellForItem(at: index) as!
  98. AlbumsCollectionViewCell
  99. cell.selection = 0
  100. }
  101. }
  102. }else{
  103. print(cell.cellHotel.name!)
  104. cell.selection = 0
  105. }
  106. }).disposed(by: disposeBag)
  107.  
  108. }
  109.  
  110. func createObservers(){
  111.  
  112. homeViewModel
  113. .hotels
  114. .observeOn(MainScheduler.instance)
  115. .subscribe(onNext: { (hotels) in
  116. let annotations = hotels.map({ (hotel) ->
  117. MKPointAnnotation in
  118. let pointAnnotation = MKPointAnnotation()
  119. if let address = hotel.address, let latitude =
  120. address.latitude,
  121. let longitude = address.longitude {
  122. if let lat = Double(latitude), let long =
  123. Double(longitude){
  124.  
  125. pointAnnotation.coordinate =
  126. CLLocationCoordinate2D(latitude: lat, longitude: long)
  127. pointAnnotation.title = hotel.name
  128. }
  129. }
  130. return pointAnnotation
  131. })
  132. self.map.addAnnotations(annotations)
  133. })
  134. .disposed(by: disposeBag)
  135.  
  136. homeViewModel
  137. .error
  138. .observeOn(MainScheduler.instance)
  139. .subscribe(onNext: { (error) in
  140. self.displayMessage(userMessage: error)
  141. })
  142. .disposed(by: disposeBag)
  143.  
  144. homeViewModel.loading.observeOn(MainScheduler.instance)
  145. .subscribe(onNext: { (state) in
  146. switch state {
  147. case true:
  148. self.showLoading("Loading hotels")
  149. case false:
  150. self.hideLoading()
  151. }
  152. })
  153. .disposed(by: disposeBag)
  154.  
  155. hotelCollectionView.rx.willDisplayCell
  156. .subscribe(onNext: ({ (cell,indexPath) in
  157. cell.alpha = 0
  158. let transform =
  159. CATransform3DTranslate(CATransform3DIdentity, -250, 0, 0)
  160. cell.layer.transform = transform
  161. UIView.animate(withDuration: 1, delay: 0,
  162. usingSpringWithDamping: 0.7, initialSpringVelocity:
  163. 0.5, options: .curveEaseOut, animations: {
  164. cell.alpha = 1
  165. cell.layer.transform = CATransform3DIdentity
  166. }, completion: nil)
  167. })).disposed(by: disposeBag)
  168. }
  169.  
  170.  
  171. func getCurrentLocation() {
  172. if (CLLocationManager.locationServicesEnabled()) {
  173. if locationManager == nil {
  174. locationManager = CLLocationManager()
  175. }
  176. locationManager?.requestWhenInUseAuthorization()
  177. locationManager.delegate = self
  178. locationManager.desiredAccuracy = kCLLocationAccuracyBest
  179. locationManager.requestAlwaysAuthorization()
  180. locationManager.startUpdatingLocation()
  181. }
  182. }
  183.  
  184. @IBAction func primaryAction(_ sender: Any) {
  185. let address = citySearch.text!
  186. self.homeViewModel.getCoordinateFrom(address: address) {
  187. coordinate, error in
  188. guard let coordinate = coordinate, error == nil else {
  189. self.homeViewModel.requestData(city: address); return }
  190. let location = CLLocationCoordinate2D(latitude:
  191. coordinate.latitude
  192. , longitude: coordinate.longitude)
  193. let span = MKCoordinateSpan(latitudeDelta: 1,
  194. longitudeDelta: 1)
  195. let region = MKCoordinateRegion(center: location,
  196. span: span)
  197. self.map.setRegion(region, animated: true)
  198. self.homeViewModel.requestData(city: address)
  199. }
  200. }
  201.  
  202.  
  203. extension HomeViewController {
  204. func locationManager(_ manager: CLLocationManager,
  205. didUpdateLocations locations: [CLLocation]) {
  206. self.homeViewModel.updateLocation(locations, updateMap: {
  207. (region) -> () in
  208. self.map.setRegion(region, animated: true)
  209. self.locationManager.stopUpdatingLocation()
  210. })
  211. }
  212.  
  213. func locationManager(_ manager: CLLocationManager,
  214. didFailWithError error: Error) {
  215. print("location error")
  216. }
  217. }
  218.  
  219. // map delegate functions
  220. extension HomeViewController {
  221. func mapView(_ mapView: MKMapView, viewFor annotation:
  222. MKAnnotation) -> MKAnnotationView? {
  223.  
  224. return self.homeViewModel.viewForAnnotation(annotation)
  225. }
  226.  
  227. func mapView(_ mapView: MKMapView, annotationView view:
  228. MKAnnotationView, calloutAccessoryControlTapped control:
  229. UIControl) {
  230. self.homeViewModel.annotationView(view)
  231. }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement