FacilityDetailViewController.swift 8.3 KB
Newer Older
1
2
3
4
5
//
//  LocationDetailViewController.swift
//  WhatsOpen
//
//  Created by Zach Knox on 4/5/17.
Zach Knox's avatar
Zach Knox committed
6
//  Copyright © 2017 SRCT. Some rights reserved.
7
8
9
//

import UIKit
Zach Knox's avatar
Zach Knox committed
10
import StoreKit
Zach Knox's avatar
Zach Knox committed
11
import MapKit
12

13
class FacilityDetailViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
14

Zach Knox's avatar
Zach Knox committed
15
	@IBOutlet var NameLabel: UILabel!
16
	@IBOutlet var PlaceLabel: UILabel!
Zach Knox's avatar
Zach Knox committed
17
	@IBOutlet var OpenLabel: UILabel!
18
    @IBOutlet var CategoryLabel: UILabel!
19
	@IBOutlet var OpenTimesList: UITableView!
20
    @IBOutlet var favoritesButton: UIButton!
Zach Knox's avatar
Zach Knox committed
21
22
23
	@IBOutlet var directionsButton: UIButton!
	@IBOutlet var shareButton: UIButton!
	
24
    var facility: Facility!
25
	
26
	override var previewActionItems: [UIPreviewActionItem] {
Zach Knox's avatar
Zach Knox committed
27
28
29
30
31
32
33
34
		var title: String
		if(Utilities.isFavoriteFacility(facility)) {
			title = "Remove from Favorites"
		}
		else {
			title = "Add to Favorites"
		}
		let favoritePreviewItem = UIPreviewAction(title: title, style: UIPreviewActionStyle.default, handler: markAsFavoritePreviewAction)
35
36
	    return [favoritePreviewItem]
	}
37
	
38
39
40
41
42
43
    /**
     Favorites button touch handler
     
     Adds a facility to favorites if it isn't a favorite,
     removes from favorites if it is a favorite.
     */
44
	@IBAction func setFavButton(_ sender: Any) {
45
        if(Utilities.isFavoriteFacility(facility)) { // if the facility is a favorite
46
			_ = Utilities.removeFavoriteFacility(facility) // remove it from favorites
47
		}
48
		else { // else add it to favorites
49
			_ = Utilities.addFavoriteFacility(facility)
50
51
52
		}
		setFavoriteButtonText()
	}
53
    
Zach Knox's avatar
Zach Knox committed
54
	@IBAction func getDirections(_ sender: Any) {
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
		let appToUse = UserDefaults.standard.value(forKey: "mapsApp") as? String
		
		if appToUse == "Google Maps" && UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!) {
			if let url = URL(string: "comgooglemaps://?q=\((facility.facilityLocation?.coordinates?.coords?.last)!)),\((facility.facilityLocation?.coordinates?.coords?.first)!)") {
				UIApplication.shared.open(url, options: [:], completionHandler: nil)
			}
		}
		else if appToUse == "Waze" && UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!) {
			if let url = URL(string: "https://waze.com/ul?ll=\((facility.facilityLocation?.coordinates?.coords?.last)!)),\((facility.facilityLocation?.coordinates?.coords?.first)!))") {
				UIApplication.shared.open(url, options: [:], completionHandler: nil)
			}
		}
		else {
			let regionDistance:CLLocationDistance = 100
			let coordinates = CLLocationCoordinate2DMake((facility.facilityLocation?.coordinates?.coords?.last)!, (facility.facilityLocation?.coordinates?.coords?.first)!)
			dump(coordinates)
			let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
			let options = [
				MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
				MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
			]
			let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
			let mapItem = MKMapItem(placemark: placemark)
			mapItem.name = facility.facilityName
			mapItem.openInMaps(launchOptions: options)
		}
Zach Knox's avatar
Zach Knox committed
81
82
83
84
	}
	
	
	@IBAction func shareFacility(_ sender: Any) {
Zach Knox's avatar
Zach Knox committed
85
86
87
88
		let coordinates = CLLocationCoordinate2DMake((facility.facilityLocation?.coordinates?.coords?.last)!, (facility.facilityLocation?.coordinates?.coords?.first)!)
		let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
		let shareSheet = UIActivityViewController(activityItems: [facility.facilityName], applicationActivities: nil)
		present(shareSheet, animated: true, completion: nil)
Zach Knox's avatar
Zach Knox committed
89
90
91
	}
	
	/**
92
93
     Change the favorite button text depending on if the facility is a favorite
     */
94
95
96
97
98
99
100
101
102
103
104
	func setFavoriteButtonText() {
		if(Utilities.isFavoriteFacility(facility)) {
			favoritesButton.setTitle("Remove from Favorites", for: .normal)
			favoritesButton.titleLabel?.text = "Remove from Favorites"
		}
		else {
			favoritesButton.setTitle("Add to Favorites", for: .normal)
			favoritesButton.titleLabel?.text = "Add to Favorites"
		}
	}
	
Zach Knox's avatar
Zach Knox committed
105
106
107
108
	override var preferredStatusBarStyle: UIStatusBarStyle {
		return .lightContent
	}
	
Zach Knox's avatar
Zach Knox committed
109
110
111
	override func viewWillAppear(_ animated: Bool) {
		super.viewWillAppear(true)
	}
Zach Knox's avatar
Zach Knox committed
112
113
114
115
116
117
118
119
120
121
122
123
124
	override func viewDidDisappear(_ animated: Bool) {
		super.viewDidDisappear(true)
		// MARK - Begging for App Reviews
		let prompt = UserDefaults.standard.integer(forKey: "reviewPrompt")
		if(arc4random_uniform(100) > 85 && prompt >= 4) {
			SKStoreReviewController.requestReview()
			UserDefaults.standard.set(0, forKey: "reviewPrompt")
		}
		else {
			UserDefaults.standard.set(prompt + 1, forKey: "reviewPrompt")
		}
		
	}
