From 95f518a582546d4d2fce3068873e617350ace83b Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Wed, 6 Jul 2022 14:07:27 +0100 Subject: [PATCH] ios: stopped state for DB management, suspend quicker/instantly on app termination (#783) * ios: stopped state for DB management, suspend quicker/instantly on app termination * update terminateChat --- apps/ios/Shared/AppDelegate.swift | 5 +++ apps/ios/Shared/Model/SuspendChat.swift | 34 +++++++++++++++---- .../Shared/Views/Database/DatabaseView.swift | 3 ++ apps/ios/SimpleXChat/AppGroup.swift | 1 + 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/apps/ios/Shared/AppDelegate.swift b/apps/ios/Shared/AppDelegate.swift index 895891838e..b06fac9ee0 100644 --- a/apps/ios/Shared/AppDelegate.swift +++ b/apps/ios/Shared/AppDelegate.swift @@ -75,6 +75,11 @@ class AppDelegate: NSObject, UIApplicationDelegate { } } + func applicationWillTerminate(_ application: UIApplication) { + logger.debug("AppDelegate: applicationWillTerminate") + terminateChat() + } + private func receiveMessages(_ completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { let complete = BGManager.shared.completionHandler { logger.debug("AppDelegate: completed BGManager.receiveMessages") diff --git a/apps/ios/Shared/Model/SuspendChat.swift b/apps/ios/Shared/Model/SuspendChat.swift index e19eb64f45..2c0261ae8f 100644 --- a/apps/ios/Shared/Model/SuspendChat.swift +++ b/apps/ios/Shared/Model/SuspendChat.swift @@ -16,6 +16,8 @@ let appSuspendTimeout: Int = 15 // seconds let bgSuspendTimeout: Int = 5 // seconds +let terminationTimeout: Int = 3 // seconds + private func _suspendChat(timeout: Int) { appStateGroupDefault.set(.suspending) apiSuspendChat(timeoutMicroseconds: timeout * 1000000) @@ -25,7 +27,9 @@ private func _suspendChat(timeout: Int) { func suspendChat() { suspendLockQueue.sync { - _suspendChat(timeout: appSuspendTimeout) + if appStateGroupDefault.get() != .stopped { + _suspendChat(timeout: appSuspendTimeout) + } } } @@ -37,18 +41,36 @@ func suspendBgRefresh() { } } +func terminateChat() { + suspendLockQueue.sync { + switch appStateGroupDefault.get() { + case .suspending: + // suspend instantly if already suspending + _chatSuspended() + apiSuspendChat(timeoutMicroseconds: 0) + case .stopped: () + default: + _suspendChat(timeout: terminationTimeout) + } + } +} + func chatSuspended() { suspendLockQueue.sync { if case .suspending = appStateGroupDefault.get() { - logger.debug("chatSuspended") - appStateGroupDefault.set(.suspended) - if ChatModel.shared.chatRunning == true { - ChatReceiver.shared.stop() - } + _chatSuspended() } } } +private func _chatSuspended() { + logger.debug("_chatSuspended") + appStateGroupDefault.set(.suspended) + if ChatModel.shared.chatRunning == true { + ChatReceiver.shared.stop() + } +} + func activateChat(appState: AppState = .active) { suspendLockQueue.sync { appStateGroupDefault.set(appState) diff --git a/apps/ios/Shared/Views/Database/DatabaseView.swift b/apps/ios/Shared/Views/Database/DatabaseView.swift index 9611089914..1b46b43e1b 100644 --- a/apps/ios/Shared/Views/Database/DatabaseView.swift +++ b/apps/ios/Shared/Views/Database/DatabaseView.swift @@ -223,6 +223,7 @@ struct DatabaseView: View { try await apiStopChat() ChatReceiver.shared.stop() await MainActor.run { m.chatRunning = false } + appStateGroupDefault.set(.stopped) } catch let error { await MainActor.run { runChat = true @@ -307,6 +308,7 @@ struct DatabaseView: View { do { try initializeChat(start: true) m.chatDbChanged = false + appStateGroupDefault.set(.active) } catch let error { fatalError("Error starting chat \(responseError(error))") } @@ -318,6 +320,7 @@ struct DatabaseView: View { m.chatRunning = true ChatReceiver.shared.start() chatLastStartGroupDefault.set(Date.now) + appStateGroupDefault.set(.active) } catch let error { runChat = false alert = .error(title: "Error starting chat", error: responseError(error)) diff --git a/apps/ios/SimpleXChat/AppGroup.swift b/apps/ios/SimpleXChat/AppGroup.swift index a1ee992750..708907bd36 100644 --- a/apps/ios/SimpleXChat/AppGroup.swift +++ b/apps/ios/SimpleXChat/AppGroup.swift @@ -23,6 +23,7 @@ public enum AppState: String { case bgRefresh case suspending case suspended + case stopped public var inactive: Bool { switch self {