# thcainthry/erabook architecture

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

- Interactive diagram: https://gitdiagram.com/thcainthry/erabook
- Repository: https://github.com/thcainthry/erabook

## Overview

##### Main Components
- User Interface (UI) Layer:
- Activities & Fragments: Includes various fragments (home, discover, favorite, login, register, user info, etc.) and activities such as MainActivity and ForgotPasswordActivity.
- Layouts and Resources: Organized under app/src/main/res (layouts, drawables, navigation graphs, etc.).
- ViewModel Layer:
- Contains ViewModels for each functional area (AuthenticationViewModel, HomeViewModel, FavoriteViewModel, etc.). This layer manages UI-related data in a lifecycle-conscious manner.
- Data & Service Layer:
- API Service Integration:
▪ NYTService handles calls to the NYT Best Sellers API.
▪ GoogleApi (inside googlebooks folder) handles search and detail retrieval from Google Books.
▪ OSMService interacts with OpenStreetMap APIs for location-based services.
- Data Models: The data package contains structured models (e.g., VolumeInfo, AccessInfo, Epub, UserDataRemote, etc.) that mirror API responses and local storage representations.
- Dependency Injection (DI) Layer:
- Modules like BooksModule, NYTModule, and OSMModule support dependency injection, ensuring that services and components are easily provided and decoupled.
- Firebase Integration:
- Authentication: Managed through Firestore Auth.
- User Favorites & Profile: Data is stored and retrieved from the Firestore Database.
- External Service Integration:
- Countdown Timer & Notification Utilities: To remind the user (with NotificationUtils).
- Intent-based redirection for book purchases (redirecting users to Barnes and Noble pages).

##### Relationships and Data Flows
- The UI layer (activities/fragments) interacts with the ViewModels. The ViewModels handle business logic and call upon repository/service classes to fetch or update data.
- The service classes (NYTService, GoogleApi, OSMService) interact with their respective external APIs.
- Data models serve as the intermediary between these network calls and the ViewModels, ensuring structured data is passed along.
- Dependency injection modules ensure that services, repositories, and ViewModels are properly instantiated and available where needed.
- For authentication and user-specific data, Firebase serves as the backend – accessed via the relevant services that interact with Firestore and Auth APIs.
- Navigation between various screens is orchestrated using the navigation graph in the res/navigation folder.

##### Architectural Patterns and Design Principles
- MVVM (Model-View-ViewModel): Evident from the structure, where Activities/Fragments (View) communicate with ViewModels, which in turn use models and services to interact with the backend.
- Separation of Concerns: Different responsibilities (UI, data handling, network operations) are clearly separated into distinct packages.
- Dependency Injection: Simplifies testing and enables easier management of component dependencies.
- External API Integration: The separation of services (for NYT, Google Books, and OpenStreetMap) isolates external dependencies, making them replaceable or mockable during testing.

## Diagram (Mermaid)

