// // DetailViewController.swift // pocloud // // Created by Patrick McDonagh on 5/22/18. // Copyright © 2018 patrickjmcd. All rights reserved. // import UIKit import MapKit import Alamofire import SwiftyJSON import RealmSwift import Kingfisher import SVProgressHUD class MapDetailViewController: UIViewController, MKMapViewDelegate, UITableViewDataSource, UITableViewDelegate { let baseURL = (UIApplication.shared.delegate as! AppDelegate).baseURL let realm = try! Realm() let user = (UIApplication.shared.delegate as! AppDelegate).user var gateway: Gateway? var devices : Results? var selectedDevice : Device? @IBOutlet weak var nameLabel: UILabel! @IBOutlet weak var mapView: MKMapView! @IBOutlet weak var detailTableView: UITableView! override func viewDidLoad() { super.viewDidLoad() mapView.delegate = self detailTableView.delegate = self detailTableView.dataSource = self nameLabel.text = gateway?.name addMapDot() getDevicesAtGateway() } func addMapDot(){ let coord = CLLocationCoordinate2D(latitude: (gateway?.address!.lat)!, longitude: (gateway?.address!.long)!) let gatewayAnnotation = GatewayAnnotation(coordinate: coord, title: (gateway?.name)!, subtitle: (gateway?.macAddress)!, gateway: gateway) mapView.addAnnotation(gatewayAnnotation) // let initialLocation = CLLocation(latitude: gateway!.address!.lat, longitude: gateway!.address!.long) let regionRadius: CLLocationDistance = 1000 let coordinateRegion = MKCoordinateRegionMakeWithDistance(coord, regionRadius, regionRadius) mapView.setRegion(coordinateRegion, animated: true) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return devices?.count ?? 0 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "dataViewCell", for: indexPath) as! MapDetailDeviceCell cell.deviceTypeLabel?.text = devices?[indexPath.row].vanityName ?? "Error" let imgUrl = URL(string: (devices?[indexPath.row].parentDeviceType.first?.imgUrl)!) cell.deviceTypeLogo.kf.setImage(with: imgUrl) cell.accessoryType = .disclosureIndicator return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { selectedDevice = devices?[indexPath.row] switch (selectedDevice?.parentDeviceType.first?.name){ case "advvfdipp": performSegue(withIdentifier: "openMaxWaterSystem", sender: self) default: performSegue(withIdentifier: "openDeviceDetailView", sender: self) } } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 60.0 } func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { let identifier = "marker" if annotation.isKind(of: GatewayAnnotation.self) { var view: MKMarkerAnnotationView if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKMarkerAnnotationView { dequeuedView.annotation = annotation view = dequeuedView } else { view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier) view.canShowCallout = true view.calloutOffset = CGPoint(x: -5, y: 5) view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) } return view } else { return nil } } func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { let location = view.annotation as! GatewayAnnotation let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving] location.mapItem().openInMaps(launchOptions: launchOptions) } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "openDeviceDetailView" { let destinationVC = segue.destination as! DeviceDetailViewController destinationVC.thisDevice = selectedDevice! } if segue.identifier == "openMaxWaterSystem" { let destinationVC = segue.destination as! MaxWaterSystemViewController destinationVC.thisDevice = selectedDevice! } } func getDevicesAtGateway(){ SVProgressHUD.show() devices = realm.objects(Device.self).filter("gatewayId == %d", (gateway?.id)!) detailTableView.reloadData() SVProgressHUD.dismiss() } }