Skip to content

Commit

Permalink
Small fixes and debug mode (#256)
Browse files Browse the repository at this point in the history
* Keep the old device list when resuming the app.

This stops the devices from leaving when you open and close the notification panel

Stop using `runtimeType` since they are compiled away (at least for web builds)

* Created log page for debugging

* Added debug option for release builds

lighthouse provider: Providers can now be added before back ends

Tapping the version number in settings 10 times enables debug mode.

Enabling debug mode enables:
 - Adding a fake back end
 - Looking at the logs
 - Looking at the material test page
  • Loading branch information
jeroen1602 authored Apr 12, 2024
1 parent a04bd91 commit 8f1eed5
Show file tree
Hide file tree
Showing 52 changed files with 793 additions and 218 deletions.
65 changes: 64 additions & 1 deletion lib/data/dao/settings_dao.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ enum SettingsIds {
groupShowOfflineWarningId(6, "groupShowOfflineWarningId"),
updateIntervalId(7, "updateIntervalId"),
appStyleId(8, "appStyleId"),
deviceProvidersEnabled(9, "deviceProvidersEnabled");
deviceProvidersEnabled(9, "deviceProvidersEnabled"),
debugMode(10, "debugMode"),
useFakeBackEnd(11, "useFakeBackEnd");

final int value;

Expand Down Expand Up @@ -323,6 +325,67 @@ class SettingsDao extends DatabaseAccessor<LighthouseDatabase>
return supportedStyles.cast<AppStyle?>()[0] ?? AppStyle.material;
}

Future<void> setDebugEnabled(final bool enabled) {
return into(simpleSettings).insert(
SimpleSetting(
settingsId: SettingsIds.debugMode.value, data: enabled ? '1' : '0'),
mode: InsertMode.insertOrReplace);
}

Stream<bool> getDebugModeEnabledStream() {
return (select(simpleSettings)
..where((final tbl) =>
tbl.settingsId.equals(SettingsIds.debugMode.value)))
.watchSingleOrNull()
.map((final event) {
if (event == null || event.data == '0' || event.data == null) {
return false;
}
return true;
});
}

Future<void> setUseFakeBackEnd(final bool use) {
return into(simpleSettings).insert(
SimpleSetting(
settingsId: SettingsIds.useFakeBackEnd.value,
data: use ? '1' : '0'),
mode: InsertMode.insertOrReplace);
}

Stream<bool> getUseFakeBackEndStream() {
return (select(simpleSettings)
..where((final tbl) =>
tbl.settingsId.equals(SettingsIds.useFakeBackEnd.value) |
tbl.settingsId.equals(SettingsIds.debugMode.value)))
.watch()
.map((final results) {
if (results.length != 2) {
return false;
}
final debugMode = results.cast<SimpleSetting?>().firstWhere(
(final x) => x?.settingsId == SettingsIds.debugMode.value,
orElse: () => null);

if (debugMode == null ||
debugMode.data == '0' ||
debugMode.data == null) {
return false;
}

final fakeBackEnd = results.cast<SimpleSetting?>().firstWhere(
(final x) => x?.settingsId == SettingsIds.useFakeBackEnd.value,
orElse: () => null);

if (fakeBackEnd == null ||
fakeBackEnd.data == '0' ||
fakeBackEnd.data == null) {
return false;
}
return true;
});
}

Stream<List<SimpleSetting>> get watchSimpleSettings {
debugPrint(
'WARNING using watchSimpleSettings, this should not happen in release mode!');
Expand Down
19 changes: 19 additions & 0 deletions lib/data/local/app_route_settings.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:lighthouse_pm/bloc.dart';
import 'package:rxdart/rxdart.dart';

class AppRouteSettings {
const AppRouteSettings(final bool? debugEnabled)
: debugEnabled = debugEnabled ?? false;

const AppRouteSettings.withDefaults() : this(null);

final bool debugEnabled;

static Stream<AppRouteSettings> getStream(final LighthousePMBloc bloc) {
return MergeStream(
[Stream.value(false), bloc.settings.getDebugModeEnabledStream()])
.map((final enabled) {
return AppRouteSettings(enabled);
});
}
}
2 changes: 2 additions & 0 deletions lib/data/local/theme_data_and_app_style_stream.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class ThemeDataAndAppStyleStream {
style = style ?? AppStyle.material,
alwaysShowScrollbar = alwaysShowScrollbar ?? false;

const ThemeDataAndAppStyleStream.withDefaults() : this(null, null, null);

final ThemeMode themeMode;
final AppStyle style;
final bool alwaysShowScrollbar;
Expand Down
64 changes: 55 additions & 9 deletions lib/lighthouse_provider/lighthouse_provider_start.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
import 'dart:async';

import 'package:bluez_back_end/bluez_back_end.dart';
import 'package:fake_back_end/fake_back_end.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_blue_plus_back_end/flutter_blue_plus_back_end.dart';
import 'package:flutter_web_bluetooth_back_end/flutter_web_bluetooth_back_end.dart';
import 'package:lighthouse_back_end/lighthouse_back_end.dart';
import 'package:lighthouse_logger/lighthouse_logger.dart';
import 'package:lighthouse_pm/bloc.dart';
import 'package:lighthouse_pm/bloc/lighthouse_v2_bloc.dart';
import 'package:lighthouse_pm/bloc/vive_base_station_bloc.dart';
import 'package:lighthouse_pm/data/helper_structures/lighthouse_providers.dart';
import 'package:lighthouse_pm/lighthouse_provider/widgets/vive_base_station_extra_info_alert_widget.dart';
import 'package:lighthouse_pm/platform_specific/mobile/android/android_launcher_shortcut/android_launcher_shortcut.dart';
import 'package:lighthouse_provider/lighthouse_provider.dart';
import 'package:lighthouse_providers/lighthouse_v2_device_provider.dart';
import 'package:lighthouse_providers/vive_base_station_device_provider.dart';
import 'package:logging/logging.dart';
import 'package:rxdart/rxdart.dart';
import 'package:shared_platform/shared_platform.dart';

class LighthouseProviderStart {
LighthouseProviderStart._();

static BehaviorSubject<List<LogRecord>> logs =
BehaviorSubject.seeded(<LogRecord>[]);

static StreamSubscription? loggerSubscription;

static void loadLibrary() {
if (SharedPlatform.isIOS || SharedPlatform.isAndroid) {
LighthouseProvider.instance
Expand All @@ -39,18 +51,9 @@ class LighthouseProviderStart {
debugPrint("Trace: ${record.stackTrace.toString()}");
}
});
// Add this back if you need to test for devices you don't own.
// you'll also need to
// import 'package:lighthouse_pm/lighthouse_back_ends/fake/fake_back_end.dart';

// LighthouseProvider.instance.addBackEnd(FakeBLEBackEnd.instance);
return true;
}());

LighthouseProvider.instance
.addProvider(LighthouseV2DeviceProvider.instance);
LighthouseProvider.instance
.addProvider(ViveBaseStationDeviceProvider.instance);
}

static void setupPersistence(final LighthousePMBloc bloc) {
Expand Down Expand Up @@ -81,4 +84,47 @@ class LighthouseProviderStart {
}
});
}

///
/// Start listening to certain settings to help set everything up.
///
static void startBlocListening(final LighthousePMBloc bloc) {
bloc.settings.getDebugModeEnabledStream().listen((final enabled) {
if (enabled) {
loggerSubscription = lighthouseLogger.onRecord.listen((final record) {
logs.add([...logs.value, record]);
});
} else {
logs.add([]);
final subscription = loggerSubscription;
loggerSubscription = null;
subscription?.cancel();
}
});

bloc.settings.getUseFakeBackEndStream().listen((final useFake) {
if (useFake) {
LighthouseProvider.instance.addBackEnd(FakeBLEBackEnd.instance);
} else {
LighthouseProvider.instance.removeBackEnd(FakeBLEBackEnd.instance);
}
});

bloc.settings.getEnabledDeviceProvidersStream().listen((final providers) {
_addOrRemoveProvider(providers, LighthouseV2DeviceProvider.instance);
_addOrRemoveProvider(providers, ViveBaseStationDeviceProvider.instance);
});
}

static void _addOrRemoveProvider(final List<LighthouseProviders> providers,
final DeviceProvider provider) {
final contains =
providers.indexWhere((final element) => element.provider == provider) >=
0;
if (contains) {
LighthouseProvider.instance.addProvider(provider);
} else {
LighthouseProvider.instance.removeProvider(provider);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class LighthouseMetadataState extends State<LighthouseMetadataPage> {

List<Widget> _generateBody() {
final Map<String, String?> map = {};
map["Device type"] = "${widget.device.runtimeType}";
map["Device type"] = widget.device.deviceType;
map["Name"] = widget.device.name;
map["Firmware version"] = widget.device.firmwareVersion;
map.addAll(widget.device.otherMetadata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class UnknownStateHelpOutAlertWidget extends StatelessWidget {

String _getClipboardString(final String version) {
var output = 'App version: $version\n'
'Device type: ${device.runtimeType}\n'
'Device type: ${device.deviceType}\n'
'Firmware version: ${device.firmwareVersion}\n';
if (currentState != null) {
output +=
Expand Down Expand Up @@ -155,7 +155,7 @@ class UnknownStateHelpOutAlertWidget extends StatelessWidget {
),
TextSpan(
style: theming.bodyTextBold,
text: '${device.runtimeType}\n',
text: '${device.deviceType}\n',
recognizer: recognizer,
),
TextSpan(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class _ViveBaseStationExtraInfoAlertState
}
}
}
if (mounted) {
if (context.mounted) {
Navigator.pop(context, text);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/lighthouse_provider/widgets/widget_for_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Widget getWidgetFromDeviceExtension(
return const Text('ID');
}

return Text(extension.runtimeType.toString());
return Text(extension.extensionName);
}

Color getButtonColorFromDeviceExtension(
Expand Down
42 changes: 32 additions & 10 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:lighthouse_pm/data/database.dart';
import 'package:lighthouse_pm/data/local/app_route_settings.dart';
import 'package:lighthouse_pm/data/local/app_style.dart';
import 'package:lighthouse_pm/data/local/theme_data_and_app_style_stream.dart';
import 'package:lighthouse_pm/lighthouse_provider/lighthouse_provider_start.dart';
import 'package:lighthouse_pm/pages/base_page.dart';
import 'package:lighthouse_pm/pages/database_test_page.dart';
import 'package:lighthouse_pm/pages/help_page.dart';
import 'package:lighthouse_pm/pages/log_page.dart';
import 'package:lighthouse_pm/pages/main_page.dart';
import 'package:lighthouse_pm/pages/not_found_page.dart';
import 'package:lighthouse_pm/pages/settings_page.dart';
Expand All @@ -18,7 +20,9 @@ import 'package:lighthouse_pm/platform_specific/mobile/android/android_launcher_
import 'package:lighthouse_pm/platform_specific/mobile/in_app_purchases.dart';
import 'package:lighthouse_pm/platform_specific/shared/intl.dart';
import 'package:provider/provider.dart';
import 'package:rxdart/rxdart.dart';
import 'package:shared_platform/shared_platform.dart';
import 'package:tuple/tuple.dart';

import 'bloc.dart';
import 'build_options.dart';
Expand Down Expand Up @@ -51,6 +55,7 @@ class MainApp extends StatelessWidget {
LighthouseProviderStart.loadLibrary();
LighthouseProviderStart.setupPersistence(mainBloc);
LighthouseProviderStart.setupCallbacks();
LighthouseProviderStart.startBlocListening(mainBloc);

if (BuildOptions.includeGooglePlayInAppPurchases) {
InAppPurchases.instance
Expand All @@ -70,19 +75,23 @@ class LighthousePMApp extends StatelessWidget with WithBlocStateless {
@override
Widget build(final BuildContext context) {
return DynamicColorBuilder(builder: (final lightColor, final darkColor) {
return StreamBuilder<ThemeDataAndAppStyleStream>(
stream:
ThemeDataAndAppStyleStream.getStream(blocWithoutListen(context)),
initialData: const ThemeDataAndAppStyleStream(null, null, null),
return StreamBuilder<
Tuple2<AppRouteSettings, ThemeDataAndAppStyleStream>>(
stream: _getMainStream(blocWithoutListen(context)),
initialData: const Tuple2(AppRouteSettings.withDefaults(),
ThemeDataAndAppStyleStream.withDefaults()),
builder: (final BuildContext context,
final AsyncSnapshot<ThemeDataAndAppStyleStream>
final AsyncSnapshot<
Tuple2<AppRouteSettings, ThemeDataAndAppStyleStream>>
themeAndStyleSnapshot) {
final data = themeAndStyleSnapshot.data;
final themeMode = data?.themeMode ?? ThemeMode.system;
final useDynamicColors = data?.style == AppStyle.materialDynamic;
final themeMode = data?.item2.themeMode ?? ThemeMode.system;
final useDynamicColors =
data?.item2.style == AppStyle.materialDynamic;
final dynamicColorsLight = lightColor ?? lightColorScheme;
final dynamicColorsDark = darkColor ?? darkColorScheme;
final scrollbarDesktop = data?.alwaysShowScrollbar ?? false;
final scrollbarDesktop = data?.item2.alwaysShowScrollbar ?? false;
final debugModeEnabled = data?.item1.debugEnabled ?? false;

final scrollbarTheme = ScrollbarThemeData(
thumbVisibility: MaterialStateProperty.all(scrollbarDesktop),
Expand Down Expand Up @@ -121,13 +130,17 @@ class LighthousePMApp extends StatelessWidget with WithBlocStateless {

routes.addAll(SettingsPage.getSubPages('/settings'));

if (debugModeEnabled) {
routes['/material'] =
(final context) => const MaterialTestPage();
routes['/log'] = (final context) => const LogPage();
}

if (!kReleaseMode) {
routes.addAll(<String, PageBuilder>{
'/databaseTest': (final context) => DatabaseTestPage()
});
routes.addAll(DatabaseTestPage.getSubPages('/databaseTest'));
routes['/material'] =
(final context) => const MaterialTestPage();
routes['/404'] = (final context) => const NotFoundPage();
}

Expand Down Expand Up @@ -162,3 +175,12 @@ BasePage _createShortcutDebugPage(final BuildContext context) {
}
return const SimpleBasePage(Text('This should not be here.'));
}

Stream<Tuple2<AppRouteSettings, ThemeDataAndAppStyleStream>> _getMainStream(
final LighthousePMBloc bloc) {
return Rx.combineLatest2(
AppRouteSettings.getStream(bloc),
ThemeDataAndAppStyleStream.getStream(bloc),
(final routeSettings, final themeDataAndAppStyle) =>
Tuple2(routeSettings, themeDataAndAppStyle));
}
3 changes: 2 additions & 1 deletion lib/pages/database/settings_dao_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@ class _SimpleSettingConverter extends DaoTableDataConverter<SimpleSetting> {
}
final themeMode = ThemeMode.values[value];
return themeMode.toString();
case SettingsIds.debugMode:
case SettingsIds.shortcutEnabledId:
return convertToBoolean(data);
case SettingsIds.groupShowOfflineWarningId:
case SettingsIds.useFakeBackEnd:
return convertToBoolean(data);
case SettingsIds.updateIntervalId:
if (data == null) {
Expand Down
Loading

0 comments on commit 8f1eed5

Please sign in to comment.