How ezatpanah/mvvm-architecture-simple-offline-caching-app works
Main Components
• User Interface (UI): Represented by activities and layouts (e.g., MainActivity, activity_main.xml, restaurant_item.xml, and related adapter classes like RestaurantAdapter.kt).
• ViewModel Layer: The mediator between the UI and the data layers (e.g., MainViewModel.kt).
• Repository Layer: Handles data operations, fetching from network and/or local DB (e.g., RestaurantRepository.kt) and uses the NetworkBoundResource pattern.
• Network/API Layer: Contains the ApiServices.kt for Retrofit setup and service definitions.
• Local Database Layer: Composed of Room database components (e.g., RestaurantDatabase.kt and RestaurantDao.kt).
• Dependency Injection Layer: Represented by Hilt modules (see di folder with ApiModule.kt and DatabaseModule.kt) that provide instances of your API service, database, etc.
• Utilities & Helpers: Additional components such as NetworkBoundResource, Constants, DataStatus, etc.
Relationships and Interactions
• The ViewModel, upon user action, calls methods on the Repository.
• The Repository handles data fetching:
• It requests data from the Network/API component through Retrofit.
• Caches or retrieves data from the local Room database.
• Leverages the NetworkBoundResource helper which abstracts the logic for caching.
• The dependency injection framework (Hilt) sits underneath, providing necessary instances to the Repository, ViewModel, and other components.
Architectural Patterns and Design Principles
• MVVM: Clearly separate the UI (Activity, adapter, layout) from the business logic (ViewModel) and data (Repository, API,/Room).
• Dependency Injection: Mark the DI component (Hilt modules) that supply dependencies such as API services and database objects.
• Offline Caching: Emphasize that the Repository uses a NetworkBoundResource pattern to decide whether to fetch fresh data from the API or use cached data.
Mention Relevant Technologies and Libraries
• Retrofit & Kotlin Coroutines for network calls.
• Room Database for local storage.
• Hilt (DI) for dependency injection.
• LiveData/Flow for reactive data streams.
• ViewBinding for UI component binding.
• Glide for image loading within the UI.