This commit is contained in:
Woit 2024-11-29 18:43:27 +01:00
parent 87642a5a10
commit eb037c452a
3 changed files with 41 additions and 45 deletions

View file

@ -8,6 +8,7 @@ struct AnotherIMApp: App {
init() {
DDLog.add(DDOSLogger.sharedInstance, with: .all)
MLProcessLock.initialize(forProcess: "MainApp")
}
var body: some Scene {

View file

@ -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() {

View file

@ -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
}
}