The app follows the Model-View-ViewModel (MVVM) architecture pattern to separate UI logic from data management.
- User Action: The user enters an amount, category, date, and note in the
AddExpenseFragmentand clicks "Save". - Fragment: The
AddExpenseFragmentcollects the input data and callsmainViewModel.insertExpense(expense). - ViewModel: The
MainViewModel(which isviewModelScopeaware) launches a coroutine and callsrepository.insertExpense(expense). - Repository: The
ExpenseRepositoryacts as a clean API for data access. It callsexpenseDao.insertExpense(expense). - DAO (Data Access Object): The
ExpenseDaoexecutes the actual SQLINSERTquery into the Room Database. - Database: The
AppDatabase(Room) stores the data in the SQLite file on the device.
- Observation: In
HomeFragment, we observeviewModel.allExpenses(aLiveData<List<Expense>>). - ViewModel:
MainViewModelexposesallExpenseswhich comes directly fromrepository.allExpenses. - Repository:
ExpenseRepositorygets this LiveData fromexpenseDao.getAllExpenses(). - DAO & Room: Room automatically triggers the LiveData update whenever the underlying database table changes (e.g., after the insert above).
- UI Update: When the LiveData emits a new list, the Observer in
HomeFragmentreceives it and callsadapter.submitList(expenses). - RecyclerView: The
ExpenseAdaptercalculates the diff and efficiently updates the list on the screen.
- View (UI):
Activity,Fragments,XML Layouts. Displays data and captures user input. - ViewModel:
MainViewModel. Holds UI state and business logic. Survives configuration changes. - Model (Data):
- Repository:
ExpenseRepository. Mediator between ViewModel and Data Sources. - Room Database:
AppDatabase,ExpenseDao,CategoryDao,Entities. The single source of truth.
- Repository: