134 lines
3.3 KiB
Swift
134 lines
3.3 KiB
Swift
//
|
|
// DeleteItemViewModifier.swift
|
|
// Browse
|
|
//
|
|
// 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
|
|
|
|
// MARK: Bindings
|
|
|
|
@Binding var item: (any FileSystemItem)?
|
|
|
|
// MARK: Properties
|
|
|
|
let deleted: ActionClosure
|
|
|
|
private let deleteItem: DeleteItemUseCase = .init()
|
|
|
|
// MARK: Body
|
|
|
|
func body(content: Content) -> some View {
|
|
content
|
|
.actionSheet(isPresented: showDeletionConfirmation) {
|
|
ActionSheet(
|
|
title: Text(
|
|
"delete_item.action_sheet.title",
|
|
bundle: .module
|
|
),
|
|
message: Text(
|
|
"delete_item.action_sheet.message \(itemName)",
|
|
bundle: .module
|
|
),
|
|
buttons: [
|
|
.destructive(Text(
|
|
"delete_item.action_sheet.button.ok",
|
|
bundle: .module
|
|
)) {
|
|
Task {
|
|
await removeItem()
|
|
}
|
|
},
|
|
.cancel(Text(
|
|
"delete_item.action_sheet.button.cancel",
|
|
bundle: .module
|
|
)) {
|
|
item = nil
|
|
},
|
|
]
|
|
)
|
|
}
|
|
.alert(isPresented: $showErrorAlert) {
|
|
Alert(
|
|
title: Text(
|
|
"delete_item.system_alert.title",
|
|
bundle: .module
|
|
),
|
|
message: Text(
|
|
"delete_item.system_alert.message",
|
|
bundle: .module
|
|
),
|
|
dismissButton: .cancel(Text(
|
|
"delete_item.system_alert.button.cancel",
|
|
bundle: .module
|
|
)) {
|
|
item = nil
|
|
}
|
|
)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
// 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
|
|
)
|
|
|
|
item = nil
|
|
|
|
deleted()
|
|
} catch {
|
|
showErrorAlert = true
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - String+Constants
|
|
|
|
private extension String {
|
|
enum Constants {
|
|
static let noName = "no name"
|
|
}
|
|
}
|