# ezatpanah/mvvm-architecture-simple-offline-caching-app architecture

> Architecture diagram of the GitHub repository ezatpanah/mvvm-architecture-simple-offline-caching-app, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ezatpanah/mvvm-architecture-simple-offline-caching-app
- Repository: https://github.com/ezatpanah/mvvm-architecture-simple-offline-caching-app

## Overview

##### 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.

## Diagram (Mermaid)

```mermaid
graph TD
    USER["User/MobileDevice"]:::user

    subgraph "UI Layer"
        UI1["MainActivity"]:::ui
        UI2["RestaurantAdapter"]:::ui
        UI3["activity_main.xml"]:::layout
        UI4["restaurant_item.xml"]:::layout
    end

    subgraph "ViewModel Layer"
        VM["MainViewModel"]:::view
    end

    subgraph "Repository Layer"
        REP["RestaurantRepository"]:::repo
        NBR["NetworkBoundResource"]:::util
    end

    subgraph "Network/API Layer"
        API["ApiServices"]:::network
    end

    subgraph "Local Database Layer"
        DB["RestaurantDatabase"]:::db
        DAO["RestaurantDao"]:::db
    end

    subgraph "Dependency Injection"
        DI1["ApiModule"]:::di
        DI2["DatabaseModule"]:::di
    end

    subgraph "Utilities & Helpers"
        CONST["Constants"]:::util
        DS["DataStatus"]:::util
    end

    USER -->|"initiates"| UI1
    UI1 -->|"uses"| UI2
    UI1 -->|"binds"| UI3
    UI1 -->|"binds"| UI4
    UI1 -->|"triggers"| VM
    VM -->|"calls"| REP
    REP -->|"uses"| NBR
    REP -->|"fetches"| API
    REP -->|"caches"| DB
    DB -->|"queries"| DAO
    REP -->|"returns-data"| VM
    VM -->|"updates"| UI1

    DI1 -->|"provides"| API
    DI2 -->|"provides"| DB
    DI1 -->|"injects"| REP
    DI1 -->|"injects"| VM

    click UI1 "https://github.com/ezatpanah/mvvm-architecture-simple-offline-caching-app/blob/master/app/src/main/java/com/ezatpanah/offlinecaching/ui/MainActivity.kt"
    click UI2 "https://github.com/ezatpanah/mvvm-architecture-simple-offline-caching-app/blob/master/app/src/main/java/com/ezatpanah/offlinecaching/adapter/RestaurantAdapter.kt"
    click UI3 "https://github.com/ezatpanah/mvvm-architecture-simple-offline-caching-app/blob/master/app/src/main/res/layout/activity_main.xml"
    click UI4 "https://github.com/ezatpanah/mvvm-architecture-simple-offline-caching-app/blob/master/app/src/main/res/layout/restaurant_item.xml"
    click VM "https://github.com/ezatpanah/mvvm-architecture-simple-offline-caching-app/blob/master/app/src/main/java/com/ezatpanah/offlinecaching/viewmodel/MainViewModel.kt"
    click REP "https://github.com/ezatpanah/mvvm-architecture-simple-offline-caching-app/blob/master/app/src/main/java/com/ezatpanah/offlinecaching/repository/RestaurantRepository.kt"
    click NBR "https://github.com/ezatpanah/mvvm-architecture-simple-offline-caching-app/blob/master/app/src/main/java/com/ezatpanah/offlinecaching/utils/NetworkBoundResource.kt"
    click API "https://github.com/ezatpanah/mvvm-architecture-simple-offline-caching-app/blob/master/app/src/main/java/com/ezatpanah/offlinecaching/api/ApiServices.kt"
    click DB "https://github.com/ezatpanah/mvvm-architecture-simple-offline-caching-app/blob/master/app/src/main/java/com/ezatpanah/offlinecaching/db/RestaurantDatabase.kt"
    click DAO "https://github.com/ezatpanah/mvvm-architecture-simple-offline-caching-app/blob/master/app/src/main/java/com/ezatpanah/offlinecaching/db/RestaurantDao.kt"
    click DI1 "https://github.com/ezatpanah/mvvm-architecture-simple-offline-caching-app/blob/master/app/src/main/java/com/ezatpanah/offlinecaching/di/ApiModule.kt"
    click DI2 "https://github.com/ezatpanah/mvvm-architecture-simple-offline-caching-app/blob/master/app/src/main/java/com/ezatpanah/offlinecaching/di/DatabaseModule.kt"
    click CONST "https://github.com/ezatpanah/mvvm-architecture-simple-offline-caching-app/blob/master/app/src/main/java/com/ezatpanah/offlinecaching/utils/Constants.kt"
    click DS "https://github.com/ezatpanah/mvvm-architecture-simple-offline-caching-app/blob/master/app/src/main/java/com/ezatpanah/offlinecaching/utils/DataStatus.kt"

    classDef user fill:#FF9999,stroke:#333,stroke-width:2px;
    classDef ui fill:#8FB9A8,stroke:#333,stroke-width:2px;
    classDef layout fill:#FCD9B8,stroke:#333,stroke-width:2px;
    classDef view fill:#F4B350,stroke:#333,stroke-width:2px;
    classDef repo fill:#F7AE81,stroke:#333,stroke-width:2px;
    classDef network fill:#B8D9F9,stroke:#333,stroke-width:2px;
    classDef db fill:#F9D9B8,stroke:#333,stroke-width:2px;
    classDef di fill:#D9C1F9,stroke:#333,stroke-width:2px;
    classDef util fill:#D0E6A5,stroke:#333,stroke-width:2px;
```
