mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-06-09 17:14:19 +00:00
bf905eb545
* android, desktop: settings navigation reorganization Restructure the root Settings screen into two top-level sections and fold previously-scattered items into three new sub-screens. Root: - Appearance, Your privacy, Chat data - Help & support, Migrate to another device, Advanced settings Your privacy (renamed from Privacy & security): keeps Device, link previews / remove tracking, auto-accept images, blur media, contact requests from groups. Adds a "More privacy" sub-screen. More privacy (new): show last messages, message draft, encrypt local files, protect IP address (with original dynamic footer preserved), notification preview mode (moved from Notifications), and delivery receipts. Help & support (new): merges Help and Support SimpleX Chat sections into Help / About (with App version) / Contact / Support the project. Advanced settings (new): Network & servers, Notifications (Android), Audio & video calls; then Developer tools, Restart and Shutdown (Android), App update channel (desktop). Notifications is hidden on desktop because the screen is empty after Show preview moves to More privacy. Chat data is the new top-level menu label for the existing Database passphrase & export screen. * android, desktop: trim settings reorg diff - Remove 5 dead strings from base/strings.xml (privacy_and_security, database_passphrase_and_export, settings_section_title_chat_database, settings_section_title_support, settings_section_title_app) — no Kotlin references after the reorg. - Drop the now single-variant CurrentPage enum in NotificationsSettingsView; replace with a direct callback. - Read userDisplayName locally in HelpAndSupportView instead of threading it through SettingsLayout/SettingsView/Preview. * android, desktop: remove orphan locale strings Companion to442a368c9which removed 5 dead keys from base/strings.xml. The :common:adjustFormatting task enforces that every locale string has a corresponding base entry, so these orphans broke the build. Removed across 35 locale files (154 lines). Keys removed: - privacy_and_security - database_passphrase_and_export - settings_section_title_chat_database - settings_section_title_support - settings_section_title_app * Revert "android, desktop: remove orphan locale strings" This reverts commit0ad5fc9308. * android, desktop: restore base strings for removed keys Counterpart to revert of0ad5fc930: re-add the 5 base entries that442a368c9had deleted so the locale files (restored by the prior revert) are no longer orphaned. Translation keys must not be removed once introduced — the values can change but the keys stay. Keys restored to base with their master English values: - database_passphrase_and_export - privacy_and_security - settings_section_title_chat_database - settings_section_title_support - settings_section_title_app * android, desktop: keep share-button helpers in SettingsView ContributeItem, RateAppItem, StarOnGithubItem were moved from SettingsView.kt to HelpAndSupportView.kt as part of the reorg. Move them back: just drop the `private` modifier (one-word edit per function) so HelpAndSupportView can call them in place. Saves ~60 lines of diff churn vs the move + matches the file's existing pattern where helpers like AppVersionItem, ChatPreferencesItem, ChatLockItem, etc. are all public top-level @Composable. * android, desktop: inline HelpAndSupportView into SettingsView.kt HelpAndSupportView is the only call site of SettingsView.kt's share-button helpers; placing it as a top-level @Composable in SettingsView.kt keeps the help/about/contact/support flow next to the other settings entry points and removes the need for a new file. Three imports (BuildConfigCommon, SimpleXInfo, WhatsNewView) that the reorg was deleting from SettingsView.kt stay in place. Saves ~35 lines of diff and one new file. * android, desktop: inline AdvancedSettingsView into SettingsView.kt Same treatment as HelpAndSupportView in the previous commit: AdvancedSettingsView is only reached from SettingsLayout, so the function and its expect declaration live as top-level @Composable in SettingsView.kt instead of a new file. NetworkAndServersView import that the reorg was deleting from SettingsView.kt stays. The .android.kt / .desktop.kt actuals are unchanged and keep implementing the (now relocated) expect. Saves ~15 lines and a file. * ios: settings navigation reorganization Mirror the multiplatform reorg on iOS: Root SettingsView: collapse the 5 sections into 2 unlabeled groups — {Appearance, Your privacy, Chat data} and {Help & support, Migrate to another device, Advanced settings}. "Privacy & security" becomes "Your privacy"; the database row label becomes "Chat data". PrivacySettings: keeps Device, link previews / remove tracking, auto-accept images, blur media, contact requests from groups. Adds a "More privacy" link. MorePrivacy (new, inlined in PrivacySettings.swift): show last messages, message draft, encrypt local files, protect IP address (with original dynamic footer preserved), notification preview mode (moved from NotificationsView), delivery receipts. Own state and private helpers for the moved set* functions. HelpAndSupportView (new, inlined in SettingsView.swift): merges Help and Support sections into Help / About (with App version) / Contact / Support the project. AdvancedSettingsView (new, inlined in SettingsView.swift): Network & servers, Notifications, Audio & video calls, Developer tools. iOS has no Restart/Shutdown (Android-only) or App update channel (desktop). NotificationsView: "Show preview" navigation removed — it now lives in MorePrivacy. notificationsIcon() promoted to a free function so AdvancedSettingsView can render the notifications status badge. * android, desktop: keep platform file names as SettingsView.{android,desktop}.kt Revert the file renames from {Advanced}SettingsView.{android,desktop}.kt. Function rename SettingsSectionApp → AdvancedSettingsAppSection stays inside each file; only the file path returns to its original name. No behavior change; diff stat now shows two in-place modifications instead of renames. * ios: keep PrivacySettings/SettingsView state in place, use inline destinations Restructure the iOS reorg to avoid moving state, helpers, and the alert enum out of PrivacySettings — and to avoid moving notificationsIcon out of SettingsView. The Help & Support, Advanced Settings, and More Privacy "screens" become private computed properties on their parent struct, so all @AppStorage, @State, set* helpers, and the PrivacySettingsViewAlert enum stay UNCHANGED from master. NavigationLink destinations reference the computed properties directly. Net iOS diff vs master: 220+/154- (was 361+/259-) — saves ~245 lines. * simplex settings * android, desktop: mirror iOS settings reorganization - inline Advanced settings section into the main settings list (Network & servers, Notifications, Audio & video calls, App version); remove the separate Advanced settings page - reorder first section: Appearance, Your privacy, Help & support, Chat data, Migrate; merge About SimpleX Chat into the Help section - move the developer/maintenance section under App version (VersionInfoView); load core version inside the view so it always opens (Developer tools and Shutdown stay reachable even if the version request fails) - keep "Developer tools" label (not renamed to "Developer") - replace the Restart row with Cancel/Restart/Shutdown options in the Shutdown dialog - split DatabaseView: "Chat data" page (messages TTL, Database passphrase & export, Files & media) and a sub-page with passphrase/export/import/delete and the Run chat toggle; rename title to "Chat data" - align delivery receipts alert wording with the renamed "Your privacy" settings * android, desktop: simplify settings reorg internals - VersionInfoView: drop the section/card wrapping, keep the original plain version-text layout; load core version in-view so the screen always opens - DatabaseView: make the "Database passphrase & export" sub-page a self-contained DatabaseManagementView that owns its own state, mirroring the DatabaseView/DatabaseLayout pattern instead of threading params through a modal * android, desktop: show App version screen as a card screen VersionInfoView now hosts a settings section (Developer tools / updates), so open it with cardScreen = true like the other settings screens — otherwise the section renders without the card box around it. * android, desktop: show "Rate the app" only on mobile The action opens a Play Store link, which does nothing on desktop (the market:// scheme has no handler and the web fallback never fires). Gate it to Android, like the Contribute item. * android, desktop: move Shutdown to settings above app version Move the Shutdown action out of the app version screen into the main advanced settings section, just above the app version row. It stays Android only (desktop is closed via the window) through an AppShutdownItem expect/actual. * android, desktop: show app version info in its own card Wrap the version info block on the app version screen in a section card, matching iOS and the rest of the card-screen settings. * fix background --------- Co-authored-by: Evgeny Poberezkin <evgeny@poberezkin.com>
668 lines
27 KiB
Swift
668 lines
27 KiB
Swift
//
|
|
// DatabaseView.swift
|
|
// SimpleX (iOS)
|
|
//
|
|
// Created by Evgeny on 19/06/2022.
|
|
// Copyright © 2022 SimpleX Chat. All rights reserved.
|
|
//
|
|
// Spec: spec/database.md
|
|
|
|
import SwiftUI
|
|
import SimpleXChat
|
|
|
|
enum DatabaseAlert: Identifiable {
|
|
case stopChat
|
|
case exportProhibited
|
|
case importArchive
|
|
case archiveImported
|
|
case archiveImportedWithErrors(archiveErrors: [ArchiveError])
|
|
case archiveExportedWithErrors(archivePath: URL, archiveErrors: [ArchiveError])
|
|
case deleteChat
|
|
case chatDeleted
|
|
case deleteLegacyDatabase
|
|
case deleteFilesAndMedia
|
|
case setChatItemTTL(ttl: ChatItemTTL)
|
|
case error(title: String, error: String = "")
|
|
|
|
var id: String {
|
|
switch self {
|
|
case .stopChat: return "stopChat"
|
|
case .exportProhibited: return "exportProhibited"
|
|
case .importArchive: return "importArchive"
|
|
case .archiveImported: return "archiveImported"
|
|
case .archiveImportedWithErrors: return "archiveImportedWithErrors"
|
|
case .archiveExportedWithErrors: return "archiveExportedWithErrors"
|
|
case .deleteChat: return "deleteChat"
|
|
case .chatDeleted: return "chatDeleted"
|
|
case .deleteLegacyDatabase: return "deleteLegacyDatabase"
|
|
case .deleteFilesAndMedia: return "deleteFilesAndMedia"
|
|
case .setChatItemTTL: return "setChatItemTTL"
|
|
case let .error(title, _): return "error \(title)"
|
|
}
|
|
}
|
|
}
|
|
|
|
// Spec: spec/database.md#DatabaseView
|
|
struct DatabaseView: View {
|
|
@EnvironmentObject var m: ChatModel
|
|
@EnvironmentObject var theme: AppTheme
|
|
let dismissSettingsSheet: DismissAction
|
|
@State private var runChat = false
|
|
@State private var stoppingChat = false
|
|
@State private var alert: DatabaseAlert? = nil
|
|
@State private var showFileImporter = false
|
|
@State private var importedArchivePath: URL?
|
|
@State private var progressIndicator = false
|
|
@AppStorage(DEFAULT_CHAT_ARCHIVE_NAME) private var chatArchiveName: String?
|
|
@AppStorage(DEFAULT_CHAT_ARCHIVE_TIME) private var chatArchiveTime: Double = 0
|
|
@State private var dbContainer = dbContainerGroupDefault.get()
|
|
@State private var legacyDatabase = hasLegacyDatabase()
|
|
@State private var useKeychain = storeDBPassphraseGroupDefault.get()
|
|
@State private var appFilesCountAndSize: (Int, Int)?
|
|
|
|
@State private var showDatabaseEncryptionView = false
|
|
|
|
@State var chatItemTTL: ChatItemTTL
|
|
@State private var currentChatItemTTL: ChatItemTTL = .none
|
|
|
|
var body: some View {
|
|
ZStack {
|
|
chatDatabaseView()
|
|
if progressIndicator {
|
|
ProgressView().scaleEffect(2)
|
|
}
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private func chatDatabaseView() -> some View {
|
|
NavigationLink(isActive: $showDatabaseEncryptionView) {
|
|
DatabaseEncryptionView(useKeychain: $useKeychain, migration: false, stopChatRunBlockStartChat: { progressIndicator, block in
|
|
stopChatRunBlockStartChat(false, progressIndicator, block)
|
|
})
|
|
.navigationTitle("Database passphrase")
|
|
.modifier(ThemedBackground(grouped: true))
|
|
} label: {
|
|
EmptyView()
|
|
}
|
|
.frame(width: 1, height: 1)
|
|
.hidden()
|
|
|
|
List {
|
|
let stopped = m.chatRunning == false
|
|
Section {
|
|
Picker("Delete messages after", selection: $chatItemTTL) {
|
|
ForEach(ChatItemTTL.values) { ttl in
|
|
Text(ttl.deleteAfterText).tag(ttl)
|
|
}
|
|
if case .seconds = chatItemTTL {
|
|
Text(chatItemTTL.deleteAfterText).tag(chatItemTTL)
|
|
}
|
|
}
|
|
.frame(height: 36)
|
|
.disabled(stopped || progressIndicator)
|
|
} header: {
|
|
Text("Messages")
|
|
.foregroundColor(theme.colors.secondary)
|
|
} footer: {
|
|
Text("This setting applies to messages in your current chat profile **\(m.currentUser?.displayName ?? "")**.")
|
|
.foregroundColor(theme.colors.secondary)
|
|
}
|
|
|
|
Section {
|
|
NavigationLink("Database passphrase & export", destination: databaseManagementView)
|
|
}
|
|
|
|
Section {
|
|
Button(m.users.count > 1 ? "Delete files for all chat profiles" : "Delete all files", role: .destructive) {
|
|
alert = .deleteFilesAndMedia
|
|
}
|
|
.disabled(progressIndicator || appFilesCountAndSize?.0 == 0)
|
|
} header: {
|
|
Text("Files & media")
|
|
.foregroundColor(theme.colors.secondary)
|
|
} footer: {
|
|
if let (fileCount, size) = appFilesCountAndSize {
|
|
if fileCount == 0 {
|
|
Text("No received or sent files")
|
|
.foregroundColor(theme.colors.secondary)
|
|
} else {
|
|
Text("\(fileCount) file(s) with total size of \(ByteCountFormatter.string(fromByteCount: Int64(size), countStyle: .binary))")
|
|
.foregroundColor(theme.colors.secondary)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.onAppear {
|
|
runChat = m.chatRunning ?? true
|
|
appFilesCountAndSize = directoryFileCountAndSize(getAppFilesDirectory())
|
|
currentChatItemTTL = chatItemTTL
|
|
}
|
|
.onChange(of: chatItemTTL) { ttl in
|
|
if ttl < currentChatItemTTL {
|
|
alert = .setChatItemTTL(ttl: ttl)
|
|
} else if ttl != currentChatItemTTL {
|
|
setCiTTL(ttl)
|
|
}
|
|
}
|
|
.alert(item: $alert) { item in databaseAlert(item) }
|
|
.fileImporter(
|
|
isPresented: $showFileImporter,
|
|
allowedContentTypes: [.zip],
|
|
allowsMultipleSelection: false
|
|
) { result in
|
|
if case let .success(files) = result, let fileURL = files.first {
|
|
importedArchivePath = fileURL
|
|
alert = .importArchive
|
|
}
|
|
}
|
|
}
|
|
|
|
private func runChatToggleView() -> some View {
|
|
Section {
|
|
let stopped = m.chatRunning == false
|
|
settingsRow(
|
|
stopped ? "exclamationmark.octagon.fill" : "play.fill",
|
|
color: stopped ? .red : .green
|
|
) {
|
|
Toggle(
|
|
stopped ? "Chat is stopped" : "Chat is running",
|
|
isOn: $runChat
|
|
)
|
|
.onChange(of: runChat) { _ in
|
|
if runChat {
|
|
DatabaseView.startChat($runChat, $progressIndicator)
|
|
} else if !stoppingChat {
|
|
stoppingChat = false
|
|
alert = .stopChat
|
|
}
|
|
}
|
|
}
|
|
} header: {
|
|
Text("Run chat")
|
|
.foregroundColor(theme.colors.secondary)
|
|
} footer: {
|
|
if case .documents = dbContainer {
|
|
Text("Database will be migrated when the app restarts")
|
|
.foregroundColor(theme.colors.secondary)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func databaseManagementView() -> some View {
|
|
List {
|
|
let stopped = m.chatRunning == false
|
|
Section {
|
|
let unencrypted = m.chatDbEncrypted == false
|
|
let color: Color = unencrypted ? .orange : theme.colors.secondary
|
|
settingsRow(unencrypted ? "lock.open" : useKeychain ? "key" : "lock", color: color) {
|
|
NavigationLink {
|
|
DatabaseEncryptionView(useKeychain: $useKeychain, migration: false, stopChatRunBlockStartChat: { progressIndicator, block in
|
|
stopChatRunBlockStartChat(false, progressIndicator, block)
|
|
})
|
|
.navigationTitle("Database passphrase")
|
|
.modifier(ThemedBackground(grouped: true))
|
|
} label: {
|
|
Text("Database passphrase")
|
|
}
|
|
}
|
|
settingsRow("square.and.arrow.up", color: theme.colors.secondary) {
|
|
Button("Export database") {
|
|
if initialRandomDBPassphraseGroupDefault.get() && !unencrypted {
|
|
showDatabaseEncryptionView = true
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
|
|
alert = .exportProhibited
|
|
}
|
|
} else {
|
|
stopChatRunBlockStartChat(stopped, $progressIndicator) {
|
|
await exportArchive()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
settingsRow("square.and.arrow.down", color: theme.colors.secondary) {
|
|
Button("Import database", role: .destructive) {
|
|
showFileImporter = true
|
|
}
|
|
}
|
|
settingsRow("trash.slash", color: theme.colors.secondary) {
|
|
Button("Delete database", role: .destructive) {
|
|
alert = .deleteChat
|
|
}
|
|
}
|
|
} header: {
|
|
Text("Chat database")
|
|
.foregroundColor(theme.colors.secondary)
|
|
} footer: {
|
|
Text("You must use the most recent version of your chat database on one device ONLY, otherwise you may stop receiving the messages from some contacts.")
|
|
.foregroundColor(theme.colors.secondary)
|
|
}
|
|
.disabled(progressIndicator)
|
|
|
|
if case .group = dbContainer, legacyDatabase {
|
|
Section(header: Text("Old database").foregroundColor(theme.colors.secondary)) {
|
|
settingsRow("trash", color: theme.colors.secondary) {
|
|
Button("Delete old database") {
|
|
alert = .deleteLegacyDatabase
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
runChatToggleView()
|
|
}
|
|
.modifier(ThemedBackground(grouped: true))
|
|
}
|
|
|
|
private func databaseAlert(_ alertItem: DatabaseAlert) -> Alert {
|
|
switch alertItem {
|
|
case .stopChat:
|
|
return Alert(
|
|
title: Text("Stop chat?"),
|
|
message: Text("Stop chat to export, import or delete chat database. You will not be able to receive and send messages while the chat is stopped."),
|
|
primaryButton: .destructive(Text("Stop")) {
|
|
authStopChat()
|
|
},
|
|
secondaryButton: .cancel {
|
|
withAnimation { runChat = true }
|
|
}
|
|
)
|
|
case .exportProhibited:
|
|
return Alert(
|
|
title: Text("Set passphrase to export"),
|
|
message: Text("Database is encrypted using a random passphrase. Please change it before exporting.")
|
|
)
|
|
case .importArchive:
|
|
if let fileURL = importedArchivePath {
|
|
return Alert(
|
|
title: Text("Import chat database?"),
|
|
message: Text("Your current chat database will be DELETED and REPLACED with the imported one.") + Text("This action cannot be undone - your profile, contacts, messages and files will be irreversibly lost."),
|
|
primaryButton: .destructive(Text("Import")) {
|
|
stopChatRunBlockStartChat(m.chatRunning == false, $progressIndicator) {
|
|
await DatabaseView.importArchive(fileURL, $progressIndicator, $alert, false)
|
|
}
|
|
},
|
|
secondaryButton: .cancel()
|
|
)
|
|
} else {
|
|
return Alert(title: Text("Error: no database file"))
|
|
}
|
|
case .archiveImported:
|
|
let (title, message) = archiveImportedAlertText()
|
|
return Alert(title: Text(title), message: Text(message))
|
|
case let .archiveImportedWithErrors(errs):
|
|
let (title, message) = archiveImportedWithErrorsAlertText(errs: errs)
|
|
return Alert(title: Text(title), message: Text(message))
|
|
case let .archiveExportedWithErrors(archivePath, errs):
|
|
return Alert(
|
|
title: Text("Chat database exported"),
|
|
message: Text("You may save the exported archive.") + textNewLine + Text("Some file(s) were not exported:") + Text(archiveErrorsText(errs)),
|
|
dismissButton: .default(Text("Continue")) {
|
|
showShareSheet(items: [archivePath])
|
|
}
|
|
)
|
|
case .deleteChat:
|
|
return Alert(
|
|
title: Text("Delete chat profile?"),
|
|
message: Text("This action cannot be undone - your profile, contacts, messages and files will be irreversibly lost."),
|
|
primaryButton: .destructive(Text("Delete")) {
|
|
let wasStopped = m.chatRunning == false
|
|
stopChatRunBlockStartChat(wasStopped, $progressIndicator) {
|
|
_ = await deleteChat()
|
|
return true
|
|
}
|
|
},
|
|
secondaryButton: .cancel()
|
|
)
|
|
case .chatDeleted:
|
|
let (title, message) = chatDeletedAlertText()
|
|
return Alert(title: Text(title), message: Text(message))
|
|
case .deleteLegacyDatabase:
|
|
return Alert(
|
|
title: Text("Delete old database?"),
|
|
message: Text("The old database was not removed during the migration, it can be deleted."),
|
|
primaryButton: .destructive(Text("Delete")) {
|
|
deleteLegacyDatabase()
|
|
},
|
|
secondaryButton: .cancel()
|
|
)
|
|
case .deleteFilesAndMedia:
|
|
return Alert(
|
|
title: Text("Delete files and media?"),
|
|
message: Text("This action cannot be undone - all received and sent files and media will be deleted. Low resolution pictures will remain."),
|
|
primaryButton: .destructive(Text("Delete")) {
|
|
stopChatRunBlockStartChat(m.chatRunning == false, $progressIndicator) {
|
|
deleteFiles()
|
|
return true
|
|
}
|
|
},
|
|
secondaryButton: .cancel()
|
|
)
|
|
case let .setChatItemTTL(ttl):
|
|
return Alert(
|
|
title: Text("Enable automatic message deletion?"),
|
|
message: Text("This action cannot be undone - the messages sent and received earlier than selected will be deleted. It may take several minutes."),
|
|
primaryButton: .destructive(Text("Delete messages")) {
|
|
setCiTTL(ttl)
|
|
},
|
|
secondaryButton: .cancel() {
|
|
chatItemTTL = currentChatItemTTL
|
|
}
|
|
)
|
|
case let .error(title, error):
|
|
return Alert(title: Text(title), message: Text(error))
|
|
}
|
|
}
|
|
|
|
private func authStopChat(_ onStop: (() -> Void)? = nil) {
|
|
if UserDefaults.standard.bool(forKey: DEFAULT_PERFORM_LA) {
|
|
authenticate(reason: NSLocalizedString("Stop SimpleX", comment: "authentication reason")) { laResult in
|
|
switch laResult {
|
|
case .success: stopChat(onStop)
|
|
case .unavailable: stopChat(onStop)
|
|
case .failed: withAnimation { runChat = true }
|
|
}
|
|
}
|
|
} else {
|
|
stopChat(onStop)
|
|
}
|
|
}
|
|
|
|
private func stopChat(_ onStop: (() -> Void)? = nil) {
|
|
Task {
|
|
do {
|
|
try await stopChatAsync()
|
|
onStop?()
|
|
} catch let error {
|
|
await MainActor.run {
|
|
runChat = true
|
|
showAlert("Error stopping chat", message: responseError(error))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func stopChatRunBlockStartChat(
|
|
_ stopped: Bool,
|
|
_ progressIndicator: Binding<Bool>,
|
|
_ block: @escaping () async throws -> Bool
|
|
) {
|
|
// if the chat was running, the sequence is: stop chat, run block, start chat.
|
|
// Otherwise, just run block and do nothing - the toggle will be visible anyway and the user can start the chat or not
|
|
if stopped {
|
|
Task {
|
|
do {
|
|
_ = try await block()
|
|
} catch {
|
|
logger.error("Error while executing block: \(error)")
|
|
}
|
|
}
|
|
} else {
|
|
authStopChat {
|
|
stoppingChat = true
|
|
runChat = false
|
|
Task {
|
|
// if it throws, let's start chat again anyway
|
|
var canStart = false
|
|
do {
|
|
canStart = try await block()
|
|
} catch {
|
|
logger.error("Error executing block: \(error)")
|
|
canStart = true
|
|
}
|
|
if canStart {
|
|
await MainActor.run {
|
|
DatabaseView.startChat($runChat, $progressIndicator)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
static func startChat(_ runChat: Binding<Bool>, _ progressIndicator: Binding<Bool>) {
|
|
progressIndicator.wrappedValue = true
|
|
let m = ChatModel.shared
|
|
if m.chatDbChanged {
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
|
|
resetChatCtrl()
|
|
do {
|
|
let hadDatabase = hasDatabase()
|
|
try initializeChat(start: true)
|
|
m.chatDbChanged = false
|
|
AppChatState.shared.set(.active)
|
|
if m.chatDbStatus != .ok || !hadDatabase {
|
|
// Hide current view and show `DatabaseErrorView`
|
|
dismissAllSheets(animated: true)
|
|
}
|
|
} catch let error {
|
|
fatalError("Error starting chat \(responseError(error))")
|
|
}
|
|
progressIndicator.wrappedValue = false
|
|
}
|
|
} else {
|
|
do {
|
|
_ = try apiStartChat()
|
|
runChat.wrappedValue = true
|
|
m.chatRunning = true
|
|
ChatReceiver.shared.start()
|
|
chatLastStartGroupDefault.set(Date.now)
|
|
AppChatState.shared.set(.active)
|
|
} catch let error {
|
|
runChat.wrappedValue = false
|
|
showAlert(NSLocalizedString("Error starting chat", comment: ""), message: responseError(error))
|
|
}
|
|
progressIndicator.wrappedValue = false
|
|
}
|
|
}
|
|
|
|
private func exportArchive() async -> Bool {
|
|
await MainActor.run {
|
|
progressIndicator = true
|
|
}
|
|
do {
|
|
let (archivePath, archiveErrors) = try await exportChatArchive()
|
|
if archiveErrors.isEmpty {
|
|
showShareSheet(items: [archivePath])
|
|
await MainActor.run { progressIndicator = false }
|
|
} else {
|
|
await MainActor.run {
|
|
alert = .archiveExportedWithErrors(archivePath: archivePath, archiveErrors: archiveErrors)
|
|
progressIndicator = false
|
|
}
|
|
}
|
|
} catch let error {
|
|
await MainActor.run {
|
|
alert = .error(title: NSLocalizedString("Error exporting chat database", comment: "alert title"), error: responseError(error))
|
|
progressIndicator = false
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
static func importArchive(
|
|
_ archivePath: URL,
|
|
_ progressIndicator: Binding<Bool>,
|
|
_ alert: Binding<DatabaseAlert?>,
|
|
_ migration: Bool
|
|
) async -> Bool {
|
|
if archivePath.startAccessingSecurityScopedResource() {
|
|
defer {
|
|
archivePath.stopAccessingSecurityScopedResource()
|
|
}
|
|
await MainActor.run {
|
|
progressIndicator.wrappedValue = true
|
|
}
|
|
do {
|
|
try await apiDeleteStorage()
|
|
try? FileManager.default.createDirectory(at: getWallpaperDirectory(), withIntermediateDirectories: true)
|
|
do {
|
|
let config = ArchiveConfig(archivePath: archivePath.path)
|
|
let archiveErrors = try await apiImportArchive(config: config)
|
|
shouldImportAppSettingsDefault.set(true)
|
|
_ = kcDatabasePassword.remove()
|
|
if archiveErrors.isEmpty {
|
|
await operationEnded(.archiveImported, progressIndicator, alert)
|
|
return true
|
|
} else {
|
|
await operationEnded(.archiveImportedWithErrors(archiveErrors: archiveErrors), progressIndicator, alert)
|
|
return migration
|
|
}
|
|
} catch let error {
|
|
await operationEnded(.error(title: NSLocalizedString("Error importing chat database", comment: "alert title"), error: responseError(error)), progressIndicator, alert)
|
|
}
|
|
} catch let error {
|
|
await operationEnded(.error(title: NSLocalizedString("Error deleting chat database", comment: "alert title"), error: responseError(error)), progressIndicator, alert)
|
|
}
|
|
} else {
|
|
showAlert("Error accessing database file")
|
|
}
|
|
return false
|
|
}
|
|
|
|
private func deleteChat() async -> Bool {
|
|
await MainActor.run {
|
|
progressIndicator = true
|
|
}
|
|
do {
|
|
try await deleteChatAsync()
|
|
appFilesCountAndSize = directoryFileCountAndSize(getAppFilesDirectory())
|
|
await DatabaseView.operationEnded(.chatDeleted, $progressIndicator, $alert)
|
|
return true
|
|
} catch let error {
|
|
await DatabaseView.operationEnded(.error(title: NSLocalizedString("Error deleting database", comment: "alert title"), error: responseError(error)), $progressIndicator, $alert)
|
|
return false
|
|
}
|
|
}
|
|
|
|
private func deleteLegacyDatabase() {
|
|
if removeLegacyDatabaseAndFiles() {
|
|
legacyDatabase = false
|
|
} else {
|
|
alert = .error(title: NSLocalizedString("Error deleting old database", comment: "alert title"))
|
|
}
|
|
}
|
|
|
|
private static func operationEnded(_ dbAlert: DatabaseAlert, _ progressIndicator: Binding<Bool>, _ alert: Binding<DatabaseAlert?>) async {
|
|
await MainActor.run {
|
|
let m = ChatModel.shared
|
|
m.chatDbChanged = true
|
|
m.chatInitialized = false
|
|
progressIndicator.wrappedValue = false
|
|
}
|
|
await withCheckedContinuation { cont in
|
|
let okAlertActionWaiting = UIAlertAction(title: NSLocalizedString("Ok", comment: "alert button"), style: .default, handler: { _ in cont.resume() })
|
|
// show these alerts globally so they are visible when all sheets will be hidden
|
|
if case .archiveImported = dbAlert {
|
|
let (title, message) = archiveImportedAlertText()
|
|
showAlert(title, message: message, actions: { [okAlertActionWaiting] })
|
|
} else if case .archiveImportedWithErrors(let errs) = dbAlert {
|
|
let (title, message) = archiveImportedWithErrorsAlertText(errs: errs)
|
|
showAlert(title, message: message, actions: { [okAlertActionWaiting] })
|
|
} else if case .chatDeleted = dbAlert {
|
|
let (title, message) = chatDeletedAlertText()
|
|
showAlert(title, message: message, actions: { [okAlertActionWaiting] })
|
|
} else if case let .error(title, error) = dbAlert {
|
|
showAlert(title, message: error, actions: { [okAlertActionWaiting] })
|
|
} else {
|
|
alert.wrappedValue = dbAlert
|
|
cont.resume()
|
|
}
|
|
}
|
|
}
|
|
|
|
private func setCiTTL(_ ttl: ChatItemTTL) {
|
|
logger.debug("DatabaseView setChatItemTTL \(ttl.seconds ?? -1)")
|
|
progressIndicator = true
|
|
Task {
|
|
do {
|
|
try await setChatItemTTL(ttl)
|
|
await MainActor.run {
|
|
m.chatItemTTL = ttl
|
|
currentChatItemTTL = ttl
|
|
afterSetCiTTL()
|
|
}
|
|
} catch {
|
|
await MainActor.run {
|
|
alert = .error(title: NSLocalizedString("Error changing setting", comment: "alert title"), error: responseError(error))
|
|
chatItemTTL = currentChatItemTTL
|
|
afterSetCiTTL()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func afterSetCiTTL() {
|
|
progressIndicator = false
|
|
appFilesCountAndSize = directoryFileCountAndSize(getAppFilesDirectory())
|
|
do {
|
|
let chats = try apiGetChats()
|
|
m.updateChats(chats)
|
|
} catch let error {
|
|
logger.error("apiGetChats: cannot update chats \(responseError(error))")
|
|
}
|
|
}
|
|
|
|
private func deleteFiles() {
|
|
deleteAppFiles()
|
|
appFilesCountAndSize = directoryFileCountAndSize(getAppFilesDirectory())
|
|
}
|
|
}
|
|
|
|
func archiveImportedAlertText() -> (String, String) {
|
|
(
|
|
NSLocalizedString("Chat database imported", comment: ""),
|
|
NSLocalizedString("Restart the app to use imported chat database", comment: "")
|
|
)
|
|
}
|
|
func archiveImportedWithErrorsAlertText(errs: [ArchiveError]) -> (String, String) {
|
|
(
|
|
NSLocalizedString("Chat database imported", comment: ""),
|
|
NSLocalizedString("Restart the app to use imported chat database", comment: "") + "\n" + NSLocalizedString("Some non-fatal errors occurred during import:", comment: "") + archiveErrorsText(errs)
|
|
)
|
|
}
|
|
|
|
private func chatDeletedAlertText() -> (String, String) {
|
|
(
|
|
NSLocalizedString("Chat database deleted", comment: ""),
|
|
NSLocalizedString("Restart the app to create a new chat profile", comment: "")
|
|
)
|
|
}
|
|
|
|
func archiveErrorsText(_ errs: [ArchiveError]) -> String {
|
|
return "\n" + errs.map(showArchiveError).joined(separator: "\n")
|
|
|
|
func showArchiveError(_ err: ArchiveError) -> String {
|
|
switch err {
|
|
case let .import(importError): importError
|
|
case let .fileError(file, fileError): "\(file): \(fileError)"
|
|
}
|
|
}
|
|
}
|
|
|
|
func stopChatAsync() async throws {
|
|
try await apiStopChat()
|
|
ChatReceiver.shared.stop()
|
|
await MainActor.run { ChatModel.shared.chatRunning = false }
|
|
AppChatState.shared.set(.stopped)
|
|
}
|
|
|
|
func deleteChatAsync() async throws {
|
|
try await apiDeleteStorage()
|
|
_ = kcDatabasePassword.remove()
|
|
storeDBPassphraseGroupDefault.set(true)
|
|
deleteAppDatabaseAndFiles()
|
|
// Clean state so when creating new user the app will start chat automatically (see CreateProfile:createProfile())
|
|
DispatchQueue.main.async {
|
|
ChatModel.shared.users = []
|
|
}
|
|
}
|
|
|
|
struct DatabaseView_Previews: PreviewProvider {
|
|
@Environment(\.dismiss) static var mockDismiss
|
|
|
|
static var previews: some View {
|
|
DatabaseView(dismissSettingsSheet: mockDismiss, chatItemTTL: .none)
|
|
}
|
|
}
|