mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2025-06-28 20:29:53 +00:00
ios: show alert when import database is failed or succeeded (#5400)
* ios: show alert when import database is failed or succeeded * don't hide error alert until pressing Ok * always skip starting chat in case of import error * changes * defer
This commit is contained in:
parent
909f52342c
commit
bcdf08488e
2 changed files with 28 additions and 8 deletions
|
@ -262,8 +262,7 @@ struct DatabaseView: View {
|
||||||
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."),
|
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")) {
|
primaryButton: .destructive(Text("Import")) {
|
||||||
stopChatRunBlockStartChat(m.chatRunning == false, $progressIndicator) {
|
stopChatRunBlockStartChat(m.chatRunning == false, $progressIndicator) {
|
||||||
_ = await DatabaseView.importArchive(fileURL, $progressIndicator, $alert)
|
await DatabaseView.importArchive(fileURL, $progressIndicator, $alert, false)
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
secondaryButton: .cancel()
|
secondaryButton: .cancel()
|
||||||
|
@ -467,9 +466,13 @@ struct DatabaseView: View {
|
||||||
static func importArchive(
|
static func importArchive(
|
||||||
_ archivePath: URL,
|
_ archivePath: URL,
|
||||||
_ progressIndicator: Binding<Bool>,
|
_ progressIndicator: Binding<Bool>,
|
||||||
_ alert: Binding<DatabaseAlert?>
|
_ alert: Binding<DatabaseAlert?>,
|
||||||
|
_ migration: Bool
|
||||||
) async -> Bool {
|
) async -> Bool {
|
||||||
if archivePath.startAccessingSecurityScopedResource() {
|
if archivePath.startAccessingSecurityScopedResource() {
|
||||||
|
defer {
|
||||||
|
archivePath.stopAccessingSecurityScopedResource()
|
||||||
|
}
|
||||||
await MainActor.run {
|
await MainActor.run {
|
||||||
progressIndicator.wrappedValue = true
|
progressIndicator.wrappedValue = true
|
||||||
}
|
}
|
||||||
|
@ -483,17 +486,17 @@ struct DatabaseView: View {
|
||||||
_ = kcDatabasePassword.remove()
|
_ = kcDatabasePassword.remove()
|
||||||
if archiveErrors.isEmpty {
|
if archiveErrors.isEmpty {
|
||||||
await operationEnded(.archiveImported, progressIndicator, alert)
|
await operationEnded(.archiveImported, progressIndicator, alert)
|
||||||
|
return true
|
||||||
} else {
|
} else {
|
||||||
await operationEnded(.archiveImportedWithErrors(archiveErrors: archiveErrors), progressIndicator, alert)
|
await operationEnded(.archiveImportedWithErrors(archiveErrors: archiveErrors), progressIndicator, alert)
|
||||||
|
return migration
|
||||||
}
|
}
|
||||||
return true
|
|
||||||
} catch let error {
|
} catch let error {
|
||||||
await operationEnded(.error(title: "Error importing chat database", error: responseError(error)), progressIndicator, alert)
|
await operationEnded(.error(title: "Error importing chat database", error: responseError(error)), progressIndicator, alert)
|
||||||
}
|
}
|
||||||
} catch let error {
|
} catch let error {
|
||||||
await operationEnded(.error(title: "Error deleting chat database", error: responseError(error)), progressIndicator, alert)
|
await operationEnded(.error(title: "Error deleting chat database", error: responseError(error)), progressIndicator, alert)
|
||||||
}
|
}
|
||||||
archivePath.stopAccessingSecurityScopedResource()
|
|
||||||
} else {
|
} else {
|
||||||
showAlert("Error accessing database file")
|
showAlert("Error accessing database file")
|
||||||
}
|
}
|
||||||
|
@ -542,6 +545,8 @@ struct DatabaseView: View {
|
||||||
} else if case .chatDeleted = dbAlert {
|
} else if case .chatDeleted = dbAlert {
|
||||||
let (title, message) = chatDeletedAlertText()
|
let (title, message) = chatDeletedAlertText()
|
||||||
showAlert(title, message: message, actions: { [okAlertActionWaiting] })
|
showAlert(title, message: message, actions: { [okAlertActionWaiting] })
|
||||||
|
} else if case let .error(title, error) = dbAlert {
|
||||||
|
showAlert("\(title)", message: error, actions: { [okAlertActionWaiting] })
|
||||||
} else {
|
} else {
|
||||||
alert.wrappedValue = dbAlert
|
alert.wrappedValue = dbAlert
|
||||||
cont.resume()
|
cont.resume()
|
||||||
|
@ -587,13 +592,13 @@ struct DatabaseView: View {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func archiveImportedAlertText() -> (String, String) {
|
func archiveImportedAlertText() -> (String, String) {
|
||||||
(
|
(
|
||||||
NSLocalizedString("Chat database imported", comment: ""),
|
NSLocalizedString("Chat database imported", comment: ""),
|
||||||
NSLocalizedString("Restart the app to use imported chat database", comment: "")
|
NSLocalizedString("Restart the app to use imported chat database", comment: "")
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
private func archiveImportedWithErrorsAlertText(errs: [ArchiveError]) -> (String, String) {
|
func archiveImportedWithErrorsAlertText(errs: [ArchiveError]) -> (String, String) {
|
||||||
(
|
(
|
||||||
NSLocalizedString("Chat database imported", comment: ""),
|
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)
|
NSLocalizedString("Restart the app to use imported chat database", comment: "") + "\n" + NSLocalizedString("Some non-fatal errors occurred during import:", comment: "") + archiveErrorsText(errs)
|
||||||
|
|
|
@ -96,6 +96,7 @@ struct MigrateToDevice: View {
|
||||||
@Binding var migrationState: MigrationToState?
|
@Binding var migrationState: MigrationToState?
|
||||||
@State private var useKeychain = storeDBPassphraseGroupDefault.get()
|
@State private var useKeychain = storeDBPassphraseGroupDefault.get()
|
||||||
@State private var alert: MigrateToDeviceViewAlert?
|
@State private var alert: MigrateToDeviceViewAlert?
|
||||||
|
@State private var databaseAlert: DatabaseAlert? = nil
|
||||||
private let tempDatabaseUrl = urlForTemporaryDatabase()
|
private let tempDatabaseUrl = urlForTemporaryDatabase()
|
||||||
@State private var chatReceiver: MigrationChatReceiver? = nil
|
@State private var chatReceiver: MigrationChatReceiver? = nil
|
||||||
// Prevent from hiding the view until migration is finished or app deleted
|
// Prevent from hiding the view until migration is finished or app deleted
|
||||||
|
@ -178,6 +179,20 @@ struct MigrateToDevice: View {
|
||||||
return Alert(title: Text(title), message: Text(error))
|
return Alert(title: Text(title), message: Text(error))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.alert(item: $databaseAlert) { item in
|
||||||
|
switch item {
|
||||||
|
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 .error(title, error):
|
||||||
|
return Alert(title: Text(title), message: Text(error))
|
||||||
|
default: // not expected this branch to be called because this alert is used only for importArchive purpose
|
||||||
|
return Alert(title: Text("Error"))
|
||||||
|
}
|
||||||
|
}
|
||||||
.interactiveDismissDisabled(backDisabled)
|
.interactiveDismissDisabled(backDisabled)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -243,7 +258,7 @@ struct MigrateToDevice: View {
|
||||||
) { result in
|
) { result in
|
||||||
if case let .success(files) = result, let fileURL = files.first {
|
if case let .success(files) = result, let fileURL = files.first {
|
||||||
Task {
|
Task {
|
||||||
let success = await DatabaseView.importArchive(fileURL, $importingArchiveFromFileProgressIndicator, Binding.constant(nil))
|
let success = await DatabaseView.importArchive(fileURL, $importingArchiveFromFileProgressIndicator, $databaseAlert, true)
|
||||||
if success {
|
if success {
|
||||||
DatabaseView.startChat(
|
DatabaseView.startChat(
|
||||||
Binding.constant(false),
|
Binding.constant(false),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue