# nikolaydymura/flutter_health_connect architecture

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

- Interactive diagram: https://gitdiagram.com/nikolaydymura/flutter_health_connect
- Repository: https://github.com/nikolaydymura/flutter_health_connect

## Overview

##### Main components
- Flutter Plugin Interface (Dart Code): This is found in the “lib” directory. It contains the public API (flutter_health_connect.dart) and supporting modules such as data types, records, and unit conversion classes. These components abstract the Health Connect operations (reading/writing health records, checking permissions, etc.) for the Flutter app.
- Android Native Module: Located in the “android” directory (especially under src/main/kotlin/dev/duynp/flutter_health_connect), it contains platform-specific plugin implementation (e.g., FlutterHealthConnectPlugin.kt, Consts.kt). This layer handles the communication between Flutter and the native Android Health Connect API.
- iOS Native Module: Found in the “ios” directory (Classes folder and the podspec), it implements the native side for iOS, ensuring that the plugin can work across platforms although the primary focus is on Android.
- Example Application: The “example” directory holds a sample Flutter app that demonstrates use cases of the plugin, including UI examples and sample code usage in main.dart. This shows how the plugin is integrated within a working Flutter project.
- External Health Connect Service: Although not part of the codebase, the integration with Google Health Connect on Android is a key external dependency. This external system is accessed via the native layer.

##### Relationships and interactions
- The Flutter application (or any consumer app) invokes functions from the Dart plugin API in the “lib” folder.
- The Dart API communicates with the platform-specific native code through Flutter’s platform channels.
- On Android, the native plugin calls Health Connect APIs (after ensuring permissions are declared and requested).
- iOS native code (if used) similarly interacts with any corresponding Apple health providers.
- The example app, contained in the “example” directory, demonstrates these interactions end-to-end.

##### Architectural patterns and design principles
- Plugin Architecture: The project follows a typical Flutter plugin design, with a clear separation between the Dart interface (cross-platform) and native implementations (platform-specific).
- Abstraction layers: The “lib” folder abstracts out health record types, unit conversions, and data model definitions. This keeps the business logic separated from native integration details.
- Modular Design: Each health record (e.g., StepsRecord, ExerciseSessionRecord) is modeled in isolated files, enhancing maintainability and extensibility.
- Configuration and Dependency Management: The inclusion of Gradle files in the Android folder and podspec/iOS Xcode project files encapsulates platform-specific build and dependency configurations.

##### Relevant technologies and frameworks
- Flutter & Dart – For the cross-platform API and application logic.
- Android (Kotlin) – For the native integration with Health Connect.
- iOS (Objective-C/Swift) – For the native integration on the iOS side.
- Gradle – For building the Android plugin.
- Xcode & CocoaPods – For building and integrating the iOS module.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Consumer Layer
    ConsumerApp["Flutter App (Consumer)"]:::consumer
    ExampleApp["Example Application"]:::consumer

    %% Plugin API (Dart Layer)
    subgraph "Plugin API (Dart Layer)"
        DartMain["flutter_health_connect.dart"]:::flutter
        DartSrc["Support Modules (lib/src)"]:::flutter
    end

    %% Native Implementations
    subgraph "Native Implementations"
        subgraph "Android Plugin Implementation (Kotlin)"
            Android_KotlinNode["FlutterHealthConnectPlugin.kt"]:::android
            Android_Consts["Consts.kt"]:::android
        end
        subgraph "iOS Plugin Implementation (Swift/ObjC)"
            iOS_Header["FlutterHealthConnectPlugin.h"]:::ios
            iOS_M["FlutterHealthConnectPlugin.m"]:::ios
            iOS_Swift["SwiftFlutterHealthConnectPlugin.swift"]:::ios
            iOS_Podspec["flutter_health_connect.podspec"]:::ios
        end
    end

    %% External Service
    HealthConnectService["Google Health Connect Service"]:::external

    %% Connections
    ConsumerApp -->|"calls"| DartMain
    ExampleApp -->|"demonstrates"| DartMain
    DartMain -->|"platformChannel"| Android_KotlinNode
    DartMain -->|"platformChannel"| iOS_Header
    Android_KotlinNode -->|"API_Call"| HealthConnectService

    %% Click Events for Flutter Plugin Interface
    click DartMain "https://github.com/nikolaydymura/flutter_health_connect/blob/master/lib/flutter_health_connect.dart"
    click DartSrc "https://github.com/nikolaydymura/flutter_health_connect/tree/master/lib/src"

    %% Click Events for Android Native Module
    click Android_KotlinNode "https://github.com/nikolaydymura/flutter_health_connect/blob/master/android/src/main/kotlin/dev/duynp/flutter_health_connect/FlutterHealthConnectPlugin.kt"
    click Android_Consts "https://github.com/nikolaydymura/flutter_health_connect/blob/master/android/src/main/kotlin/dev/duynp/flutter_health_connect/Consts.kt"

    %% Click Events for iOS Native Module
    click iOS_Header "https://github.com/nikolaydymura/flutter_health_connect/blob/master/ios/Classes/FlutterHealthConnectPlugin.h"
    click iOS_M "https://github.com/nikolaydymura/flutter_health_connect/blob/master/ios/Classes/FlutterHealthConnectPlugin.m"
    click iOS_Swift "https://github.com/nikolaydymura/flutter_health_connect/blob/master/ios/Classes/SwiftFlutterHealthConnectPlugin.swift"
    click iOS_Podspec "https://github.com/nikolaydymura/flutter_health_connect/blob/master/ios/flutter_health_connect.podspec"

    %% Click Event for Example Application
    click ExampleApp "https://github.com/nikolaydymura/flutter_health_connect/blob/master/example/lib/main.dart"

    %% Styles
    classDef flutter fill:#A3D5FF,stroke:#333,stroke-width:2px;
    classDef android fill:#B5EAD7,stroke:#333,stroke-width:2px;
    classDef ios fill:#FFB7B2,stroke:#333,stroke-width:2px;
    classDef external fill:#FFDFBA,stroke:#333,stroke-width:2px;
    classDef consumer fill:#C9C9FF,stroke:#333,stroke-width:2px;
```
