Implemented the item deletion in the DeleteItemViewModifier view modifier for the Browse module.
This commit is contained in:
parent
13010816e1
commit
b333c7acac
@ -28,7 +28,7 @@
|
|||||||
// DeleteItemViewModifier
|
// DeleteItemViewModifier
|
||||||
|
|
||||||
"delete_item.action_sheet.title" = "Delete an item";
|
"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.ok" = "Yes, please delete it.";
|
||||||
"delete_item.action_sheet.button.cancel" = "No, I reconsidered.";
|
"delete_item.action_sheet.button.cancel" = "No, I reconsidered.";
|
||||||
"delete_item.system_alert.title" = "...";
|
"delete_item.system_alert.title" = "...";
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
// Copyright © 2022 Röck+Cöde. All rights reserved.
|
// Copyright © 2022 Röck+Cöde. All rights reserved.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
import DataModels
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
extension View {
|
extension View {
|
||||||
@ -23,7 +24,11 @@ extension View {
|
|||||||
|
|
||||||
func delete(
|
func delete(
|
||||||
item: Binding<(any FileSystemItem)?>,
|
item: Binding<(any FileSystemItem)?>,
|
||||||
|
deleted: @escaping ActionClosure
|
||||||
) -> some View {
|
) -> some View {
|
||||||
modifier(DeleteItemViewModifier(item: item))
|
modifier(DeleteItemViewModifier(
|
||||||
|
item: item,
|
||||||
|
deleted: deleted
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,9 +5,15 @@
|
|||||||
// Created by Javier Cicchelli on 16/12/2022.
|
// Created by Javier Cicchelli on 16/12/2022.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
import DataModels
|
||||||
|
import KeychainStorage
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct DeleteItemViewModifier: ViewModifier {
|
struct DeleteItemViewModifier: ViewModifier {
|
||||||
|
|
||||||
|
// MARK: Storages
|
||||||
|
|
||||||
|
@KeychainStorage(key: .KeychainStorage.account) private var account: Account?
|
||||||
|
|
||||||
// MARK: States
|
// MARK: States
|
||||||
|
|
||||||
@ -17,6 +23,12 @@ struct DeleteItemViewModifier: ViewModifier {
|
|||||||
|
|
||||||
@Binding var item: (any FileSystemItem)?
|
@Binding var item: (any FileSystemItem)?
|
||||||
|
|
||||||
|
// MARK: Properties
|
||||||
|
|
||||||
|
let deleted: ActionClosure
|
||||||
|
|
||||||
|
private let deleteItem: DeleteItemUseCase = .init()
|
||||||
|
|
||||||
// MARK: Body
|
// MARK: Body
|
||||||
|
|
||||||
func body(content: Content) -> some View {
|
func body(content: Content) -> some View {
|
||||||
@ -28,7 +40,7 @@ struct DeleteItemViewModifier: ViewModifier {
|
|||||||
bundle: .module
|
bundle: .module
|
||||||
),
|
),
|
||||||
message: Text(
|
message: Text(
|
||||||
"delete_item.action_sheet.message",
|
"delete_item.action_sheet.message \(itemName)",
|
||||||
bundle: .module
|
bundle: .module
|
||||||
),
|
),
|
||||||
buttons: [
|
buttons: [
|
||||||
@ -36,7 +48,9 @@ struct DeleteItemViewModifier: ViewModifier {
|
|||||||
"delete_item.action_sheet.button.ok",
|
"delete_item.action_sheet.button.ok",
|
||||||
bundle: .module
|
bundle: .module
|
||||||
)) {
|
)) {
|
||||||
// TODO: implement the deletion of an item from the backend.
|
Task {
|
||||||
|
await removeItem()
|
||||||
|
}
|
||||||
},
|
},
|
||||||
.cancel(Text(
|
.cancel(Text(
|
||||||
"delete_item.action_sheet.button.cancel",
|
"delete_item.action_sheet.button.cancel",
|
||||||
@ -58,9 +72,11 @@ struct DeleteItemViewModifier: ViewModifier {
|
|||||||
bundle: .module
|
bundle: .module
|
||||||
),
|
),
|
||||||
dismissButton: .cancel(Text(
|
dismissButton: .cancel(Text(
|
||||||
"delete_item.system_alert.button.dismiss",
|
"delete_item.system_alert.button.cancel",
|
||||||
bundle: .module
|
bundle: .module
|
||||||
))
|
)) {
|
||||||
|
item = nil
|
||||||
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -70,7 +86,46 @@ struct DeleteItemViewModifier: ViewModifier {
|
|||||||
// MARK: - Helpers
|
// MARK: - Helpers
|
||||||
|
|
||||||
private extension DeleteItemViewModifier {
|
private extension DeleteItemViewModifier {
|
||||||
|
|
||||||
|
// MARK: Computed
|
||||||
|
|
||||||
|
var itemName: String {
|
||||||
|
item?.name ?? .Constants.noName
|
||||||
|
}
|
||||||
|
|
||||||
var showDeletionConfirmation: Binding<Bool> {
|
var showDeletionConfirmation: Binding<Bool> {
|
||||||
.init { item != nil } set: { _ in }
|
.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"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user