forked from RocketChat/Rocket.Chat.iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppManager.swift
203 lines (162 loc) · 6.39 KB
/
AppManager.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
//
// AppManager.swift
// Rocket.Chat
//
// Created by Rafael Kellermann Streit on 10/10/17.
// Copyright © 2017 Rocket.Chat. All rights reserved.
//
import Foundation
import RealmSwift
struct AppManager {
private static let kApplicationServerURLKey = "RC_SERVER_URL"
/**
The app allows the user to fix a URL and disable the multi-server support
by adding the value "RC_SERVER_URL" to the Info.plist file. This will imply
in not allowing the user to type a custom server URL when authenticating.
- returns: The custom URL, if this app has some URL fixed in the settings.
*/
static var applicationServerURL: URL? {
if let serverURL = Bundle.main.object(forInfoDictionaryKey: kApplicationServerURLKey) as? String {
return URL(string: serverURL)
}
return nil
}
/**
The app won't support multi-server if we have a fixed URL into the app.
- returns: If the server supports multi-server feature.
*/
static var supportsMultiServer: Bool {
return applicationServerURL == nil
}
/**
Default room Id
If set, App will go straight to this room after launching
*/
static var initialRoomId: String?
/**
Room Id for the currently active room.
*/
static var currentRoomId: String? {
return ChatViewController.shared?.subscription?.rid
}
}
extension AppManager {
static func changeSelectedServer(index: Int) {
SocketManager.disconnect { _, _ in
DatabaseManager.selectDatabase(at: index)
DatabaseManager.changeDatabaseInstance(index: index)
AuthSettingsManager.shared.clearCachedSettings()
AuthSettingsManager.shared.updateCachedSettings()
AuthManager.recoverAuthIfNeeded()
reloadApp()
}
}
static func changeToServerIfExists(serverUrl: String, roomId: String? = nil) -> Bool {
guard let index = DatabaseManager.serverIndexForUrl(serverUrl) else {
return false
}
if index != DatabaseManager.selectedIndex {
AppManager.initialRoomId = roomId
changeSelectedServer(index: index)
} else if let roomId = roomId, let subscription = Subscription.find(rid: roomId) {
ChatViewController.shared?.subscription = subscription
} else {
changeSelectedServer(index: index)
}
return true
}
static func addServer(serverUrl: String, credentials: DeepLinkCredentials? = nil, roomId: String? = nil) {
SocketManager.disconnect { _, _ in }
AppManager.initialRoomId = roomId
WindowManager.open(.auth(serverUrl: serverUrl, credentials: credentials))
}
static func changeToOrAddServer(serverUrl: String, credentials: DeepLinkCredentials? = nil, roomId: String? = nil) {
guard let url = URL(string: serverUrl), changeToServerIfExists(serverUrl: url.absoluteString, roomId: roomId) else {
return addServer(serverUrl: serverUrl, credentials: credentials, roomId: roomId)
}
}
static func reloadApp() {
SocketManager.disconnect { (_, _) in
DispatchQueue.main.async {
if AuthManager.isAuthenticated() != nil {
if let currentUser = AuthManager.currentUser() {
BugTrackingCoordinator.identifyCrashReports(withUser: currentUser)
}
WindowManager.open(.chat)
} else {
WindowManager.open(.auth(serverUrl: "", credentials: nil))
}
}
}
}
}
// MARK: Open Rooms
extension AppManager {
static func openDirectMessage(username: String, completion: (() -> Void)? = nil) {
func openDirectMessage() -> Bool {
guard let directMessageRoom = Subscription.find(name: username, subscriptionType: [.directMessage]) else { return false }
let controller = ChatViewController.shared
controller?.subscription = directMessageRoom
completion?()
return true
}
// Check if already have a direct message room with this user
if openDirectMessage() == true {
return
}
// If not, create a new direct message
SubscriptionManager.createDirectMessage(username, completion: { response in
guard !response.isError() else { return }
guard let auth = AuthManager.isAuthenticated() else { return }
SubscriptionManager.updateSubscriptions(auth) { _ in
_ = openDirectMessage()
}
})
}
static func openChannel(name: String) {
func openChannel() -> Bool {
guard let channel = Subscription.find(name: name, subscriptionType: [.channel]) else { return false }
ChatViewController.shared?.subscription = channel
return true
}
// Check if we already have this channel
if openChannel() == true {
return
}
// If not, fetch it
let currentRealm = Realm.current
let request = SubscriptionInfoRequest(roomName: name)
API.current()?.fetch(request, succeeded: { result in
DispatchQueue.main.async {
Realm.executeOnMainThread(realm: currentRealm, { realm in
guard let values = result.channel else { return }
let subscription = Subscription.getOrCreate(realm: realm, values: values, updates: { object in
object?.rid = object?.identifier ?? ""
})
realm.add(subscription, update: true)
})
_ = openChannel()
}
}, errored: nil)
}
}
// MARK: Deep Link
extension AppManager {
static func handleDeepLink(_ url: URL) -> DeepLink? {
guard let deepLink = DeepLink(url: url) else { return nil }
AppManager.handleDeepLink(deepLink)
return deepLink
}
static func handleDeepLink(_ deepLink: DeepLink) {
switch deepLink {
case let .auth(host, credentials):
changeToOrAddServer(serverUrl: host, credentials: credentials)
case let .room(host, roomId):
changeToOrAddServer(serverUrl: host, roomId: roomId)
case let .mention(name):
openDirectMessage(username: name)
case let .channel(name):
openChannel(name: name)
}
}
}