125
126
    override func viewDidLoad() {
        super.viewDidLoad()
127
		
128
129
		modalPresentationCapturesStatusBarAppearance = true
		
Zach Knox's avatar
Zach Knox committed
130
131
132
133
134
135
		var name = facility.facilityName
		let separator = name.index(of: "[")
		if separator != nil {
			name = String(name[..<separator!]).replacingOccurrences(of: "\\s+$",with: "", options: .regularExpression)
		}
		NameLabel.text = name
136
137
138
		PlaceLabel.text = facility.facilityLocation!.building
        CategoryLabel.text = facility.category?.categoryName.uppercased()

Zach Knox's avatar
Zach Knox committed
139
		
140
		let open = Utilities.isOpen(facility: facility)
141
142
143
        OpenLabel.text = Utilities.openOrClosedUntil(facility)
        OpenLabel.layer.cornerRadius = 4
        OpenLabel.layer.masksToBounds = true
Zach Knox's avatar
Zach Knox committed
144
145
146
147
		if(open) {
			OpenLabel.backgroundColor = UIColor(red:0.00, green:0.40, blue:0.20, alpha:1.0)
		}
		else {
Eyad Hasan's avatar
Eyad Hasan committed
148
			OpenLabel.backgroundColor = UIColor(red:0.17, green:0.17, blue: 0.17, alpha: 1.0)
Zach Knox's avatar
Zach Knox committed
149
		}
Zach Knox's avatar
Zach Knox committed
150
		
151
		setFavoriteButtonText()
Zach Knox's avatar
Zach Knox committed
152
153
154
		favoritesButton.tintColor = UIColor.white
		favoritesButton.backgroundColor = UIColor(red:0.00, green:0.40, blue:0.20, alpha:1.0)
		favoritesButton.layer.cornerRadius = 10
Zach Knox's avatar
Zach Knox committed
155
156
157
		directionsButton.tintColor = UIColor.white
		directionsButton.backgroundColor = #colorLiteral(red: 0, green: 0.4793452024, blue: 0.9990863204, alpha: 1)
		directionsButton.layer.cornerRadius = 10
158
159
160
161
162
163
164
165
166
167
168
		
		let appToUse = UserDefaults.standard.value(forKey: "mapsApp") as? String
		if appToUse == "Google Maps" && UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!) {
			directionsButton.setTitle("View in Google Maps", for: .normal)
		}
		else if appToUse == "Waze" && UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!) {
			directionsButton.setTitle("View in Waze", for: .normal)
		}
		else {
			directionsButton.setTitle("View in Maps", for: .normal)
		}
Zach Knox's avatar
Zach Knox committed
169
170
171
172
173
		shareButton.tintColor = UIColor.white
		shareButton.backgroundColor = UIColor.orange
		shareButton.layer.cornerRadius = 10
		shareButton.setImage(#imageLiteral(resourceName: "shareIcon"), for: .normal)
		shareButton.setTitle("", for: .normal)
174

Zac Wood's avatar
Zac Wood committed
175
		OpenTimesList.bounces = false
176
		
Zach Knox's avatar
Zach Knox committed
177
178
179
		if #available(iOS 11.0, *) {
			navigationItem.largeTitleDisplayMode = .never
		}
Zac Wood's avatar
Zac Wood committed
180
181
182
183
184
185
        
//        NameLabel.font = UIFont.preferredFont(forTextStyle: .headline)
//        PlaceLabel.font = UIFont.preferredFont(forTextStyle: .subheadline)
//        OpenLabel.font = UIFont.preferredFont(forTextStyle: .body)
//        favoritesButton.titleLabel?.font = UIFont.preferredFont(forTextStyle: .body)
        
186
187
188
189
190
191
192
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

193
194
195
    func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
196
    }
197

198
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
Zach Knox's avatar
Zach Knox committed
199
		return Utilities.getCurrentSchedule(facility)!.openTimes.count
200
    }
201

202
203
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = OpenTimesList.dequeueReusableCell(withIdentifier: "LocationDetailCell", for: indexPath)
204

205
206
		cell.selectionStyle = .none
		
Zach Knox's avatar
Zach Knox committed
207
208
209
210
		let schedule = Utilities.getCurrentSchedule(facility)
		let openTime = schedule!.openTimes[indexPath.row]
		cell.textLabel?.text = Utilities.getDayOfWeek(Day(rawValue: openTime.startDay)!)
		cell.detailTextLabel?.text = Utilities.getFormattedStartandEnd(openTime)
211

Zac Wood's avatar
Zac Wood committed
212
213
        cell.textLabel?.font = UIFont.preferredFont(forTextStyle: .headline)
        cell.detailTextLabel?.font = UIFont.preferredFont(forTextStyle: .body)
214

215
        return cell
216
    }
217
218
			
	func markAsFavoritePreviewAction(_ sendingAction: UIPreviewAction, sender: UIViewController) {
Zach Knox's avatar
Zach Knox committed
219
		if(Utilities.isFavoriteFacility(facility)) {
220
			_ = Utilities.removeFavoriteFacility(facility)
Zach Knox's avatar
Zach Knox committed
221
222
		}
		else {
223
			_ = Utilities.addFavoriteFacility(facility)
Zach Knox's avatar
Zach Knox committed
224
		}
225
	}
226
}