Skip to content

Commit

Permalink
Add ability to delete backup by swipe from RestoreCloudViewController
Browse files Browse the repository at this point in the history
  • Loading branch information
ant013 committed Jul 26, 2023
1 parent d0de1df commit df27cae
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class RestoreCloudService {

private var cancellables = Set<AnyCancellable>()

private let deleteItemCompletedSubject = PassthroughSubject<Bool, Never>()
@Published var items = [Item]()

init(cloudAccountBackupManager: CloudAccountBackupManager, accountManager: AccountManager) {
Expand Down Expand Up @@ -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<Bool, Never> {
deleteItemCompletedSubject.eraseToAnyPublisher()
}

}

extension RestoreCloudService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ extension RestoreCloudViewModel {
restoreSubject.eraseToAnyPublisher()
}

var deleteItemCompletedPublisher: AnyPublisher<Bool, Never> {
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
Expand Down

0 comments on commit df27cae

Please sign in to comment.