added timer autostart

This commit is contained in:
Vadim 2024-05-04 19:58:19 +02:00
parent fbd78986df
commit 231ef0f39c
17 changed files with 169 additions and 28 deletions

View file

@ -24,6 +24,7 @@ class UserPreferencesService {
static const caffeineKey = "caffeine"; static const caffeineKey = "caffeine";
static const hapticsKey = "haptics"; static const hapticsKey = "haptics";
static const autostartTimerKey = "autostartTimer";
static const volumeActionKey = "volumeAction"; static const volumeActionKey = "volumeAction";
static const localeKey = "locale"; static const localeKey = "locale";
@ -127,6 +128,9 @@ class UserPreferencesService {
bool get haptics => _sharedPreferences.getBool(hapticsKey) ?? true; bool get haptics => _sharedPreferences.getBool(hapticsKey) ?? true;
set haptics(bool value) => _sharedPreferences.setBool(hapticsKey, value); set haptics(bool value) => _sharedPreferences.setBool(hapticsKey, value);
bool get autostartTimer => _sharedPreferences.getBool(autostartTimerKey) ?? true;
set autostartTimer(bool value) => _sharedPreferences.setBool(autostartTimerKey, value);
VolumeAction get volumeAction => VolumeAction.values.firstWhere( VolumeAction get volumeAction => VolumeAction.values.firstWhere(
(e) => e.toString() == _sharedPreferences.getString(volumeActionKey), (e) => e.toString() == _sharedPreferences.getString(volumeActionKey),
orElse: () => VolumeAction.shutter, orElse: () => VolumeAction.shutter,

View file

@ -21,8 +21,7 @@ class SettingsInteractor {
void setCameraEvCalibration(double value) => _userPreferencesService.cameraEvCalibration = value; void setCameraEvCalibration(double value) => _userPreferencesService.cameraEvCalibration = value;
double get lightSensorEvCalibration => _userPreferencesService.lightSensorEvCalibration; double get lightSensorEvCalibration => _userPreferencesService.lightSensorEvCalibration;
void setLightSensorEvCalibration(double value) => void setLightSensorEvCalibration(double value) => _userPreferencesService.lightSensorEvCalibration = value;
_userPreferencesService.lightSensorEvCalibration = value;
bool get isCaffeineEnabled => _userPreferencesService.caffeine; bool get isCaffeineEnabled => _userPreferencesService.caffeine;
Future<void> enableCaffeine(bool enable) async { Future<void> enableCaffeine(bool enable) async {
@ -31,12 +30,15 @@ class SettingsInteractor {
}); });
} }
bool get isAutostartTimerEnabled => _userPreferencesService.autostartTimer;
void enableAutostartTimer(bool enable) => _userPreferencesService.autostartTimer = enable;
Future<void> disableVolumeHandling() async { Future<void> disableVolumeHandling() async {
await _volumeEventsService.setVolumeHandling(false); await _volumeEventsService.setVolumeHandling(false);
} }
Future<void> restoreVolumeHandling() async { Future<void> restoreVolumeHandling() async {
await _volumeEventsService await _volumeEventsService.setVolumeHandling(_userPreferencesService.volumeAction != VolumeAction.none);
.setVolumeHandling(_userPreferencesService.volumeAction != VolumeAction.none);
} }
VolumeAction get volumeAction => _userPreferencesService.volumeAction; VolumeAction get volumeAction => _userPreferencesService.volumeAction;

View file

@ -19,4 +19,6 @@ class TimerInteractor {
Future<void> responseVibration() async { Future<void> responseVibration() async {
if (_userPreferencesService.haptics) await _hapticsService.responseVibration(); if (_userPreferencesService.haptics) await _hapticsService.responseVibration();
} }
bool get isAutostartTimerEnabled => _userPreferencesService.autostartTimer;
} }

View file

@ -72,6 +72,7 @@
"general": "General", "general": "General",
"keepScreenOn": "Keep screen on", "keepScreenOn": "Keep screen on",
"haptics": "Haptics", "haptics": "Haptics",
"autostartTimer": "Autostart timer",
"volumeKeysAction": "Shutter by volume keys", "volumeKeysAction": "Shutter by volume keys",
"language": "Language", "language": "Language",
"chooseLanguage": "Choose language", "chooseLanguage": "Choose language",

View file

@ -72,6 +72,7 @@
"general": "Général", "general": "Général",
"keepScreenOn": "Garder l'écran allumé", "keepScreenOn": "Garder l'écran allumé",
"haptics": "Haptiques", "haptics": "Haptiques",
"autostartTimer": "Minuterie de démarrage automatique",
"volumeKeysAction": "Obturateur par boutons de volume", "volumeKeysAction": "Obturateur par boutons de volume",
"language": "Langue", "language": "Langue",
"chooseLanguage": "Choisissez la langue", "chooseLanguage": "Choisissez la langue",

View file

@ -72,6 +72,7 @@
"general": "Общие", "general": "Общие",
"keepScreenOn": "Запрет блокировки", "keepScreenOn": "Запрет блокировки",
"haptics": "Вибрация", "haptics": "Вибрация",
"autostartTimer": "Автозапуск таймера",
"volumeKeysAction": "Затвор по кнопкам громкости", "volumeKeysAction": "Затвор по кнопкам громкости",
"language": "Язык", "language": "Язык",
"chooseLanguage": "Выберите язык", "chooseLanguage": "Выберите язык",

View file

@ -72,6 +72,7 @@
"general": "通用", "general": "通用",
"keepScreenOn": "保持屏幕常亮", "keepScreenOn": "保持屏幕常亮",
"haptics": "震动", "haptics": "震动",
"autostartTimer": "自动启动定时器",
"volumeKeysAction": "音量键快门", "volumeKeysAction": "音量键快门",
"language": "语言", "language": "语言",
"chooseLanguage": "选择语言", "chooseLanguage": "选择语言",

View file

@ -0,0 +1,15 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:lightmeter/interactors/settings_interactor.dart';
class TimerListTileBloc extends Cubit<bool> {
final SettingsInteractor _settingsInteractor;
TimerListTileBloc(
this._settingsInteractor,
) : super(_settingsInteractor.isAutostartTimerEnabled);
void onChanged(bool value) {
_settingsInteractor.enableAutostartTimer(value);
emit(value);
}
}

View file

@ -0,0 +1,18 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:lightmeter/screens/settings/components/general/components/timer/bloc_list_tile_timer.dart';
import 'package:lightmeter/screens/settings/components/general/components/timer/widget_list_tile_timer.dart';
import 'package:lightmeter/screens/settings/flow_settings.dart';
class TimerListTileProvider extends StatelessWidget {
const TimerListTileProvider({super.key});
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => TimerListTileBloc(SettingsInteractorProvider.of(context)),
child: const TimerListTile(),
);
}
}

