Code cleanup

This commit is contained in:
Vicnet
2023-10-18 13:34:24 +02:00
parent f55158e99e
commit 05c30a2cff
4 changed files with 74 additions and 67 deletions

View File

@@ -28,7 +28,7 @@ struct CategoryDetailView: View {
}
}
.navigationDestination(for: Recipe.self) { recipe in
RecipeDetailView(viewModel: viewModel, recipe: recipe)//.id(recipe.recipe_id)
RecipeDetailView(viewModel: viewModel, recipe: recipe)
}
.navigationTitle(categoryName == "*" ? "Other" : categoryName)
.toolbar {

View File

@@ -100,13 +100,10 @@ struct MainView: View {
self.selectedCategory = cat
}
}
}
.refreshable {
await viewModel.loadCategoryList(needsUpdate: true)
}
// TODO: SET DEFAULT CATEGORY
}
}

View File

@@ -100,14 +100,14 @@ struct RecipeDetailView: View {
}
struct RecipeDurationSection: View {
fileprivate struct RecipeDurationSection: View {
@State var recipeDetail: RecipeDetail
var body: some View {
HStack(alignment: .center) {
if let prepTime = recipeDetail.prepTime {
VStack {
SecondaryLabel(text: String(localized: "Prep time"))
SecondaryLabel(text: LocalizedStringKey("Preparation"))
Text(DateFormatter.formatDate(duration: prepTime))
.lineLimit(1)
}.padding()
@@ -115,7 +115,7 @@ struct RecipeDurationSection: View {
if let cookTime = recipeDetail.cookTime {
VStack {
SecondaryLabel(text: String(localized: "Cook time"))
SecondaryLabel(text: LocalizedStringKey("Cooking"))
Text(DateFormatter.formatDate(duration: cookTime))
.lineLimit(1)
}.padding()
@@ -123,7 +123,7 @@ struct RecipeDurationSection: View {
if let totalTime = recipeDetail.totalTime {
VStack {
SecondaryLabel(text: String(localized: "Total time"))
SecondaryLabel(text: LocalizedStringKey("Total time"))
Text(DateFormatter.formatDate(duration: totalTime))
.lineLimit(1)
}.padding()
@@ -133,18 +133,19 @@ struct RecipeDurationSection: View {
}
struct RecipeIngredientSection: View {
fileprivate struct RecipeIngredientSection: View {
@State var recipeDetail: RecipeDetail
var body: some View {
VStack(alignment: .leading) {
Divider()
HStack {
if recipeDetail.recipeYield == 0 {
SecondaryLabel(text: String(localized: "Ingredients"))
SecondaryLabel(text: LocalizedStringKey("Ingredients"))
} else if recipeDetail.recipeYield == 1 {
SecondaryLabel(text: String(localized: "Ingredients per serving"))
SecondaryLabel(text: LocalizedStringKey("Ingredients per serving"))
} else {
SecondaryLabel(text: String(localized: "Ingredients for \(recipeDetail.recipeYield) servings"))
SecondaryLabel(text: LocalizedStringKey("Ingredients for \(recipeDetail.recipeYield) servings"))
}
Spacer()
}
@@ -156,7 +157,9 @@ struct RecipeIngredientSection: View {
}
}
struct IngredientListItem: View {
fileprivate struct IngredientListItem: View {
@State var ingredient: String
@State var isSelected: Bool = false
@@ -180,13 +183,15 @@ struct IngredientListItem: View {
}
}
struct RecipeToolSection: View {
fileprivate struct RecipeToolSection: View {
@State var recipeDetail: RecipeDetail
var body: some View {
VStack(alignment: .leading) {
Divider()
HStack {
SecondaryLabel(text: String(localized: "Tools"))
SecondaryLabel(text: LocalizedStringKey("Tools"))
Spacer()
}
ForEach(recipeDetail.tool, id: \.self) { tool in
@@ -201,13 +206,15 @@ struct RecipeToolSection: View {
}
}
struct RecipeInstructionSection: View {
fileprivate struct RecipeInstructionSection: View {
@State var recipeDetail: RecipeDetail
var body: some View {
VStack(alignment: .leading) {
Divider()
HStack {
SecondaryLabel(text: String(localized: "Instructions"))
SecondaryLabel(text: LocalizedStringKey("Instructions"))
Spacer()
}
ForEach(0..<recipeDetail.recipeInstructions.count) { ix in
@@ -222,9 +229,8 @@ struct RecipeInstructionSection: View {
struct SecondaryLabel: View {
let text: String
fileprivate struct SecondaryLabel: View {
let text: LocalizedStringKey
var body: some View {
Text(text)
.foregroundColor(.secondary)

View File

@@ -10,50 +10,6 @@ import SwiftUI
import PhotosUI
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."
}
}
}
struct RecipeEditView: View {
@ObservedObject var viewModel: MainViewModel
@@ -332,7 +288,7 @@ struct RecipeEditView: View {
struct EditableListSection: View {
fileprivate struct EditableListSection: View {
@State var title: LocalizedStringKey
@Binding var items: [String]
@@ -377,7 +333,7 @@ struct EditableListSection: View {
}
struct DurationPicker: View {
fileprivate struct DurationPicker: View {
@State var title: LocalizedStringKey
@ObservedObject var duration: Duration
@@ -401,7 +357,9 @@ struct DurationPicker: View {
}
}
class Duration: ObservableObject {
fileprivate class Duration: ObservableObject {
@Published var minuteComponent: String = "00" {
didSet {
if minuteComponent.count > 2 {
@@ -449,3 +407,49 @@ class Duration: ObservableObject {
return "PT\(hourComponent)H\(minuteComponent)M00S"
}
}
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."
}
}
}