This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
AppDelegate.swift
117 lines (94 loc) · 4.01 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
//
// AppDelegate.swift
// Numu Tracker
//
// Created by Bradley Root on 9/11/16.
// Copyright © 2016 Numu Tracker. All rights reserved.
//
import UIKit
import PusherSwift
import UserNotifications
import SwiftyJSON
import Fabric
import Crashlytics
import CloudKit
import SpotifyLogin
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
// Production
let pusher = Pusher(key: "")
// Development
//let pusher = Pusher(key: "")
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Fabric.with([Crashlytics.self])
NumuReviewHelper.incrementActivityCount()
let redirectURL: URL = URL(string: "numu://")!
SpotifyLogin.shared.configure(clientID: "SPOTIFY_CLIENT_ID", clientSecret: "SPOTIFY_CLIENT_SECRET", redirectURL: redirectURL)
NotificationCenter.default.addObserver(
self, selector: #selector(self.runLogInOperations), name: .LoggedOut, object: nil)
NotificationCenter.default.addObserver(
self, selector: #selector(self.ckDataChange), name: Notification.Name.CKAccountChanged, object: nil)
runLogInOperations()
return true
}
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
let handled = SpotifyLogin.shared.applicationOpenURL(url) { (_) in }
return handled
}
@objc func ckDataChange() {
// Need to re-run authorization procedure here.
runLogInOperations()
}
deinit {
NotificationCenter.default.removeObserver(self)
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
pusher.nativePusher.register(deviceToken: deviceToken)
if let username = NumuCredential.shared.getUsername() {
if defaults.newReleased {
pusher.nativePusher.subscribe(interestName: "newReleased_" + username)
print("Turned on new notifications")
} else {
pusher.nativePusher.unsubscribe(interestName: "newReleased_" + username)
print("Turned off new notifications")
}
if defaults.newAnnouncements {
pusher.nativePusher.subscribe(interestName: "newAnnouncements_" + username)
print("Turned on new music friday notifications")
} else {
pusher.nativePusher.unsubscribe(interestName: "newAnnouncements_" + username)
print("Turned off new music friday notifications")
}
if defaults.moreReleases {
pusher.nativePusher.subscribe(interestName: "moreReleases_" + username)
print("Turned on more releases notifications")
} else {
pusher.nativePusher.unsubscribe(interestName: "moreReleases_" + username)
print("Turned off more releases notifications")
}
}
}
@objc fileprivate func runLogInOperations() {
let queue = OperationQueue()
// Get and store CK record ID if available
let getCKUserOperation = GetCKUserOperation()
// If user account exists already, link it to CK or create new account
let registerWithCKOperation = RegisterWithCKOperation()
// Try to auth with any credentials
let authOperation = AuthOperation()
registerWithCKOperation.addDependency(getCKUserOperation)
authOperation.addDependency(registerWithCKOperation)
authOperation.completionBlock = {
print("Finished Auth Process!!!")
DispatchQueue.main.async(execute: {
if defaults.logged {
UIApplication.shared.registerForRemoteNotifications()
}
})
}
queue.addOperations([getCKUserOperation, registerWithCKOperation, authOperation], waitUntilFinished: false)
}
}