-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
RootTableViewController.swift
479 lines (430 loc) · 17.5 KB
/
RootTableViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//
// RootTableViewController.swift
// TidepoolKit Example
//
// Created by Darin Krauss on 1/10/20.
// Copyright © 2020 Tidepool Project. All rights reserved.
//
import os.log
import UIKit
import SwiftUI
import TidepoolKit
import AuthenticationServices
class RootTableViewController: UITableViewController, TAPIObserver {
private let api: TAPI
private var session: TSession? {
didSet {
UserDefaults.standard.session = session
if let session = session {
self.environment = session.environment
} else {
self.dataSetId = nil
}
updateViews()
}
}
private var environment: TEnvironment? {
didSet {
UserDefaults.standard.environment = environment
updateViews()
}
}
private var dataSetId: String? {
didSet {
UserDefaults.standard.dataSetId = dataSetId
if dataSetId == nil {
self.datumSelectors = nil
}
updateViews()
}
}
private var datumSelectors: [TDatum.Selector]? {
didSet {
updateViews()
}
}
private let logging = Logging()
required init?(coder: NSCoder) {
self.session = UserDefaults.standard.session
self.api = TAPI(clientId: "tidepoolkit-example", redirectURL: URL(string: "org.tidepool.tidepoolkit.auth://redirect")!, session: session)
self.environment = UserDefaults.standard.environment
self.dataSetId = UserDefaults.standard.dataSetId
super.init(coder: coder)
Task {
await api.setLogging(logging)
await api.addObserver(self)
}
}
func apiDidUpdateSession(_ session: TSession?) {
self.session = session
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(TextButtonTableViewCell.self, forCellReuseIdentifier: TextButtonTableViewCell.className)
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(share))
updateViews()
}
private func updateViews() {
tableView.reloadData()
navigationItem.rightBarButtonItem?.isEnabled = session != nil
}
private struct SharedStatus: Codable, Equatable {
let session: TSession
let dataSetId: String?
}
@objc func share() {
guard let session = session,
let data = try? JSONEncoder.pretty.encode(SharedStatus(session: session, dataSetId: dataSetId)),
let text = String(data: data, encoding: .utf8) else
{
return
}
let activityItem = UTF8TextFileActivityItem(name: "Status")
if let error = activityItem.write(text: text) {
present(UIAlertController(error: error), animated: true)
} else {
present(UIActivityViewController(activityItems: [activityItem], applicationActivities: nil), animated: true)
}
}
// MARK: - UITableView
private enum Section: Int, CaseIterable {
case status
case authentication
case profile
case dataSet
case datum
}
private enum Authentication: Int, CaseIterable {
case account
case refresh
case revoke
}
private enum Profile: Int, CaseIterable {
case get
}
private enum DataSet: Int, CaseIterable {
case list
case create
}
private enum Datum: Int, CaseIterable {
case list
case create
case delete
}
override func numberOfSections(in tableView: UITableView) -> Int {
return Section.allCases.count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch Section(rawValue: section)! {
case .status:
return NSLocalizedString("Status", comment: "The title for the header of the status section")
case .authentication:
return NSLocalizedString("Authentication", comment: "The title for the header of the authentication section")
case .profile:
return NSLocalizedString("Profile", comment: "The title for the header of the profile section")
case .dataSet:
return NSLocalizedString("Data Set", comment: "The title for the header of the data set section")
case .datum:
return NSLocalizedString("Datum", comment: "The title for the header of the datum section")
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch Section(rawValue: section)! {
case .status:
return 1
case .authentication:
return Authentication.allCases.count
case .profile:
return Profile.allCases.count
case .dataSet:
return DataSet.allCases.count
case .datum:
return Datum.allCases.count
}
}
private let defaultStatusLabelText = NSLocalizedString("-", comment: "The default status label text")
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch Section(rawValue: indexPath.section)! {
case .status:
let cell = tableView.dequeueReusableCell(withIdentifier: StatusTableViewCell.className, for: indexPath) as! StatusTableViewCell
cell.environmentLabel?.text = environment?.description ?? defaultStatusLabelText
cell.authenticationTokenLabel?.text = session?.accessToken ?? defaultStatusLabelText
cell.userIdLabel?.text = session?.userId ?? defaultStatusLabelText
cell.dataSetIdLabel?.text = dataSetId ?? defaultStatusLabelText
return cell
case .authentication:
let cell = tableView.dequeueReusableCell(withIdentifier: TextButtonTableViewCell.className, for: indexPath) as! TextButtonTableViewCell
switch Authentication(rawValue: indexPath.row)! {
case .account:
cell.textLabel?.text = NSLocalizedString("Account", comment: "The text label of the account cell")
cell.accessoryType = .disclosureIndicator
cell.isEnabled = session == nil
case .refresh:
cell.textLabel?.text = NSLocalizedString("Refresh", comment: "The text label of the authentication refresh cell")
cell.isEnabled = session != nil
case .revoke:
cell.textLabel?.text = NSLocalizedString("Revoke Token", comment: "The text label of the authentication revoke cell")
cell.isEnabled = session != nil
}
return cell
case .profile:
let cell = tableView.dequeueReusableCell(withIdentifier: TextButtonTableViewCell.className, for: indexPath) as! TextButtonTableViewCell
cell.accessoryType = .disclosureIndicator
cell.isEnabled = session != nil
switch Profile(rawValue: indexPath.row)! {
case .get:
cell.textLabel?.text = NSLocalizedString("Get Profile", comment: "The text label of the get profile cell")
}
return cell
case .dataSet:
let cell = tableView.dequeueReusableCell(withIdentifier: TextButtonTableViewCell.className, for: indexPath) as! TextButtonTableViewCell
cell.accessoryType = .disclosureIndicator
cell.isEnabled = session != nil
switch DataSet(rawValue: indexPath.row)! {
case .list:
cell.textLabel?.text = NSLocalizedString("List Data Sets", comment: "The text label of the list data sets cell")
case .create:
cell.textLabel?.text = NSLocalizedString("Create Data Set", comment: "The text label of the create data set cell")
}
return cell
case .datum:
let cell = tableView.dequeueReusableCell(withIdentifier: TextButtonTableViewCell.className, for: indexPath) as! TextButtonTableViewCell
cell.accessoryType = .disclosureIndicator
switch Datum(rawValue: indexPath.row)! {
case .list:
cell.textLabel?.text = NSLocalizedString("List Data", comment: "The text label of the list data cell")
cell.isEnabled = session != nil
case .create:
cell.textLabel?.text = NSLocalizedString("Create Data", comment: "The text label of the create data cell")
cell.isEnabled = session != nil && dataSetId != nil
case .delete:
cell.textLabel?.text = NSLocalizedString("Delete Data", comment: "The text label of the delete data cell")
cell.isEnabled = session != nil && dataSetId != nil && datumSelectors != nil
}
return cell
}
}
override func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
switch Section(rawValue: indexPath.section)! {
case .status:
return false
case .authentication:
switch Authentication(rawValue: indexPath.row)! {
case .account:
return true
default:
return session != nil
}
case .datum:
switch Datum(rawValue: indexPath.row)! {
case .create:
return session != nil && dataSetId != nil
case .delete:
return session != nil && dataSetId != nil && datumSelectors != nil
default:
return session != nil
}
default:
return session != nil
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch Section(rawValue: indexPath.section)! {
case .status:
break
case .authentication:
let cell = tableView.cellForRow(at: indexPath) as! TextButtonTableViewCell
switch Authentication(rawValue: indexPath.row)! {
case .account:
showAccount()
case .refresh:
Task {
cell.isLoading = true
await refresh()
cell.stopLoading()
}
case .revoke:
Task {
cell.isLoading = true
await revokeToken()
cell.stopLoading()
}
}
case .profile:
let cell = tableView.cellForRow(at: indexPath) as! TextButtonTableViewCell
Task {
cell.isLoading = true
await getProfile()
cell.stopLoading()
}
case .dataSet:
let cell = tableView.cellForRow(at: indexPath) as! TextButtonTableViewCell
cell.isLoading = true
Task {
switch DataSet(rawValue: indexPath.row)! {
case .list:
await listDataSets()
case .create:
await createDataSet()
}
cell.stopLoading()
}
case .datum:
let cell = tableView.cellForRow(at: indexPath) as! TextButtonTableViewCell
cell.isLoading = true
Task {
switch Datum(rawValue: indexPath.row)! {
case .list:
await listData()
case .create:
await createData()
case .delete:
await deleteData()
}
cell.stopLoading()
}
}
tableView.deselectRow(at: indexPath, animated: true)
}
// MARK: - Authentication
private func showAccount() {
Task {
let environments = try await TEnvironment.fetchEnvironments()
let currentEnvironment = self.session?.environment ?? self.api.defaultEnvironment ?? environments.first!
let view = LoginView(
selectedEnvironment: currentEnvironment,
isLoggedIn: session != nil,
environments: environments) { environment throws in
let sessionProvider = ASWebAuthenticationSessionProvider(contextProviding: self)
let auth = OAuth2Authenticator(api: self.api, environment: environment, sessionProvider: sessionProvider)
try await auth.login()
} logout: {
Task {
await self.api.logout()
}
}
let loginViewController = UIHostingController(rootView: view)
present(loginViewController, animated: true)
}
}
private func refresh() async {
do {
try await api.refreshSession()
} catch {
self.present(UIAlertController(error: error), animated: true)
}
}
private func revokeToken() async {
do {
try await api.revokeTokens()
} catch {
self.present(UIAlertController(error: error), animated: true)
}
}
private func logout() async {
await api.logout()
}
// MARK: - Profile
private func getProfile() async {
do {
let profile = try await api.getProfile()
self.display(profile, withTitle: "Get Profile")
} catch {
self.present(UIAlertController(error: error), animated: true)
}
}
// MARK: - Data Set
private func listDataSets() async {
let filter = TDataSet.Filter(clientName: Bundle.main.bundleIdentifier)
do {
let dataSets = try await api.listDataSets(filter: filter)
self.dataSetId = dataSets.first?.uploadId
self.display(dataSets, withTitle: "List Data Sets")
} catch {
self.present(UIAlertController(error: error), animated: true)
}
}
private func createDataSet() async {
let client = TDataSet.Client(name: Bundle.main.bundleIdentifier!, version: Bundle.main.semanticVersion!)
let deduplicator = TDataSet.Deduplicator(name: .none)
let dataSet = TDataSet(dataSetType: .continuous, client: client, deduplicator: deduplicator)
do {
let dataSet = try await api.createDataSet(dataSet)
self.dataSetId = dataSet.uploadId
self.display(dataSet, withTitle: "Create Data Set")
} catch {
self.present(UIAlertController(error: error), animated: true)
}
}
// MARK: - Datum
private func listData() async {
let filter = TDatum.Filter(dataSetId: dataSetId)
do {
let (data, malformed) = try await api.listData(filter: filter)
if !malformed.isEmpty {
self.present(UIAlertController(error: "Response contains malformed data.") {
self.display(malformed, withTitle: "MALFORMED - List Data")
}, animated: true)
} else {
self.display(data, withTitle: "List Data")
}
} catch {
self.present(UIAlertController(error: error), animated: true)
}
}
private func createData() async {
let data = Sample.Datum.data()
do {
try await api.createData(data, dataSetId: dataSetId!)
} catch {
if let error = error as? TError {
if case .requestMalformedJSON(_, _, let errors) = error {
self.present(UIAlertController(error: "Request contains errors.") {
self.display(errors, withTitle: "ERRORS - Create Data")
}, animated: true)
} else {
self.present(UIAlertController(error: error), animated: true)
}
} else {
self.datumSelectors = data.compactMap { $0.selector }
}
}
}
private func deleteData() async {
do {
try await api.deleteData(withSelectors: datumSelectors!, dataSetId: dataSetId!)
self.datumSelectors = nil
} catch {
self.present(UIAlertController(error: error), animated: true)
}
}
// MARK: - Internal
private func display<E>(_ encodable: E, withTitle title: String? = nil) where E: Encodable {
do {
display(try JSONEncoder.pretty.encode(encodable), withTitle: title)
} catch let error {
logging.error("Failure to encode object as JSON data [\(error)]")
present(UIAlertController(error: "Failure to encode object as JSON data."), animated: true)
}
}
private func display(_ object: Any, withTitle title: String? = nil) {
do {
display(try JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]), withTitle: title)
} catch let error {
logging.error("Failure to encode object as JSON data [\(error)]")
present(UIAlertController(error: "Failure to encode object as JSON data."), animated: true)
}
}
private func display(_ data: Data, withTitle title: String? = nil) {
guard let text = String(data: data, encoding: .utf8) else {
present(UIAlertController(error: "Failure to decode JSON data as string."), animated: true)
return
}
show(TextViewController(text: text, withTitle: title), sender: self)
}
}
extension RootTableViewController: ASWebAuthenticationPresentationContextProviding {
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
return (UIApplication.shared.delegate as! AppDelegate).window!
}
}