disable volume handling when on Settings screen

This commit is contained in:
Vadim 2023-07-05 15:45:36 +02:00
parent 1b19b417dc
commit d17894d4a5
5 changed files with 43 additions and 12 deletions

View file

@ -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<bool> setVolumeHandling(bool enableHandling) async {
if (!Platform.isAndroid) {
return false;
}
return volumeHandlingChannel
.invokeMethod<bool>("setVolumeHandling", enableHandling)
.then((value) => value!);
@ -22,6 +28,12 @@ class VolumeEventsService {
/// KEYCODE_VOLUME_DOWN = 25;
/// pressed
Stream<int> volumeButtonsEventStream() {
return volumeEventsChannel.receiveBroadcastStream().cast<int>();
if (!Platform.isAndroid) {
return const Stream.empty();
}
return volumeEventsChannel
.receiveBroadcastStream()
.cast<int>()
.where((event) => event == 24 || event == 25);
}
}

View file

@ -31,9 +31,16 @@ class SettingsInteractor {
});
}
Future<void> disableVolumeHandling() async {
await _volumeEventsService.setVolumeHandling(false);
}
Future<void> restoreVolumeHandling() async {
await _volumeEventsService
.setVolumeHandling(_userPreferencesService.volumeAction != VolumeAction.none);
}
VolumeAction get volumeAction => _userPreferencesService.volumeAction;
Future<void> 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;
}

View file

@ -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<VolumeKey> _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;

View file

@ -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<SettingsScreen> createState() => _SettingsScreenState();
}
class _SettingsScreenState extends State<SettingsScreen> {
@override
void didChangeDependencies() {
super.didChangeDependencies();
context.get<SettingsInteractor>().disableVolumeHandling();
}
@override
void deactivate() {
context.get<SettingsInteractor>().restoreVolumeHandling();
super.deactivate();
}
@override
Widget build(BuildContext context) {
return ScaffoldMessenger(

View file

@ -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)));
});
}