From 3640f0ec06e38ba85adc8636f6201f6a28d5a0e3 Mon Sep 17 00:00:00 2001 From: Woit Date: Wed, 20 Nov 2024 16:52:49 +0100 Subject: [PATCH] fix weird crash --- Monal/Monal.xcodeproj/project.pbxproj | 27 ++++++--- Monal/another.im/AnotherIMApp.swift | 8 +++ .../another.im/Views/Enter/LoginScreen.swift | 4 +- Monal/another.im/Views/RootView.swift | 2 +- Monal/another.im/XMPP/AimErrors.swift | 3 + Monal/another.im/XMPP/MonalXmppWrapper.swift | 59 ++++++++++++++++++- Monal/monalxmpp/monalxmpp.h | 19 +++--- 7 files changed, 99 insertions(+), 23 deletions(-) create mode 100644 Monal/another.im/XMPP/AimErrors.swift diff --git a/Monal/Monal.xcodeproj/project.pbxproj b/Monal/Monal.xcodeproj/project.pbxproj index 8ae935e..e9f735d 100644 --- a/Monal/Monal.xcodeproj/project.pbxproj +++ b/Monal/Monal.xcodeproj/project.pbxproj @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ 08CAF17FA202CF3CB760D93C /* Pods_NotificationService.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2B7A5555D807EE78C95217FD /* Pods_NotificationService.framework */; }; + 1767C5109B06AA6FDCD990E6 /* AimErrors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 404FB187FD6CB3DD41DAC820 /* AimErrors.swift */; }; 1C8760F8FB99C27D0C3A6ED1 /* RegistrationScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82D4382FFF3D97C4E1D7727C /* RegistrationScreen.swift */; }; 1D3623260D0F684500981E51 /* MonalAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 1D3623250D0F684500981E51 /* MonalAppDelegate.m */; }; 1D60589B0D05DD56006BFB54 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 29B97316FDCFA39411CA2CEA /* main.m */; }; @@ -584,6 +585,7 @@ 3DC5035B2822F5220064C8A7 /* OmemoKeysView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OmemoKeysView.swift; sourceTree = ""; }; 3EB7A7084FA9A8F68A3D251C /* Pods-MonalXMPPUnitTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-MonalXMPPUnitTests.debug.xcconfig"; path = "Target Support Files/Pods-MonalXMPPUnitTests/Pods-MonalXMPPUnitTests.debug.xcconfig"; sourceTree = ""; }; 4049F81F60EA5B7A57A4E9C6 /* Pods-NotificationService.beta.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-NotificationService.beta.xcconfig"; path = "Target Support Files/Pods-NotificationService/Pods-NotificationService.beta.xcconfig"; sourceTree = ""; }; + 404FB187FD6CB3DD41DAC820 /* AimErrors.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = AimErrors.swift; sourceTree = ""; }; 43FFAD161EF5A0B1CB149814 /* Pods-shareSheet.alpha-ios.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-shareSheet.alpha-ios.xcconfig"; path = "Target Support Files/Pods-shareSheet/Pods-shareSheet.alpha-ios.xcconfig"; sourceTree = ""; }; 4862C3A0242FB4F709B8F3FF /* Pods-MonalXMPPUnitTests.beta.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-MonalXMPPUnitTests.beta.xcconfig"; path = "Target Support Files/Pods-MonalXMPPUnitTests/Pods-MonalXMPPUnitTests.beta.xcconfig"; sourceTree = ""; }; 4A614910EEF29D66DD4B37E3 /* Pods-NotificaionService.adhoc.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-NotificaionService.adhoc.xcconfig"; path = "Target Support Files/Pods-NotificaionService/Pods-NotificaionService.adhoc.xcconfig"; sourceTree = ""; }; @@ -1401,7 +1403,7 @@ name = tools; sourceTree = ""; }; - 29B97314FDCFA39411CA2CEA = { + 29B97314FDCFA39411CA2CEA /* CustomTemplate */ = { isa = PBXGroup; children = ( 8414ADF92A7ABAC900EFFCCC /* Packages */, @@ -1455,7 +1457,6 @@ 2D8673A73C41D0B1697FE97D /* LoginScreen.swift */, 82D4382FFF3D97C4E1D7727C /* RegistrationScreen.swift */, ); - name = Enter; path = Enter; sourceTree = ""; }; @@ -1481,6 +1482,7 @@ isa = PBXGroup; children = ( 7E6AF38E2CEB9110004328B5 /* MonalXmppWrapper.swift */, + 404FB187FD6CB3DD41DAC820 /* AimErrors.swift */, ); path = XMPP; sourceTree = ""; @@ -2034,7 +2036,7 @@ eu, "es-AR", ); - mainGroup = 29B97314FDCFA39411CA2CEA; + mainGroup = 29B97314FDCFA39411CA2CEA /* CustomTemplate */; packageReferences = ( C1F5C7AD2777638B0001F295 /* XCRemoteSwiftPackageReference "swift-collections" */, 841898A82957712000FEC77D /* XCRemoteSwiftPackageReference "ViewExtractor" */, @@ -2625,6 +2627,7 @@ 335B19D1B8E6A4D5FE71380C /* WelcomeScreen.swift in Sources */, C11E557980B75AA90738EC9C /* LoginScreen.swift in Sources */, 1C8760F8FB99C27D0C3A6ED1 /* RegistrationScreen.swift in Sources */, + 1767C5109B06AA6FDCD990E6 /* AimErrors.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -3570,7 +3573,7 @@ CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; CODE_SIGN_ENTITLEMENTS = another.im/Resources/another.im.entitlements; - CODE_SIGN_IDENTITY = "Apple Development: Egor Mitrofanov (PM89F56B43)"; + CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = 1; DEBUG_INFORMATION_FORMAT = dwarf; @@ -3607,6 +3610,7 @@ MTL_FAST_MATH = YES; PRODUCT_BUNDLE_IDENTIFIER = com.mitrofanovegor.anotherim; PRODUCT_NAME = "$(TARGET_NAME)"; + PROVISIONING_PROFILE_SPECIFIER = ""; SUPPORTED_PLATFORMS = "iphoneos iphonesimulator"; SUPPORTS_MACCATALYST = NO; SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO; @@ -3654,7 +3658,7 @@ CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; CODE_SIGN_ENTITLEMENTS = another.im/Resources/another.im.entitlements; - CODE_SIGN_IDENTITY = "Apple Development: Egor Mitrofanov (PM89F56B43)"; + CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COPY_PHASE_STRIP = NO; CURRENT_PROJECT_VERSION = 1; @@ -3696,6 +3700,7 @@ MTL_FAST_MATH = YES; PRODUCT_BUNDLE_IDENTIFIER = com.mitrofanovegor.anotherim; PRODUCT_NAME = "$(TARGET_NAME)"; + PROVISIONING_PROFILE_SPECIFIER = ""; SDKROOT = iphoneos; SUPPORTED_PLATFORMS = "iphoneos iphonesimulator"; SUPPORTS_MACCATALYST = NO; @@ -3745,7 +3750,7 @@ CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; CODE_SIGN_ENTITLEMENTS = another.im/Resources/another.im.entitlements; - CODE_SIGN_IDENTITY = "Apple Development: Egor Mitrofanov (PM89F56B43)"; + CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COPY_PHASE_STRIP = NO; CURRENT_PROJECT_VERSION = 1; @@ -3787,6 +3792,7 @@ MTL_FAST_MATH = YES; PRODUCT_BUNDLE_IDENTIFIER = com.mitrofanovegor.anotherim; PRODUCT_NAME = "$(TARGET_NAME)"; + PROVISIONING_PROFILE_SPECIFIER = ""; SDKROOT = iphoneos; SUPPORTED_PLATFORMS = "iphoneos iphonesimulator"; SUPPORTS_MACCATALYST = NO; @@ -3836,7 +3842,7 @@ CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; CODE_SIGN_ENTITLEMENTS = another.im/Resources/another.im.entitlements; - CODE_SIGN_IDENTITY = "Apple Development: Egor Mitrofanov (PM89F56B43)"; + CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COPY_PHASE_STRIP = NO; CURRENT_PROJECT_VERSION = 1; @@ -3878,6 +3884,7 @@ MTL_FAST_MATH = YES; PRODUCT_BUNDLE_IDENTIFIER = com.mitrofanovegor.anotherim; PRODUCT_NAME = "$(TARGET_NAME)"; + PROVISIONING_PROFILE_SPECIFIER = ""; SDKROOT = iphoneos; SUPPORTED_PLATFORMS = "iphoneos iphonesimulator"; SUPPORTS_MACCATALYST = NO; @@ -3927,7 +3934,7 @@ CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; CODE_SIGN_ENTITLEMENTS = another.im/Resources/another.im.entitlements; - CODE_SIGN_IDENTITY = "Apple Development: Egor Mitrofanov (PM89F56B43)"; + CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COPY_PHASE_STRIP = NO; CURRENT_PROJECT_VERSION = 1; @@ -3969,6 +3976,7 @@ MTL_FAST_MATH = YES; PRODUCT_BUNDLE_IDENTIFIER = com.mitrofanovegor.anotherim; PRODUCT_NAME = "$(TARGET_NAME)"; + PROVISIONING_PROFILE_SPECIFIER = ""; SDKROOT = iphoneos; SUPPORTED_PLATFORMS = "iphoneos iphonesimulator"; SUPPORTS_MACCATALYST = NO; @@ -4018,7 +4026,7 @@ CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; CODE_SIGN_ENTITLEMENTS = another.im/Resources/another.im.entitlements; - CODE_SIGN_IDENTITY = "Apple Development: Egor Mitrofanov (PM89F56B43)"; + CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COPY_PHASE_STRIP = NO; CURRENT_PROJECT_VERSION = 1; @@ -4060,6 +4068,7 @@ MTL_FAST_MATH = YES; PRODUCT_BUNDLE_IDENTIFIER = com.mitrofanovegor.anotherim; PRODUCT_NAME = "$(TARGET_NAME)"; + PROVISIONING_PROFILE_SPECIFIER = ""; SDKROOT = iphoneos; SUPPORTED_PLATFORMS = "iphoneos iphonesimulator"; SUPPORTS_MACCATALYST = NO; diff --git a/Monal/another.im/AnotherIMApp.swift b/Monal/another.im/AnotherIMApp.swift index 12ddadd..cf5e028 100644 --- a/Monal/another.im/AnotherIMApp.swift +++ b/Monal/another.im/AnotherIMApp.swift @@ -2,6 +2,7 @@ import SwiftUI @main struct AnotherIMApp: App { + @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate @StateObject var wrapper = MonalXmppWrapper() var body: some Scene { @@ -11,3 +12,10 @@ struct AnotherIMApp: App { } } } + +class AppDelegate: NSObject, UIApplicationDelegate { + // func application(_: UIApplication, willFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool { + // // [IPC initializeForProcess:@"MainApp"]; + // // [MLProcessLock initializeForProcess:@"MainApp"]; + // } +} diff --git a/Monal/another.im/Views/Enter/LoginScreen.swift b/Monal/another.im/Views/Enter/LoginScreen.swift index 09de1c6..315207b 100644 --- a/Monal/another.im/Views/Enter/LoginScreen.swift +++ b/Monal/another.im/Views/Enter/LoginScreen.swift @@ -98,8 +98,8 @@ struct LoginScreen: View { private func tryLogin() async { do { - // try await clientsStore.tryLogin(jidStr, pass) - router.dismissScreen() + try await wrapper.tryLogin(jidStr, pass) + wrapper.accountsAvailability = .someEnabled } catch { router.showAlert( .alert, diff --git a/Monal/another.im/Views/RootView.swift b/Monal/another.im/Views/RootView.swift index 3dabf5a..da73dea 100644 --- a/Monal/another.im/Views/RootView.swift +++ b/Monal/another.im/Views/RootView.swift @@ -7,7 +7,7 @@ struct RootView: View { var body: some View { if wrapper.accountsAvailability == .someEnabled { // main flow here - EmptyView() + Text("Test ME!") } else { RouterView { _ in WelcomeScreen() diff --git a/Monal/another.im/XMPP/AimErrors.swift b/Monal/another.im/XMPP/AimErrors.swift new file mode 100644 index 0000000..dd621d5 --- /dev/null +++ b/Monal/another.im/XMPP/AimErrors.swift @@ -0,0 +1,3 @@ +enum AimErrors: Error { + case loginError +} diff --git a/Monal/another.im/XMPP/MonalXmppWrapper.swift b/Monal/another.im/XMPP/MonalXmppWrapper.swift index 86ba822..65606dd 100644 --- a/Monal/another.im/XMPP/MonalXmppWrapper.swift +++ b/Monal/another.im/XMPP/MonalXmppWrapper.swift @@ -10,15 +10,27 @@ enum AccountsAvailability { final class MonalXmppWrapper: ObservableObject { @Published var accountsAvailability: AccountsAvailability = .noAccounts - let xmpp: MLXMPPManager - let db: DataLayer + private let xmpp: MLXMPPManager + private let db: DataLayer init() { xmpp = MLXMPPManager.sharedInstance() db = DataLayer.sharedInstance() + // here is some inits (just for now) + MLProcessLock.initialize(forProcess: "MainApp") + checkAccountsOnLoad() } + + // 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 + } + } } // MARK: - Accounts @@ -37,3 +49,46 @@ private extension MonalXmppWrapper { } } } + +// MARK: - 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) { 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) + } + return result + } +} diff --git a/Monal/monalxmpp/monalxmpp.h b/Monal/monalxmpp/monalxmpp.h index a8965da..07422f2 100644 --- a/Monal/monalxmpp/monalxmpp.h +++ b/Monal/monalxmpp/monalxmpp.h @@ -14,17 +14,18 @@ FOUNDATION_EXPORT double monalxmppVersionNumber; //! Project version string for monalxmpp. FOUNDATION_EXPORT const unsigned char monalxmppVersionString[]; -#import "MLContact.h" #import "DataLayer.h" -#import "xmpp.h" -#import "MLOMEMO.h" -#import "MLSignalStore.h" -#import "MLXMPPManager.h" +#import "HelperTools+Quicksy_CountryCodes.h" +#import "HelperTools.h" +#import "MLCall.h" +#import "MLContact.h" +#import "MLDelayableTimer.h" #import "MLImageManager.h" #import "MLMucProcessor.h" +#import "MLOMEMO.h" +#import "MLProcessLock.h" +#import "MLSignalStore.h" #import "MLVoIPProcessor.h" -#import "MLCall.h" -#import "HelperTools.h" -#import "HelperTools+Quicksy_CountryCodes.h" -#import "MLDelayableTimer.h" +#import "MLXMPPManager.h" #import "Quicksy_Country.h" +#import "xmpp.h"