Added category 'All' recipes

This commit is contained in:
Vicnet
2023-10-22 20:28:51 +02:00
parent 05c30a2cff
commit 8f32946e27
17 changed files with 574 additions and 243 deletions

View File

@@ -12,6 +12,7 @@ import PhotosUI
struct RecipeEditView: View {
@EnvironmentObject var alertHandler: AlertHandler
@ObservedObject var viewModel: MainViewModel
@State var recipe: RecipeDetail = RecipeDetail()
@Binding var isPresented: Bool
@@ -25,8 +26,6 @@ struct RecipeEditView: View {
@State private var keywords: [String] = []
@State private var keywordSuggestions: [String] = []
@State private var alertMessage: ErrorMessages = .GENERIC
@State private var presentAlert: Bool = false
@State private var waitingForUpload: Bool = false
var body: some View {
@@ -43,8 +42,9 @@ struct RecipeEditView: View {
Menu {
Button {
print("Delete recipe.")
alertMessage = .CONFIRM_DELETE
presentAlert = true
alertHandler.present(alert: .CONFIRM_DELETE) {
deleteRecipe()
}
} label: {
Image(systemName: "trash")
.foregroundStyle(.red)
@@ -152,18 +152,24 @@ struct RecipeEditView: View {
keywords.append(keyword)
}
}
.alert(alertMessage.localizedTitle, isPresented: $presentAlert) {
switch alertMessage {
case .CONFIRM_DELETE:
Button("Cancel", role: .cancel) { }
Button("Delete", role: .destructive) {
deleteRecipe()
.alert(alertHandler.alert.localizedTitle, isPresented: $alertHandler.presentAlert) {
ForEach(alertHandler.alert.alertButtons) { buttonType in
if buttonType == .OK {
Button(AlertButton.OK.rawValue, role: .cancel) {
alertHandler.alertAction()
alertHandler.dismiss()
}
} else if buttonType == .CANCEL {
Button(AlertButton.CANCEL.rawValue, role: .cancel) { }
} else if buttonType == .DELETE {
Button(AlertButton.DELETE.rawValue, role: .destructive) {
alertHandler.alertAction()
alertHandler.dismiss()
}
}
default:
Button("Ok", role: .cancel) { }
}
} message: {
Text(alertMessage.localizedDescription)
Text(alertHandler.alert.localizedDescription)
}
}
@@ -179,9 +185,8 @@ struct RecipeEditView: View {
func recipeValid() -> Bool {
// Check if the recipe has a name
if recipe.name == "" {
self.alertMessage = .NO_TITLE
self.presentAlert = true
if recipe.name.replacingOccurrences(of: " ", with: "") == "" {
alertHandler.present(alert: .NO_TITLE)
return false
}
// Check if the recipe has a unique name
@@ -194,8 +199,7 @@ struct RecipeEditView: View {
.replacingOccurrences(of: " ", with: "")
.lowercased()
{
self.alertMessage = .DUPLICATE
self.presentAlert = true
alertHandler.present(alert: .DUPLICATE)
return false
}
}
@@ -266,11 +270,7 @@ struct RecipeEditView: View {
guard let data = data else { return }
do {
let error = try JSONDecoder().decode(ServerMessage.self, from: data)
DispatchQueue.main.sync {
alertMessage = .CUSTOM(title: "Error.", description: LocalizedStringKey(stringLiteral: error.msg))
presentAlert = true
return
}
// TODO: Better error handling (Show error to user!)
} catch {
}
@@ -410,46 +410,3 @@ fileprivate class Duration: ObservableObject {
fileprivate enum ErrorMessages: Error {
case NO_TITLE,
DUPLICATE,
UPLOAD_ERROR,
CONFIRM_DELETE,
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 .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 .CUSTOM(title: let title, description: _):
return title
default:
return "Error."
}
}
}