Files
POCloud-iOS/pocloud/Controller/MapViewController.swift
Patrick McDonagh 80f2241604 Adds firebase auth
2018-06-01 13:36:16 -05:00

134 lines
4.4 KiB
Swift

//
// MapViewViewController.swift
// pocloud
//
// Created by Patrick McDonagh on 5/22/18.
// Copyright © 2018 patrickjmcd. All rights reserved.
//
import UIKit
import Alamofire
import SwiftyJSON
import SVProgressHUD
import MapKit
import PromiseKit
import RealmSwift
class MapViewController: UIViewController, MKMapViewDelegate {
let baseURL = (UIApplication.shared.delegate as! AppDelegate).baseURL
let realm = try! Realm()
let locationManager = CLLocationManager()
let user = (UIApplication.shared.delegate as! AppDelegate).user
let appAuth = AppAuth()
var addresses : Results<Address>?
var gateways : Results<Gateway>?
var devices : Results<Device>?
var deviceTypes : Results<DeviceType>?
var selectedGateway : Gateway?
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
mapView.delegate = self
checkLocationAuthorizationStatus()
SVProgressHUD.show()
loadData()
firstly {
getDeviceTypes(baseURL: self.baseURL, authToken: (self.user?.authToken)!)
}.then { _ in
getAddresses(baseURL: self.baseURL, authToken: (self.user?.authToken)!)
}.then { _ in
getGateways(baseURL: self.baseURL, authToken: (self.user?.authToken)!)
}.then { _ in
getDevices(baseURL: self.baseURL, authToken: (self.user?.authToken)!)
}.done { _ in
self.showMarkersOnMap()
SVProgressHUD.dismiss()
}.catch { error in
print("Error in getting data in MapViewViewController: \(error)")
}
let buttonItem = MKUserTrackingBarButtonItem(mapView: mapView)
self.navigationItem.rightBarButtonItem = buttonItem
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Log Out", style: .plain, target: self, action: #selector(self.logOut))
}
@objc func logOut(){
appAuth.logOut()
navigationController?.popToRootViewController(animated: true)
}
func showMarkersOnMap() {
for gtw in gateways! {
let coord = CLLocationCoordinate2D(latitude: (gtw.address?.lat)!, longitude: (gtw.address?.long)!)
let gatewayAnnotation = GatewayAnnotation(coordinate: coord, title: gtw.name, subtitle: gtw.macAddress, gateway: gtw)
mapView.addAnnotation(gatewayAnnotation)
}
}
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 gatewayAnnotation = (view.annotation as! GatewayAnnotation)
selectedGateway = gatewayAnnotation.gateway
performSegue(withIdentifier: "openDetailView", sender: nil)
}
func checkLocationAuthorizationStatus() {
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
mapView.showsUserLocation = true
} else {
locationManager.requestWhenInUseAuthorization()
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "openDetailView" {
let destinationVC = segue.destination as! MapDetailViewController
destinationVC.gateway = selectedGateway
}
}
//MARK: - Realm Functions
func loadData() {
devices = realm.objects(Device.self)
deviceTypes = realm.objects(DeviceType.self)
gateways = realm.objects(Gateway.self)
addresses = realm.objects(Address.self)
}
}