diff --git a/UnstoppableWallet/UnstoppableWallet/Core/Managers/CloudAccountBackupManager.swift b/UnstoppableWallet/UnstoppableWallet/Core/Managers/CloudAccountBackupManager.swift index ec7ef6406b..0af97e8429 100644 --- a/UnstoppableWallet/UnstoppableWallet/Core/Managers/CloudAccountBackupManager.swift +++ b/UnstoppableWallet/UnstoppableWallet/Core/Managers/CloudAccountBackupManager.swift @@ -155,11 +155,16 @@ extension CloudAccountBackupManager { } func delete(uniqueId: Data) async throws { + let hex = uniqueId.hs.hex + try await delete(uniqueId: hex) + } + + func delete(uniqueId: String) async throws { guard let iCloudUrl else { throw BackupError.urlNotAvailable } - guard let item = items.first(where: { name, backup in backup.id == uniqueId.hs.hex }) else { + guard let item = items.first(where: { name, backup in backup.id == uniqueId }) else { throw BackupError.itemNotFound } diff --git a/UnstoppableWallet/UnstoppableWallet/Modules/RestoreAccount/RestoreCloud/RestoreCloudService.swift b/UnstoppableWallet/UnstoppableWallet/Modules/RestoreAccount/RestoreCloud/RestoreCloudService.swift index 857c619302..44eeefe5f8 100644 --- a/UnstoppableWallet/UnstoppableWallet/Modules/RestoreAccount/RestoreCloud/RestoreCloudService.swift +++ b/UnstoppableWallet/UnstoppableWallet/Modules/RestoreAccount/RestoreCloud/RestoreCloudService.swift @@ -7,6 +7,7 @@ class RestoreCloudService { private var cancellables = Set() + private let deleteItemCompletedSubject = PassthroughSubject() @Published var items = [Item]() init(cloudAccountBackupManager: CloudAccountBackupManager, accountManager: AccountManager) { @@ -48,6 +49,22 @@ class RestoreCloudService { } extension RestoreCloudService { + + func remove(id: String) { + Task { + do { + try await cloudAccountBackupManager.delete(uniqueId: id) + deleteItemCompletedSubject.send(true) + } catch { + deleteItemCompletedSubject.send(false) + } + } + } + + var deleteItemCompletedPublisher: AnyPublisher { + deleteItemCompletedSubject.eraseToAnyPublisher() + } + } extension RestoreCloudService { diff --git a/UnstoppableWallet/UnstoppableWallet/Modules/RestoreAccount/RestoreCloud/RestoreCloudViewController.swift b/UnstoppableWallet/UnstoppableWallet/Modules/RestoreAccount/RestoreCloud/RestoreCloudViewController.swift index 591446a473..985da08dcd 100644 --- a/UnstoppableWallet/UnstoppableWallet/Modules/RestoreAccount/RestoreCloud/RestoreCloudViewController.swift +++ b/UnstoppableWallet/UnstoppableWallet/Modules/RestoreAccount/RestoreCloud/RestoreCloudViewController.swift @@ -67,6 +67,12 @@ class RestoreCloudViewController: ThemeViewController { self?.restore(item: $0) }.store(in: &cancellables) + viewModel.deleteItemCompletedPublisher + .receive(on: DispatchQueue.main) + .sink { [weak self] in + self?.deleteBackupCompleted(successful: $0) + }.store(in: &cancellables) + tableView.buildSections() } @@ -84,17 +90,37 @@ class RestoreCloudViewController: ThemeViewController { navigationController?.pushViewController(viewController, animated: true) } + private func deleteBackupCompleted(successful: Bool) { + if successful { + HudHelper.instance.show(banner: .deleted) + } else { + HudHelper.instance.show(banner: .error(string: "backup.cloud.cant_delete_file".localized)) + } + } + private func sync(viewItem: RestoreCloudViewModel.ViewItem) { emptyView.isHidden = !viewItem.isEmpty tableView.reload() } + private func deleteRowAction(id: String) -> RowAction { + RowAction(pattern: .icon( + image: UIImage(named: "circle_minus_shifted_24"), + background: UIColor(red: 0, green: 0, blue: 0, alpha: 0) + ), action: { [weak self] cell in + self?.viewModel.remove(id: id) + }) + } + private func row(viewItem: RestoreCloudViewModel.BackupViewItem, rowInfo: RowInfo) -> RowProtocol { - tableView.universalRow62( + let rowAction = deleteRowAction(id: viewItem.uniqueId) + + return tableView.universalRow62( id: viewItem.uniqueId, title: .body(viewItem.name), description: .subhead2(viewItem.description), accessoryType: .disclosure, + rowActionProvider: { [ rowAction ] }, isFirst: rowInfo.isFirst, isLast: rowInfo.isLast ) { [weak self] in diff --git a/UnstoppableWallet/UnstoppableWallet/Modules/RestoreAccount/RestoreCloud/RestoreCloudViewModel.swift b/UnstoppableWallet/UnstoppableWallet/Modules/RestoreAccount/RestoreCloud/RestoreCloudViewModel.swift index cb8a422d97..026487f953 100644 --- a/UnstoppableWallet/UnstoppableWallet/Modules/RestoreAccount/RestoreCloud/RestoreCloudViewModel.swift +++ b/UnstoppableWallet/UnstoppableWallet/Modules/RestoreAccount/RestoreCloud/RestoreCloudViewModel.swift @@ -47,6 +47,14 @@ extension RestoreCloudViewModel { restoreSubject.eraseToAnyPublisher() } + var deleteItemCompletedPublisher: AnyPublisher { + service.deleteItemCompletedPublisher + } + + func remove(id: String) { + service.remove(id: id) + } + func didTap(id: String) { guard let item = service.items.first(where: { item in item.backup.id == id }) else { return