View file

@ -0,0 +1,27 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/res/dimens.dart';
import 'package:lightmeter/screens/settings/components/general/components/timer/bloc_list_tile_timer.dart';
import 'package:lightmeter/screens/settings/components/shared/disable/widget_disable.dart';
import 'package:lightmeter/utils/context_utils.dart';
class TimerListTile extends StatelessWidget {
const TimerListTile({super.key});
@override
Widget build(BuildContext context) {
return Disable(
disable: !context.isPro,
child: BlocBuilder<TimerListTileBloc, bool>(
builder: (context, state) => SwitchListTile(
secondary: const Icon(Icons.timer_outlined),
title: Text(S.of(context).autostartTimer),
value: state && context.isPro,
onChanged: context.read<TimerListTileBloc>().onChanged,
contentPadding: const EdgeInsets.symmetric(horizontal: Dimens.paddingM),
),
),
);
}
}

View file

@ -5,6 +5,7 @@ import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/screens/settings/components/general/components/caffeine/provider_list_tile_caffeine.dart'; import 'package:lightmeter/screens/settings/components/general/components/caffeine/provider_list_tile_caffeine.dart';
import 'package:lightmeter/screens/settings/components/general/components/haptics/provider_list_tile_haptics.dart'; import 'package:lightmeter/screens/settings/components/general/components/haptics/provider_list_tile_haptics.dart';
import 'package:lightmeter/screens/settings/components/general/components/language/widget_list_tile_language.dart'; import 'package:lightmeter/screens/settings/components/general/components/language/widget_list_tile_language.dart';
import 'package:lightmeter/screens/settings/components/general/components/timer/provider_list_tile_timer.dart';
import 'package:lightmeter/screens/settings/components/general/components/volume_actions/provider_list_tile_volume_actions.dart'; import 'package:lightmeter/screens/settings/components/general/components/volume_actions/provider_list_tile_volume_actions.dart';
import 'package:lightmeter/screens/settings/components/shared/settings_section/widget_settings_section.dart'; import 'package:lightmeter/screens/settings/components/shared/settings_section/widget_settings_section.dart';
@ -18,6 +19,7 @@ class GeneralSettingsSection extends StatelessWidget {
children: [ children: [
const CaffeineListTileProvider(), const CaffeineListTileProvider(),
const HapticsListTileProvider(), const HapticsListTileProvider(),
const TimerListTileProvider(),
if (Platform.isAndroid) const VolumeActionsListTileProvider(), if (Platform.isAndroid) const VolumeActionsListTileProvider(),
const LanguageListTile(), const LanguageListTile(),
], ],

View file

@ -14,6 +14,8 @@ class TimerBloc extends Bloc<TimerEvent, TimerState> {
on<TimerEndedEvent>(_onTimerEnded); on<TimerEndedEvent>(_onTimerEnded);
on<StopTimerEvent>(_onStopTimer); on<StopTimerEvent>(_onStopTimer);
on<ResetTimerEvent>(_onResetTimer); on<ResetTimerEvent>(_onResetTimer);
if (_timerInteractor.isAutostartTimerEnabled) add(const StartTimerEvent());
} }
Future<void> _onStartTimer(StartTimerEvent _, Emitter emit) async { Future<void> _onStartTimer(StartTimerEvent _, Emitter emit) async {

View file

@ -326,6 +326,26 @@ void main() {
verify(() => sharedPreferences.setBool(UserPreferencesService.hapticsKey, false)).called(1); verify(() => sharedPreferences.setBool(UserPreferencesService.hapticsKey, false)).called(1);
}); });
}); });
group('autostartTimer', () {
test('get default', () {
when(() => sharedPreferences.getBool(UserPreferencesService.autostartTimerKey)).thenReturn(null);
expect(service.autostartTimer, true);
});
test('get', () {
when(() => sharedPreferences.getBool(UserPreferencesService.autostartTimerKey)).thenReturn(true);
expect(service.autostartTimer, true);
});
test('set', () {
when(() => sharedPreferences.setBool(UserPreferencesService.autostartTimerKey, false))
.thenAnswer((_) => Future.value(true));
service.autostartTimer = false;
verify(() => sharedPreferences.setBool(UserPreferencesService.autostartTimerKey, false)).called(1);
});
});
group('volumeAction', () { group('volumeAction', () {
test('get default', () { test('get default', () {
when(() => sharedPreferences.getBool(UserPreferencesService.volumeActionKey)).thenReturn(null); when(() => sharedPreferences.getBool(UserPreferencesService.volumeActionKey)).thenReturn(null);

View file

@ -85,6 +85,28 @@ void main() {
}, },
); );
group(
'AutostartTimer',
() {
test('isAutostartTimerEnabled', () {
when(() => mockUserPreferencesService.autostartTimer).thenReturn(true);
expect(interactor.isAutostartTimerEnabled, true);
when(() => mockUserPreferencesService.autostartTimer).thenReturn(false);
expect(interactor.isAutostartTimerEnabled, false);
verify(() => mockUserPreferencesService.autostartTimer).called(2);
});
test('enableAutostartTimer(true)', () {
when(() => mockUserPreferencesService.autostartTimer = true).thenReturn(true);
interactor.enableAutostartTimer(true);
verify(() => mockUserPreferencesService.autostartTimer = true).called(1);
when(() => mockUserPreferencesService.autostartTimer = true).thenReturn(false);
interactor.enableAutostartTimer(false);
verify(() => mockUserPreferencesService.autostartTimer = false).called(1);
});
},
);
group( group(
'Volume action', 'Volume action',
() { () {
@ -123,8 +145,7 @@ void main() {
}); });
test('setVolumeAction(VolumeAction.shutter)', () async { test('setVolumeAction(VolumeAction.shutter)', () async {
when(() => mockUserPreferencesService.volumeAction = VolumeAction.shutter) when(() => mockUserPreferencesService.volumeAction = VolumeAction.shutter).thenReturn(VolumeAction.shutter);
.thenReturn(VolumeAction.shutter);
when(() => mockVolumeEventsService.setVolumeHandling(true)).thenAnswer((_) async => true); when(() => mockVolumeEventsService.setVolumeHandling(true)).thenAnswer((_) async => true);
expectLater(interactor.setVolumeAction(VolumeAction.shutter), isA<Future<void>>()); expectLater(interactor.setVolumeAction(VolumeAction.shutter), isA<Future<void>>());
verify(() => mockVolumeEventsService.setVolumeHandling(true)).called(1); verify(() => mockVolumeEventsService.setVolumeHandling(true)).called(1);
@ -132,8 +153,7 @@ void main() {
}); });
test('setVolumeAction(VolumeAction.none)', () async { test('setVolumeAction(VolumeAction.none)', () async {
when(() => mockUserPreferencesService.volumeAction = VolumeAction.none) when(() => mockUserPreferencesService.volumeAction = VolumeAction.none).thenReturn(VolumeAction.none);
.thenReturn(VolumeAction.none);
when(() => mockVolumeEventsService.setVolumeHandling(false)).thenAnswer((_) async => false); when(() => mockVolumeEventsService.setVolumeHandling(false)).thenAnswer((_) async => false);
expectLater(interactor.setVolumeAction(VolumeAction.none), isA<Future<void>>()); expectLater(interactor.setVolumeAction(VolumeAction.none), isA<Future<void>>());
verify(() => mockVolumeEventsService.setVolumeHandling(false)).called(1); verify(() => mockVolumeEventsService.setVolumeHandling(false)).called(1);

View file

@ -60,4 +60,15 @@ void main() {
}); });
}, },
); );
group(
'AutostartTimer',
() {
test('isAutostartTimerEnabled', () {
when(() => mockUserPreferencesService.autostartTimer).thenReturn(true);
expect(interactor.isAutostartTimerEnabled, true);
verify(() => mockUserPreferencesService.autostartTimer).called(1);
});
},
);
} }

View file

@ -9,32 +9,40 @@ import 'package:test/test.dart';
class _MockTimerInteractor extends Mock implements TimerInteractor {} class _MockTimerInteractor extends Mock implements TimerInteractor {}
void main() { void main() {
late _MockTimerInteractor meteringInteractor; late _MockTimerInteractor timerInteractor;
late TimerBloc bloc;
setUp(() { setUpAll(() {
meteringInteractor = _MockTimerInteractor(); timerInteractor = _MockTimerInteractor();
when(meteringInteractor.quickVibration).thenAnswer((_) async {}); when(() => timerInteractor.isAutostartTimerEnabled).thenReturn(true);
when(meteringInteractor.responseVibration).thenAnswer((_) async {}); when(timerInteractor.quickVibration).thenAnswer((_) async {});
when(timerInteractor.responseVibration).thenAnswer((_) async {});
bloc = TimerBloc(meteringInteractor, const Duration(seconds: 1));
});
tearDown(() {
bloc.close();
}); });
blocTest<TimerBloc, TimerState>(
'Autostart',
build: () => TimerBloc(timerInteractor, const Duration(seconds: 1)),
verify: (_) {
verify(() => timerInteractor.quickVibration()).called(1);
},
expect: () => [
isA<TimerResumedState>(),
],
);
blocTest<TimerBloc, TimerState>( blocTest<TimerBloc, TimerState>(
'Start -> wait till the end -> reset', 'Start -> wait till the end -> reset',
build: () => bloc, build: () => TimerBloc(timerInteractor, const Duration(seconds: 1)),
act: (bloc) async { setUp: () {
when(() => timerInteractor.isAutostartTimerEnabled).thenReturn(false);
},
act: (bloc) {
bloc.add(const StartTimerEvent()); bloc.add(const StartTimerEvent());
bloc.add(const TimerEndedEvent()); bloc.add(const TimerEndedEvent());
bloc.add(const ResetTimerEvent()); bloc.add(const ResetTimerEvent());
}, },
verify: (_) { verify: (_) {
verify(() => meteringInteractor.quickVibration()).called(1); verify(() => timerInteractor.quickVibration()).called(1);
verify(() => meteringInteractor.responseVibration()).called(1); verify(() => timerInteractor.responseVibration()).called(1);
}, },
expect: () => [ expect: () => [
isA<TimerResumedState>(), isA<TimerResumedState>(),
@ -45,7 +53,10 @@ void main() {
blocTest<TimerBloc, TimerState>( blocTest<TimerBloc, TimerState>(
'Start -> stop -> start -> wait till the end', 'Start -> stop -> start -> wait till the end',
build: () => bloc, build: () => TimerBloc(timerInteractor, const Duration(seconds: 1)),
setUp: () {
when(() => timerInteractor.isAutostartTimerEnabled).thenReturn(false);
},
act: (bloc) async { act: (bloc) async {
bloc.add(const StartTimerEvent()); bloc.add(const StartTimerEvent());
bloc.add(const StopTimerEvent()); bloc.add(const StopTimerEvent());
@ -53,8 +64,8 @@ void main() {
bloc.add(const TimerEndedEvent()); bloc.add(const TimerEndedEvent());
}, },
verify: (_) { verify: (_) {
verify(() => meteringInteractor.quickVibration()).called(3); verify(() => timerInteractor.quickVibration()).called(3);
verify(() => meteringInteractor.responseVibration()).called(1); verify(() => timerInteractor.responseVibration()).called(1);
}, },
expect: () => [ expect: () => [
isA<TimerResumedState>(), isA<TimerResumedState>(),

View file

@ -3,6 +3,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:golden_toolkit/golden_toolkit.dart'; import 'package:golden_toolkit/golden_toolkit.dart';
import 'package:lightmeter/data/models/exposure_pair.dart'; import 'package:lightmeter/data/models/exposure_pair.dart';
import 'package:lightmeter/data/models/theme_type.dart'; import 'package:lightmeter/data/models/theme_type.dart';
import 'package:lightmeter/data/shared_prefs_service.dart';
import 'package:lightmeter/providers/user_preferences_provider.dart'; import 'package:lightmeter/providers/user_preferences_provider.dart';
import 'package:lightmeter/res/dimens.dart'; import 'package:lightmeter/res/dimens.dart';
import 'package:lightmeter/screens/shared/animated_circular_button/widget_button_circular_animated.dart'; import 'package:lightmeter/screens/shared/animated_circular_button/widget_button_circular_animated.dart';
@ -82,7 +83,9 @@ void main() {
} }
setUpAll(() { setUpAll(() {
SharedPreferences.setMockInitialValues({}); SharedPreferences.setMockInitialValues({
UserPreferencesService.autostartTimerKey: false,
});
}); });
testGoldens( testGoldens(