This commit is contained in:
Woit 2024-12-02 16:50:23 +01:00
parent f8ce451b63
commit 6a4acfb6f3
15 changed files with 157 additions and 149 deletions

View file

@ -13,6 +13,7 @@
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 */; };
2075230B91C7DB319CCBA11D /* WrapperXMPP.swift in Sources */ = {isa = PBXBuildFile; fileRef = 998D2BEBAB8C1CA1AC471B4A /* WrapperXMPP.swift */; };
20D3611C2C10E12500E46587 /* BoardingCards.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20D3611B2C10E12500E46587 /* BoardingCards.swift */; };
20D8C65E2C3C37FE00E6BDA2 /* MediaGallery.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20D8C65D2C3C37FE00E6BDA2 /* MediaGallery.swift */; };
20ED55852BADDA5C0005783E /* GeneralSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20ED55842BADDA5C0005783E /* GeneralSettings.swift */; };
@ -139,7 +140,6 @@
7E1C0AC72CEF68C000B8FEC0 /* MainTabScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E1C0AC62CEF68C000B8FEC0 /* MainTabScreen.swift */; };
7E1C0ACB2CEF6C7800B8FEC0 /* ContactsScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E1C0AC92CEF6C7800B8FEC0 /* ContactsScreen.swift */; };
7E1C0ACC2CEF6C7800B8FEC0 /* AddContactOrChannelScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E1C0AC82CEF6C7800B8FEC0 /* AddContactOrChannelScreen.swift */; };
7E6AF38F2CEB9110004328B5 /* MonalXmppWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E6AF38E2CEB9110004328B5 /* MonalXmppWrapper.swift */; };
7E6AF3902CEB982F004328B5 /* sworim.sqlite in Resources */ = {isa = PBXBuildFile; fileRef = 2601D9CA0FBF25EF004DB939 /* sworim.sqlite */; };
7E71758D2CECC5C70059F30B /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 7E71758B2CECC5C70059F30B /* Localizable.strings */; };
7E71758E2CECC5C70059F30B /* server_features.plist in Resources */ = {isa = PBXBuildFile; fileRef = 7E71758A2CECC5C70059F30B /* server_features.plist */; };
@ -242,6 +242,7 @@
84FC37552897521500634E3E /* snprintf.m in Sources */ = {isa = PBXBuildFile; fileRef = 84FC37542897521400634E3E /* snprintf.m */; };
84FC37572897523500634E3E /* metamacros.h in Headers */ = {isa = PBXBuildFile; fileRef = 84FC37562897523500634E3E /* metamacros.h */; };
84FC375928981A5600634E3E /* PasswordMigration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84FC375828981A5600634E3E /* PasswordMigration.swift */; };
90722912B34CD0DFD6ED291E /* WrapperChat.swift in Sources */ = {isa = PBXBuildFile; fileRef = 585781EBEDA697BA88FC803D /* WrapperChat.swift */; };
952EBC802BAF72F300183DBF /* DebugView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 952EBC7F2BAF72F300183DBF /* DebugView.swift */; };
C10490492612ED2F0054AC9E /* MLEmoji.swift in Sources */ = {isa = PBXBuildFile; fileRef = C10490482612ED2F0054AC9E /* MLEmoji.swift */; };
C10490E32612F3D00054AC9E /* MLCrypto.swift in Sources */ = {isa = PBXBuildFile; fileRef = C10490E22612F3D00054AC9E /* MLCrypto.swift */; };
@ -646,6 +647,7 @@
54E594BC2523C34A00E4172B /* MLPubSub.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MLPubSub.m; sourceTree = "<group>"; };
54F0B81828231690003664BD /* WelcomeLogIn.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WelcomeLogIn.swift; sourceTree = "<group>"; };
54F0B81B282316F5003664BD /* RegisterAccount.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RegisterAccount.swift; sourceTree = "<group>"; };
585781EBEDA697BA88FC803D /* WrapperChat.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = WrapperChat.swift; sourceTree = "<group>"; };
59F4A459FBC6040A0F8CCAF3 /* Pods-NotificaionService.alpha-catalyst.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-NotificaionService.alpha-catalyst.xcconfig"; path = "Target Support Files/Pods-NotificaionService/Pods-NotificaionService.alpha-catalyst.xcconfig"; sourceTree = "<group>"; };
5B9C86E0A568734587FE9BA2 /* Pods_monalxmpp.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_monalxmpp.framework; sourceTree = BUILT_PRODUCTS_DIR; };
5BACDACCFE405FE0C903C897 /* Pods-MonalUITests.alpha.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-MonalUITests.alpha.xcconfig"; path = "Target Support Files/Pods-MonalUITests/Pods-MonalUITests.alpha.xcconfig"; sourceTree = "<group>"; };
@ -664,7 +666,6 @@
7E1C0AC62CEF68C000B8FEC0 /* MainTabScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainTabScreen.swift; sourceTree = "<group>"; };
7E1C0AC82CEF6C7800B8FEC0 /* AddContactOrChannelScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddContactOrChannelScreen.swift; sourceTree = "<group>"; };
7E1C0AC92CEF6C7800B8FEC0 /* ContactsScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactsScreen.swift; sourceTree = "<group>"; };
7E6AF38E2CEB9110004328B5 /* MonalXmppWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MonalXmppWrapper.swift; sourceTree = "<group>"; };
7E6E446D2CECB76500505D5C /* another.im.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = another.im.entitlements; sourceTree = "<group>"; };
7E7175892CECC5C70059F30B /* launchscreen.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = launchscreen.storyboard; sourceTree = "<group>"; };
7E71758A2CECC5C70059F30B /* server_features.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = server_features.plist; sourceTree = "<group>"; };
@ -775,6 +776,7 @@
9705AFFB59AF72A9B79C1D7B /* Pods-MonalXMPPUnitTests.adhoc.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-MonalXMPPUnitTests.adhoc.xcconfig"; path = "Target Support Files/Pods-MonalXMPPUnitTests/Pods-MonalXMPPUnitTests.adhoc.xcconfig"; sourceTree = "<group>"; };
9760CF4718351300C4256921 /* Pods-shareSheet.appstore-quicksy.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-shareSheet.appstore-quicksy.xcconfig"; path = "Target Support Files/Pods-shareSheet/Pods-shareSheet.appstore-quicksy.xcconfig"; sourceTree = "<group>"; };
9899D670570190DCBE9EEDDB /* Pods-monalxmpp.appstore-quicksy.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-monalxmpp.appstore-quicksy.xcconfig"; path = "Target Support Files/Pods-monalxmpp/Pods-monalxmpp.appstore-quicksy.xcconfig"; sourceTree = "<group>"; };
998D2BEBAB8C1CA1AC471B4A /* WrapperXMPP.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = WrapperXMPP.swift; sourceTree = "<group>"; };
A2ED40D3515305509E3E166C /* Pods-MonalUITests.alpha-catalyst.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-MonalUITests.alpha-catalyst.xcconfig"; path = "Target Support Files/Pods-MonalUITests/Pods-MonalUITests.alpha-catalyst.xcconfig"; sourceTree = "<group>"; };
A4C686567AC126CDDFB1BE44 /* Pods-NotificaionService.beta.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-NotificaionService.beta.xcconfig"; path = "Target Support Files/Pods-NotificaionService/Pods-NotificaionService.beta.xcconfig"; sourceTree = "<group>"; };
AA697C1F9B9637B86665DFF1 /* Pods-NotificationService.appstore-quicksy.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-NotificationService.appstore-quicksy.xcconfig"; path = "Target Support Files/Pods-NotificationService/Pods-NotificationService.appstore-quicksy.xcconfig"; sourceTree = "<group>"; };
@ -1452,7 +1454,7 @@
name = tools;
sourceTree = "<group>";
};
29B97314FDCFA39411CA2CEA /* CustomTemplate */ = {
29B97314FDCFA39411CA2CEA = {
isa = PBXGroup;
children = (
8414ADF92A7ABAC900EFFCCC /* Packages */,
@ -1578,10 +1580,10 @@
7E8D7AE52CECD05C009AD3DF /* XMPP */ = {
isa = PBXGroup;
children = (
7E6AF38E2CEB9110004328B5 /* MonalXmppWrapper.swift */,
404FB187FD6CB3DD41DAC820 /* AimErrors.swift */,
E23C31EA05858AFC0AB43D06 /* Scenaries */,
8C5F2E42A69F9A7B93A20F78 /* Models */,
ABC8671064FCFD060C57E114 /* Wrappers */,
);
path = XMPP;
sourceTree = "<group>";
@ -1787,6 +1789,16 @@
path = Models;
sourceTree = "<group>";
};
ABC8671064FCFD060C57E114 /* Wrappers */ = {
isa = PBXGroup;
children = (
998D2BEBAB8C1CA1AC471B4A /* WrapperXMPP.swift */,
585781EBEDA697BA88FC803D /* WrapperChat.swift */,
);
name = Wrappers;
path = Wrappers;
sourceTree = "<group>";
};
C1049187261301530054AC9E /* MonalXMPPUnitTests */ = {
isa = PBXGroup;
children = (
@ -2174,7 +2186,7 @@
eu,
"es-AR",
);
mainGroup = 29B97314FDCFA39411CA2CEA /* CustomTemplate */;
mainGroup = 29B97314FDCFA39411CA2CEA;
packageReferences = (
C1F5C7AD2777638B0001F295 /* XCRemoteSwiftPackageReference "swift-collections" */,
841898A82957712000FEC77D /* XCRemoteSwiftPackageReference "ViewExtractor" */,
@ -2780,7 +2792,6 @@
7E1C0ACC2CEF6C7800B8FEC0 /* AddContactOrChannelScreen.swift in Sources */,
7E8D7AFE2CECEDB3009AD3DF /* LoadingScreen.swift in Sources */,
7E995F252CEAC5D2005B30EE /* RootView.swift in Sources */,
7E6AF38F2CEB9110004328B5 /* MonalXmppWrapper.swift in Sources */,
335B19D1B8E6A4D5FE71380C /* WelcomeScreen.swift in Sources */,
C11E557980B75AA90738EC9C /* LoginScreen.swift in Sources */,
1C8760F8FB99C27D0C3A6ED1 /* RegistrationScreen.swift in Sources */,
@ -2790,6 +2801,8 @@
07D2570A3091519439B47A6A /* Chat.swift in Sources */,
DEDA22BC3DBA90E586F6B7CE /* Message.swift in Sources */,
5C2B6A5C14F38F315ECC3F5E /* ScenarioLogIn.swift in Sources */,
2075230B91C7DB319CCBA11D /* WrapperXMPP.swift in Sources */,
90722912B34CD0DFD6ED291E /* WrapperChat.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -3737,7 +3750,7 @@
CODE_SIGN_ENTITLEMENTS = another.im/Resources/another.im.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 2;
CURRENT_PROJECT_VERSION = 3;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = K78H7BT98L;
@ -3828,7 +3841,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 2;
CURRENT_PROJECT_VERSION = 3;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = K78H7BT98L;
@ -3925,7 +3938,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 2;
CURRENT_PROJECT_VERSION = 3;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = K78H7BT98L;
@ -4022,7 +4035,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 2;
CURRENT_PROJECT_VERSION = 3;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = K78H7BT98L;
@ -4119,7 +4132,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 2;
CURRENT_PROJECT_VERSION = 3;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = K78H7BT98L;
@ -4216,7 +4229,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 2;
CURRENT_PROJECT_VERSION = 3;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = K78H7BT98L;

View file

@ -4,7 +4,7 @@ import SwiftUI
@main
struct AnotherIMApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@StateObject var wrapper = MonalXmppWrapper()
@StateObject var wrapper = WrapperXMPP()
init() {
DDLog.add(DDOSLogger.sharedInstance, with: .all)

View file

@ -2,7 +2,7 @@ import Foundation
import SwiftUI
struct ConversationMessageRow: View {
@EnvironmentObject var chatWrapper: MonalChatWrapper
@EnvironmentObject var chatWrapper: WrapperXMPP
let message: Message
@State private var offset: CGSize = .zero

View file

@ -4,7 +4,7 @@ import SwiftUI
struct ConversationScreen: View {
@Environment(\.router) var router
@EnvironmentObject var chatWrapper: MonalChatWrapper
@EnvironmentObject var chatWrapper: WrapperChat
@State private var autoScroll = true
@State private var firstIsVisible = true

View file

@ -3,7 +3,7 @@ import Foundation
import SwiftUI
struct ConversationSettingsScreen: View {
@EnvironmentObject var chatWrapper: MonalChatWrapper
@EnvironmentObject var chatWrapper: WrapperChat
@Environment(\.router) var router
var body: some View {

View file

@ -3,7 +3,7 @@ import UIKit
struct ConversationTextInput: View {
@Environment(\.router) var router
@EnvironmentObject var chatWrapper: MonalChatWrapper
@EnvironmentObject var chatWrapper: WrapperChat
@State private var messageStr = ""
@FocusState private var isFocused: Bool

View file

@ -2,7 +2,7 @@ import Combine
import SwiftUI
struct LoginScreen: View {
@EnvironmentObject var wrapper: MonalXmppWrapper
@EnvironmentObject var wrapper: WrapperXMPP
@Environment(\.router) var router
enum Field {

View file

@ -1,7 +1,7 @@
import SwiftUI
struct WelcomeScreen: View {
@EnvironmentObject var wrapper: MonalXmppWrapper
@EnvironmentObject var wrapper: WrapperXMPP
@Environment(\.router) var router
var body: some View {

View file

@ -1,7 +1,7 @@
import SwiftUI
struct ChatsListScreen: View {
@EnvironmentObject var wrapper: MonalXmppWrapper
@EnvironmentObject var wrapper: WrapperXMPP
@Environment(\.router) var router
var body: some View {
@ -43,7 +43,7 @@ struct ChatsListScreen: View {
}
private struct ChatsRow: View {
@EnvironmentObject var wrapper: MonalXmppWrapper
@EnvironmentObject var wrapper: WrapperXMPP
@Environment(\.router) var router
var chat: Chat

View file

@ -1,7 +1,7 @@
import SwiftUI
struct AddContactOrChannelScreen: View {
@EnvironmentObject var wrapper: MonalXmppWrapper
@EnvironmentObject var wrapper: WrapperXMPP
@Environment(\.router) var router
enum Field {

View file

@ -1,7 +1,7 @@
import SwiftUI
struct ContactsScreen: View {
@EnvironmentObject var wrapper: MonalXmppWrapper
@EnvironmentObject var wrapper: WrapperXMPP
@Environment(\.router) var router
var body: some View {
@ -43,7 +43,7 @@ struct ContactsScreen: View {
}
private struct ContactsScreenRow: View {
@EnvironmentObject var wrapper: MonalXmppWrapper
@EnvironmentObject var wrapper: WrapperXMPP
@Environment(\.router) var router
var contact: Contact

View file

@ -9,7 +9,7 @@ private enum Tab {
}
struct MainTabScreen: View {
@EnvironmentObject var wrapper: MonalXmppWrapper
@EnvironmentObject var wrapper: WrapperXMPP
@State private var selectedTab: Tab = .chats

View file

@ -2,7 +2,7 @@ import SwiftfulRouting
import SwiftUI
struct RootView: View {
@EnvironmentObject var wrapper: MonalXmppWrapper
@EnvironmentObject var wrapper: WrapperXMPP
var body: some View {
if wrapper.accountsAvailability == .someEnabled {

View file

@ -0,0 +1,111 @@
import Foundation
import monalxmpp
final class WrapperChat: ObservableObject {
@Published private(set) var messages: [Message] = []
@Published var replyText: String = ""
@Published private(set) var mamRequestInProgress = false
@Published var isOmemoEnabled: Bool {
didSet {
toggleOmemo(isOmemoEnabled)
}
}
let contact: Contact
private let monalContact: MLContact
private let account: Account
private let xmpp: MLXMPPManager
private let db: DataLayer
private var notificationObservers: [AnyObject] = []
init(account: Account, contact: Contact, db: DataLayer, xmpp: MLXMPPManager) {
self.contact = contact
self.account = account
self.db = db
self.xmpp = xmpp
// swiftlint:disable:next force_unwrapping
monalContact = db.contactList().first { $0.accountID.intValue == contact.ownerId && $0.contactJid == contact.contactJid }!
isOmemoEnabled = monalContact.isEncrypted
subscribe()
NotificationCenter.default.post(name: Notification.Name(kMonalNewMessageNotice), object: nil)
//
}
deinit {
notificationObservers.forEach { NotificationCenter.default.removeObserver($0) }
}
var chatTitle: String {
contact.name
}
func sendText(_ text: String) {
let newMessageId = UUID().uuidString
_ = db.addMessageHistory(
to: contact.contactJid,
forAccount: monalContact.accountID,
withMessage: text,
actuallyFrom: account.jid,
withId: newMessageId,
encrypted: monalContact.isEncrypted,
messageType: kMessageTypeText,
mimeType: nil,
size: nil
)
print(newMessageId)
xmpp.sendMessage(text, to: monalContact, isEncrypted: monalContact.isEncrypted, isUpload: false, messageId: newMessageId)
}
func requestMAM() {
if mamRequestInProgress { return }
mamRequestInProgress = true
guard let acc = xmpp.getEnabledAccount(forID: NSNumber(value: account.id)) else { return }
let lastStanzaId = messages.last?.stanzaId // last here because list is reversed
?? db.lastStanzaId(forAccount: NSNumber(value: account.id))
acc.setMAMQueryMostRecentFor(monalContact, before: lastStanzaId) { [weak self] msgs, _ in
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
self?.mamRequestInProgress = false
if !(msgs ?? []).isEmpty {
self?.refreshMessages()
}
}
}
}
}
private extension WrapperChat {
func subscribe() {
let newMsg = NotificationCenter.default.addObserver(forName: Notification.Name(kMonalNewMessageNotice), object: nil, queue: .main) { [weak self] _ in
self?.refreshMessages()
}
notificationObservers.append(newMsg)
let sentMsg = NotificationCenter.default.addObserver(forName: Notification.Name(kMonalSentMessageNotice), object: nil, queue: .main) { [weak self] _ in
self?.refreshMessages()
}
notificationObservers.append(sentMsg)
}
func refreshMessages() {
let messages = db.messages(forContact: contact.contactJid, forAccount: NSNumber(value: contact.ownerId))
.compactMap { obj -> Message? in
guard let message = obj as? MLMessage else { return nil }
return Message(message)
}
.sorted { $0.timestamp > $1.timestamp }
self.messages = messages
}
func toggleOmemo(_ new: Bool) {
if monalContact.isEncrypted != new {
monalContact.toggleEncryption(new)
}
}
}

View file

@ -7,7 +7,7 @@ enum AccountsAvailability {
case someEnabled
}
final class MonalXmppWrapper: ObservableObject {
final class WrapperXMPP: ObservableObject {
@Published private(set) var accountsAvailability: AccountsAvailability = .noAccounts
@Published private(set) var accounts: [Account] = []
@Published private(set) var contacts: [Contact] = []
@ -19,8 +19,6 @@ final class MonalXmppWrapper: ObservableObject {
private var notificationObservers: [AnyObject] = []
init() {
// here is some inits (just for now)
// init monalxmpp components
xmpp = MLXMPPManager.sharedInstance()
db = DataLayer.sharedInstance()
@ -29,10 +27,6 @@ final class MonalXmppWrapper: ObservableObject {
subscribeToUpdates()
xmpp.reconnectAll()
NotificationCenter.default.post(name: Notification.Name(kMonalRefresh), object: nil)
// reconnect
// xmpp.nowForegrounded()
// xmpp.connectIfNecessary()
}
deinit {
@ -41,7 +35,7 @@ final class MonalXmppWrapper: ObservableObject {
}
// MARK: - Public
extension MonalXmppWrapper {
extension WrapperXMPP {
func tryLogin(_ login: String, _ password: String) async throws {
let scenario = ScenarioLogIn()
let result = await scenario.tryLogin(login, password)
@ -65,14 +59,14 @@ extension MonalXmppWrapper {
}
}
func chat(with: Contact) -> MonalChatWrapper {
func chat(with: Contact) -> WrapperChat {
// swiftlint:disable:next force_unwrapping
let account = accounts.first { $0.id == with.ownerId }!
let chatModel = MonalChatWrapper(account: account, contact: with, db: db, xmpp: xmpp)
let chatModel = WrapperChat(account: account, contact: with, db: db, xmpp: xmpp)
return chatModel
}
func chat(with: Chat) -> MonalChatWrapper? {
func chat(with: Chat) -> WrapperChat? {
guard let account = accounts.first(where: { $0.id == with.accountId }) else { return nil }
var contact = contacts.first(where: { $0.ownerId == with.accountId && $0.contactJid == with.participantJid })
@ -89,13 +83,13 @@ extension MonalXmppWrapper {
}
guard let contact else { return nil }
let chatModel = MonalChatWrapper(account: account, contact: contact, db: db, xmpp: xmpp)
let chatModel = WrapperChat(account: account, contact: contact, db: db, xmpp: xmpp)
return chatModel
}
}
// MARK: - Handle notifications
private extension MonalXmppWrapper {
private extension WrapperXMPP {
func subscribeToUpdates() {
// General
let generalRefresh = NotificationCenter.default.addObserver(forName: Notification.Name(kMonalRefresh), object: nil, queue: .main) { [weak self] _ in
@ -151,113 +145,3 @@ private extension MonalXmppWrapper {
}
}
}
// MARK: - Chat object
final class MonalChatWrapper: ObservableObject {
@Published private(set) var messages: [Message] = []
@Published var replyText: String = ""
@Published private(set) var mamRequestInProgress = false
@Published var isOmemoEnabled: Bool {
didSet {
toggleOmemo(isOmemoEnabled)
}
}
let contact: Contact
private let monalContact: MLContact
private let account: Account
private let xmpp: MLXMPPManager
private let db: DataLayer
private var notificationObservers: [AnyObject] = []
init(account: Account, contact: Contact, db: DataLayer, xmpp: MLXMPPManager) {
self.contact = contact
self.account = account
self.db = db
self.xmpp = xmpp
// swiftlint:disable:next force_unwrapping
monalContact = db.contactList().first { $0.accountID.intValue == contact.ownerId && $0.contactJid == contact.contactJid }!
isOmemoEnabled = monalContact.isEncrypted
subscribe()
NotificationCenter.default.post(name: Notification.Name(kMonalNewMessageNotice), object: nil)
//
}
deinit {
notificationObservers.forEach { NotificationCenter.default.removeObserver($0) }
}
var chatTitle: String {
contact.name
}
func sendText(_ text: String) {
let newMessageId = UUID().uuidString
_ = db.addMessageHistory(
to: contact.contactJid,
forAccount: monalContact.accountID,
withMessage: text,
actuallyFrom: account.jid,
withId: newMessageId,
encrypted: monalContact.isEncrypted,
messageType: kMessageTypeText,
mimeType: nil,
size: nil
)
print(newMessageId)
xmpp.sendMessage(text, to: monalContact, isEncrypted: monalContact.isEncrypted, isUpload: false, messageId: newMessageId)
}
func requestMAM() {
if mamRequestInProgress { return }
mamRequestInProgress = true
guard let acc = xmpp.getEnabledAccount(forID: NSNumber(value: account.id)) else { return }
let lastStanzaId = messages.last?.stanzaId // last here because list is reversed
?? db.lastStanzaId(forAccount: NSNumber(value: account.id))
acc.setMAMQueryMostRecentFor(monalContact, before: lastStanzaId) { [weak self] msgs, _ in
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
self?.mamRequestInProgress = false
if !(msgs ?? []).isEmpty {
self?.refreshMessages()
}
}
}
}
}
private extension MonalChatWrapper {
func subscribe() {
let newMsg = NotificationCenter.default.addObserver(forName: Notification.Name(kMonalNewMessageNotice), object: nil, queue: .main) { [weak self] _ in
self?.refreshMessages()
}
notificationObservers.append(newMsg)
let sentMsg = NotificationCenter.default.addObserver(forName: Notification.Name(kMonalSentMessageNotice), object: nil, queue: .main) { [weak self] _ in
self?.refreshMessages()
}
notificationObservers.append(sentMsg)
}
func refreshMessages() {
let messages = db.messages(forContact: contact.contactJid, forAccount: NSNumber(value: contact.ownerId))
.compactMap { obj -> Message? in
guard let message = obj as? MLMessage else { return nil }
return Message(message)
}
.sorted { $0.timestamp > $1.timestamp }
self.messages = messages
}
func toggleOmemo(_ new: Bool) {
if monalContact.isEncrypted != new {
monalContact.toggleEncryption(new)
}
}
}