wip
This commit is contained in:
parent
684b37247a
commit
eddec7412c
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
enum ClientStoreError: Error {
|
||||
case clientNotFound
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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";
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -129,29 +129,17 @@ struct AddContactOrChannelScreen: View {
|
|||
router.dismissModal()
|
||||
}
|
||||
|
||||
do {
|
||||
try await clientsStore.addRoster(ownerCredentials, contactJID: contactJID, name: nil, groups: [])
|
||||
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) {
|
||||
} catch {
|
||||
router.showAlert(
|
||||
.alert,
|
||||
title: L10n.Global.Error.title,
|
||||
subtitle: subtitle
|
||||
subtitle: L10n.Contacts.Add.serverError
|
||||
) {
|
||||
Button(L10n.Global.ok, role: .cancel) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue