Last active
September 15, 2022 00:46
-
-
Save benlmyers/31578478af8db7eb8158bd995da19fa6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FoodViewModel.swift | |
// MVVM-SwiftUI | |
// | |
// Created by Ben Myers on 9/13/22. | |
// | |
import SwiftUI | |
import Foundation | |
/** | |
Encapsulates logic for food. | |
*/ | |
class FoodViewModel: ObservableObject { | |
// MARK: - Wrapped Properties | |
/// All of the foods. | |
@Published var allFoods: [Food] = [ | |
Food(name: "honeycrisp apple", favorite: true), | |
Food(name: "steak tacos", favorite: true), | |
Food(name: "chicken soup", favorite: false) | |
] | |
/// Whether to display the user's favorite foods **only**, or **all of the foods**. | |
@Published var showAll: Bool = false | |
// MARK: - Methods | |
/** | |
Sets the food as a favorite, or not a favorite. | |
- parameter favorite: Whether to favorite the food. | |
- parameter food: The food to change the status of. | |
*/ | |
func toggleFavorite(food: Binding<Food>) { | |
food.wrappedValue.favorite.toggle() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment