# deepkushmarkup/firebase_chatapp architecture

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

- Interactive diagram: https://gitdiagram.com/deepkushmarkup/firebase_chatapp
- Repository: https://github.com/deepkushmarkup/firebase_chatapp

## Overview

##### Project Type and Purpose
- This is a cross-platform mobile chat application built in Flutter.
- It uses Firebase as a Backend-as-a-Service (BaaS) for authentication, real-time data (Firestore), file storage, and notifications.
- It follows an enhanced clean-architecture style with a clear separation of presentation, domain, and data layers. State management is handled via Flutter Bloc (Cubits).

##### File Structure Analysis
- Root:
- pubspec.yaml, pubspec.lock – dependency definitions
- android/, ios/, linux/, web/ – platform-specific projects for deployment
- assets/ – images, Lottie animations
- lib/ (core Dart code):
- main.dart – application entry point and Firebase initialization
- api/ – low-level wrappers around REST/APIs (notification_access_token.dart, apis.dart)
- helper/ – utilities (dialog helpers, date formatting)
- models/ – domain entities (UserModel, chat_user, message)
- screens/ – UI screens organized by feature (auth, chat, profile, AI screen, group chat)
- widgets/ – reusable UI components (message cards, profile images, dialogs)
- tests/ – unit tests for key screens

##### Key Technologies and Architectural Patterns
- Flutter Bloc (Cubit) for unidirectional data flow and separation of UI from business logic
- Clean architecture layers:
- Presentation (screens, widgets)
- State Management (Cubit classes)
- Domain/Models (data structures and business rules)
- Data (Firebase Firestore, Storage, Auth, network API wrappers)
- Firebase services: Auth, Firestore, Storage, Cloud Messaging (via apis.dart)
- Real-time data via Firestore streams

##### Main Components
a. Presentation Layer (Client UI)
- Screens (Login, Register, ChatScreen, GroupChat, AI Screen, Profile, Splash)
- Widgets (MessageCard, ChatUserCard, AIMessageCard, ProfileImage, Dialogs)

b. State Management Layer
- Cubits/Blocs managing request and response flows between UI and domain/data

c. Domain/Data Layer
- Models (UserModel, ChatUser, Message)
- Repositories/Services (FirebaseAuthService, FirestoreService, StorageService, NotificationService)
- API Wrappers (notification_access_token.dart, apis.dart)

d. External Services (BaaS)
- Firebase Auth
- Firestore Database (Chats, Users collections)
- Firebase Storage (media assets)
- Firebase Cloud Messaging (push notifications)
- (Optional) Third-party AI service if used by ai_screen

e. Deployment Pipeline
- Deployment Script (bash script invoking Flutter, build flavors, Firebase CLI)