```mermaid
flowchart TD

    subgraph "UI Layer"
        A_MainActivity["MainActivity"]
        A_ForgotPassword["ForgotPasswordActivity"]
        F_Home["HomeFragment"]
        F_BookDetailsNYT["BookDetailsNYTFragment"]
        F_NYTGenre["NYTGenreFragment"]
        F_Discover["DiscoverFragment"]
        F_DiscoverDetails["DiscoverDetailsFragment"]
        F_Camera["CameraFragment"]
        F_Favorite["FavoriteFragment"]
        F_FavoriteDetails["FavoriteDetailsFragment"]
        F_FocusTime["FocusTimeFragment"]
        F_Location["LocationFragment"]
        F_Login["LoginFragment"]
        F_Register["RegisterFragment"]
        F_UserInfo["UserInfoFragment"]
        F_UserProfile["UserProfileFragment"]
    end

    subgraph "ViewModel Layer"
        VM_Auth["AuthenticationViewModel"]
        VM_UserInfo["UserInfoViewModel"]
        VM_Home["HomeViewModel"]
        VM_Favorite["FavoriteViewModel"]
        VM_Focus["FocusViewModel"]
        VM_SharedGoogleBooks["SharedGoogleBooksViewModel"]
        VM_Location["LocationViewModel"]
    end

    subgraph "Data & Service Layer"
        S_NYT["NYTService"]
        S_Google["GoogleApi"]
        S_OSM["OSMService"]
        S_Models["Data Models"]
    end

    subgraph "Dependency Injection Modules"
        DI_Books["BooksModule"]
        DI_NYT["NYTModule"]
        DI_OSM["OSMModule"]
    end

    subgraph "Firebase Integration"
        Firebase_Backend["Firebase Auth & Firestore"]
    end

    subgraph "Utility & External Services"
        U_Notification["NotificationUtils"]
        U_Extensions["Extensions & Resource"]
        External_NYT_API["NYT_BestSellers_API"]
        External_Google_API["Google_Books_API"]
        External_OSM_API["OpenStreetMap_API"]
    end

    subgraph "Navigation & Layout"
        Nav_Graph["Navigation Graph"]
        Layout_Dir["Layout & Resources"]
    end

    %% UI Layer to ViewModel Layer
    A_MainActivity -->|"UserAction"| VM_Auth
    F_Home -->|"UserInteraction"| VM_Home
    F_Favorite -->|"UserInteraction"| VM_Favorite
    F_FocusTime -->|"TimerAction"| VM_Focus
    F_Discover -->|"SearchAction"| VM_SharedGoogleBooks
    F_Location -->|"LocationRequest"| VM_Location
    F_UserInfo -->|"ProfileView"| VM_UserInfo

    %% ViewModel Layer to Data & Service Layer
    VM_Home -->|"FetchNYTData"| S_NYT
    VM_SharedGoogleBooks -->|"FetchGoogleData"| S_Google
    VM_Location -->|"FetchLocationData"| S_OSM

    %% ViewModel Layer to Firebase Integration
    VM_Auth -->|"AuthOperation"| Firebase_Backend
    VM_UserInfo -->|"UserProfileOp"| Firebase_Backend
    VM_Favorite -->|"FavoriteDataOp"| Firebase_Backend

    %% Data & Service Layer to External APIs
    S_NYT -->|"RequestData"| External_NYT_API
    S_Google -->|"RequestData"| External_Google_API
    S_OSM -->|"RequestData"| External_OSM_API

    %% Dependency Injection Modules connections
    DI_NYT --> S_NYT
    DI_OSM --> S_OSM
    DI_Books --> S_Google
    DI_Books --> VM_SharedGoogleBooks
    DI_NYT --> VM_Home
    DI_OSM --> VM_Location

    %% Utility connections
    VM_Focus -->|"Notify"| U_Notification
    F_Home -->|"UIHelpers"| U_Extensions

    %% Navigation & Layout connections
    A_MainActivity -->|"Navigate"| Nav_Graph
    F_Home -->|"Uses"| Layout_Dir

    %% Styling Classes
    class A_MainActivity,A_ForgotPassword,F_Home,F_BookDetailsNYT,F_NYTGenre,F_Discover,F_DiscoverDetails,F_Camera,F_Favorite,F_FavoriteDetails,F_FocusTime,F_Location,F_Login,F_Register,F_UserInfo,F_UserProfile ui
    class VM_Auth,VM_UserInfo,VM_Home,VM_Favorite,VM_Focus,VM_SharedGoogleBooks,VM_Location viewmodel
    class S_NYT,S_Google,S_OSM,S_Models service
    class DI_Books,DI_NYT,DI_OSM di
    class Firebase_Backend firebase
    class U_Notification,U_Extensions,External_NYT_API,External_Google_API,External_OSM_API utility
    class Nav_Graph,Layout_Dir navigation

    classDef ui fill:#B3E5FC,stroke:#0288D1,stroke-width:2px
    classDef viewmodel fill:#C8E6C9,stroke:#388E3C,stroke-width:2px
    classDef service fill:#FFCCBC,stroke:#D84315,stroke-width:2px
    classDef di fill:#D1C4E9,stroke:#512DA8,stroke-width:2px
    classDef firebase fill:#F8BBD0,stroke:#C2185B,stroke-width:2px
    classDef utility fill:#FFF9C4,stroke:#FBC02D,stroke-width:2px
    classDef navigation fill:#DCEDC8,stroke:#7CB342,stroke-width:2px

    %% Click Events for UI Layer - Activities
    click A_MainActivity "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/activities/MainActivity.kt"
    click A_ForgotPassword "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/firebaseActivities/ForgotPasswordActivity.kt"

    %% Click Events for UI Layer - Fragments
    click F_Home "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/fragments/home/HomeFragment.kt"
    click F_BookDetailsNYT "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/fragments/home/BookDetailsNYTFragment.kt"
    click F_NYTGenre "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/fragments/home/NYTGenreFragment.kt"
    click F_Discover "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/fragments/discoverGoogle/DiscoverFragment.kt"
    click F_DiscoverDetails "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/fragments/discoverGoogle/DiscoverDetailsFragment.kt"
    click F_Camera "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/fragments/discoverGoogle/CameraFragment.kt"
    click F_Favorite "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/fragments/favorite/FavoriteFragment.kt"
    click F_FavoriteDetails "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/fragments/favorite/FavoriteDetailsFragment.kt"
    click F_FocusTime "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/fragments/focustimer/FocusTimeFragment.kt"
    click F_Location "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/fragments/location/LocationFragment.kt"
    click F_Login "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/fragments/login/LoginFragment.kt"
    click F_Register "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/fragments/register/RegisterFragment.kt"
    click F_UserInfo "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/fragments/userInfo/UserInfoFragment.kt"
    click F_UserProfile "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/fragments/userProfile/UserProfileFragment.kt"

    %% Click Events for ViewModel Layer
    click VM_Auth "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/activities/AuthenticationViewModel.kt"
    click VM_UserInfo "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/fragments/userInfo/UserInfoViewModel.kt"
    click VM_Home "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/fragments/home/HomeViewModel.kt"
    click VM_Favorite "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/fragments/favorite/FavoriteViewModel.kt"
    click VM_Focus "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/fragments/focustimer/FocusViewModel.kt"
    click VM_SharedGoogleBooks "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/fragments/discoverGoogle/SharedGoogleBooksViewModel.kt"
    click VM_Location "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/fragments/location/LocationViewModel.kt"

    %% Click Events for Data & Service Layer
    click S_NYT "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/data/nytService/NYTService.kt"
    click S_Google "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/data/googlebooks/GoogleApi.kt"
    click S_OSM "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/data/OSMService.kt"

    %% Click Events for Dependency Injection Modules
    click DI_Books "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/di/BooksModule.kt"
    click DI_NYT "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/di/NYTModule.kt"
    click DI_OSM "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/di/OSMModule.kt"

    %% Click Event for Firebase Integration
    click Firebase_Backend "https://github.com/thcainthry/erabook/blob/master/app/google-services.json"

    %% Click Events for Utility & External Services
    click U_Notification "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/util/NotificationUtils.kt"
    click U_Extensions "https://github.com/thcainthry/erabook/blob/master/app/src/main/java/com/example/erabook/util/Extensions.kt"

    %% Click Events for Navigation & Layout
    click Nav_Graph "https://github.com/thcainthry/erabook/blob/master/app/src/main/res/navigation/main_nav_graph.xml"
    click Layout_Dir "https://github.com/thcainthry/erabook/tree/master/app/src/main/res/layout"
```
