Simplified alert handling

This commit is contained in:
Vicnet
2023-11-10 15:43:48 +01:00
parent 0ea44f1321
commit 220ae96a8d
4 changed files with 23 additions and 40 deletions

View File

@@ -0,0 +1,79 @@
//
// AlertHandler.swift
// Nextcloud Cookbook iOS Client
//
// Created by Vincent Meilinger on 18.10.23.
//
import Foundation
import SwiftUI
enum AlertButton: LocalizedStringKey, Identifiable {
var id: Self {
return self
}
case OK = "Ok", DELETE = "Delete", CANCEL = "Cancel"
}
enum AlertType: Error {
case NO_TITLE,
DUPLICATE,
UPLOAD_ERROR,
CONFIRM_DELETE,
LOGIN_FAILED,
GENERIC,
CUSTOM(title: LocalizedStringKey, description: LocalizedStringKey)
var localizedDescription: LocalizedStringKey {
switch self {
case .NO_TITLE:
return "Please enter a recipe name."
case .DUPLICATE:
return "A recipe with that name already exists."
case .UPLOAD_ERROR:
return "Unable to upload your recipe. Please check your internet connection."
case .CONFIRM_DELETE:
return "This action is not reversible!"
case .LOGIN_FAILED:
return "Please check your credentials and internet connection."
case .CUSTOM(title: _, description: let description):
return description
default:
return "An unknown error occured."
}
}
var localizedTitle: LocalizedStringKey {
switch self {
case .NO_TITLE:
return "Missing recipe name."
case .DUPLICATE:
return "Duplicate recipe."
case .UPLOAD_ERROR:
return "Network error."
case .CONFIRM_DELETE:
return "Delete recipe?"
case .LOGIN_FAILED:
return "Login failed."
case .CUSTOM(title: let title, description: _):
return title
default:
return "Error."
}
}
var alertButtons: [AlertButton] {
switch self {
case .CONFIRM_DELETE:
return [.CANCEL, .DELETE]
default:
return [.OK]
}
}
}

View File

@@ -12,11 +12,14 @@ import PhotosUI
struct RecipeEditView: View {
@EnvironmentObject var alertHandler: AlertHandler
@ObservedObject var viewModel: MainViewModel
@State var recipe: RecipeDetail = RecipeDetail()
@Binding var isPresented: Bool
@State var uploadNew: Bool = true
@State private var presentAlert = false
@State private var alertType: AlertType = .GENERIC
@State private var alertAction: () -> () = {}
@StateObject private var prepDuration: Duration = Duration()
@StateObject private var cookDuration: Duration = Duration()
@@ -43,9 +46,9 @@ struct RecipeEditView: View {
Menu {
Button {
print("Delete recipe.")
alertHandler.present(alert: .CONFIRM_DELETE) {
deleteRecipe()
}
alertType = .CONFIRM_DELETE
alertAction = deleteRecipe
presentAlert = true
} label: {
Image(systemName: "trash")
.foregroundStyle(.red)
@@ -177,24 +180,22 @@ struct RecipeEditView: View {
keywords.append(keyword)
}
}
.alert(alertHandler.alert.localizedTitle, isPresented: $alertHandler.presentAlert) {
ForEach(alertHandler.alert.alertButtons) { buttonType in
.alert(alertType.localizedTitle, isPresented: $presentAlert) {
ForEach(alertType.alertButtons) { buttonType in
if buttonType == .OK {
Button(AlertButton.OK.rawValue, role: .cancel) {
alertHandler.alertAction()
alertHandler.dismiss()
alertAction()
}
} else if buttonType == .CANCEL {
Button(AlertButton.CANCEL.rawValue, role: .cancel) { }
} else if buttonType == .DELETE {
Button(AlertButton.DELETE.rawValue, role: .destructive) {
alertHandler.alertAction()
alertHandler.dismiss()
alertAction()
}
}
}
} message: {
Text(alertHandler.alert.localizedDescription)
Text(alertType.localizedDescription)
}
}
@@ -211,7 +212,9 @@ struct RecipeEditView: View {
func recipeValid() -> Bool {
// Check if the recipe has a name
if recipe.name.replacingOccurrences(of: " ", with: "") == "" {
alertHandler.present(alert: .NO_TITLE)
alertType = .NO_TITLE
alertAction = {}
presentAlert = true
return false
}
// Check if the recipe has a unique name
@@ -224,7 +227,9 @@ struct RecipeEditView: View {
.replacingOccurrences(of: " ", with: "")
.lowercased()
{
alertHandler.present(alert: .DUPLICATE)
alertType = .DUPLICATE
alertAction = {}
presentAlert = true
return false
}
}