/ Caffinator / ViewController.swift
ViewController.swift
  1  //
  2  //  ViewController.swift
  3  //  Caffinator
  4  //
  5  //  Created by Luigi Mangione on 3/16/17.
  6  //  Copyright © 2017 AppRoar Studios. All rights reserved.
  7  //
  8  
  9  import UIKit
 10  import CoreLocation
 11  import MapKit
 12  
 13  class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
 14  
 15      @IBOutlet weak var mapViewHeight: NSLayoutConstraint!
 16      
 17      @IBOutlet weak var locationLabel: UILabel!
 18      @IBOutlet weak var infoLabel: UILabel!
 19      @IBOutlet weak var infoSegment: UISegmentedControl!
 20      var selectedLocation: Location?
 21      var mapConstraint1 : NSLayoutConstraint!
 22      var mapConstraint2 : NSLayoutConstraint!
 23      
 24      @IBOutlet var superView: UIView!
 25      
 26      @IBOutlet weak var mapView: MKMapView! {
 27          didSet {
 28              self.mapView.delegate = self
 29              self.mapView.mapType = .standard
 30              self.mapView.showsUserLocation = true
 31          }
 32      }
 33      
 34      @IBAction func toggleInfo(_ sender: Any) {
 35          let segment = (sender as! UISegmentedControl)
 36          if segment.selectedSegmentIndex == 0{
 37              showDetails()
 38          }
 39          if segment.selectedSegmentIndex == 1{
 40              showMenu()
 41          }
 42      }
 43      
 44      let manager = CLLocationManager()
 45          
 46      override func viewDidLoad() {
 47          super.viewDidLoad()
 48          // Do any additional setup after loading the view, typically from a nib.
 49          
 50          // setting up
 51          self.manager.delegate = self
 52          self.manager.desiredAccuracy = kCLLocationAccuracyBest
 53          
 54          // request permission
 55          self.manager.requestWhenInUseAuthorization()
 56          
 57          // actually start getting location
 58          self.manager.startUpdatingLocation()
 59          
 60          
 61          // set the mapView's initial region
 62          let walnutStreet = CLLocationCoordinate2D(latitude: 39.955333, longitude: -75.197939)
 63          let region = MKCoordinateRegion(center: walnutStreet, span: MKCoordinateSpan(latitudeDelta: 0.025, longitudeDelta: 0.025))
 64          self.mapView.setRegion(region, animated: true)
 65          
 66          // detect rotation changes
 67          NotificationCenter.default.addObserver(self, selector: #selector(ViewController.rotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
 68          
 69          // add map pins to map
 70          NetworkManager.load(closure: {(locations) in
 71              if locations != nil {
 72                  NetworkManager.locations = locations as! [Location]
 73                  for location in NetworkManager.locations {
 74                      location.addressToCoord()
 75                      self.mapView.addAnnotation(location)
 76                  }
 77              }
 78          })
 79          
 80          
 81          mapConstraint1 = NSLayoutConstraint(item: mapView, attribute: .height, relatedBy: .equal, toItem: superView, attribute: .height , multiplier: 0.5, constant: 0.0)
 82          mapConstraint2 = NSLayoutConstraint(item: mapView, attribute: .height, relatedBy: .equal, toItem: superView, attribute: .height , multiplier: 1.0, constant: 0.0)
 83          mapConstraint1.isActive = true
 84          mapConstraint2.isActive = false
 85          mapViewHeight.isActive = false
 86         
 87        
 88      }
 89      
 90     
 91      func rotated() {
 92          if UIDeviceOrientationIsLandscape(UIDevice.current.orientation) {
 93              infoPane(shouldHide: true)
 94              //mapViewHeight.constant = 1
 95              mapConstraint1.isActive = false
 96              mapConstraint2.isActive = true
 97              print (mapConstraint2.isActive)
 98              self.view.layoutIfNeeded()
 99          }
100          if UIDeviceOrientationIsPortrait(UIDevice.current.orientation) {
101              infoPane(shouldHide: false)
102              //mapViewHeight.constant = 0.5
103              mapConstraint1.isActive = true
104              mapConstraint2.isActive = false
105              print (mapConstraint2.isActive)
106              self.view.layoutIfNeeded()
107          }
108      }
109      
110      func infoPane(shouldHide: Bool){
111          infoSegment.isHidden = shouldHide
112          infoLabel.isHidden = shouldHide
113          locationLabel.isHidden = shouldHide
114      }
115  
116      func mapView(_ mapView: MKMapView,
117                   didSelect view: MKAnnotationView){
118          let annotation = view.annotation
119          if let location = annotation as? Location {
120              selectedLocation = location
121              locationLabel.text = selectedLocation?.name
122              if infoSegment.selectedSegmentIndex == 0{
123                  showDetails()
124              }
125              if infoSegment.selectedSegmentIndex == 1{
126                  showMenu()
127              }
128          }
129      }
130      
131      func showMenu(){
132          if (selectedLocation != nil){
133              infoLabel.text = "Menu:\n" + (selectedLocation?.menu)!
134          }
135      }
136      
137      func showDetails(){
138          if (selectedLocation != nil){
139              var details = ""
140              details += "Address: " + (selectedLocation?.address)! + "\n"
141              details += "Phone: " + (selectedLocation?.phone)! + "\n"
142              details += "Hours: " + (selectedLocation?.hours)!
143              infoLabel.text = details
144          }
145      }
146  
147      override func didReceiveMemoryWarning() {
148          super.didReceiveMemoryWarning()
149          // Dispose of any resources that can be recreated.
150      }
151  }