# brianleiser/desafio-android architecture

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

- Interactive diagram: https://gitdiagram.com/brianleiser/desafio-android
- Repository: https://github.com/brianleiser/desafio-android

## Overview

##### Project Type and Purpose
- This is a native Android mobile application (“PicPay - Desafio Android”) whose main function is to fetch and display a list of users from a backend service.
- The README describes an architecture-improvement challenge: maintain UI state on configuration changes, enforce null-safety, add caching, support evolving business/presentation logic, and enable both unit and instrumented testing.

##### File Structure
- Top-level files: Gradle scripts, settings, wrapper—standard Android/Gradle project.
- `app/src/main/java/com/picpay/desafio/android`: houses the UI (`MainActivity.kt`), service interface (`PicPayService.kt`), data model (`User.kt`), and RecyclerView adapter classes.
- Test directories:
- `app/src/test/java/.../ExampleServiceTest.kt` for local unit tests.
- `app/src/androidTest/java/.../MainActivityTest.kt` for instrumented UI tests.
- No explicit layers for domain or data cache present—this is an opportunity to introduce Repository/Use-Case/Local-Data patterns.

##### Key Insights from the README
- Requirements call for:
- Preservation of screen state across rotations (ViewModel + LiveData/StateFlow)
- Null-safety (strict Kotlin types, proper initialization)
- Caching (in-memory and/or persistent—Room or DataStore)
- Separation of concerns to accommodate business/presentation changes
- Fast unit tests for presentation/business/data logic, plus faithful instrumented tests

##### Main Components
a. Presentation Layer
- **MainActivity** (Android Activity)
- **RecyclerView** + **UserListAdapter** & **ViewHolder**
- **ViewModel** (new component to add) exposing LiveData/StateFlow of UI state
b. Domain Layer (optional but recommended)
- **UseCase/Interactor** for “FetchUsers” business logic
c. Data Layer
- **Repository** interface (e.g., `UserRepository`)
- **RemoteDataSource**: `PicPayService` (Retrofit/OkHttp)
- **LocalDataSource**: in-memory/LRU cache or Room database
d. External Services
- **PicPay REST API** (HTTPS backend)
e. Test Modules
- **Unit Tests** exercising ViewModel, UseCase, Repository in isolation
- **Instrumented Tests** driving MainActivity and RecyclerView

##### Relationships and Data Flow
- UI triggers ViewModel events (e.g., “refresh”) → ViewModel invokes UseCase → UseCase calls Repository
- Repository:
- Check LocalDataSource for cached data → if stale/empty, call RemoteDataSource → store results in LocalDataSource → return data
- ViewModel observes Repository results → emits UI state to Activity → Activity renders list via Adapter
- Lifecycle boundaries: ViewModel survives configuration, Activity does not

##### Architectural Patterns and Principles
- MVVM (Model-View-ViewModel)
- Repository pattern with Data Sources
- Single-Source-of-Truth for user data
- Clean separation: Presentation ↔ Domain ↔ Data
- Dependency Injection (e.g., Hilt or Dagger) to wire Retrofit, Repository, ViewModel
- Null-safety enforced via Kotlin types and architecture components

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        direction TB
        MA["MainActivity"]:::presentation
        VM["UserViewModel"]:::presentation
        APT["RecyclerView Adapter\nUserListAdapter"]:::presentation
        DCB["RecyclerView DiffCallback\nUserListDiffCallback"]:::presentation
        VHB["RecyclerView ViewHolder\nUserListItemViewHolder"]:::presentation
        LAY1["Layout: activity_main.xml"]:::presentation
        LAY2["Layout: list_item_user.xml"]:::presentation
    end

    %% Domain Layer
    subgraph "Domain Layer"
        direction TB
        UC["FetchUsersUseCase"]:::domain
        UM["User Model\nUser.kt"]:::domain
    end

    %% Data Layer
    subgraph "Data Layer"
        direction TB
        UR["UserRepository"]:::data
        subgraph "LocalDataSource"
            direction TB
            DB["Room Database\n(cylinder)"]:::data
        end
        subgraph "RemoteDataSource"
            direction TB
            PS["PicPayService\n(Retrofit)"]:::data
        end
    end

    %% External Services
    PSRV["PicPay REST API"]:::external

    %% Test Modules
    subgraph "Test Modules"
        direction TB
        UT["Unit Tests\nExampleServiceTest.kt"]:::test
        IT["Instrumented Tests\nMainActivityTest.kt"]:::test
    end

    %% Connections
    MA -->|renders via| APT
    APT -->|uses| DCB
    APT -->|uses| VHB
    MA -->|binds to| VM
    VM -.->|calls| UC
    UC -->|invokes| UR
    UR -->|reads/writes| DB
    UR -->|fetches| PS
    PS -->|HTTP| PSRV
    DB -- data --> UR
    PSRV -- JSON --> PS
    VM -.->|"observes LiveData/Flow"| MA

    %% Tests
    UT -->|tests| PS
    UT -->|tests| UC
    UT -->|tests| UR
    IT -->|tests UI| MA

    %% Click Events
    click MA "https://github.com/brianleiser/desafio-android/blob/master/app/src/main/java/com/picpay/desafio/android/MainActivity.kt"
    click APT "https://github.com/brianleiser/desafio-android/blob/master/app/src/main/java/com/picpay/desafio/android/UserListAdapter.kt"
    click DCB "https://github.com/brianleiser/desafio-android/blob/master/app/src/main/java/com/picpay/desafio/android/UserListDiffCallback.kt"
    click VHB "https://github.com/brianleiser/desafio-android/blob/master/app/src/main/java/com/picpay/desafio/android/UserListItemViewHolder.kt"
    click UM "https://github.com/brianleiser/desafio-android/blob/master/app/src/main/java/com/picpay/desafio/android/User.kt"
    click PS "https://github.com/brianleiser/desafio-android/blob/master/app/src/main/java/com/picpay/desafio/android/PicPayService.kt"
    click LAY1 "https://github.com/brianleiser/desafio-android/blob/master/app/src/main/res/layout/activity_main.xml"
    click LAY2 "https://github.com/brianleiser/desafio-android/blob/master/app/src/main/res/layout/list_item_user.xml"
    click UT "https://github.com/brianleiser/desafio-android/blob/master/app/src/test/java/com/picpay/desafio/android/ExampleServiceTest.kt"
    click IT "https://github.com/brianleiser/desafio-android/blob/master/app/src/androidTest/java/com/picpay/desafio/android/MainActivityTest.kt"

    %% Styles
    classDef presentation fill:#E3F2FD,stroke:#90CAF9,stroke-width:2px
    classDef domain fill:#E8F5E9,stroke:#66BB6A,stroke-width:2px
    classDef data fill:#FFF3E0,stroke:#FFA726,stroke-width:2px
    classDef external fill:#F5F5F5,stroke:#BDBDBD,stroke-width:2px
    classDef test fill:#F3E5F5,stroke:#AB47BC,stroke-width:2px
```
