From eb037c452a5c3a2115fb296cd0d57531c950bc41 Mon Sep 17 00:00:00 2001 From: Woit Date: Fri, 29 Nov 2024 18:43:27 +0100 Subject: [PATCH] wip --- Monal/another.im/AnotherIMApp.swift | 1 + Monal/another.im/XMPP/MonalXmppWrapper.swift | 47 +------------------ .../XMPP/Scenaries/ScenarioLogIn.swift | 38 +++++++++++++++ 3 files changed, 41 insertions(+), 45 deletions(-) diff --git a/Monal/another.im/AnotherIMApp.swift b/Monal/another.im/AnotherIMApp.swift index c3ec087..94bf33d 100644 --- a/Monal/another.im/AnotherIMApp.swift +++ b/Monal/another.im/AnotherIMApp.swift @@ -8,6 +8,7 @@ struct AnotherIMApp: App { init() { DDLog.add(DDOSLogger.sharedInstance, with: .all) + MLProcessLock.initialize(forProcess: "MainApp") } var body: some Scene { diff --git a/Monal/another.im/XMPP/MonalXmppWrapper.swift b/Monal/another.im/XMPP/MonalXmppWrapper.swift index 0bc6cfb..ae3dd1f 100644 --- a/Monal/another.im/XMPP/MonalXmppWrapper.swift +++ b/Monal/another.im/XMPP/MonalXmppWrapper.swift @@ -20,7 +20,6 @@ final class MonalXmppWrapper: ObservableObject { init() { // here is some inits (just for now) - MLProcessLock.initialize(forProcess: "MainApp") // init monalxmpp components xmpp = MLXMPPManager.sharedInstance() @@ -43,8 +42,8 @@ final class MonalXmppWrapper: ObservableObject { // MARK: - Public extension MonalXmppWrapper { func tryLogin(_ login: String, _ password: String) async throws { - let loginObject = LoginTry(xmpp: xmpp) - let result = await loginObject.tryLogin(login, password) + let scenario = ScenarioLogIn() + let result = await scenario.tryLogin(login, password) if !result { throw AimErrors.loginError } else { @@ -94,48 +93,6 @@ extension MonalXmppWrapper { } } -// MARK: - Try login from Login screen -private final class LoginTry { - weak var xmpp: MLXMPPManager? - - var successObserver: AnyObject? - var failureObserver: AnyObject? - - init(xmpp: MLXMPPManager) { - self.xmpp = xmpp - } - - // TODO: Добавить автовключение отключенных аккаунтов при попытке ввести тот же JID - // Обработать кейс когда бесячий monalxmpp возвращает nil при попытке добавить тот же JID - func tryLogin(_ login: String, _ password: String) async -> Bool { - async let notify = await withCheckedContinuation { [weak self] continuation in - self?.successObserver = NotificationCenter.default.addObserver(forName: Notification.Name("kMLResourceBoundNotice"), object: nil, queue: .main) { _ in - continuation.resume(returning: true) - } - self?.failureObserver = NotificationCenter.default.addObserver(forName: Notification.Name("kXMPPError"), object: nil, queue: .main) { _ in - continuation.resume(returning: false) - } - } - - defer { - if let successObserver { - NotificationCenter.default.removeObserver(successObserver) - } - if let failureObserver { - NotificationCenter.default.removeObserver(failureObserver) - } - } - - let accountNumber = xmpp?.login(login, password: password) - let result = await notify - if let accountNumber, !result { - xmpp?.removeAccount(forAccountID: accountNumber) - } - - return result - } -} - // MARK: - Handle notifications private extension MonalXmppWrapper { func subscribeToUpdates() { diff --git a/Monal/another.im/XMPP/Scenaries/ScenarioLogIn.swift b/Monal/another.im/XMPP/Scenaries/ScenarioLogIn.swift index fecc4ab..616cb21 100644 --- a/Monal/another.im/XMPP/Scenaries/ScenarioLogIn.swift +++ b/Monal/another.im/XMPP/Scenaries/ScenarioLogIn.swift @@ -1 +1,39 @@ import Foundation +import monalxmpp + +final class ScenarioLogIn { + private let xmpp = MLXMPPManager.sharedInstance() + + private var successObserver: AnyObject? + private var failureObserver: AnyObject? + + // TODO: Добавить автовключение отключенных аккаунтов при попытке ввести тот же JID + // Обработать кейс когда бесячий monalxmpp возвращает nil при попытке добавить тот же JID + func tryLogin(_ login: String, _ password: String) async -> Bool { + async let notify = await withCheckedContinuation { [weak self] continuation in + self?.successObserver = NotificationCenter.default.addObserver(forName: Notification.Name("kMLResourceBoundNotice"), object: nil, queue: .main) { _ in + continuation.resume(returning: true) + } + self?.failureObserver = NotificationCenter.default.addObserver(forName: Notification.Name("kXMPPError"), object: nil, queue: .main) { _ in + continuation.resume(returning: false) + } + } + + defer { + if let successObserver { + NotificationCenter.default.removeObserver(successObserver) + } + if let failureObserver { + NotificationCenter.default.removeObserver(failureObserver) + } + } + + let accountNumber = xmpp.login(login, password: password) + let result = await notify + if let accountNumber, !result { + xmpp.removeAccount(forAccountID: accountNumber) + } + + return result + } +}