Implemented the item deletion in the DeleteItemViewModifier view modifier for the Browse module.

This commit is contained in:
Javier Cicchelli 2022-12-16 17:22:02 +01:00
parent 13010816e1
commit b333c7acac
3 changed files with 66 additions and 6 deletions

View File

@ -28,7 +28,7 @@
// DeleteItemViewModifier
"delete_item.action_sheet.title" = "Delete an item";
"delete_item.action_sheet.message" = "You are about to delete an item named \"%@\" from this folder.\n\nAre you sure you wish to proceed?";
"delete_item.action_sheet.message %@" = "You are about to delete an item named \"%@\" from this folder.\n\nAre you sure you wish to proceed?";
"delete_item.action_sheet.button.ok" = "Yes, please delete it.";
"delete_item.action_sheet.button.cancel" = "No, I reconsidered.";
"delete_item.system_alert.title" = "...";

View File

@ -6,6 +6,7 @@
// Copyright © 2022 Röck+Cöde. All rights reserved.
//
import DataModels
import SwiftUI
extension View {
@ -23,7 +24,11 @@ extension View {
func delete(
item: Binding<(any FileSystemItem)?>,
deleted: @escaping ActionClosure
) -> some View {
modifier(DeleteItemViewModifier(item: item))
modifier(DeleteItemViewModifier(
item: item,
deleted: deleted
))
}
}

View File

@ -5,10 +5,16 @@
// Created by Javier Cicchelli on 16/12/2022.
//
import DataModels
import KeychainStorage
import SwiftUI
struct DeleteItemViewModifier: ViewModifier {
// MARK: Storages
@KeychainStorage(key: .KeychainStorage.account) private var account: Account?
// MARK: States
@State private var showErrorAlert: Bool = false
@ -17,6 +23,12 @@ struct DeleteItemViewModifier: ViewModifier {
@Binding var item: (any FileSystemItem)?
// MARK: Properties
let deleted: ActionClosure
private let deleteItem: DeleteItemUseCase = .init()
// MARK: Body
func body(content: Content) -> some View {
@ -28,7 +40,7 @@ struct DeleteItemViewModifier: ViewModifier {
bundle: .module
),
message: Text(
"delete_item.action_sheet.message",
"delete_item.action_sheet.message \(itemName)",
bundle: .module
),
buttons: [
@ -36,7 +48,9 @@ struct DeleteItemViewModifier: ViewModifier {
"delete_item.action_sheet.button.ok",
bundle: .module
)) {
// TODO: implement the deletion of an item from the backend.
Task {
await removeItem()
}
},
.cancel(Text(
"delete_item.action_sheet.button.cancel",
@ -58,9 +72,11 @@ struct DeleteItemViewModifier: ViewModifier {
bundle: .module
),
dismissButton: .cancel(Text(
"delete_item.system_alert.button.dismiss",
"delete_item.system_alert.button.cancel",
bundle: .module
))
)) {
item = nil
}
)
}
}
@ -70,7 +86,46 @@ struct DeleteItemViewModifier: ViewModifier {
// MARK: - Helpers
private extension DeleteItemViewModifier {
// MARK: Computed
var itemName: String {
item?.name ?? .Constants.noName
}
var showDeletionConfirmation: Binding<Bool> {
.init { item != nil } set: { _ in }
}
// MARK: Functions
func removeItem() async {
guard
let id = item?.id,
let account
else {
showErrorAlert = true
return
}
do {
try await deleteItem(
id: id,
username: account.username,
password: account.password
)
deleted()
} catch {
showErrorAlert = true
}
}
}
// MARK: - String+Constants
private extension String {
enum Constants {
static let noName = "no name"
}
}