# android10/android-cleanarchitecture architecture

> Architecture diagram of the GitHub repository android10/android-cleanarchitecture, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/android10/android-cleanarchitecture
- Repository: https://github.com/android10/android-cleanarchitecture

## Overview

##### Project Type and Purpose
- This is an Android sample app that implements the Clean Architecture pattern.
- Its purpose is to demonstrate how to separate concerns in an Android application, making maintenance, testing, and scalability easier.

##### File Structure to Determine the Main Layers
- There are three main modules or packages:
a. Presentation: Contains Android UI code (Activities, Fragments, Adapters, Views) and dependency injection classes (Dagger modules and components).
b. Domain: Contains business logic, use cases (interactors such as GetUserDetails and GetUserList), domain models (e.g., User), exception handling (error bundles), and thread executor abstractions.
c. Data: Responsible for data handling and persistence. This layer implements caching (FileManager, UserCache), network calls (RestApi), entity mapping, repository implementations (UserDataRepository) and defines data sources (CloudUserDataStore, DiskUserDataStore).

##### Main Components and Their Interactions
- Presentation Layer:
- Android UI components (Activities, Fragments, Adapters and Views) reside here.
- Dependency Injection setup is provided through Dagger (modules and components in internal/di).
- It calls use cases (located in the domain layer) via presenters (UserListPresenter, UserDetailsPresenter).
- Domain Layer:
- Contains use cases (GetUserDetails and GetUserList) which encapsulate business logic.
- Contains domain models (User) and abstract executor interfaces (ThreadExecutor, PostExecutionThread) to control threading.
- Defines a repository interface (UserRepository) which is implemented in the data layer.
- Data Layer:
- Implements the repository pattern (UserDataRepository) to provide data to the domain layer.
- Houses data sources for remote calls (RestApi/ApiConnection) and local persistence (cache implementations, FileManager).
- Uses entity mappers (UserEntityDataMapper, UserEntityJsonMapper) to convert raw data into domain entities.

- For each section, list the key components:
- Presentation: UI components (activities, fragments), DI components (ApplicationComponent, ActivityComponent, etc.), navigators.
- Domain: Use Cases, Domain Models, Exception Contracts, and Execution Threads.
- Data: Repository implementations, Data Sources (Cloud & Disk), Caching mechanisms, Network APIs, and Entity Mappers.
- From Presentation → Domain: The presentation layer (via its presenters) calls use case methods.
- From Domain → Data: The use cases in the domain layer call the repository interface, which is implemented in the data layer.

##### Important Architectural Patterns and Design Principles
- Clean Architecture: Emphasize separation of concerns with domain logic being isolated from the data and presentation layers.
- Dependency Injection: Note the use of Dagger for managing dependencies in the presentation layer.
- Repository Pattern: Show how repositories abstract the source of data (remote vs. local).
- Interaction Layers: Make it clear that the presentation layer never directly accesses data sources but goes through the domain layer's use cases.

##### Incorporate Technology and Framework Details
- Android SDK: Underlying framework for the Presentation layer.
- Gradle: Build system used to manage project dependencies and builds.
- Dagger 2: Dependency Injection framework observed in the presentation internal/di directory.
- Networking or REST: Represented via classes like RestApi and ApiConnection for network operations.
- Caching: Illustrate caching techniques with classes like UserCache, FileManager, and serializers.

