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

import UIKit
10
11
import SafariServices
import MessageUI
12
import StoreKit
13
import WhatsOpenKit
14

15
class SettingsTableViewController: UITableViewController, MFMailComposeViewControllerDelegate {
16

17
18
19
20
	override var preferredStatusBarStyle: UIStatusBarStyle {
		return .default
	}
	
21
22
23
24
	@IBAction func doneButton(_ sender: Any) {
		self.dismiss(animated: true, completion: nil)
	}
	
Zach Knox's avatar
Zach Knox committed
25
26
27
28
	override func viewWillAppear(_ animated: Bool) {
		tableView.reloadData()
	}
	
29
30
    override func viewDidLoad() {
        super.viewDidLoad()
31
32
		
		tableView.estimatedRowHeight = 44.0
Zach Knox's avatar
Zach Knox committed
33
		tableView.rowHeight = UITableView.automaticDimension
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

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

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
50
        return 4
51
52
53
54
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
55
56
57
58
		if(section == 0) {
			return 1
		}
		else if(section == 1) {
59
			return 2
Zach Knox's avatar
icons    
Zach Knox committed
60
61
		}
		else if(section == 2) {
62
			return 2
63
64
		}
		else if(section == 3) {
65
			return 3
66
67
68
69
		}
		else {
			return 0
		}
70
71
72
73
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

74
75
		switch indexPath.section {
		case 0:
76
			let cell = tableView.dequeueReusableCell(withIdentifier: "Setting", for: indexPath) as! SettingTableViewCell
77
			cell.textLabel!.text = "Are Our Hours Wrong?"
78
			return cell
79
		case 1:
80
81
82
83
84
85
86
87
88
89
90
			if indexPath.row == 0 {
				let cell = tableView.dequeueReusableCell(withIdentifier: "Setting", for: indexPath) as! SettingTableViewCell
				cell.textLabel!.text = "Select App Icon"
				cell.accessoryType = .disclosureIndicator
				return cell
			}
			else if indexPath.row == 1 {
				let cell = tableView.dequeueReusableCell(withIdentifier: "settingDefaultCell", for: indexPath)
				cell.textLabel!.text = "Select Maps App"
				cell.textLabel?.font = UIFont.preferredFont(forTextStyle: .body)
				cell.detailTextLabel?.font = UIFont.preferredFont(forTextStyle: .footnote)
Zach Knox's avatar
Zach Knox committed
91
				cell.detailTextLabel?.text = WOPDatabaseController.getDefaults().value(forKey: "mapsApp") as? String
92
93
94
95
				cell.accessoryType = .disclosureIndicator
				return cell
			}

Zach Knox's avatar
icons    
Zach Knox committed
96
		case 2:
97
98
			let cell = tableView.dequeueReusableCell(withIdentifier: "settingSelection", for: indexPath)
			cell.accessoryType = .disclosureIndicator
99
100
101
			switch indexPath.row {
			case 0:
				cell.textLabel?.text = "Show Alerts"
102
103
            	cell.textLabel?.font = UIFont.preferredFont(forTextStyle: .body)
            	cell.detailTextLabel?.font = UIFont.preferredFont(forTextStyle: .footnote)
104
				let alerts = WOPUtilities.getAlertDefaults()
105
				var i = 0
106
				for c in alerts {
107
108
109
110
111
					if(c.value == true) {
						i += 1
					}
				}
				var detail: String
112
				if(i == alerts.count) {
113
114
115
116
117
118
					detail = "All Selected"
				}
				else {
					detail = "\(i) Selected"
				}
				cell.detailTextLabel?.text = detail
119
120
121
				return cell
			case 1:
				cell.textLabel?.text = "Show Campuses"
122
123
				cell.textLabel?.font = UIFont.preferredFont(forTextStyle: .body)
            	cell.detailTextLabel?.font = UIFont.preferredFont(forTextStyle: .footnote)
124
				let campuses = WOPUtilities.getCampusDefaults()
125
				var i = 0
Zach Knox's avatar
Zach Knox committed
126
				for c in campuses {
127
128
129
130
131
					if(c.value == true) {
						i += 1
					}
				}
				var detail: String
Zach Knox's avatar
Zach Knox committed
132
				if(i == campuses.count) {
133
134
135
136
137
138
139
140
141
142
143
					detail = "All Selected"
				}
				else {
					detail = "\(i) Selected"
				}
				cell.detailTextLabel?.text = detail
				return cell
			default:
				return UITableViewCell() // don't do this
			}

144
145
		case 3:
			let cell = tableView.dequeueReusableCell(withIdentifier: "Setting", for: indexPath) as! SettingTableViewCell
146
147
148
149
			switch indexPath.row {
			case 0:
				cell.textLabel!.text = "Review on the App Store"
			case 1:
150
151
				cell.textLabel!.text = "About SRCT"
				cell.linkURL = URL(string: "https://srct.gmu.edu")
152
153
154
155
156
			case 2:
				cell.textLabel!.text = "About What's Open"
			default:
				cell.textLabel!.text = "rip"
			}
157
			return cell
158
159
160
		default:
			break
		}
161
162
        // Configure the cell...

163
        return tableView.dequeueReusableCell(withIdentifier: "Setting", for: indexPath) as! SettingTableViewCell // don't let this happen
164
    }
165
166
167
168
169
	
