diff --git a/lib/data/volume_events_service.dart b/lib/data/volume_events_service.dart index f43e41b..adb422e 100644 --- a/lib/data/volume_events_service.dart +++ b/lib/data/volume_events_service.dart @@ -1,3 +1,5 @@ +import 'dart:io'; + import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; @@ -10,8 +12,12 @@ class VolumeEventsService { const VolumeEventsService(); - /// Returns current status of volume handling + /// If set to `false` we allow system to handle key events. + /// Returns current status of volume handling. Future setVolumeHandling(bool enableHandling) async { + if (!Platform.isAndroid) { + return false; + } return volumeHandlingChannel .invokeMethod("setVolumeHandling", enableHandling) .then((value) => value!); @@ -22,6 +28,12 @@ class VolumeEventsService { /// KEYCODE_VOLUME_DOWN = 25; /// pressed Stream volumeButtonsEventStream() { - return volumeEventsChannel.receiveBroadcastStream().cast(); + if (!Platform.isAndroid) { + return const Stream.empty(); + } + return volumeEventsChannel + .receiveBroadcastStream() + .cast() + .where((event) => event == 24 || event == 25); } } diff --git a/lib/interactors/settings_interactor.dart b/lib/interactors/settings_interactor.dart index 7bb6f7b..4eeb8b9 100644 --- a/lib/interactors/settings_interactor.dart +++ b/lib/interactors/settings_interactor.dart @@ -31,9 +31,16 @@ class SettingsInteractor { }); } + Future disableVolumeHandling() async { + await _volumeEventsService.setVolumeHandling(false); + } + Future restoreVolumeHandling() async { + await _volumeEventsService + .setVolumeHandling(_userPreferencesService.volumeAction != VolumeAction.none); + } + VolumeAction get volumeAction => _userPreferencesService.volumeAction; Future setVolumeAction(VolumeAction value) async { - /// If user selects `VolumeAction.volume` we allow system to handle key events await _volumeEventsService.setVolumeHandling(value != VolumeAction.none); _userPreferencesService.volumeAction = value; } diff --git a/lib/screens/metering/components/shared/volume_keys_notifier/notifier_volume_keys.dart b/lib/screens/metering/components/shared/volume_keys_notifier/notifier_volume_keys.dart index a60f70e..17e35f2 100644 --- a/lib/screens/metering/components/shared/volume_keys_notifier/notifier_volume_keys.dart +++ b/lib/screens/metering/components/shared/volume_keys_notifier/notifier_volume_keys.dart @@ -4,7 +4,7 @@ import 'package:flutter/material.dart'; import 'package:lightmeter/data/models/volume_action.dart'; import 'package:lightmeter/data/volume_events_service.dart'; -class VolumeKeysNotifier extends ChangeNotifier { +class VolumeKeysNotifier extends ChangeNotifier with RouteAware { final VolumeEventsService volumeEventsService; late final StreamSubscription _volumeKeysSubscription; VolumeKey _value = VolumeKey.up; @@ -13,7 +13,6 @@ class VolumeKeysNotifier extends ChangeNotifier { // TODO: add RouteObserver and disable overriden action if SettingScreen is opened _volumeKeysSubscription = volumeEventsService .volumeButtonsEventStream() - .where((event) => event == 24 || event == 25) .map((event) => event == 24 ? VolumeKey.up : VolumeKey.down) .listen((event) { value = event; diff --git a/lib/screens/settings/screen_settings.dart b/lib/screens/settings/screen_settings.dart index 2adf906..3c745bd 100644 --- a/lib/screens/settings/screen_settings.dart +++ b/lib/screens/settings/screen_settings.dart @@ -1,14 +1,33 @@ import 'package:flutter/material.dart'; import 'package:lightmeter/generated/l10n.dart'; +import 'package:lightmeter/interactors/settings_interactor.dart'; import 'package:lightmeter/screens/settings/components/about/widget_settings_section_about.dart'; import 'package:lightmeter/screens/settings/components/general/widget_settings_section_general.dart'; import 'package:lightmeter/screens/settings/components/metering/widget_settings_section_metering.dart'; import 'package:lightmeter/screens/settings/components/theme/widget_settings_section_theme.dart'; import 'package:lightmeter/screens/shared/sliver_screen/screen_sliver.dart'; +import 'package:lightmeter/utils/inherited_generics.dart'; -class SettingsScreen extends StatelessWidget { +class SettingsScreen extends StatefulWidget { const SettingsScreen({super.key}); + @override + State createState() => _SettingsScreenState(); +} + +class _SettingsScreenState extends State { + @override + void didChangeDependencies() { + super.didChangeDependencies(); + context.get().disableVolumeHandling(); + } + + @override + void deactivate() { + context.get().restoreVolumeHandling(); + super.deactivate(); + } + @override Widget build(BuildContext context) { return ScaffoldMessenger( diff --git a/test/data/volume_events_service_test.dart b/test/data/volume_events_service_test.dart index 8566d3a..ab4f18a 100644 --- a/test/data/volume_events_service_test.dart +++ b/test/data/volume_events_service_test.dart @@ -35,10 +35,4 @@ void main() { test('false', () async => expectLater(service.setVolumeHandling(false), completion(false))); }); - - group('volumeButtonsEventStream', () { - test('true', () async => expectLater(service.setVolumeHandling(true), completion(true))); - - test('false', () async => expectLater(service.setVolumeHandling(false), completion(false))); - }); }