Build and maintain a SwiftUI-based iOS ordering app with Firebase backend, MVVM architecture, and real-time cart management
This skill guides AI agents in developing and maintaining a SwiftUI-based iOS ordering platform application with Firebase backend integration, following MVVM architecture patterns and SwiftUI best practices.
The application follows the Model-View-ViewModel (MVVM) architectural pattern using SwiftUI's `@Observable` macro:
The app uses SwiftUI's environment system for shared state:
```swift
// CartViewViewModel is injected as environment object
@Environment(CartViewViewModel.self) var cartViewModel
```
Environment objects are configured in `TestingOwnOrderingPlatformApp.swift` and propagated through the view hierarchy.
Authentication is managed by `MainViewViewModel`:
```
MainView (authentication gate)
├── LoginView (unauthenticated state)
└── TabView (authenticated state)
├── MenuView (Order tab)
├── CartView (Cart tab)
└── ProfileView (Profile tab)
```
**Collections:**
- `name`: String
- `description`: String
- `price`: Double
- `isEntree`: Bool
- `isAppetizer`: Bool
- `isVegetarian`: Bool
**Real-time data fetching:**
```swift
@FirestoreQuery var menuItems: [MenuItem]
```
**Write operations:**
```swift
let db = Firestore.firestore()
// For dictionaries
db.document(path).setData(data)
// For Codable objects
db.document(path).setData(from: object)
```
**User verification:**
```swift
guard let userId = Auth.auth().currentUser?.uid else { return }
```
| Action | Keyboard Shortcut | Menu Path |
|--------|------------------|-----------|
| Build | `Cmd+B` | Product → Build |
| Run | `Cmd+R` | Product → Run |
| Clean | `Cmd+Shift+K` | Product → Clean Build Folder |
| Test | `Cmd+U` | Product → Test |
When adding a new view:
1. Create view file in `/Views/` following `[ViewName]View.swift` convention
2. Create corresponding ViewModel in `/ViewModel/` as `[ViewName]ViewModel.swift`
3. Mark ViewModel class with `@Observable` macro
4. Import required Firebase modules:
```swift
import FirebaseAuth
import FirebaseFirestore
```
5. Inject environment dependencies in view
6. Wire up data bindings between view and ViewModel
Cart operations follow this pattern:
```swift
// 1. Update local state
cartViewModel.addItemsToCart(menuItemId: id, count: quantity)
// 2. Persist to Firestore (handled automatically by CartViewViewModel)
// This calls updateCartItemsToFirebase() internally
```
**Important notes:**
The project uses Swift Package Manager (SPM) for dependency management:
**Problem**: Firebase not initializing properly
**Solution**: Verify `GoogleService-Info.plist` is added to project target and Firebase is configured in `AppDelegate.application(_:didFinishLaunchingWithOptions:)`
**Problem**: Cart state out of sync between local and Firestore
**Solution**: Ensure every cart mutation calls `updateCartItemsToFirebase()`. Use the pattern:
```swift
// Update local state
self.cartItems[itemId] = quantity
// Sync to Firestore
updateCartItemsToFirebase()
```
**Problem**: Environment object not available in view
**Solution**: Verify `CartViewViewModel` is injected in app entry point and accessed via `@Environment(CartViewViewModel.self)` (not `@EnvironmentObject`)
```swift
// 1. Update MenuItem model if needed
struct MenuItem: Identifiable, Codable {
var id: String?
var name: String
var category: String // Add new field
// ...
}
// 2. Update MenuViewModel
@Observable
class MenuViewModel {
var selectedCategory: String = "All"
func filterItems(_ items: [MenuItem]) -> [MenuItem] {
guard selectedCategory != "All" else { return items }
return items.filter { $0.category == selectedCategory }
}
}
// 3. Update MenuView
struct MenuView: View {
@FirestoreQuery var menuItems: [MenuItem]
@State private var viewModel = MenuViewModel()
var body: some View {
VStack {
Picker("Category", selection: $viewModel.selectedCategory) {
Text("All").tag("All")
Text("Entrees").tag("Entrees")
Text("Appetizers").tag("Appetizers")
}
.pickerStyle(.segmented)
List(viewModel.filterItems(menuItems)) { item in
MenuItemRow(item: item)
}
}
}
}
```
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/swiftui-firebase-ordering-platform-hulxa3/raw