Skip to content

Commit

Permalink
Refactor functionality to avoid hardcoded recipe strings and use bett…
Browse files Browse the repository at this point in the history
…er terminology. Added category field on Recipe schema
  • Loading branch information
marcabreracast committed Nov 17, 2023
1 parent 0f38f93 commit 85c4ebd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import RealmSwift
class Recipe: Object, Identifiable {
@Persisted(primaryKey: true) var _id: ObjectId?

@Persisted var category: String?

@Persisted var calories: Double?

@Persisted var cautions: List<String>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct RecipesListView: View {
@ObservedResults(Recipe.self) var recipes
@ObservedResults(Purchase.self) var purchases
@State private var isPresentingModal = false
@State private var selectedCategory: String = "general"
@State private var selectedCategory: RecipeCategory = .general

var body: some View {
TabView {
Expand All @@ -34,11 +34,11 @@ struct RecipesListView: View {
.font(.system(size: 20, weight: .semibold))
HStack {
CapsuleButton(title: "General") {
selectedCategory = "general"
selectedCategory = .general
}
if showPremiumButton {
CapsuleButton(title: "Premium") {
selectedCategory = "premium"
selectedCategory = .premium
}
}
}
Expand Down Expand Up @@ -83,13 +83,13 @@ struct RecipesListView: View {
}

var filteredRecipes: Results<Recipe> {
if selectedCategory == "premium" {
if selectedCategory == .premium {
return recipes.where {
($0.cuisineType == "japanese")
($0.category == "free")
}
} else {
return recipes.where {
$0.cuisineType == "american"
$0.category == "purchased"
}
}
}
Expand All @@ -100,3 +100,8 @@ struct RecipesListView: View {
}
}
}

enum RecipeCategory: String {
case general
case premium
}

0 comments on commit 85c4ebd

Please sign in to comment.