# happytechchannel/happy_tech_mastering_api_with_flutter architecture

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

- Interactive diagram: https://gitdiagram.com/happytechchannel/happy_tech_mastering_api_with_flutter
- Repository: https://github.com/happytechchannel/happy_tech_mastering_api_with_flutter

## Overview

##### Project Type and Purpose
- This is a cross-platform mobile (and desktop/web) client built with Flutter.
- It implements a UI layer (screens & widgets), state management (Cubit/BLoC), and a data layer that calls REST endpoints via Dio and caches responses.

##### Top-Level Architecture
- Frontend: Flutter framework code under `lib/` plus platform embedding in `android/`, `ios/`, `linux/`, `macos/`, `windows/`, `web/`.
- Core/Data: API client, interceptors, endpoints, cache helper, and error handling in `lib/core/`.
- State Management: Cubit classes in `lib/cubit/`.
- Domain/Models: Data models in `lib/models/`.
- Repository: Abstraction over data sources in `lib/repositories/`.
- UI: Screens and reusable widgets in `lib/screens/` and `lib/widgets/`.

##### Key Components
a. Presentation Layer
- Flutter Engine & Widgets (platform channels omitted for simplicity)
- UI Screens (`SignInScreen`, `SignUpScreen`, `ProfileScreen`)
- Widgets (`CustomInputField`, `PickImageWidget`, etc.)
b. State Layer
- `UserCubit` / `UserState` handling UI events & state transitions
c. Domain Layer
- `UserRepository` interface and implementation
d. Data Layer
- API Consumer (Dio setup in `dio_consumer.dart`)
- API Interceptors (`api_interceptors.dart`)
- Endpoints definition (`end_points.dart`)
- Cache Helper (`cache_helper.dart`)
e. External Systems
- Remote REST API (hosted server)
- Local Storage (shared preferences or similar via cache helper)

##### Interactions & Data Flow
1. User taps a button in a Flutter Screen →
2. Screen dispatches an event to `UserCubit`. →
3. `UserCubit` calls `UserRepository`. →
4. `UserRepository` chooses between cache or remote:
– Checks `CacheHelper`; if stale or missing, calls API Consumer →
– `DioConsumer` sends HTTP request to external REST API →
– API responds JSON → Interceptors handle auth/refresh tokens →
– JSON parsed into Model (`SignInModel`, `UserModel`) →
5. Repository stores result in `CacheHelper` and returns model to Cubit →
6. `UserCubit` emits new `UserState` →
7. Screen rebuilds with updated data.

