diff --git a/ios/NotificationService/NotificationService.swift b/ios/NotificationService/NotificationService.swift index d2d31f720..6960d47cc 100644 --- a/ios/NotificationService/NotificationService.swift +++ b/ios/NotificationService/NotificationService.swift @@ -16,7 +16,7 @@ class NotificationService: UNNotificationServiceExtension { return } - rocketchat = RocketChat.instanceForServer(server: data.host.removeTrailingSlash()) + rocketchat = RocketChat(server: data.host.removeTrailingSlash()) // If the notification has the content on the payload, show it if data.notificationType != .messageIdOnly { @@ -35,6 +35,10 @@ class NotificationService: UNNotificationServiceExtension { } } + override func serviceExtensionTimeWillExpire() { + rocketchat = nil + } + func processPayload(payload: Payload) { // If is a encrypted message if payload.messageType == .e2e { diff --git a/ios/ReplyNotification.swift b/ios/ReplyNotification.swift index 5d0840632..b21b219ee 100644 --- a/ios/ReplyNotification.swift +++ b/ios/ReplyNotification.swift @@ -32,7 +32,7 @@ class ReplyNotification: RNNotificationEventHandler { if let data = (notification["ejson"] as? String)?.data(using: .utf8) { if let payload = try? JSONDecoder().decode(Payload.self, from: data), let rid = payload.rid { if let msg = (response as? UNTextInputNotificationResponse)?.userText { - let rocketchat = RocketChat.instanceForServer(server: payload.host.removeTrailingSlash()) + let rocketchat = RocketChat(server: payload.host.removeTrailingSlash()) let backgroundTask = UIApplication.shared.beginBackgroundTask(expirationHandler: nil) rocketchat.sendMessage(rid: rid, message: msg, threadIdentifier: payload.tmid) { response in guard let response = response, response.success else { diff --git a/ios/Shared/RocketChat/API/API.swift b/ios/Shared/RocketChat/API/API.swift index 68b2a7c0e..5e9953a4d 100644 --- a/ios/Shared/RocketChat/API/API.swift +++ b/ios/Shared/RocketChat/API/API.swift @@ -34,19 +34,21 @@ final class API { final let credentials: Credentials? final let decoder = JSONDecoder() - static var instances: [Server: API] = [:] - convenience init?(server: Server) { guard let server = URL(string: server.removeTrailingSlash()) else { return nil } - self.init(server: server) + guard let credentials = Storage().getCredentials(server: server.absoluteString) else { + return nil + } + + self.init(server: server, credentials: credentials) } - init(server: URL) { + init(server: URL, credentials: Credentials) { self.server = server - self.credentials = Storage.shared.getCredentials(server: server.absoluteString) + self.credentials = credentials } func fetch(request: T, retry: Retry? = nil, completion: @escaping((APIResponse) -> Void)) { diff --git a/ios/Shared/RocketChat/Encryption.swift b/ios/Shared/RocketChat/Encryption.swift index e8a7956d4..97b1f28ba 100644 --- a/ios/Shared/RocketChat/Encryption.swift +++ b/ios/Shared/RocketChat/Encryption.swift @@ -37,8 +37,9 @@ final class Encryption { private final let encoder = JSONEncoder() init(server: String, rid: String) { - self.privateKey = Storage.shared.getPrivateKey(server: server) - self.credentials = Storage.shared.getCredentials(server: server) + let storage = Storage() + self.privateKey = storage.getPrivateKey(server: server) + self.credentials = storage.getCredentials(server: server) self.server = server self.rid = rid diff --git a/ios/Shared/RocketChat/RocketChat.swift b/ios/Shared/RocketChat/RocketChat.swift index 294bfebce..a13c11933 100644 --- a/ios/Shared/RocketChat/RocketChat.swift +++ b/ios/Shared/RocketChat/RocketChat.swift @@ -15,7 +15,6 @@ final class RocketChat { let server: Server let api: API? - static var instances: [Server: RocketChat] = [:] var encryptionInstances: [RoomId: Encryption] = [:] static private var queue = DispatchQueue(label: "chat.rocket.instanceQueue") @@ -26,18 +25,6 @@ final class RocketChat { self.api = API(server: server) } - static func instanceForServer(server: Server) -> RocketChat { - queue.sync { - if let rocketchat = instances[server] { - return rocketchat - } - - let rocketchat = RocketChat(server: server) - instances[server] = rocketchat - return rocketchat - } - } - func getPushWithId(_ msgId: String, completion: @escaping((Notification?) -> Void)) { api?.fetch(request: PushRequest(msgId: msgId), retry: Retry(retries: 4)) { response in switch response { diff --git a/ios/Shared/RocketChat/Storage.swift b/ios/Shared/RocketChat/Storage.swift index 7f2496897..709817104 100644 --- a/ios/Shared/RocketChat/Storage.swift +++ b/ios/Shared/RocketChat/Storage.swift @@ -14,8 +14,6 @@ struct Credentials { } class Storage { - static let shared = Storage() - final var mmkv: MMKV? = nil init() {