Simplified alert handling
This commit is contained in:
@@ -11,7 +11,6 @@ import SwiftUI
|
||||
struct Nextcloud_Cookbook_iOS_ClientApp: App {
|
||||
@StateObject var userSettings = UserSettings()
|
||||
@StateObject var mainViewModel = MainViewModel()
|
||||
@StateObject var alertHandler = AlertHandler()
|
||||
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
@@ -31,7 +30,6 @@ struct Nextcloud_Cookbook_iOS_ClientApp: App {
|
||||
.init(identifier: userSettings.language ==
|
||||
SupportedLanguage.DEVICE.rawValue ? (Locale.current.language.languageCode?.identifier ?? "en") : userSettings.language)
|
||||
)
|
||||
.environmentObject(alertHandler)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,26 +9,6 @@ import Foundation
|
||||
import SwiftUI
|
||||
|
||||
|
||||
|
||||
class AlertHandler: ObservableObject {
|
||||
@Published var presentAlert: Bool = false
|
||||
var alert: AlertType = .GENERIC
|
||||
var alertAction: () -> () = {}
|
||||
|
||||
func present(alert: AlertType, onConfirm: @escaping () -> () = {}) {
|
||||
self.alert = alert
|
||||
self.alertAction = onConfirm
|
||||
self.presentAlert = true
|
||||
}
|
||||
|
||||
func dismiss() {
|
||||
self.alertAction = {}
|
||||
self.alert = .GENERIC
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
enum AlertButton: LocalizedStringKey, Identifiable {
|
||||
var id: Self {
|
||||
return self
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user