2024-11-19 12:59:22 +00:00
|
|
|
|
import Foundation
|
|
|
|
|
import monalxmpp
|
|
|
|
|
|
|
|
|
|
final class MonalXmppWrapper: ObservableObject {
|
2024-11-21 16:03:55 +00:00
|
|
|
|
@Published private(set) var accountsAvailability: AccountsAvailability = .noAccounts
|
|
|
|
|
@Published private(set) var accounts: [Account] = []
|
|
|
|
|
@Published private(set) var contacts: [Contact] = []
|
2024-11-19 12:59:22 +00:00
|
|
|
|
|
2024-11-20 15:52:49 +00:00
|
|
|
|
private let xmpp: MLXMPPManager
|
|
|
|
|
private let db: DataLayer
|
2024-11-19 12:59:22 +00:00
|
|
|
|
|
2024-11-21 16:03:55 +00:00
|
|
|
|
private var notificationObservers: [AnyObject] = []
|
|
|
|
|
private var isInitialized: Bool = false
|
|
|
|
|
|
2024-11-19 12:59:22 +00:00
|
|
|
|
init() {
|
2024-11-20 15:52:49 +00:00
|
|
|
|
// here is some inits (just for now)
|
|
|
|
|
MLProcessLock.initialize(forProcess: "MainApp")
|
|
|
|
|
|
2024-11-21 13:32:38 +00:00
|
|
|
|
// init monalxmpp components
|
|
|
|
|
xmpp = MLXMPPManager.sharedInstance()
|
|
|
|
|
db = DataLayer.sharedInstance()
|
|
|
|
|
|
2024-11-21 16:03:55 +00:00
|
|
|
|
// subscribe to monalxmpp notifications and fire notification for update
|
|
|
|
|
subscribeToUpdates()
|
|
|
|
|
NotificationCenter.default.post(name: Notification.Name(kMonalRefresh), object: nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
|
notificationObservers.forEach { NotificationCenter.default.removeObserver($0) }
|
2024-11-19 12:59:22 +00:00
|
|
|
|
}
|
2024-11-20 15:52:49 +00:00
|
|
|
|
|
|
|
|
|
// try login
|
|
|
|
|
func tryLogin(_ login: String, _ password: String) async throws {
|
|
|
|
|
let loginObject = LoginTry(xmpp: xmpp)
|
|
|
|
|
let result = await loginObject.tryLogin(login, password)
|
|
|
|
|
if !result {
|
|
|
|
|
throw AimErrors.loginError
|
2024-11-19 12:59:22 +00:00
|
|
|
|
} else {
|
2024-11-21 16:03:55 +00:00
|
|
|
|
NotificationCenter.default.post(name: Notification.Name(kMonalRefresh), object: nil)
|
2024-11-19 12:59:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-20 15:52:49 +00:00
|
|
|
|
|
2024-11-21 13:32:38 +00:00
|
|
|
|
// MARK: - Try login from Login screen
|
2024-11-20 15:52:49 +00:00
|
|
|
|
private final class LoginTry {
|
|
|
|
|
weak var xmpp: MLXMPPManager?
|
|
|
|
|
|
|
|
|
|
var successObserver: AnyObject?
|
|
|
|
|
var failureObserver: AnyObject?
|
|
|
|
|
|
|
|
|
|
init(xmpp: MLXMPPManager) {
|
|
|
|
|
self.xmpp = xmpp
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-21 13:32:38 +00:00
|
|
|
|
// TODO: Добавить автовключение отключенных аккаунтов при попытке ввести тот же JID
|
2024-11-20 15:52:49 +00:00
|
|
|
|
// Обработать кейс когда бесячий 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) { notification in
|
|
|
|
|
print(notification.debugDescription)
|
|
|
|
|
continuation.resume(returning: true)
|
|
|
|
|
}
|
|
|
|
|
self?.failureObserver = NotificationCenter.default.addObserver(forName: Notification.Name("kXMPPError"), object: nil, queue: .main) { notification in
|
|
|
|
|
print(notification.debugDescription)
|
|
|
|
|
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)
|
|
|
|
|
}
|
2024-11-20 19:51:05 +00:00
|
|
|
|
|
2024-11-20 15:52:49 +00:00
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-21 16:03:55 +00:00
|
|
|
|
|
|
|
|
|
// MARK: - Handle notifications
|
|
|
|
|
private extension MonalXmppWrapper {
|
|
|
|
|
func subscribeToUpdates() {
|
|
|
|
|
let generalRefresh = NotificationCenter.default.addObserver(forName: Notification.Name(kMonalRefresh), object: nil, queue: .main) { [weak self] _ in
|
|
|
|
|
// get accounts
|
|
|
|
|
let accounts = self?.db.accountList()
|
|
|
|
|
.compactMap { dict -> Account? in
|
|
|
|
|
guard let dict = dict as? NSDictionary else { return nil }
|
|
|
|
|
return Account(dict)
|
|
|
|
|
} ?? []
|
|
|
|
|
self?.accounts = accounts
|
|
|
|
|
|
|
|
|
|
// start connect if it initialization process
|
|
|
|
|
if !(self?.isInitialized ?? true) {
|
|
|
|
|
self?.xmpp.reconnectAll()
|
|
|
|
|
self?.isInitialized = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// mark accounts availability
|
|
|
|
|
if accounts.isEmpty {
|
|
|
|
|
self?.accountsAvailability = .noAccounts
|
|
|
|
|
} else if !accounts.filter({ $0.isEnabled }).isEmpty {
|
|
|
|
|
self?.accountsAvailability = .allDisabled
|
|
|
|
|
} else {
|
|
|
|
|
self?.accountsAvailability = .someEnabled
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get contacts for active accounts
|
|
|
|
|
//
|
|
|
|
|
}
|
|
|
|
|
notificationObservers.append(generalRefresh)
|
|
|
|
|
}
|
|
|
|
|
}
|