##### Relationships and Data Flows
- UI triggers events → Cubit processes event → calls repository/service → interacts with Firebase → returns streams or futures → Cubit emits new state → UI rebuilds.
- Media upload flows: UI → Cubit → StorageService.upload() → returns URL → FirestoreService.createMessage(mediaURL).
- Real-time updates: Firestore stream listeners push new chat documents into Cubit.
- Authentication flow: UI → AuthCubit.login() → FirebaseAuth.signInWithPhoneNumber → onSuccess navigate to HomeScreen.
- Notification flow: NotificationService fetches token via apis.dart, registers with FCM, triggers push notifications.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Initialization"
        AppInit["App Initialization"]:::frontend
    end

    subgraph "Presentation Layer"
        LoginScreen["Login Screen"]:::frontend
        RegisterScreen["Register Screen"]:::frontend
        SplashScreen["Splash Screen"]:::frontend
        HomeScreen["Home / Chat List"]:::frontend
        ChatScreen["One-to-One Chat Screen"]:::frontend
        GroupChatScreen["Group Chat Screen"]:::frontend
        AltGroupChat["Alternative Group Chat"]:::frontend
        AIScreen["AI-Assisted Chat Screen"]:::frontend
        ProfileScreen["User Profile Screen"]:::frontend
        ViewProfile["View Other User Profile"]:::frontend
        MessageCard["MessageCard"]:::frontend
        ChatUserCard["ChatUserCard"]:::frontend
        AIMessageCard["AIMessageCard"]:::frontend
        ProfileImage["ProfileImage"]:::frontend
        ProfileDialog["ProfileDialog"]:::frontend
    end

    subgraph "State Management Layer"
        AuthCubit["AuthCubit"]:::state
        ChatCubit["ChatCubit"]:::state
        GroupChatCubit["GroupChatCubit"]:::state
        AICubit["AICubit"]:::state
        ProfileCubit["ProfileCubit"]:::state
    end

    subgraph "Domain & Models"
        UserModel["UserModel"]:::domain
        ChatUser["ChatUser"]:::domain
        MessageModel["Message Model"]:::domain
    end

    subgraph "Data Layer & Services"
        FirebaseAuthService["FirebaseAuthService"]:::data
        FirestoreService["FirestoreService"]:::data
        StorageService["StorageService"]:::data
        NotificationService["NotificationService"]:::data
        ApisFile["apis.dart"]:::data
        TokenFile["notification_access_token.dart"]:::data
        DialogsUtil["dialogs.dart"]:::data
        DateUtil["my_date_util.dart"]:::data
    end

    subgraph "External Services (Firebase BaaS)"
        Auth["Firebase Auth"]:::external
        FirestoreDB["Firestore Database"]:::external
        Storage["Firebase Storage"]:::external
        FCM["Firebase Cloud Messaging"]:::external
    end

    subgraph "Deployment Pipeline"
        CI["CI/CD Script"]:::deployment
        FlutterCLI["Flutter CLI Builds"]:::deployment
    end

    %% Initialization to Presentation
    AppInit -->|initializes| SplashScreen

    %% UI to Cubits
    SplashScreen -->|navigate| AuthCubit
    LoginScreen -->|triggers| AuthCubit
    RegisterScreen -->|triggers| AuthCubit
    HomeScreen -->|triggers| ChatCubit
    ChatScreen -->|triggers| ChatCubit
    GroupChatScreen -->|triggers| GroupChatCubit
    AltGroupChat -->|triggers| GroupChatCubit
    AIScreen -->|triggers| AICubit
    ProfileScreen -->|triggers| ProfileCubit
    ViewProfile -->|triggers| ProfileCubit

    %% Cubits to Services
    AuthCubit -->|calls| FirebaseAuthService
    ChatCubit -->|uses| FirestoreService
    ChatCubit -->|uploads| StorageService
    GroupChatCubit -->|uses| FirestoreService
    AICubit -->|calls| ApisFile
    AICubit -->|calls| TokenFile
    ProfileCubit -->|uses| FirestoreService

    %% Services to External
    FirebaseAuthService -->|auth requests| Auth
    FirestoreService -->|reads/writes| FirestoreDB
    StorageService -->|stores media| Storage
    NotificationService -->|push/register| FCM
    ApisFile -->|fetch token| FCM
    TokenFile -->|refresh token| FCM

    %% Real-time Streams
    FirestoreDB -.->|streams updates| ChatCubit
    FirestoreDB -.->|streams updates| GroupChatCubit

    %% Helpers
    MessageCard -->|utilizes| DialogsUtil
    ChatUserCard -->|utilizes| DialogsUtil
    AIMessageCard -->|formats| DateUtil
    ProfileDialog -->|utilizes| DialogsUtil

    %% Deployment
    CI -->|runs| FlutterCLI

    %% Click Events
    click AppInit "https://github.com/deepkushmarkup/firebase_chatapp/blob/main/lib/main.dart"
    click AppInit "https://github.com/deepkushmarkup/firebase_chatapp/blob/main/lib/firebase_options.dart"
    click ApisFile "https://github.com/deepkushmarkup/firebase_chatapp/blob/main/lib/api/apis.dart"
    click TokenFile "https://github.com/deepkushmarkup/firebase_chatapp/blob/main/lib/api/notification_access_token.dart"
    click DialogsUtil "https://github.com/deepkushmarkup/firebase_chatapp/blob/main/lib/helper/dialogs.dart"
    click DateUtil "https://github.com/deepkushmarkup/firebase_chatapp/blob/main/lib/helper/my_date_util.dart"
    click UserModel "https://github.com/deepkushmarkup/firebase_chatapp/blob/main/lib/models/UserModel.dart"
    click ChatUser "https://github.com/deepkushmarkup/firebase_chatapp/blob/main/lib/models/chat_user.dart"
    click MessageModel "https://github.com/deepkushmarkup/firebase_chatapp/blob/main/lib/models/message.dart"
    click LoginScreen "https://github.com/deepkushmarkup/firebase_chatapp/blob/main/lib/screens/auth/login_screen.dart"
    click RegisterScreen "https://github.com/deepkushmarkup/firebase_chatapp/blob/main/lib/screens/auth/register_screen.dart"
    click SplashScreen "https://github.com/deepkushmarkup/firebase_chatapp/blob/main/lib/screens/splash_screen.dart"
    click HomeScreen "https://github.com/deepkushmarkup/firebase_chatapp/blob/main/lib/screens/home_screen.dart"
    click ChatScreen "https://github.com/deepkushmarkup/firebase_chatapp/blob/main/lib/screens/chat_screen.dart"
    click GroupChatScreen "https://github.com/deepkushmarkup/firebase_chatapp/blob/main/lib/screens/group_chat.dart"
    click AltGroupChat "https://github.com/deepkushmarkup/firebase_chatapp/blob/main/lib/screens/GroupChat_Screen.dart"
    click AIScreen "https://github.com/deepkushmarkup/firebase_chatapp/blob/main/lib/screens/ai_screen.dart"
    click ProfileScreen "https://github.com/deepkushmarkup/firebase_chatapp/blob/main/lib/screens/profile_screen.dart"
    click ViewProfile "https://github.com/deepkushmarkup/firebase_chatapp/blob/main/lib/screens/view_profile_screen.dart"
    click MessageCard "https://github.com/deepkushmarkup/firebase_chatapp/blob/main/lib/widgets/message_card.dart"
    click ChatUserCard "https://github.com/deepkushmarkup/firebase_chatapp/blob/main/lib/widgets/chat_user_card.dart"
    click AIMessageCard "https://github.com/deepkushmarkup/firebase_chatapp/blob/main/lib/widgets/ai_message_card.dart"
    click ProfileImage "https://github.com/deepkushmarkup/firebase_chatapp/blob/main/lib/widgets/profile_image.dart"
    click ProfileDialog "https://github.com/deepkushmarkup/firebase_chatapp/blob/main/lib/widgets/dialogs/profile_dialog.dart"

    %% Styles
    classDef frontend fill:#ADD8E6,stroke:#000,color:#000;
    classDef state fill:#90EE90,stroke:#000,color:#000;
    classDef domain fill:#FFD580,stroke:#000,color:#000;
    classDef data fill:#FFA500,stroke:#000,color:#000;
    classDef external fill:#DDA0DD,stroke:#000,color:#000;
    classDef deployment fill:#E0E0E0,stroke:#000,color:#000;
```
