Files
POCloud-iOS/pocloud/Controller/CompanyDetailViewController.swift
2018-06-11 10:21:03 -05:00

103 lines
3.6 KiB
Swift

//
// CompanyViewController.swift
// pocloud
//
// Created by Patrick McDonagh on 6/6/18.
// Copyright © 2018 patrickjmcd. All rights reserved.
//
import UIKit
import MapKit
class CompanyDetailViewController: UIViewController, MKMapViewDelegate, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var tableView: UITableView!
var thisCompany : Company?
var selectedGateway : Gateway?
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
mapView.delegate = self
title = thisCompany?.name
addMapDot()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let company = thisCompany{
return company.gateways.count
} else {
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "companyGatewayCell", for: indexPath)
cell.textLabel?.text = thisCompany?.gateways[indexPath.row].name
return cell
}
func addMapDot(){
let coord = CLLocationCoordinate2D(latitude: (thisCompany?.address!.lat)!, longitude: (thisCompany?.address!.long)!)
let gatewayAnnotation = GatewayAnnotation(coordinate: coord, title: (thisCompany?.name)!, subtitle: (thisCompany?.address?.streetAddress)!, gateway: nil)
mapView.addAnnotation(gatewayAnnotation)
let regionRadius: CLLocationDistance = 1000
let coordinateRegion = MKCoordinateRegionMakeWithDistance(coord, regionRadius, regionRadius)
mapView.setRegion(coordinateRegion, animated: true)
}
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 tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedGateway = thisCompany?.gateways[indexPath.row]
performSegue(withIdentifier: "openGatewayDetail", sender: self)
}
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 == "openGatewayDetail" {
let destinationVC = segue.destination as! MapDetailViewController
destinationVC.gateway = selectedGateway
}
}
}