	override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
		let cell = self.tableView(tableView, cellForRowAt: indexPath)
		
		if let settingcell = cell as? SettingTableViewCell {
170
			// Eventually we should change this logic to make it not reliant on English text
171
172
173
			if settingcell.linkURL != nil {
				self.showDetailViewController(SFSafariViewController(url: settingcell.linkURL!), sender: settingcell)
			}
174
			else if settingcell.textLabel!.text == "Are Our Hours Wrong?" {
175
176
177
178
179
180
181
182
183
184
185
186
187
188
				let mailvc = initMail(subject: "What's Open - Your Hours are Wrong", to: "srct@gmu.edu")
				if !MFMailComposeViewController.canSendMail() {
					/*
					let alert = UIAlertController(title: "Mail Not Available", message: "Make sure your mail account is properly set up.", preferredStyle: .alert)
					alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: .`default`, handler: { _ in
						NSLog("The \"OK\" alert occured.")
					}))
					present(alert, animated: true)
					*/
					// Do literally nothing
				}
				else {
					present(mailvc, animated: true)
				}
189
			}
Zach Knox's avatar
icons    
Zach Knox committed
190
191
192
193
			else if settingcell.textLabel?.text == "Select App Icon" {
				let vc = self.storyboard?.instantiateViewController(withIdentifier: "setAppIcon")
				self.show(vc!, sender: settingcell)
			}
194
			else if settingcell.textLabel?.text == "Review on the App Store" {
195
196
197
198
199
				let appId = "1331260366"
				
				let urlString = "itms-apps://itunes.apple.com/us/app/whats-open-at-mason/id\(appId)?action=write-review"
				
				if let url = URL(string: urlString) {
Zach Knox's avatar
Zach Knox committed
200
					UIApplication.shared.open((url), options: convertToUIApplicationOpenExternalURLOptionsKeyDictionary([:]), completionHandler: nil)
201
				}
202
			}
203
204
205
			else if settingcell.textLabel!.text == "About What's Open" {
				let avc = self.storyboard?.instantiateViewController(withIdentifier: "about")
				self.show(avc!, sender: settingcell)
206
207
208
209
210
211
			}
		}
		else {
			return
		}
	}
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
	
	func initMail(subject: String, to: String) -> MFMailComposeViewController {
		let mailto = MFMailComposeViewController()
		mailto.mailComposeDelegate = self
		mailto.setSubject(subject)
		mailto.setToRecipients([to])
		let df = DateFormatter()
		let now = Date()
		mailto.setMessageBody("\n\n"+df.string(from: now), isHTML: false)
		return mailto
	}
	
	func mailComposeController(_ controller: MFMailComposeViewController,
							   didFinishWith result: MFMailComposeResult, error: Error?) {
		// Check the result or perform other tasks.
		
		// Dismiss the mail compose view controller.
		controller.dismiss(animated: true, completion: nil)
	}
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266

    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

267
268
269
270
	func updateFacilities() {
		// This isn't important for settings but is required for segueing to the views below
		return
	}
271
	
272
273
274
275
276
277
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
278
279
		
		if segue.identifier == "settingSelection" {
280
281
282
			if (sender as! UITableViewCell).textLabel?.text == "Show Alerts" {
				let destination = segue.destination as! FilterSelectionTableViewController
				destination.navigationItem.title = "Show Alerts"
283
284
285
				destination.getFunc = WOPUtilities.getAlertDefaults
				destination.selectFunc = WOPUtilities.setAlertDefaults
				destination.selectAllFunc = WOPUtilities.setAllAlertDefaults
286
287
288
289
290
				destination.updateFacilities = updateFacilities
			}
			else if (sender as! UITableViewCell).textLabel?.text == "Show Campuses" {
				let destination = segue.destination as! FilterSelectionTableViewController
				destination.navigationItem.title = "Show Campuses"
291
292
293
				destination.getFunc = WOPUtilities.getCampusDefaults
				destination.selectFunc = WOPUtilities.setCampusDefaults
				destination.selectAllFunc = WOPUtilities.setAllCampusDefaults
294
295
				destination.updateFacilities = updateFacilities
			}
296
		}
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
		else if segue.identifier == "settingDefault" {
			if (sender as! UITableViewCell).textLabel?.text == "Select Maps App" {
				let destination = segue.destination as! SelectOneDefaultTableViewController
				destination.navigationItem.title = "Select Maps App"
				destination.defaultKey = "mapsApp"
				var options = ["Apple Maps"]
				if UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!) {
					options.append("Google Maps")
				}
				if UIApplication.shared.canOpenURL(URL(string:"waze://")!) {
					options.append("Waze")
				}
				destination.options = options
			}
		}
312
    }
313
	
314
315

}
Zach Knox's avatar
Zach Knox committed
316
317
318
319
320

// Helper function inserted by Swift 4.2 migrator.
fileprivate func convertToUIApplicationOpenExternalURLOptionsKeyDictionary(_ input: [String: Any]) -> [UIApplication.OpenExternalURLOptionsKey: Any] {
	return Dictionary(uniqueKeysWithValues: input.map { key, value in (UIApplication.OpenExternalURLOptionsKey(rawValue: key), value)})
}