mirror of
https://github.com/vodemn/m3_lightmeter.git
synced 2024-11-25 17:00:39 +00:00
disable volume handling when on Settings screen
This commit is contained in:
parent
1b19b417dc
commit
d17894d4a5
5 changed files with 43 additions and 12 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
|
|
||||||
|
@ -10,8 +12,12 @@ class VolumeEventsService {
|
||||||
|
|
||||||
const 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 {
|
Future<bool> setVolumeHandling(bool enableHandling) async {
|
||||||
|
if (!Platform.isAndroid) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return volumeHandlingChannel
|
return volumeHandlingChannel
|
||||||
.invokeMethod<bool>("setVolumeHandling", enableHandling)
|
.invokeMethod<bool>("setVolumeHandling", enableHandling)
|
||||||
.then((value) => value!);
|
.then((value) => value!);
|
||||||
|
@ -22,6 +28,12 @@ class VolumeEventsService {
|
||||||
/// KEYCODE_VOLUME_DOWN = 25;
|
/// KEYCODE_VOLUME_DOWN = 25;
|
||||||
/// pressed
|
/// pressed
|
||||||
Stream<int> volumeButtonsEventStream() {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
VolumeAction get volumeAction => _userPreferencesService.volumeAction;
|
||||||
Future<void> setVolumeAction(VolumeAction value) async {
|
Future<void> setVolumeAction(VolumeAction value) async {
|
||||||
/// If user selects `VolumeAction.volume` we allow system to handle key events
|
|
||||||
await _volumeEventsService.setVolumeHandling(value != VolumeAction.none);
|
await _volumeEventsService.setVolumeHandling(value != VolumeAction.none);
|
||||||
_userPreferencesService.volumeAction = value;
|
_userPreferencesService.volumeAction = value;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ import 'package:flutter/material.dart';
|
||||||
import 'package:lightmeter/data/models/volume_action.dart';
|
import 'package:lightmeter/data/models/volume_action.dart';
|
||||||
import 'package:lightmeter/data/volume_events_service.dart';
|
import 'package:lightmeter/data/volume_events_service.dart';
|
||||||
|
|
||||||
class VolumeKeysNotifier extends ChangeNotifier {
|
class VolumeKeysNotifier extends ChangeNotifier with RouteAware {
|
||||||
final VolumeEventsService volumeEventsService;
|
final VolumeEventsService volumeEventsService;
|
||||||
late final StreamSubscription<VolumeKey> _volumeKeysSubscription;
|
late final StreamSubscription<VolumeKey> _volumeKeysSubscription;
|
||||||
VolumeKey _value = VolumeKey.up;
|
VolumeKey _value = VolumeKey.up;
|
||||||
|
@ -13,7 +13,6 @@ class VolumeKeysNotifier extends ChangeNotifier {
|
||||||
// TODO: add RouteObserver and disable overriden action if SettingScreen is opened
|
// TODO: add RouteObserver and disable overriden action if SettingScreen is opened
|
||||||
_volumeKeysSubscription = volumeEventsService
|
_volumeKeysSubscription = volumeEventsService
|
||||||
.volumeButtonsEventStream()
|
.volumeButtonsEventStream()
|
||||||
.where((event) => event == 24 || event == 25)
|
|
||||||
.map((event) => event == 24 ? VolumeKey.up : VolumeKey.down)
|
.map((event) => event == 24 ? VolumeKey.up : VolumeKey.down)
|
||||||
.listen((event) {
|
.listen((event) {
|
||||||
value = event;
|
value = event;
|
||||||
|
|
|
@ -1,14 +1,33 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:lightmeter/generated/l10n.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/about/widget_settings_section_about.dart';
|
||||||
import 'package:lightmeter/screens/settings/components/general/widget_settings_section_general.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/metering/widget_settings_section_metering.dart';
|
||||||
import 'package:lightmeter/screens/settings/components/theme/widget_settings_section_theme.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/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});
|
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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return ScaffoldMessenger(
|
return ScaffoldMessenger(
|
||||||
|
|
|
@ -35,10 +35,4 @@ void main() {
|
||||||
|
|
||||||
test('false', () async => expectLater(service.setVolumeHandling(false), completion(false)));
|
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)));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue