Skip to content

Instantly share code, notes, and snippets.

@benlmyers
Last active September 15, 2022 00:46
Show Gist options
  • Save benlmyers/31578478af8db7eb8158bd995da19fa6 to your computer and use it in GitHub Desktop.
Save benlmyers/31578478af8db7eb8158bd995da19fa6 to your computer and use it in GitHub Desktop.
//
// 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