This commit is contained in:
fmodf 2024-08-11 23:52:01 +02:00
parent 684b37247a
commit eddec7412c
8 changed files with 48 additions and 34 deletions

View file

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

View file

@ -0,0 +1,3 @@
enum ClientStoreError: Error {
case clientNotFound
}

View file

@ -38,6 +38,10 @@ final class ClientsStore: ObservableObject {
updatedClients.append(contentsOf: newClients) updatedClients.append(contentsOf: newClients)
clients = updatedClients clients = updatedClients
} }
private func client(for credentials: Credentials) -> Client? {
clients.first { $0.credentials == credentials }
}
} }
extension ClientsStore { extension ClientsStore {
@ -74,4 +78,19 @@ extension ClientsStore {
return allRosters 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)
}
} }

View file

@ -31,8 +31,7 @@
// MARK: Add contact/channel screen // MARK: Add contact/channel screen
"Contacts.Add.title" = "Add Contact"; "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.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.serverError" = "Contact adding dailed. Server returned error";
"Contacts.Add.connectionError" = "You need to be connected to internet for add contact/channel";

View file

@ -113,7 +113,6 @@ struct LoginScreen: View {
do { do {
try await clientsStore.tryLogin(jidStr, pass) try await clientsStore.tryLogin(jidStr, pass)
} catch { } catch {
router.dismissModal()
router.showAlert( router.showAlert(
.alert, .alert,
title: L10n.Global.Error.title, title: L10n.Global.Error.title,

View file

@ -129,29 +129,17 @@ struct AddContactOrChannelScreen: View {
router.dismissModal() router.dismissModal()
} }
do {
try await clientsStore.addRoster(ownerCredentials, contactJID: contactJID, name: nil, groups: [])
router.dismissScreen() router.dismissScreen()
} catch {
// 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( router.showAlert(
.alert, .alert,
title: L10n.Global.Error.title, title: L10n.Global.Error.title,
subtitle: subtitle subtitle: L10n.Contacts.Add.serverError
) { ) {
Button(L10n.Global.ok, role: .cancel) {} Button(L10n.Global.ok, role: .cancel) {}
} }
} }
}
} }

View file

@ -3,7 +3,6 @@ import SwiftUI
struct ContactsScreen: View { struct ContactsScreen: View {
@Environment(\.router) var router @Environment(\.router) var router
@EnvironmentObject var clientsStore: ClientsStore @EnvironmentObject var clientsStore: ClientsStore
// @StateObject var rostersStore = RostersStore()
@State private var rosters: [Roster] = [] @State private var rosters: [Roster] = []
@ -22,9 +21,7 @@ struct ContactsScreen: View {
image: Image(systemName: "plus"), image: Image(systemName: "plus"),
action: { action: {
router.showScreen(.fullScreenCover) { _ in router.showScreen(.fullScreenCover) { _ in
Text("") AddContactOrChannelScreen()
// AddContactOrChannelScreen()
// .environmentObject(rostersStore)
} }
} }
) )
@ -49,10 +46,6 @@ struct ContactsScreen: View {
.task { .task {
rosters = await clientsStore.actualRosters rosters = await clientsStore.actualRosters
} }
// .loadingIndicator(isShowingLoader)
// .fullScreenCover(isPresented: $addPanelPresented) {
// AddContactOrChannelScreen(isPresented: $addPanelPresented)
// }
// .alert(isPresented: $isErrorAlertPresented) { // .alert(isPresented: $isErrorAlertPresented) {
// Alert( // Alert(
// title: Text(L10n.Global.Error.title), // title: Text(L10n.Global.Error.title),

View file

@ -9,6 +9,8 @@ private enum Tab {
} }
struct MainTabScreen: View { struct MainTabScreen: View {
@EnvironmentObject var clientsStore: ClientsStore
@State private var selectedTab: Tab = .conversations @State private var selectedTab: Tab = .conversations
var body: some View { var body: some View {
@ -38,6 +40,9 @@ struct MainTabScreen: View {
TabBar(selectedTab: $selectedTab) TabBar(selectedTab: $selectedTab)
} }
} }
.onLoad {
clientsStore.reconnectAll()
}
} }
} }