Skip to content

Commit

Permalink
Fix backup module routing
Browse files Browse the repository at this point in the history
  • Loading branch information
ant013 committed Oct 9, 2023
1 parent 2511d35 commit 6429ea2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
18 changes: 10 additions & 8 deletions UnstoppableWallet/UnstoppableWallet/Core/Crypto/FullBackup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class BackupAppViewModel: ObservableObject {
@Published var passwordButtonDisabled = true
@Published var passwordButtonProcessing = false

private var dismissSubject = PassthroughSubject<Void, Never>()
@Published var sharePresented: URL?

init(accountManager: AccountManager, contactManager: ContactBookManager, cloudBackupManager: CloudBackupManager, favoritesManager: FavoritesManager, evmSyncSourceManager: EvmSyncSourceManager) {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -336,6 +338,12 @@ extension BackupAppViewModel {
}
}

extension BackupAppViewModel {
var dismissPublisher: AnyPublisher<Void, Never> {
dismissSubject.eraseToAnyPublisher()
}
}

extension BackupAppViewModel {
struct AccountItem: Comparable, Identifiable {
let accountId: String
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ComponentKit
import SwiftUI
import ThemeKit
import ComponentKit

struct BackupPasswordView: View {
@ObservedObject var viewModel: BackupAppViewModel
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
}
Expand Down Expand Up @@ -51,15 +52,15 @@ struct BackupTypeView: View {
}
}

@ViewBuilder func navigation(image: String, text: String, isAvailable: Binding<Bool> = .constant(true), action: @escaping () -> Void = {}) -> some View {
@ViewBuilder func navigation(image: String, text: String, isAvailable: Binding<Bool> = .constant(true), isActive: Binding<Bool>, 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)
Expand Down

0 comments on commit 6429ea2

Please sign in to comment.