Hey all
I am currently working on the recipes app but wanted to add some additional features. One feature that im adding (I didn’t finish the course, though I didn’t see it as a topic) is a favorites section. I have the ability to add favorites to another view, but the changes won’t stay when the app is terminated.
I tried AppStorage but I guess I am implementing it incorrectly.
Recipe Model:
class Recipe: Identifiable, Decodable {
var id:UUID?
var name:String
var featured:Bool
var isFavorite: Bool
var image:String
var imageURL: URL?
var description:String
var tag: String
var tag1: String
var prepTime:String
var cookTime:String
var totalTime:String
var servings:Int
var highlights:[String]
var calories: String?
var satFat: String?
var sugar: String?
var protein: String?
var sodium: String?
var fat: String?
var carbs: String?
var dietaryFiber: String?
var cholesterol: String?
var ingredients:[Ingredient]
var directions:[String]
}
ButtonView:
import SwiftUI
struct FavoriteButton: View {
@Binding var isSet: Bool
var body: some View {
Button {
isSet.toggle()
} label: {
Image(systemName: isSet ? "heart.fill" : "heart")
.font(.title2)
.labelStyle(.iconOnly)
.foregroundColor(isSet ? .red : .pink )
.cornerRadius(10)
.animation(.easeInOut(duration: 0.5))
}
}
}
FavoritesView:
import SwiftUI
struct FavoriteListView: View {
@EnvironmentObject var model:RecipeModel
@State private var showFavoritesOnly = true
@AppStorage("isFavorite") var isFavorite = false
var filteredRecipe: [Recipe] {
model.recipes.filter { favorites in
(!showFavoritesOnly || favorites.isFavorite)
}
}
var body: some View {
NavigationView {
ScrollView {
LazyVStack(alignment: .leading) {
Toggle(isOn: $isFavorite) {
Text("Favorites only")
}.opacity(0.0)
ForEach(filteredRecipe) { items in
NavigationLink(destination: {
RecipeDetailView(recipe: items, isSet: showFavoritesOnly)
}, label: {
Image(items.image)
.resizable()
.scaledToFit()
.frame(width: 75, height: 50)
.cornerRadius(10)
VStack(alignment: .leading) {
HStack {
Text(items.name)
.font(.caption)
.foregroundColor(.black)
.lineLimit(1)
Spacer()
Image(systemName: "chevron.right")
.padding()
}
}
})
}.listStyle(.grouped)
}
.padding()
}.navigationTitle("Favorite Recipes")
I currently have a default of 4 recipes marked as favorites in the JSON file. When I mark other recipes as favorites, or even mark it as unfavorite, it works. But when I terminate and reopen the app, it goes back to the default. Sorry if this is a silly question but not entirely sure if I am using AppStorage properly here or even saving the wrong values.
I appreciate any help and absolutely love this community.