From 50d64cf96bf1ce7707b9c7993250b95a4062c25a Mon Sep 17 00:00:00 2001 From: fmodf Date: Fri, 4 Oct 2024 17:58:04 +0200 Subject: [PATCH] wip --- .../AppData/Services/Database.swift | 16 ++++++++++++++++ .../AppData/Store/ClientsStore.swift | 13 +++++++++++++ .../Main/ChatList/ChatsCreateScreenMain.swift | 2 +- .../View/Main/Settings/SettingsScreen.swift | 18 ++++++++++++++---- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/ConversationsClassic/AppData/Services/Database.swift b/ConversationsClassic/AppData/Services/Database.swift index 5b2d77c..de219b7 100644 --- a/ConversationsClassic/AppData/Services/Database.swift +++ b/ConversationsClassic/AppData/Services/Database.swift @@ -10,6 +10,7 @@ typealias DBStorable = Codable & FetchableRecord & Identifiable & PersistableRec final class Database { static let shared = Database() let dbQueue: DatabaseQueue + private var dbPath: String private init() { do { @@ -25,6 +26,7 @@ final class Database { // Open or create the database let databaseURL = directoryURL.appendingPathComponent("db.sqlite") dbQueue = try DatabaseQueue(path: databaseURL.path, configuration: Database.config) + dbPath = databaseURL.path // Some debug info #if DEBUG @@ -53,3 +55,17 @@ private extension Database { return config }() } + +// MARK: - flush all data for debug +#if DEBUG + extension Database { + func flushAllData() { + // nullable queue and remove db file + do { + try FileManager.default.removeItem(atPath: dbPath) + } catch { + print("Error: \(error)") + } + } + } +#endif diff --git a/ConversationsClassic/AppData/Store/ClientsStore.swift b/ConversationsClassic/AppData/Store/ClientsStore.swift index 8ea544d..91399ca 100644 --- a/ConversationsClassic/AppData/Store/ClientsStore.swift +++ b/ConversationsClassic/AppData/Store/ClientsStore.swift @@ -193,3 +193,16 @@ private extension ClientsStore { } } } + +// MARK: - Remove all data for debug +#if DEBUG + extension ClientsStore { + func flushAllData() { + clients.forEach { $0.disconnect() } + clients.removeAll() + actualRosters.removeAll() + actualChats.removeAll() + ready = false + } + } +#endif diff --git a/ConversationsClassic/View/Main/ChatList/ChatsCreateScreenMain.swift b/ConversationsClassic/View/Main/ChatList/ChatsCreateScreenMain.swift index 0ad89b7..5bc5b52 100644 --- a/ConversationsClassic/View/Main/ChatList/ChatsCreateScreenMain.swift +++ b/ConversationsClassic/View/Main/ChatList/ChatsCreateScreenMain.swift @@ -59,7 +59,7 @@ struct ChatsCreateScreenMain: View { print("Tapped findChannel") } - // for contacts list + // for contacts list (later) // let rosters = store.state.rostersState.rosters.filter { !$0.locallyDeleted } // if rosters.isEmpty { // ChatsCreateRowSeparator() diff --git a/ConversationsClassic/View/Main/Settings/SettingsScreen.swift b/ConversationsClassic/View/Main/Settings/SettingsScreen.swift index 99c77da..81b4af7 100644 --- a/ConversationsClassic/View/Main/Settings/SettingsScreen.swift +++ b/ConversationsClassic/View/Main/Settings/SettingsScreen.swift @@ -1,16 +1,26 @@ import SwiftUI struct SettingsScreen: View { + @EnvironmentObject var clientsStore: ClientsStore + var body: some View { ZStack { // bg Color.Material.Background.light .ignoresSafeArea() - // content - Text("Soon!") - .font(.head1l) - .foregroundColor(.Material.Elements.active) + // debug buttons + #if DEBUG + // clean all data + Button { + clientsStore.flushAllData() + Database.shared.flushAllData() + } label: { + Text("Clean all (for test)") + .font(.title) + .foregroundColor(.Material.Elements.active) + } + #endif } } }