From eddec7412c1fe8ecdf094b403f686960bed0bd49 Mon Sep 17 00:00:00 2001 From: fmodf Date: Sun, 11 Aug 2024 23:52:01 +0200 Subject: [PATCH] wip --- .../AppData/Model/Roster.swift | 8 +++++ .../AppData/Store/ClientStoreError.swift | 3 ++ .../AppData/Store/ClientsStore.swift | 19 +++++++++++ .../Resources/Strings/Localizable.strings | 3 +- .../View/Entering/LoginScreen.swift | 1 - .../Contacts/AddContactOrChannelScreen.swift | 34 ++++++------------- .../View/Main/Contacts/ContactsScreen.swift | 9 +---- .../View/Main/MainTabScreen.swift | 5 +++ 8 files changed, 48 insertions(+), 34 deletions(-) create mode 100644 ConversationsClassic/AppData/Store/ClientStoreError.swift diff --git a/ConversationsClassic/AppData/Model/Roster.swift b/ConversationsClassic/AppData/Model/Roster.swift index 1eb0873..11749c0 100644 --- a/ConversationsClassic/AppData/Model/Roster.swift +++ b/ConversationsClassic/AppData/Model/Roster.swift @@ -70,3 +70,11 @@ extension Roster { } } } + +extension Roster { + static func fetchDeletedLocally() async throws -> [Roster] { + try await Database.shared.dbQueue.read { db in + try Roster.filter(Column("locallyDeleted") == true).fetchAll(db) + } + } +} diff --git a/ConversationsClassic/AppData/Store/ClientStoreError.swift b/ConversationsClassic/AppData/Store/ClientStoreError.swift new file mode 100644 index 0000000..4ab57bb --- /dev/null +++ b/ConversationsClassic/AppData/Store/ClientStoreError.swift @@ -0,0 +1,3 @@ +enum ClientStoreError: Error { + case clientNotFound +} diff --git a/ConversationsClassic/AppData/Store/ClientsStore.swift b/ConversationsClassic/AppData/Store/ClientsStore.swift index a11af86..9cbe322 100644 --- a/ConversationsClassic/AppData/Store/ClientsStore.swift +++ b/ConversationsClassic/AppData/Store/ClientsStore.swift @@ -38,6 +38,10 @@ final class ClientsStore: ObservableObject { updatedClients.append(contentsOf: newClients) clients = updatedClients } + + private func client(for credentials: Credentials) -> Client? { + clients.first { $0.credentials == credentials } + } } extension ClientsStore { @@ -74,4 +78,19 @@ extension ClientsStore { return allRosters } } + + func addRoster(_ credentials: Credentials, contactJID: String, name: String?, groups: [String]) async throws { + // check that roster exist in db as locally deleted and undelete it + let deletedLocally = try await Roster.fetchDeletedLocally() + if var roster = deletedLocally.first(where: { $0.contactBareJid == contactJID }) { + try await roster.setLocallyDeleted(false) + return + } + + // add new roster + guard let client = client(for: credentials) else { + throw ClientStoreError.clientNotFound + } + try await client.addRoster(contactJID, name: name, groups: groups) + } } diff --git a/ConversationsClassic/Resources/Strings/Localizable.strings b/ConversationsClassic/Resources/Strings/Localizable.strings index dd4cd68..11921d7 100644 --- a/ConversationsClassic/Resources/Strings/Localizable.strings +++ b/ConversationsClassic/Resources/Strings/Localizable.strings @@ -31,8 +31,7 @@ // MARK: Add contact/channel screen "Contacts.Add.title" = "Add Contact"; "Contacts.Add.explanation" = "Contact or group/channel name are usually JID in format name@domain.ltd (like email)"; -"Contacts.Add.serverError" = "Contact not added. Server returns error."; -"Contacts.Add.connectionError" = "You need to be connected to internet for add contact/channel"; +"Contacts.Add.serverError" = "Contact adding dailed. Server returned error"; diff --git a/ConversationsClassic/View/Entering/LoginScreen.swift b/ConversationsClassic/View/Entering/LoginScreen.swift index 665357a..2cf8c4f 100644 --- a/ConversationsClassic/View/Entering/LoginScreen.swift +++ b/ConversationsClassic/View/Entering/LoginScreen.swift @@ -113,7 +113,6 @@ struct LoginScreen: View { do { try await clientsStore.tryLogin(jidStr, pass) } catch { - router.dismissModal() router.showAlert( .alert, title: L10n.Global.Error.title, diff --git a/ConversationsClassic/View/Main/Contacts/AddContactOrChannelScreen.swift b/ConversationsClassic/View/Main/Contacts/AddContactOrChannelScreen.swift index 3263e34..c756d39 100644 --- a/ConversationsClassic/View/Main/Contacts/AddContactOrChannelScreen.swift +++ b/ConversationsClassic/View/Main/Contacts/AddContactOrChannelScreen.swift @@ -129,29 +129,17 @@ struct AddContactOrChannelScreen: View { router.dismissModal() } - router.dismissScreen() - - // let result = await rostersStore.addRoster(ownerCredentials, contactJID: contactJID, name: nil, groups: []) - // - // switch result { - // case .success: - // router.dismissScreen() - // - // case .connectionError: - // showErrorAlert(subtitle: L10n.Contacts.Add.connectionError) - // - // case .serverError: - // showErrorAlert(subtitle: L10n.Contacts.Add.serverError) - // } - } - - private func showErrorAlert(subtitle: String) { - router.showAlert( - .alert, - title: L10n.Global.Error.title, - subtitle: subtitle - ) { - Button(L10n.Global.ok, role: .cancel) {} + do { + try await clientsStore.addRoster(ownerCredentials, contactJID: contactJID, name: nil, groups: []) + router.dismissScreen() + } catch { + router.showAlert( + .alert, + title: L10n.Global.Error.title, + subtitle: L10n.Contacts.Add.serverError + ) { + Button(L10n.Global.ok, role: .cancel) {} + } } } } diff --git a/ConversationsClassic/View/Main/Contacts/ContactsScreen.swift b/ConversationsClassic/View/Main/Contacts/ContactsScreen.swift index a6eae7e..9bd70e5 100644 --- a/ConversationsClassic/View/Main/Contacts/ContactsScreen.swift +++ b/ConversationsClassic/View/Main/Contacts/ContactsScreen.swift @@ -3,7 +3,6 @@ import SwiftUI struct ContactsScreen: View { @Environment(\.router) var router @EnvironmentObject var clientsStore: ClientsStore - // @StateObject var rostersStore = RostersStore() @State private var rosters: [Roster] = [] @@ -22,9 +21,7 @@ struct ContactsScreen: View { image: Image(systemName: "plus"), action: { router.showScreen(.fullScreenCover) { _ in - Text("") - // AddContactOrChannelScreen() - // .environmentObject(rostersStore) + AddContactOrChannelScreen() } } ) @@ -49,10 +46,6 @@ struct ContactsScreen: View { .task { rosters = await clientsStore.actualRosters } - // .loadingIndicator(isShowingLoader) - // .fullScreenCover(isPresented: $addPanelPresented) { - // AddContactOrChannelScreen(isPresented: $addPanelPresented) - // } // .alert(isPresented: $isErrorAlertPresented) { // Alert( // title: Text(L10n.Global.Error.title), diff --git a/ConversationsClassic/View/Main/MainTabScreen.swift b/ConversationsClassic/View/Main/MainTabScreen.swift index 2e986f0..736bbcb 100644 --- a/ConversationsClassic/View/Main/MainTabScreen.swift +++ b/ConversationsClassic/View/Main/MainTabScreen.swift @@ -9,6 +9,8 @@ private enum Tab { } struct MainTabScreen: View { + @EnvironmentObject var clientsStore: ClientsStore + @State private var selectedTab: Tab = .conversations var body: some View { @@ -38,6 +40,9 @@ struct MainTabScreen: View { TabBar(selectedTab: $selectedTab) } } + .onLoad { + clientsStore.reconnectAll() + } } }