# flourish-savings/flourish-sdk-react-native architecture

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

- Interactive diagram: https://gitdiagram.com/flourish-savings/flourish-sdk-react-native
- Repository: https://github.com/flourish-savings/flourish-sdk-react-native

## Overview

##### Project Type and Purpose
- This project is a React Native SDK that enables mobile apps to integrate Flourish’s functionality. Its primary role is to initialize a webview configured with given partner credentials (partnerId, secret, etc.), facilitate API communication, and handle a set of events (back button actions, game completion events, etc.).
- The README confirms that the SDK encapsulates loading a webview that connects to Flourish’s backend. There is also an “example” directory showcasing how to integrate this SDK into a mobile app.

##### File Structure
- Notice the separation into multiple logical parts:
- The “src” directory contains the core SDK implementation. Inside, separate subdirectories organize tests (__tests__), components, configurations (config), events, and service (API communication).
- The “example” directory provides a sample React Native app demonstrating integration and usage of the SDK.
- Other files (like babel.config.js, tsconfig.json) suggest the use of TypeScript and modern JavaScript tooling.
- Configuration files in “src/config” (for production and staging) and utility files (env.ts) are key parts that define how the SDK behaves in different environments.
- GitHub workflows and CI/CD configuration files indicate that the project is maintained through automated builds and tests.

##### Main Components
a. SDK Initialization Module:
- Located in the entry point (src/index.tsx), this component is responsible for receiving initialization parameters (partnerId, secret, language, environment, customer_code, category, and optional WebView options).
b. Configuration Management:
- The “src/config” folder contains environment-specific configurations (production, staging). Represent this as a separate block showing how environment settings are chosen during initialization.
c. API Service Component:
- Found in “src/service/api.ts” and “src/service/api.types.ts”, this module handles communication with Flourish’s backend (authentication, token retrieval, etc.).
d. Event Management System:
- The “src/events” folder contains files like eventManager.ts and eventStrategy.ts. This subsystem registers callbacks and manages events (like BACK_BUTTON_PRESSED, TRIVIA_GAME_FINISHED, etc.).
e. WebView & UI Components:
- The actual UI components (in “src/components” such as GenericErrorScreen, HomePage, RefreshTokenScreen) and the Flourish component that encapsulates the webview.
f. Example Integration:
- The “example” directory, with its own configuration (babel, metro, webpack), demonstrates the integration of the SDK in a full application. Represent it as an external consumer of the SDK.

##### Defining the Relationships and Data Flow
- Initialization process feeding into the Configuration Management component, which sets the environment (staging or production) accordingly.
- From initialization, the flow proceeds to the API Service component where credentials are used to authenticate with Flourish’s backend.
- Once authenticated, the Flourish component loads a webview that encapsulates the rendered Flourish functionality.
- Parallel to this, the Event Management System hooks into the webview; events fired during user interactions (such as back button presses or game events) are caught and routed back to the client via registered callback functions.

##### Important Architectural Patterns
- Modularity: the logical separation between configuration, API service, event management, and UI components.
- Separation between core SDK functionality (in “src”) and the integration example (in “example”).
- Dependency injection pattern where initialization parameters (credentials, environment, and options) are passed to configure the system.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Nodes
    EI["Example Integration"]:::example
    FB["Flourish Backend"]:::external

    %% Core SDK Nodes
    SI["SDK Initialization"]:::core

    %% Configuration Management Subgraph
    subgraph "Configuration Management"
        CI["Config Index"]:::core
        CP["Production Config"]:::core
        CS["Staging Config"]:::core
    end

    %% API Service Subgraph
    subgraph "API Service"
        AS["API Service"]:::core
        AT["API Types"]:::core
    end

    %% Event Management Subgraph
    subgraph "Event Management"
        EM["Event Manager"]:::core
        ES["Event Strategy"]:::core
    end

    %% UI Components & WebView Subgraph
    subgraph "UI Components & WebView"
        FW["Flourish WebView"]:::core
        HP["HomePage"]:::core
        GE["GenericErrorScreen"]:::core
        RT["RefreshTokenScreen"]:::core
    end

    %% Flow Connections
    EI -->|"initializes"| SI
    SI -->|"loadsConfig"| CI

    CI -->|"selectsProduction"| CP
    CI -->|"selectsStaging"| CS

    SI -->|"usesCredentials"| AS
    AS -->|"includes"| AT
    AS -->|"authenticates"| FB
    FB -->|"returnsToken"| AS

    SI -->|"loadsWebView"| FW

    FW -->|"rendersUI"| HP
    HP -->|"displaysError"| GE
    GE -->|"triggersRefresh"| RT

    FW -->|"emitsEvents"| EM
    EM -->|"implements"| ES
    EM -->|"eventCallback"| EI

    %% Styling Classes
    classDef core fill:#AED6F1,stroke:#5DADE2,stroke-width:2px;
    classDef example fill:#A9DFBF,stroke:#52BE80,stroke-width:2px;
    classDef external fill:#F9E79F,stroke:#F4D03F,stroke-width:2px;

    %% Click Events
    click SI "https://github.com/flourish-savings/flourish-sdk-react-native/blob/main/src/index.tsx"
    click CI "https://github.com/flourish-savings/flourish-sdk-react-native/blob/main/src/config/index.ts"
    click CP "https://github.com/flourish-savings/flourish-sdk-react-native/blob/main/src/config/config.production.ts"
    click CS "https://github.com/flourish-savings/flourish-sdk-react-native/blob/main/src/config/config.staging.ts"
    click AS "https://github.com/flourish-savings/flourish-sdk-react-native/blob/main/src/service/api.ts"
    click AT "https://github.com/flourish-savings/flourish-sdk-react-native/blob/main/src/service/api.types.ts"
    click EM "https://github.com/flourish-savings/flourish-sdk-react-native/blob/main/src/events/eventManager.ts"
    click ES "https://github.com/flourish-savings/flourish-sdk-react-native/blob/main/src/events/eventStrategy.ts"
    click HP "https://github.com/flourish-savings/flourish-sdk-react-native/blob/main/src/components/HomePage.tsx"
    click GE "https://github.com/flourish-savings/flourish-sdk-react-native/blob/main/src/components/GenericErrorScreen.tsx"
    click RT "https://github.com/flourish-savings/flourish-sdk-react-native/blob/main/src/components/RefreshTokenScreen.tsx"
    click EI "https://github.com/flourish-savings/flourish-sdk-react-native/blob/main/example/App.js"
    click EI "https://github.com/flourish-savings/flourish-sdk-react-native/blob/main/example/src/App.tsx"
```
