mv-experiment #1

Merged
fmodf merged 88 commits from mv-experiment into develop 2024-09-03 15:13:59 +00:00
8 changed files with 48 additions and 34 deletions
Showing only changes of commit eddec7412c - Show all commits

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

View file

@ -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";

View file

@ -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,

View file

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

View file

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

View file

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