Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jacobsapps/e04666c4a55ff782e4161ea1ee07a5e4 to your computer and use it in GitHub Desktop.
Save jacobsapps/e04666c4a55ff782e4161ea1ee07a5e4 to your computer and use it in GitHub Desktop.
final class CardCollectionViewModel: ObservableObject {
@Published var cards: [Card] = []
func addCard(_ card: Card) {
cards.insert(card, at: 0)
}
}
struct CardGridView: View {
@StateObject private var viewModel = CardCollectionViewModel()
@State private var selectedCard: Card? = nil
@State private var showGeneration: Bool = false
var body: some View {
NavigationStack {
ScrollView {
LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 3), spacing: 12) {
ForEach(viewModel.cards) { card in
CardView(card: card)
.onTapGesture {
selectedCard = card
}
}
}
.padding()
}
.sheet(isPresented: $showGeneration) {
GenerationView { newCard in
viewModel.addCard(newCard)
showGeneration = false
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment