<commit_before>//
//  MapViewController.swift
//  OnTheMap
//
//  Created by <NAME> on 2016-03-14.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit
import MapKit

/// This displays a map with pins corresponding to student locations
/// as returned by a Parse API.
class MapViewController: UIViewController, MKMapViewDelegate {

    var studentLocations = [StudentLocation]()
    var annotations = [MKPointAnnotation]()
    
    // MARK: Outlets
    
    @IBOutlet weak var mapView: MKMapView!
    
    
    // MARK: life cycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // setting the Navigation bar
        // setting the title
        title = "On the map"
        // create an array for the buttons on the right
        var navBarItems = [UIBarButtonItem]()
        navBarItems.append(UIBarButtonItem(barButtonSystemItem: .Refresh, target: self, action: #selector(MapViewController.refreshData)))
        navBarItems.append(UIBarButtonItem(image: UIImage(named: "MapNavBarItem"), style: .Plain, target: self, action: #selector(MapViewController.enterLocation)))
        // setting the left side button
        parentViewController!.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Reply, target: self, action: #selector(MapViewController.logout))
        // adding the right side buttons
        parentViewController!.navigationItem.rightBarButtonItems = navBarItems
        
     }
    
    override func viewWillAppear(animated: Bool) {
        
        // TODO: change the post icon to either the '+' version if nothing was yet posted
        // or 'u' version if posting will update an already posted location for that user
        // (test if objectID == "", or perhaps add a property for it)
        
        refreshData()
        
    }

    // MARK: Data functions
    
    func refreshData() {
 
        API.sharedInstance().getLocationsData { (studentLocations, annotations, error) -> Void in
            
            guard (error == nil) else {
                print("Error returned by getLocationData in MapViewController: \(error)")
                // TODO: alert user
                return
            }
            
            if let studentLocations = studentLocations, annotations = annotations {
                self.studentLocations = studentLocations
                self.annotations = annotations
                performUIUpdatesOnMain {
                    self.completeGetLocationData(annotations)
                }
            } else {
                print("studentLocations or annotation is nil, error was \(error)")
            }
            
        }
        
    }
 
    func completeGetLocationData(annotations: [MKPointAnnotation]) {
        
        self.mapView.addAnnotations(annotations)
        
    }

    
    // MARK: user actions
    
    @IBAction func userTappedRefresh(sender: AnyObject) {
        
        refreshData()
        
    }
    
    @IBAction func userTappedAddLocation(sender: AnyObject) {
        let controller = self.storyboard!.instantiateViewControllerWithIdentifier("LocationView") as! LocationViewController
        self.presentViewController(controller, animated: true, completion: nil)
    }
    
    @IBAction func userTappedLogout(sender: AnyObject) {
    }
    
    @IBAction func unwindToMap(unwindSegue: UIStoryboardSegue) {
        if let postingViewController = unwindSegue.sourceViewController as? LocationPostingViewController {

            if Model.sharedInstance().userInformation?.objectId != "" {

                // first, fetch new data from Parse API
                refreshData()
                // then change the span to center on the newly posted area
                let latitudeDelta = CLLocationDegrees(1.0)
                let longitudeDelta = CLLocationDegrees(1.0)
                let span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta)
                let coordinate = CLLocationCoordinate2D(latitude: (Model.sharedInstance().userInformation?.latitude)!, longitude: (Model.sharedInstance().userInformation?.longitude)!)
                let region = MKCoordinateRegionMake(coordinate, span)
                mapView.setRegion(region, animated: true)
               
            }
            
        }
        else if let locationViewController = unwindSegue.sourceViewController as? LocationViewController {
            print("Coming from location")
        }
    }

    // MARK: MapView Delegates
    
    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        
        let reuseId = "pin"
        
        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
        
        if pinView == nil {<commit_msg>add code to present URL when annotation tapped in MapViewController<commit_after>//
//  MapViewController.swift
//  OnTheMap
//
//  Created by <NAME> on 2016-03-14.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit
import MapKit

/// This displays a map with pins corresponding to student locations
/// as returned by a Parse API.
class MapViewController: UIViewController, MKMapViewDelegate {

    var studentLocations = [StudentLocation]()
    var annotations = [MKPointAnnotation]()
    
    // MARK: Outlets
    
    @IBOutlet weak var mapView: MKMapView!
    
    
    // MARK: life cycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        mapView.delegate = self
        
        // setting the Navigation bar
        // setting the title
        title = "On the map"
        // create an array for the buttons on the right
        var navBarItems = [UIBarButtonItem]()
        navBarItems.append(UIBarButtonItem(barButtonSystemItem: .Refresh, target: self, action: #selector(MapViewController.refreshData)))
        navBarItems.append(UIBarButtonItem(image: UIImage(named: "MapNavBarItem"), style: .Plain, target: self, action: #selector(MapViewController.enterLocation)))
        // setting the left side button
        parentViewController!.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Reply, target: self, action: #selector(MapViewController.logout))
        // adding the right side buttons
        parentViewController!.navigationItem.rightBarButtonItems = navBarItems
        
     }
    
    override func viewWillAppear(animated: Bool) {
        
        // TODO: change the post icon to either the '+' version if nothing was yet posted
        // or 'u' version if posting will update an already posted location for that user
        // (test if objectID == "", or perhaps add a property for it)
        
        refreshData()
        
    }

    // MARK: Data functions
    
    func refreshData() {
 
        API.sharedInstance().getLocationsData { (studentLocations, annotations, error) -> Void in
            
            guard (error == nil) else {
                print("Error returned by getLocationData in MapViewController: \(error)")
                // TODO: alert user
                return
            }
            
            if let studentLocations = studentLocations, annotations = annotations {
                self.studentLocations = studentLocations
                self.annotations = annotations
                performUIUpdatesOnMain {
                    self.completeGetLocationData(annotations)
                }
            } else {
                print("studentLocations or annotation is nil, error was \(error)")
            }
            
        }
        
    }
 
    func completeGetLocationData(annotations: [MKPointAnnotation]) {
        
        self.mapView.addAnnotations(annotations)
        
    }

    
    // MARK: user actions
    
    @IBAction func userTappedRefresh(sender: AnyObject) {
        
        refreshData()
        
    }
    
    @IBAction func userTappedAddLocation(sender: AnyObject) {
        let controller = self.storyboard!.instantiateViewControllerWithIdentifier("LocationView") as! LocationViewController
        self.presentViewController(controller, animated: true, completion: nil)
    }
    
    @IBAction func userTappedLogout(sender: AnyObject) {
    }
    
    @IBAction func unwindToMap(unwindSegue: UIStoryboardSegue) {
        if let postingViewController = unwindSegue.sourceViewController as? LocationPostingViewController {

            if Model.sharedInstance().userInformation?.objectId != "" {

                // first, fetch new data from Parse API
                refreshData()
                // then change the span to center on the newly posted area
                let latitudeDelta = CLLocationDegrees(1.0)
                let longitudeDelta = CLLocationDegrees(1.0)
                let span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta)
                let coordinate = CLLocationCoordinate2D(latitude: (Model.sharedInstance().userInformation?.latitude)!, longitude: (Model.sharedInstance().userInformation?.longitude)!)
                let region = MKCoordinateRegionMake(coordinate, span)
                mapView.setRegion(region, animated: true)
               
            }
            
        }
        else if let locationViewController = unwindSegue.sourceViewController as? LocationViewController {
            print("Coming from location")
        }
    }

    // MARK: MapView Delegates
    
    //Opens the mediaURL in Safari when the annotation info box is tapped.
    func mapView(mapView: MKMapView, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
        
        UIApplication.sharedApplication().openURL(NSURL(string: view.annotation!.subtitle!!)!)
    }

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
                
        let reuseId = "pin"
        
        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
        
        if pinView == nil {
{'fraction_non_alphanumeric': 0.07948410317936412, 'fraction_numerical': 0.0033993201359728054, 'mean_word_length': 2.243514915693904, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1663300', 'n_tokens_mistral': 2637, 'n_tokens_neox': 2437, 'n_words': 676}