# leongiscoding/fyp2 architecture

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

- Interactive diagram: https://gitdiagram.com/leongiscoding/fyp2
- Repository: https://github.com/leongiscoding/fyp2

## Overview

##### Project type and purpose
- This is a cross-platform Flutter application (Android, iOS, Web) for BMI & calorie estimation that uses on-device TensorFlow Lite models and Firebase for authentication and Firestore persistence.

##### File structure
- Top-level “android”, “ios”, and “web” folders represent platform runners.
- The “lib” directory holds your Dart code, organized into pages, UI components, controllers, ML utilities, Firebase services, and theme.
- The “assets” folder contains TFLite model files and label definitions.
- Configuration files (.env, firebase.json, pubspec.yaml) manage environment variables, Firebase setup, and dependencies.

##### README for insights
- While the README is generic, the presence of Firebase config files and TensorFlow Lite assets signals core external dependencies and ML integration.

##### Main components & interactions
a. Client Platforms
- Android Runner
- iOS Runner
- Web Runner
All embed the Flutter Engine and Dart VM.
b. Flutter Application Layers
1. Presentation Layer
- Pages (loginPage.dart, mainPage.dart, profilePage.dart, resultPage.dart, signUpPage.dart)
- Reusable Components (lib/component/…)
2. Business Logic Layer
- Controllers (bmi_calculator.dart, calorie_estimator.dart wrapper)
3. Services Layer
- ML Service (lib/ml/calorie_estimator.dart loads TFLite models from assets)
- Firebase Services (auth_service.dart, firestore_service.dart, profile_firestore_service.dart)
4. Data Layer
- Firestore Database
- On-device TFLite models (assets/model.tflite, assets/model_fruit_v2.tflite)
c. External Services
- Firebase Authentication
- Firebase Firestore

