Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions openhealth/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:shared_preferences/shared_preferences.dart';

/// When built with `--dart-define=OG_DEMO=true`, the app runs the in-memory
/// demo driver (see [buildDefaultDriver]) and auto-connects on launch so it
/// lands directly on the populated dashboard — used for simulator/feature
/// verification. Defaults to false; production builds are unaffected.
const bool kOgDemo = bool.fromEnvironment('OG_DEMO', defaultValue: false);

Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();

Expand All @@ -35,9 +41,31 @@ Future<CgmAppController> _bootstrap() async {
driver: buildDefaultDriver(),
);
await controller.initialize();
if (kOgDemo) {
// In demo mode (simulator/feature verification) the demo driver only
// surfaces its sensor after a scan, so auto-scan and auto-connect to land
// directly on the populated dashboard. Strictly gated behind OG_DEMO and
// skipped when a previous session was already restored from preferences.
unawaited(_autoConnectDemoSensor(controller));
}
return controller;
}

/// Scans with the demo driver and connects to the first discovered sensor so
/// OG_DEMO builds open straight onto the populated dashboard.
Future<void> _autoConnectDemoSensor(CgmAppController controller) async {
if (controller.snapshot != null) {
// A persisted session is already (re)connecting; don't interfere.
return;
}
await controller.scan();
final sensors = controller.sensors;
if (sensors.isEmpty) {
return;
}
await controller.connect(sensors.first);
}

class _BootstrapApp extends StatefulWidget {
const _BootstrapApp();

Expand Down
11 changes: 11 additions & 0 deletions openhealth/lib/src/driver_factory_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ import 'package:cgm_aidex/cgm_aidex.dart';
import 'package:cgm_ble_flutter/cgm_ble_flutter.dart';
import 'package:cgm_core/cgm_core.dart';

import 'demo_driver.dart';

/// When built with `--dart-define=OG_DEMO=true`, native/simulator builds use the
/// in-memory [DemoCgmDriver] instead of the real BLE driver, so the app can be
/// exercised in the iOS simulator (which has no Bluetooth). Defaults to false,
/// so production builds are unchanged and keep using the real Aidex driver.
const bool kOgDemo = bool.fromEnvironment('OG_DEMO', defaultValue: false);

CgmDriver buildPlatformDriver() {
if (kOgDemo) {
return DemoCgmDriver();
}
return AidexSensorDriver(const FlutterBluePlusTransport());
}
23 changes: 23 additions & 0 deletions openhealth/test/og_demo_flag_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:cgm_aidex/cgm_aidex.dart';
import 'package:cgm_core/cgm_core.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:openglucose/src/demo_driver.dart';
import 'package:openglucose/src/driver_factory_io.dart';

void main() {
group('OG_DEMO driver selection', () {
test('selects DemoCgmDriver when OG_DEMO is set, real driver otherwise', () {
final CgmDriver driver = buildPlatformDriver();

// `kOgDemo` is a compile-time constant from --dart-define=OG_DEMO.
// Run with `flutter test --dart-define=OG_DEMO=true` to exercise the
// demo branch; without the define, production behavior is asserted.
if (kOgDemo) {
expect(driver, isA<DemoCgmDriver>());
} else {
expect(driver, isA<AidexSensorDriver>());
expect(driver, isNot(isA<DemoCgmDriver>()));
}
});
});
}