/
AppDelegate.swift
executable file
·353 lines (294 loc) · 12 KB
/
AppDelegate.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
//
// AppDelegate.swift
// Todo
//
// Created by Pasin Suriyentrakorn on 2/8/16.
// Copyright © 2016 Couchbase. All rights reserved.
//
import UIKit
import UserNotifications
import CouchbaseLiteSwift
import Fabric
import Crashlytics
// Configuration:
let kLoggingEnabled = true
let kLoginFlowEnabled = true
let kSyncEnabled = true
let kSyncEndpoint = "ws://ec2-3-90-70-164.compute-1.amazonaws.com:4984/todo"
let kSyncWithPushNotification = true
// Custom conflict resolver
enum CCRType {
case local, remote, delete;
}
let kCCREnabled = false
let kCCRType: CCRType = .remote
// Database Encryption:
// Note: changing this value requires to delete the app before rerun:
let kDatabaseEncryptionKey: String? = nil
// QE:
let kQEFeaturesEnabled = true
// Crashlytics:
let kCrashlyticsEnabled = true
// Constants:
let kActivities = ["Stopped", "Offline", "Connecting", "Idle", "Busy"]
// Logout Method
enum LogoutMethod {
case closeDatabase, deleteDatabase;
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, LoginViewControllerDelegate {
var window: UIWindow?
var database: Database!
var replicator: Replicator!
var changeListener: ListenerToken?
func application(_ application: UIApplication, didFinishLaunchingWithOptions
launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
initCrashlytics()
if kLoggingEnabled {
Database.log.console.level = .info
}
if kLoginFlowEnabled {
login(username: nil)
} else {
do {
try startSession(username: "todo")
} catch let error as NSError {
NSLog("Cannot start a session: %@", error)
return false
}
}
return true
}
// MARK: - Session
func startSession(username:String, withPassword password:String? = nil) throws {
try openDatabase(username: username)
Session.username = username
Session.password = password
var resolver: ConflictResolverProtocol?
if kCCREnabled {
resolver = TestConflictResolver() { (conflict) -> Document? in
switch kCCRType {
case .local:
return conflict.localDocument
case .remote:
return conflict.remoteDocument
case .delete:
return nil;
}
}
}
startReplication(withUsername: username, password: password, resolver: resolver)
showApp()
registerRemoteNotification()
}
func openDatabase(username:String) throws {
let config = DatabaseConfiguration()
if let password = kDatabaseEncryptionKey {
config.encryptionKey = EncryptionKey.password(password)
}
database = try Database(name: username, config: config)
createDatabaseIndex()
}
func closeDatabase() throws {
try database.close()
}
func deleteDatabase() throws {
try database.delete()
}
func createDatabaseIndex() {
// For task list query:
let type = ValueIndexItem.expression(Expression.property("type"))
let name = ValueIndexItem.expression(Expression.property("name"))
let taskListId = ValueIndexItem.expression(Expression.property("taskList.id"))
let task = ValueIndexItem.expression(Expression.property("task"))
do {
let index = IndexBuilder.valueIndex(items: type, name)
try database.createIndex(index, withName: "task-list")
} catch let error as NSError {
NSLog("Couldn't create index (type, name): %@", error);
}
// For tasks query:
do {
let index = IndexBuilder.valueIndex(items: type, taskListId, task)
try database.createIndex(index, withName: "tasks")
} catch let error as NSError {
NSLog("Couldn't create index (type, taskList.id, task): %@", error);
}
}
// MARK: - Login
func login(username: String? = nil) {
let storyboard = window!.rootViewController!.storyboard
let navigation = storyboard!.instantiateViewController(
withIdentifier: "LoginNavigationController") as! UINavigationController
let loginController = navigation.topViewController as! LoginViewController
loginController.delegate = self
loginController.username = username
window!.rootViewController = navigation
}
func logout(method: LogoutMethod) {
let startTime = Date().timeIntervalSinceReferenceDate
if method == .closeDatabase {
do {
try closeDatabase()
} catch let error as NSError {
NSLog("Cannot close database: %@", error)
return
}
} else if method == .deleteDatabase {
do {
try deleteDatabase()
} catch let error as NSError {
NSLog("Cannot delete database: %@", error)
return
}
}
let endTime = Date().timeIntervalSinceReferenceDate
NSLog("Logout took in \(endTime - startTime) seconds")
let oldUsername = Session.username
Session.username = nil
Session.password = nil
login(username: oldUsername)
}
func showApp() {
guard let root = window?.rootViewController, let storyboard = root.storyboard else {
return
}
let controller = storyboard.instantiateInitialViewController()
window!.rootViewController = controller
}
// MARK: - LoginViewControllerDelegate
func login(controller: UIViewController, withUsername username: String,
andPassword password: String) {
processLogin(controller: controller, withUsername: username, withPassword: password)
}
func processLogin(controller: UIViewController, withUsername username: String,
withPassword password: String) {
do {
try startSession(username: username, withPassword: password)
} catch let error as NSError {
Ui.showMessage(on: controller,
title: "Error",
message: "Login has an error occurred, code = \(error.code).")
NSLog("Cannot start a session: %@", error)
}
}
// MARK: - Replication
func startReplication(withUsername username:String, password:String? = "", resolver: ConflictResolverProtocol?) {
guard kSyncEnabled else {
return
}
let auth = kLoginFlowEnabled ? BasicAuthenticator(username: username, password: password!) : nil
let target = URLEndpoint(url: URL(string: kSyncEndpoint)!)
let config = ReplicatorConfiguration(database: database, target: target)
config.continuous = true
config.authenticator = auth
config.conflictResolver = resolver
NSLog(">> Custom Conflict Resolver: Enabled = \(kCCREnabled); Type = \(kCCRType)")
replicator = Replicator(config: config)
changeListener = replicator.addChangeListener({ (change) in
let s = change.status
let activity = kActivities[Int(s.activity.rawValue)]
let e = change.status.error as NSError?
let error = e != nil ? ", error: \(e!.description)" : ""
NSLog("[Todo] Replicator: \(activity), \(s.progress.completed)/\(s.progress.total)\(error)")
UIApplication.shared.isNetworkActivityIndicatorVisible = (s.activity == .busy)
if let code = e?.code {
if code == 401 {
Ui.showMessage(on: self.window!.rootViewController!,
title: "Authentication Error",
message: "Your username or password is not correct",
error: nil,
onClose: {
self.logout(method: .closeDatabase)
})
}
}
})
replicator.start()
}
func stopReplication() {
guard kSyncEnabled else {
return
}
replicator.stop()
replicator.removeChangeListener(withToken: changeListener!)
changeListener = nil
}
// MARK: Push Notification Sync
func registerRemoteNotification() {
guard kSyncWithPushNotification else {
return
}
UIApplication.shared.registerForRemoteNotifications()
}
func startPushNotificationSync() {
guard kSyncWithPushNotification else {
return
}
NSLog("[Todo] Start Push Notification ...")
let target = URLEndpoint(url: URL(string: kSyncEndpoint)!)
let config = ReplicatorConfiguration(database: database, target: target)
if kLoginFlowEnabled, let u = Session.username, let p = Session.password {
config.authenticator = BasicAuthenticator(username: u, password: p)
}
let repl = Replicator(config: config)
changeListener = repl.addChangeListener({ (change) in
let s = change.status
let activity = kActivities[Int(s.activity.rawValue)]
let e = change.status.error as NSError?
let error = e != nil ? ", error: \(e!.description)" : ""
NSLog("[Todo] Push-Notification-Replicator: \(activity), \(s.progress.completed)/\(s.progress.total)\(error)")
UIApplication.shared.isNetworkActivityIndicatorVisible = (s.activity == .busy)
if let code = e?.code {
if code == 401 {
NSLog("ERROR: Authentication Error, username or password is not correct");
}
}
})
repl.start()
}
// MARK: UNUserNotificationCenterDelegate
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Note: Normally the application will send the device token to
// the backend server so that the backend server can use that to
// send the push notification to the application. We are just printing
// to the console here.
let tokenStrs = deviceToken.map { data -> String in
return String(format: "%02.2hhx", data)
}
let token = tokenStrs.joined()
NSLog("Push Notification Device Token: \(token)")
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
NSLog("WARNING: Failed to register for the remote notification: \(error)")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// Start single shot replicator:
self.startPushNotificationSync()
completionHandler(.newData)
}
// MARK: Crashlytics
func initCrashlytics() {
if !kCrashlyticsEnabled {
return
}
Fabric.with([Crashlytics.self])
if let info = Bundle(for: Database.self).infoDictionary {
if let version = info["CFBundleShortVersionString"] {
Crashlytics.sharedInstance().setObjectValue(version, forKey: "Version")
}
if let build = info["CFBundleShortVersionString"] {
Crashlytics.sharedInstance().setObjectValue(build, forKey: "Build")
}
}
}
}
class TestConflictResolver: ConflictResolverProtocol {
let _resolver: (Conflict) -> Document?
init(_ resolver: @escaping (Conflict) -> Document?) {
_resolver = resolver
}
func resolve(conflict: Conflict) -> Document? {
return _resolver(conflict)
}
}