##### Architectural Patterns & Principles
- Layered Architecture   (Presentation → State → Domain → Data)
- Repository Pattern      (separates data sources)
- BLoC/Cubit Pattern      (manages state & events)
- Dependency Injection    (pass repository & API consumer into Cubit)
- Separation of Concerns  (UI, business logic, data access, caching, error handling)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Presentation Layer
    subgraph "Presentation Layer"
        direction TB
        S1["SignInScreen"]:::presentation
        S2["SignUpScreen"]:::presentation
        S3["ProfileScreen"]:::presentation
        W1["CustomInputField"]:::presentation
        W2["PickImageWidget"]:::presentation
        W3["AlreadyHaveAnAccountWidget"]:::presentation
        W4["DontHaveAnAccountWidget"]:::presentation
        W5["ForgetPasswordWidget"]:::presentation
        W6["PageHeader"]:::presentation
        W7["PageHeading"]:::presentation
        W8["CustomFormButton"]:::presentation
    end

    %% State Layer
    subgraph "State Layer"
        direction TB
        UC["UserCubit"]:::state
        US["UserState"]:::state
    end

    %% Domain Layer
    subgraph "Domain Layer"
        direction TB
        UR["UserRepository"]:::domain
    end

    %% Data Layer
    subgraph "Data Layer"
        direction TB
        AC["API Consumer"]:::data
        DC["Dio Consumer"]:::data
        AI["API Interceptors"]:::data
        EP["Endpoints"]:::data
        CH["CacheHelper"]:::data
        UI["UploadImageFunction"]:::data
    end

    %% External Systems
    subgraph "External Systems"
        direction TB
        EXTAPI["REST API"]:::external
        LOCAL["Local Storage"]:::external
    end

    %% Connections
    S1 -->|"tap/signIn()"| UC
    S2 -->|"tap/signUp()"| UC
    S3 -->|"tap/getUserProfile()"| UC

    UC -->|"signIn()/getUserProfile()"| UR

    UR -->|"checkCache()"| CH
    CH -->|"cacheHit"| UR
    UR -->|"fetchFromAPI()"| DC
    DC -->|"uses API Consumer"| AC
    AC -->|"sendRequest()"| EXTAPI
    EXTAPI -->|"response JSON"| AC
    AC -->|"intercept()/errorHandling"| AI
    AI -->|"token refresh"| AC
    AC -->|"parseModel"| UR

    UR -->|"saveCache()"| CH
    CH -->|"cachedData"| UR

    UR -->|"UserModel"| UC
    UC -->|"emit new State"| US
    US -->|"update UI"| S1
    US -->|"update UI"| S2
    US -->|"update UI"| S3

    %% Click Events
    click S1 "https://github.com/happytechchannel/happy_tech_mastering_api_with_flutter/blob/main/lib/screens/sign_in_screen.dart"
    click S2 "https://github.com/happytechchannel/happy_tech_mastering_api_with_flutter/blob/main/lib/screens/sign_up_screen.dart"
    click S3 "https://github.com/happytechchannel/happy_tech_mastering_api_with_flutter/blob/main/lib/screens/profile_screen.dart"
    click W1 "https://github.com/happytechchannel/happy_tech_mastering_api_with_flutter/blob/main/lib/widgets/custom_input_field.dart"
    click W2 "https://github.com/happytechchannel/happy_tech_mastering_api_with_flutter/blob/main/lib/widgets/pick_image_widget.dart"
    click W3 "https://github.com/happytechchannel/happy_tech_mastering_api_with_flutter/blob/main/lib/widgets/already_have_an_account_widget.dart"
    click W4 "https://github.com/happytechchannel/happy_tech_mastering_api_with_flutter/blob/main/lib/widgets/dont_have_an_account.dart"
    click W5 "https://github.com/happytechchannel/happy_tech_mastering_api_with_flutter/blob/main/lib/widgets/forget_password_widget.dart"
    click W6 "https://github.com/happytechchannel/happy_tech_mastering_api_with_flutter/blob/main/lib/widgets/page_header.dart"
    click W7 "https://github.com/happytechchannel/happy_tech_mastering_api_with_flutter/blob/main/lib/widgets/page_heading.dart"
    click W8 "https://github.com/happytechchannel/happy_tech_mastering_api_with_flutter/blob/main/lib/widgets/custom_form_button.dart"
    click UC "https://github.com/happytechchannel/happy_tech_mastering_api_with_flutter/blob/main/lib/cubit/user_cubit.dart"
    click US "https://github.com/happytechchannel/happy_tech_mastering_api_with_flutter/blob/main/lib/cubit/user_state.dart"
    click UR "https://github.com/happytechchannel/happy_tech_mastering_api_with_flutter/blob/main/lib/repositories/user_repository.dart"
    click AC "https://github.com/happytechchannel/happy_tech_mastering_api_with_flutter/blob/main/lib/core/api/api_consumer.dart"
    click DC "https://github.com/happytechchannel/happy_tech_mastering_api_with_flutter/blob/main/lib/core/api/dio_consumer.dart"
    click AI "https://github.com/happytechchannel/happy_tech_mastering_api_with_flutter/blob/main/lib/core/api/api_interceptors.dart"
    click EP "https://github.com/happytechchannel/happy_tech_mastering_api_with_flutter/blob/main/lib/core/api/end_ponits.dart"
    click CH "https://github.com/happytechchannel/happy_tech_mastering_api_with_flutter/blob/main/lib/cache/cache_helper.dart"
    click UI "https://github.com/happytechchannel/happy_tech_mastering_api_with_flutter/blob/main/lib/core/functions/upload_image_to_api.dart"

    %% Styles
    classDef presentation fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef state fill:#90EE90,stroke:#000,stroke-width:1px
    classDef domain fill:#FFA500,stroke:#000,stroke-width:1px
    classDef data fill:#D8BFD8,stroke:#000,stroke-width:1px
    classDef external fill:#F5DEB3,stroke:#000,stroke-width:1px
```
