In this module, you'll be building a simple grocery list app from scratch using the Model-View-ViewModel (MVVM) architecture and implementing navigation between views.
- Understand the basics of MVVM architecture
- Implement a simple MVVM structure in SwiftUI
- Add navigation between views
- Create and manage grocery lists using SwiftUI components
- Add items to your grocery list
- Clone the stated repository into your local device.
- Open Xcode and create a new SwiftUI project named
SimpleGroceryListApp
inside the stated repository. - Make sure the project is set up correctly and can be run in the simulator.
Before we start coding, let's understand the basic components of MVVM:
- Model: Represents the grocery item data and the business logic.
- View: Represents the UI.
- ViewModel: Acts as a bridge between the Model and the View, managing the state and logic.
In this app, we'll be creating a simple grocery list app to create and manage a list of grocery items.
- Implementation
- Create a simple model for the grocery items.
- This could include a multitude of aspects for each item:
- Name of the specific grocery item
- The quantity of the grocery item
- The price of the item
- Notes for each grocery item
- The specific category the item belongs on
- Created a basic model for your grocery items.
- Implementation
- Implement a simple ViewModel to manage the list of grocery items.
- This would connect to the model created for the grocery items
- In addition, you have have to ensure that every grocery item created is represented through the model as well
- Created a ViewModel to manage your grocery items.
- Implementation
- Implement the list view to display grocery items using SwiftUI components.
- Using the app screenshot below, recreate the UI for the Grocery application.
- Implement the title for the list, similarly to shown below
- Create an add product button that would connect to the screen mockup in the below section
- Represent each item added from the other view in the below section
- Specifically include the quantity for each item, the category, the title, the price, and the notes
- Mockup
- Displayed grocery items using SwiftUI components.
- Added navigation to create a new grocery list and add items to the list.
- Implementation
- Implement the view to add new items to the grocery list.
- Using the app screenshot below, recreate the UI for the Grocery application.
- Add an option to add the category for the specific item
- Add a textbox to add the items name
- A counter or textbook implementation for the quantity
- The price for the item
- The overall notes to add for the specific item
- Mockup
- Created a view to add new grocery items.
Now, run the app in the simulator or preview mode. You should be able to create a grocery list and add items to the list.
You've just built a simple grocery list app using the MVVM architecture and navigation in SwiftUI!
- Understand the basics of MVVM architecture.
- Implement a simple MVVM structure in SwiftUI.
- Add navigation between views.
- Create and manage grocery lists using SwiftUI components.
- Add items to your grocery list.