From 339aab2bb4f1cf5be786bcc9bd8ca52c813fe5dd Mon Sep 17 00:00:00 2001 From: fmodf Date: Mon, 7 Oct 2024 18:58:02 +0200 Subject: [PATCH] wip --- .../AppData/Services/Database+Migrations.swift | 16 ++++++++++++---- .../AppData/Services/Database.swift | 15 ++++++++++++--- .../AppData/Store/ClientsStore.swift | 1 - .../Helpers/UserDefaultsWrapper.swift | 5 +++++ .../View/Main/Settings/SettingsScreen.swift | 1 + 5 files changed, 30 insertions(+), 8 deletions(-) diff --git a/ConversationsClassic/AppData/Services/Database+Migrations.swift b/ConversationsClassic/AppData/Services/Database+Migrations.swift index b6a2f7a..c7bdba5 100644 --- a/ConversationsClassic/AppData/Services/Database+Migrations.swift +++ b/ConversationsClassic/AppData/Services/Database+Migrations.swift @@ -94,12 +94,20 @@ extension Database { table.primaryKey(["account", "id"], onConflict: .replace) } - try db.alter(table: "messages") { table in - table.add(column: "secure", .boolean).notNull().defaults(to: false) + do { + try db.alter(table: "messages") { table in + table.add(column: "secure", .boolean).notNull().defaults(to: false) + } + } catch { + print("Error adding columns: \(error)\nProbably already added") } - try db.alter(table: "chats") { table in - table.add(column: "encrypted", .boolean).notNull().defaults(to: false) + do { + try db.alter(table: "chats") { table in + table.add(column: "encrypted", .boolean).notNull().defaults(to: false) + } + } catch { + print("Error adding columns: \(error)\nProbably already added") } } diff --git a/ConversationsClassic/AppData/Services/Database.swift b/ConversationsClassic/AppData/Services/Database.swift index de219b7..84bc6f5 100644 --- a/ConversationsClassic/AppData/Services/Database.swift +++ b/ConversationsClassic/AppData/Services/Database.swift @@ -60,11 +60,20 @@ private extension Database { #if DEBUG extension Database { func flushAllData() { - // nullable queue and remove db file do { - try FileManager.default.removeItem(atPath: dbPath) + try dbQueue.write { db in + // Fetch all table names + let tables = try String.fetchAll(db, sql: """ + SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'; + """) + + // Generate and execute DELETE statements for each table + for table in tables { + try db.execute(sql: "DELETE FROM \(table);") + } + } } catch { - print("Error: \(error)") + print("Error flushing all data: \(error)") } } } diff --git a/ConversationsClassic/AppData/Store/ClientsStore.swift b/ConversationsClassic/AppData/Store/ClientsStore.swift index 91399ca..0eda5a6 100644 --- a/ConversationsClassic/AppData/Store/ClientsStore.swift +++ b/ConversationsClassic/AppData/Store/ClientsStore.swift @@ -202,7 +202,6 @@ private extension ClientsStore { clients.removeAll() actualRosters.removeAll() actualChats.removeAll() - ready = false } } #endif diff --git a/ConversationsClassic/Helpers/UserDefaultsWrapper.swift b/ConversationsClassic/Helpers/UserDefaultsWrapper.swift index a72fde2..5898983 100644 --- a/ConversationsClassic/Helpers/UserDefaultsWrapper.swift +++ b/ConversationsClassic/Helpers/UserDefaultsWrapper.swift @@ -33,4 +33,9 @@ enum UserSettings { @Storage(key: kSecureChatsByDefault, defaultValue: false) static var secureChatsByDefault: Bool + + static func reset() { + omemoDeviceId = 0 + secureChatsByDefault = false + } } diff --git a/ConversationsClassic/View/Main/Settings/SettingsScreen.swift b/ConversationsClassic/View/Main/Settings/SettingsScreen.swift index 6f493e6..c925cb1 100644 --- a/ConversationsClassic/View/Main/Settings/SettingsScreen.swift +++ b/ConversationsClassic/View/Main/Settings/SettingsScreen.swift @@ -66,6 +66,7 @@ struct SettingsScreen: View { Button("Delete", role: .destructive) { clientsStore.flushAllData() Database.shared.flushAllData() + UserSettings.reset() } } }