diff --git a/UnstoppableWallet/UnstoppableWallet/Core/Crypto/FullBackup.swift b/UnstoppableWallet/UnstoppableWallet/Core/Crypto/FullBackup.swift index d811cd8c2d..e22ba17c5f 100644 --- a/UnstoppableWallet/UnstoppableWallet/Core/Crypto/FullBackup.swift +++ b/UnstoppableWallet/UnstoppableWallet/Core/Crypto/FullBackup.swift @@ -21,14 +21,16 @@ extension FullBackup: Codable { case timestamp } -// init(from decoder: Decoder) throws { -// let container = try decoder.container(keyedBy: CodingKeys.self) -// wallets = (try? container.decode([RestoreCloudModule.RestoredBackup].self, forKey: .wallets)) ?? [] -// watchlistIds = (try? container.decode([String].self, forKey: .watchlistIds)) ?? [] -// contacts = try? container.decode([BackupContact].self, forKey: .contacts) -// evmSyncSources = try? container.decode(SyncSourceBackup.self, forKey: .evmSyncSources) -// settings = try? container.decode(SettingsBackup.self, forKey: .settings) -// } + init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + id = try container.decode(String.self, forKey: .id) + wallets = (try? container.decode([RestoreCloudModule.RestoredBackup].self, forKey: .wallets)) ?? [] + watchlistIds = (try? container.decode([String].self, forKey: .watchlistIds)) ?? [] + contacts = try? container.decode(BackupCrypto.self, forKey: .contacts) + settings = try? container.decode(SettingsBackup.self, forKey: .settings) + version = try container.decode(Int.self, forKey: .version) + timestamp = try? container.decode(TimeInterval.self, forKey: .timestamp) + } func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) diff --git a/UnstoppableWallet/UnstoppableWallet/Modules/Settings/BackupApp/BackupAppViewModel.swift b/UnstoppableWallet/UnstoppableWallet/Modules/Settings/BackupApp/BackupAppViewModel.swift index 77dc46cf5b..64c4c72aa3 100644 --- a/UnstoppableWallet/UnstoppableWallet/Modules/Settings/BackupApp/BackupAppViewModel.swift +++ b/UnstoppableWallet/UnstoppableWallet/Modules/Settings/BackupApp/BackupAppViewModel.swift @@ -85,6 +85,7 @@ class BackupAppViewModel: ObservableObject { @Published var passwordButtonDisabled = true @Published var passwordButtonProcessing = false + private var dismissSubject = PassthroughSubject() @Published var sharePresented: URL? init(accountManager: AccountManager, contactManager: ContactBookManager, cloudBackupManager: CloudBackupManager, favoritesManager: FavoritesManager, evmSyncSourceManager: EvmSyncSourceManager) { @@ -227,7 +228,7 @@ extension BackupAppViewModel { // Backup Name VieeModel extension BackupAppViewModel { var nextName: String { - let name = { (_: String) in [Self.backupNamePrefix, "1"].joined(separator: " ") } + let name = { [Self.backupNamePrefix, $0].joined(separator: " ") } switch destination { case .cloud: let exists = cloudBackupManager @@ -249,7 +250,7 @@ extension BackupAppViewModel { func validateName() { if name.isEmpty { nameCautionState = .caution(.init(text: NameError.empty.localizedDescription, type: .error)) - } else if (cloudBackupManager.existFilenames + [Self.backupNamePrefix + "2"]).contains(where: { $0.lowercased() == name.lowercased() }) { + } else if destination == .cloud, cloudBackupManager.existFilenames.contains(where: { $0.lowercased() == name.lowercased() }) { nameCautionState = .caution(.init(text: NameError.alreadyExist.localizedDescription, type: .error)) } else { nameCautionState = .none @@ -318,6 +319,7 @@ extension BackupAppViewModel { try cloudBackupManager.save(fields: configuration, passphrase: password, name: name) passwordButtonProcessing = false await showSuccess() + dismissSubject.send() } catch { passwordButtonProcessing = false await show(error: error) @@ -336,6 +338,12 @@ extension BackupAppViewModel { } } +extension BackupAppViewModel { + var dismissPublisher: AnyPublisher { + dismissSubject.eraseToAnyPublisher() + } +} + extension BackupAppViewModel { struct AccountItem: Comparable, Identifiable { let accountId: String diff --git a/UnstoppableWallet/UnstoppableWallet/Modules/Settings/BackupApp/BackupPassword/BackupPasswordView.swift b/UnstoppableWallet/UnstoppableWallet/Modules/Settings/BackupApp/BackupPassword/BackupPasswordView.swift index e8d71c289e..1f7c6b2526 100644 --- a/UnstoppableWallet/UnstoppableWallet/Modules/Settings/BackupApp/BackupPassword/BackupPasswordView.swift +++ b/UnstoppableWallet/UnstoppableWallet/Modules/Settings/BackupApp/BackupPassword/BackupPasswordView.swift @@ -1,6 +1,6 @@ +import ComponentKit import SwiftUI import ThemeKit -import ComponentKit struct BackupPasswordView: View { @ObservedObject var viewModel: BackupAppViewModel @@ -67,7 +67,7 @@ struct BackupPasswordView: View { .animation(.default, value: viewModel.passwordButtonProcessing) } .sheet(item: $viewModel.sharePresented) { url in - let completion: UIActivityViewController.CompletionWithItemsHandler = { type, success, list, error in + let completion: UIActivityViewController.CompletionWithItemsHandler = { _, success, _, error in if success { showDone() backupPresented = false @@ -82,6 +82,9 @@ struct BackupPasswordView: View { ActivityViewController(activityItems: [url], completionWithItemsHandler: completion) } } + .onReceive(viewModel.dismissPublisher) { + backupPresented = false + } .navigationBarTitle("backup.password.title".localized) .navigationBarTitleDisplayMode(.inline) .toolbar { diff --git a/UnstoppableWallet/UnstoppableWallet/Modules/Settings/BackupApp/BackupType/BackupTypeView.swift b/UnstoppableWallet/UnstoppableWallet/Modules/Settings/BackupApp/BackupType/BackupTypeView.swift index aee60de716..07a2ed5232 100644 --- a/UnstoppableWallet/UnstoppableWallet/Modules/Settings/BackupApp/BackupType/BackupTypeView.swift +++ b/UnstoppableWallet/UnstoppableWallet/Modules/Settings/BackupApp/BackupType/BackupTypeView.swift @@ -5,7 +5,8 @@ struct BackupTypeView: View { @ObservedObject var viewModel: BackupAppViewModel @Binding var backupPresented: Bool - @State var navigationPushed = false + @State var cloudNavigationPushed = false + @State var localNavigationPushed = false @State var cloudAlertPresented = false var body: some View { @@ -16,11 +17,11 @@ struct BackupTypeView: View { .padding(EdgeInsets(top: 0, leading: .margin16, bottom: .margin12, trailing: .margin16)) ListSection { - navigation(image: "icloud_24", text: "backup_type.cloud".localized, isAvailable: $viewModel.cloudAvailable) { + navigation(image: "icloud_24", text: "backup_type.cloud".localized, isAvailable: $viewModel.cloudAvailable, isActive: $cloudNavigationPushed) { if viewModel.cloudAvailable { viewModel.destination = .cloud } else { cloudAlertPresented = true } } - navigation(image: "file_24", text: "backup_type.file".localized) { + navigation(image: "file_24", text: "backup_type.file".localized, isActive: $localNavigationPushed) { viewModel.destination = .local } } @@ -51,15 +52,15 @@ struct BackupTypeView: View { } } - @ViewBuilder func navigation(image: String, text: String, isAvailable: Binding = .constant(true), action: @escaping () -> Void = {}) -> some View { + @ViewBuilder func navigation(image: String, text: String, isAvailable: Binding = .constant(true), isActive: Binding, action: @escaping () -> Void = {}) -> some View { if isAvailable.wrappedValue { NavigationRow( destination: { BackupListView(viewModel: viewModel, backupPresented: $backupPresented) }, - isActive: $navigationPushed + isActive: isActive ) { row(image: image, text: text.localized) } - .onChange(of: navigationPushed) { _ in action() } + .onChange(of: isActive.wrappedValue) { _ in action() } } else { ClickableRow(action: action) { row(image: image, text: text.localized)