##### Final Elements
- Each major component (e.g., Use Cases, Repository, UI components, DI Components) should be distinctly labeled.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        UI["UI Components (Activities, Fragments)"] 
        DI["DI Components (Dagger Modules/Components)"]
        Presenter["Presenters"]
        Navigator["Adapters & Components"]
    end

    %% Domain Layer
    subgraph "Domain Layer"
        UseCases["Use Cases (GetUserDetails, GetUserList)"]
        DomainModel["Domain Model (User)"]
        Executor["Executor Interfaces (ThreadExecutor,PostExecutionThread)"]
        RepoInterface["Repository Interface (UserRepository)"]
    end

    %% Data Layer
    subgraph "Data Layer"
        RepoImpl["Repository Implementation (UserDataRepository)"]
        Caching["Local Caching (FileManager,UserCache,Serializers)"]
        EntityMapper["Entity Mapping (UserEntityDataMapper,UserEntityJsonMapper)"]
        Network["Networking API (RestApi,ApiConnection)"]
        DataSources["Data Sources (CloudUserDataStore,DiskUserDataStore)"]
        DataExecutor["Executor Implementation (JobExecutor)"]
        ExceptionHandling["Exception Handling (NetworkConnectionException,RepositoryErrorBundle,UserNotFoundException)"]
    end

    %% Interactions between layers
    UI -->|"calls"| Presenter
    DI -->|"injects dependencies into"| Presenter
    Navigator -->|"renders alongside"| UI

    Presenter -->|"invokes"| UseCases
    UseCases -->|"calls"| RepoInterface
    RepoInterface -->|"implemented by"| RepoImpl

    RepoImpl -->|"fetches data from"| DataSources
    RepoImpl -->|"maps data via"| EntityMapper
    RepoImpl -->|"communicates over"| Network
    RepoImpl -->|"caches data with"| Caching
    DataSources -->|"reads/writes"| Caching

    RepoImpl -->|"handles errors via"| ExceptionHandling
    UseCases -->|"operates on"| Executor
    RepoImpl -->|"executes on"| DataExecutor

    %% Backflow of Data
    RepoImpl -- "returns results" --> UseCases
    UseCases -- "passes data" --> Presenter
    Presenter -- "updates UI" --> UI

    %% Styling for Layers
    class UI,DI,Presenter,Navigator fill:#f7b05b,stroke:#e05d28,stroke-width:2px;
    class UseCases,DomainModel,Executor,RepoInterface fill:#6baed6,stroke:#3182bd,stroke-width:2px;
    class RepoImpl,Caching,EntityMapper,Network,DataSources,DataExecutor,ExceptionHandling fill:#8cd17d,stroke:#3aab4e,stroke-width:2px;

    %% Click Events for Presentation Layer
    click UI "https://github.com/android10/android-cleanarchitecture/tree/master/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view"
    click UI "https://github.com/android10/android-cleanarchitecture/tree/master/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/activity"
    click UI "https://github.com/android10/android-cleanarchitecture/tree/master/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/fragment"
    click Navigator "https://github.com/android10/android-cleanarchitecture/tree/master/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/adapter"
    click Navigator "https://github.com/android10/android-cleanarchitecture/tree/master/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/view/component"
    click DI "https://github.com/android10/android-cleanarchitecture/tree/master/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/internal/di"
    click Presenter "https://github.com/android10/android-cleanarchitecture/tree/master/presentation/src/main/java/com/fernandocejas/android10/sample/presentation/presenter"

    %% Click Events for Domain Layer
    click UseCases "https://github.com/android10/android-cleanarchitecture/tree/master/domain/src/main/java/com/fernandocejas/android10/sample/domain/interactor"
    click DomainModel "https://github.com/android10/android-cleanarchitecture/tree/master/domain/src/main/java/com/fernandocejas/android10/sample/domain"
    click Executor "https://github.com/android10/android-cleanarchitecture/tree/master/domain/src/main/java/com/fernandocejas/android10/sample/domain/executor"
    click RepoInterface "https://github.com/android10/android-cleanarchitecture/tree/master/domain/src/main/java/com/fernandocejas/android10/sample/domain/repository"

    %% Click Events for Data Layer
    click RepoImpl "https://github.com/android10/android-cleanarchitecture/tree/master/data/src/main/java/com/fernandocejas/android10/sample/data/repository"
    click Caching "https://github.com/android10/android-cleanarchitecture/tree/master/data/src/main/java/com/fernandocejas/android10/sample/data/cache"
    click Caching "https://github.com/android10/android-cleanarchitecture/tree/master/data/src/main/java/com/fernandocejas/android10/sample/data/cache/serializer"
    click EntityMapper "https://github.com/android10/android-cleanarchitecture/tree/master/data/src/main/java/com/fernandocejas/android10/sample/data/entity/mapper"
    click Network "https://github.com/android10/android-cleanarchitecture/tree/master/data/src/main/java/com/fernandocejas/android10/sample/data/net"
    click DataSources "https://github.com/android10/android-cleanarchitecture/tree/master/data/src/main/java/com/fernandocejas/android10/sample/data/repository/datasource"
    click DataExecutor "https://github.com/android10/android-cleanarchitecture/tree/master/data/src/main/java/com/fernandocejas/android10/sample/data/executor"
    click ExceptionHandling "https://github.com/android10/android-cleanarchitecture/tree/master/data/src/main/java/com/fernandocejas/android10/sample/data/exception"
```