##### Architectural patterns & design principles
- Separation of Concerns: UI (pages & widgets), Logic (controllers), Services (ML & Firebase)
- MVC-like layering: Widgets ← Controllers ← Services ← External Systems
- Cross-platform runner abstraction via Flutter Engine

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Platform Runners"
        AndroidRunner["Android Runner"]:::platform
        iOSRunner["iOS Runner"]:::platform
        WebRunner["Web Runner"]:::platform
    end

    subgraph "Flutter App"
        subgraph "Presentation Layer"
            subgraph "Pages"
                LoginPage["LoginPage Widget"]:::presentation
                MainPage["MainPage Widget"]:::presentation
                ProfilePage["ProfilePage Widget"]:::presentation
                ResultPage["ResultPage Widget"]:::presentation
                SignUpPage["SignUpPage Widget"]:::presentation
            end
            subgraph "Reusable Components"
                LoginComponents["Login/Signup Components"]:::presentation
                MainComponents["Main Page Components"]:::presentation
                ProfileComponents["Profile Page Components"]:::presentation
                ResultComponents["Result Page Components"]:::presentation
            end
        end

        subgraph "Business Logic Layer"
            BMICalculator["BMI Calculator Controller"]:::logic
        end

        subgraph "Services Layer"
            CalorieService["Calorie Estimator Service"]:::service
            AuthService["Auth Service"]:::service
            FirestoreService["Firestore Service"]:::service
            ProfileFirestoreService["Profile Firestore Service"]:::service
            FirebaseOptions["Firebase Config Options"]:::service
        end

        subgraph "Data Layer"
            TFLiteModel["BMI/Calorie Model"]:::data
            FruitModel["Fruit Model"]:::data
            LabelFile["Label Definitions"]:::data
            ConfigFiles["Config Files (firebase.json, .env)"]:::data
        end
    end

    subgraph "External Services"
        FirebaseAuth["Firebase Authentication"]:::external
        FirestoreDB["Firebase Firestore"]:::external
    end

    AndroidRunner -->|"runs on"| LoginPage
    iOSRunner -->|"runs on"| LoginPage
    WebRunner -->|"runs on"| LoginPage

    LoginPage -->|"1.authenticateUser()"| AuthService
    AuthService -->|"2.verify credentials"| FirebaseAuth
    FirebaseAuth -->|"3.auth result"| AuthService
    AuthService -->|"4.navigate"| MainPage

    MainPage -->|"fetchUserProfile()"| ProfileFirestoreService
    ProfileFirestoreService -->|"get user data"| FirestoreDB
    FirestoreDB -->|"profile data"| ProfileFirestoreService
    ProfileFirestoreService -->|"profile data"| MainPage

    MainPage -->|"navigate to Profile"| ProfilePage
    ProfilePage -->|"updateProfile()"| ProfileFirestoreService
    ProfileFirestoreService -->|"save profile"| FirestoreDB

    MainPage -->|"enter food data"| ResultPage
    ResultPage -->|"estimateCalories()"| CalorieService
    CalorieService -->|"load model"| TFLiteModel
    CalorieService -->|"load model"| FruitModel
    CalorieService -->|"load labels"| LabelFile
    CalorieService -->|"inference"| ResultPage
    ResultPage -->|"saveResult()"| FirestoreService
    FirestoreService -->|"save data"| FirestoreDB

    ConfigFiles -->|"config options"| FirebaseOptions
    FirebaseOptions -->|"init SDK"| AuthService
    FirebaseOptions -->|"init SDK"| FirestoreService
    FirebaseOptions -->|"init SDK"| ProfileFirestoreService

    click AndroidRunner "https://github.com/leongiscoding/fyp2/blob/main/android/app/src/main/java/com/example/fyp2/MainActivity.java"
    click iOSRunner "https://github.com/leongiscoding/fyp2/blob/main/ios/Runner/AppDelegate.swift"
    click WebRunner "https://github.com/leongiscoding/fyp2/blob/main/web/index.html"
    click LoginPage "https://github.com/leongiscoding/fyp2/blob/main/lib/page/loginPage.dart"
    click MainPage "https://github.com/leongiscoding/fyp2/blob/main/lib/page/mainPage.dart"
    click ProfilePage "https://github.com/leongiscoding/fyp2/blob/main/lib/page/profilePage.dart"
    click ResultPage "https://github.com/leongiscoding/fyp2/blob/main/lib/page/resultPage.dart"
    click SignUpPage "https://github.com/leongiscoding/fyp2/blob/main/lib/page/signUpPage.dart"
    click LoginComponents "https://github.com/leongiscoding/fyp2/tree/main/lib/component/login_signup/"
    click MainComponents "https://github.com/leongiscoding/fyp2/tree/main/lib/component/main_page/"
    click ProfileComponents "https://github.com/leongiscoding/fyp2/tree/main/lib/component/profile_page/"
    click ResultComponents "https://github.com/leongiscoding/fyp2/tree/main/lib/component/result_page/"
    click BMICalculator "https://github.com/leongiscoding/fyp2/blob/main/lib/controller/bmi_calculator.dart"
    click CalorieService "https://github.com/leongiscoding/fyp2/blob/main/lib/ml/calorie_estimator.dart"
    click AuthService "https://github.com/leongiscoding/fyp2/blob/main/lib/firebase/auth_service.dart"
    click FirestoreService "https://github.com/leongiscoding/fyp2/blob/main/lib/firebase/firestore_service.dart"
    click ProfileFirestoreService "https://github.com/leongiscoding/fyp2/blob/main/lib/firebase/profile_firestore_service.dart"
    click FirebaseOptions "https://github.com/leongiscoding/fyp2/blob/main/lib/firebase_options.dart"
    click TFLiteModel "https://github.com/leongiscoding/fyp2/blob/main/assets/model.tflite"
    click FruitModel "https://github.com/leongiscoding/fyp2/blob/main/assets/model_fruit_v2.tflite"
    click LabelFile "https://github.com/leongiscoding/fyp2/blob/main/assets/labels.txt"
    click ConfigFiles "https://github.com/leongiscoding/fyp2/blob/main/firebase.json"
    click ConfigFiles "https://github.com/leongiscoding/fyp2/blob/main/.env"

    classDef platform fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef presentation fill:#90EE90,stroke:#333,stroke-width:1px
    classDef logic fill:#98FB98,stroke:#333,stroke-width:1px
    classDef service fill:#90EE90,stroke:#333,stroke-width:1px
    classDef data fill:#FFE4B5,stroke:#333,stroke-width:1px
    classDef external fill:#FFD580,stroke:#333,stroke-width:1px
```
