Last active
September 15, 2022 00:48
-
-
Save benlmyers/6488adf45c0970bee9a8ea2fdb7ff705 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
// | |
// FoodView.swift | |
// MVVM-SwiftUI | |
// | |
// Created by Ben Myers on 9/13/22. | |
// | |
import SwiftUI | |
struct FoodView: View { | |
// MARK: - Wrapped Properties | |
/// The view model for `FoodView`. | |
@StateObject var viewModel = FoodViewModel() | |
// MARK: - Body View | |
var body: some View { | |
ScrollView { | |
VStack { | |
Toggle("Show All", isOn: $viewModel.showAll) | |
ForEach($viewModel.allFoods, id: \.wrappedValue.name) { foodItem in | |
if foodItem.wrappedValue.favorite || viewModel.showAll { | |
foodItemView(foodItem) | |
} | |
} | |
} | |
} | |
} | |
// MARK: - Supporting Views | |
/** | |
Displays a food item. | |
- parameter food: The food item to display. | |
*/ | |
func foodItemView(_ food: Binding<Food>) -> some View { | |
HStack { | |
Text(food.wrappedValue.emoji) | |
Text(food.wrappedValue.name.capitalized) | |
Spacer() | |
if food.wrappedValue.favorite { | |
Image(systemName: "star.fill").foregroundColor(.yellow) | |
.onTapGesture { viewModel.toggleFavorite(food: food) } | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment