diff --git a/CHANGELOG_UNRELEASED.md b/CHANGELOG_UNRELEASED.md index cee465e..e248cd2 100644 --- a/CHANGELOG_UNRELEASED.md +++ b/CHANGELOG_UNRELEASED.md @@ -8,4 +8,8 @@ actions such as selecting a POI or starting navigation from outside the map screen (e.g., from a list or a notification). Since the MapViewController is only available once the MapView has fully loaded, this helper provides a simple, awaitable ensureMapViewController() method—powered by a - Dart Completer—that resolves automatically when the controller becomes ready. \ No newline at end of file + Dart Completer—that resolves automatically when the controller becomes ready. + +### Added + +- Added new message to let map-viewer know what's the current device preferred color scheme. \ No newline at end of file diff --git a/lib/src/controller.dart b/lib/src/controller.dart index 545d5ac..f9af034 100644 --- a/lib/src/controller.dart +++ b/lib/src/controller.dart @@ -114,6 +114,15 @@ class MapViewController { _webViewController.reload(); } + /// Sets the viewer `preferredTheme` according to the current platform brightness. + /// + /// This does not override a theme already selected by the user inside the + /// webview; it only provides the preferred theme when the viewer applies system + /// preferences. + void _setPreferredTheme(Brightness brightness) { + _sendPreferredTheme(brightness); + } + /// Selects the given Building in the map. /// To set the initial Building use [MapViewConfiguration]. void selectBuilding(String identifier) async { @@ -272,7 +281,8 @@ class MapViewController { /// Select a floor of the current building by its [Floor.identifier]. /// /// **NOTE**: introducing an invalid identifier may result in unexpected behaviours. - void selectFloor(String identifier, {SelectCartographyOptions? options}) async { + void selectFloor(String identifier, + {SelectCartographyOptions? options}) async { int floorId = int.tryParse(identifier) ?? 0; final message = { "identifier": floorId, @@ -347,6 +357,17 @@ class MapViewController { _sendMessage(WV_APP_CONFIG, jsonEncode(configItems)); } + void _sendPreferredTheme(Brightness brightness) { + dynamic message = { + "theme": brightness == Brightness.dark ? 'dark' : 'light' + }; + + _sendMessage( + WV_MESSAGE_UI_SET_PREFERRED_THEME, + jsonEncode(message), + ); + } + void _setRoute( DirectionsMessage directionsMessage, SitumRoute situmRoute) async { situmRoute.rawContent["identifier"] = directionsMessage.identifier; diff --git a/lib/src/situm_map_view.dart b/lib/src/situm_map_view.dart index 3f804db..c8f43c5 100644 --- a/lib/src/situm_map_view.dart +++ b/lib/src/situm_map_view.dart @@ -31,7 +31,7 @@ class MapView extends StatefulWidget { State createState() => _MapViewState(); } -class _MapViewState extends State { +class _MapViewState extends State with WidgetsBindingObserver { static MapViewController? wyfController; static PlatformWebViewController? webViewController; static PlatformWebViewWidget? webViewWidget; @@ -44,6 +44,7 @@ class _MapViewState extends State { @override void initState() { super.initState(); + WidgetsBinding.instance.addObserver(this); mapViewConfiguration = widget.configuration; // Avoid re-initializations of the underlying WebView (PlatformView) if @@ -176,6 +177,12 @@ class _MapViewState extends State { ?.loadRequest(LoadRequestParams(uri: Uri.parse(mapViewUrl))); } + void _syncPlatformPreferredTheme() { + wyfController?._setPreferredTheme( + WidgetsBinding.instance.platformDispatcher.platformBrightness, + ); + } + void _displayBlankScreen(bool value) { if (mounted) { setState(() { @@ -221,9 +228,14 @@ class _MapViewState extends State { } } + @override + void didChangePlatformBrightness() { + _syncPlatformPreferredTheme(); + } + @override void dispose() { - super.dispose(); + WidgetsBinding.instance.removeObserver(this); // IMPORTANT: We use a static reference to support the // "persistUnderlyingWidget" feature. Because of that, this state must be // explicitly unregistered when disposed; otherwise it may continue receiving @@ -231,6 +243,7 @@ class _MapViewState extends State { // unwanted rebuilds in an inconsistent state. _InternalMessageBridge.unregister(); // wyfController?.onWidgetDisposed(); + super.dispose(); } void _onErrorRetryLoad() { @@ -255,6 +268,7 @@ class _MapViewState extends State { _shouldDisplayMainFrameError = false; } }); + _syncPlatformPreferredTheme(); } } } diff --git a/lib/wayfinding.dart b/lib/wayfinding.dart index 9ec191b..bb8d829 100644 --- a/lib/wayfinding.dart +++ b/lib/wayfinding.dart @@ -60,6 +60,7 @@ const WV_MESSAGE_CALIBRATION_STOPPED = "calibration.stopped"; // Configuration const WV_APP_CONFIG = "app.set_config_item"; +const WV_MESSAGE_UI_SET_PREFERRED_THEME = "ui.set_preferred_theme"; const WV_APP_CONFIG_ITEM_TTS_ENGINE = "internal.tts.engine"; // Location actions