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 hapticsKey = "haptics";
static const autostartTimerKey = "autostartTimer";
static const volumeActionKey = "volumeAction";
static const localeKey = "locale";
@ -127,6 +128,9 @@ class UserPreferencesService {
bool get haptics => _sharedPreferences.getBool(hapticsKey) ?? true;
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(
(e) => e.toString() == _sharedPreferences.getString(volumeActionKey),
orElse: () => VolumeAction.shutter,

View file

@ -21,8 +21,7 @@ class SettingsInteractor {
void setCameraEvCalibration(double value) => _userPreferencesService.cameraEvCalibration = value;
double get lightSensorEvCalibration => _userPreferencesService.lightSensorEvCalibration;
void setLightSensorEvCalibration(double value) =>
_userPreferencesService.lightSensorEvCalibration = value;
void setLightSensorEvCalibration(double value) => _userPreferencesService.lightSensorEvCalibration = value;
bool get isCaffeineEnabled => _userPreferencesService.caffeine;
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 {
await _volumeEventsService.setVolumeHandling(false);
}
Future<void> restoreVolumeHandling() async {
await _volumeEventsService
.setVolumeHandling(_userPreferencesService.volumeAction != VolumeAction.none);
await _volumeEventsService.setVolumeHandling(_userPreferencesService.volumeAction != VolumeAction.none);
}
VolumeAction get volumeAction => _userPreferencesService.volumeAction;

View file

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

View file

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

View file

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

View file

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

View file

@ -72,6 +72,7 @@
"general": "通用",
"keepScreenOn": "保持屏幕常亮",
"haptics": "震动",
"autostartTimer": "自动启动定时器",
"volumeKeysAction": "音量键快门",
"language": "语言",
"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/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/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/shared/settings_section/widget_settings_section.dart';
@ -18,6 +19,7 @@ class GeneralSettingsSection extends StatelessWidget {
children: [
const CaffeineListTileProvider(),
const HapticsListTileProvider(),
const TimerListTileProvider(),
if (Platform.isAndroid) const VolumeActionsListTileProvider(),
const LanguageListTile(),
],

View file

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

View file

@ -326,6 +326,26 @@ void main() {
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', () {
test('get default', () {
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(
'Volume action',
() {
@ -123,8 +145,7 @@ void main() {
});
test('setVolumeAction(VolumeAction.shutter)', () async {
when(() => mockUserPreferencesService.volumeAction = VolumeAction.shutter)
.thenReturn(VolumeAction.shutter);
when(() => mockUserPreferencesService.volumeAction = VolumeAction.shutter).thenReturn(VolumeAction.shutter);
when(() => mockVolumeEventsService.setVolumeHandling(true)).thenAnswer((_) async => true);
expectLater(interactor.setVolumeAction(VolumeAction.shutter), isA<Future<void>>());
verify(() => mockVolumeEventsService.setVolumeHandling(true)).called(1);
@ -132,8 +153,7 @@ void main() {
});
test('setVolumeAction(VolumeAction.none)', () async {
when(() => mockUserPreferencesService.volumeAction = VolumeAction.none)
.thenReturn(VolumeAction.none);
when(() => mockUserPreferencesService.volumeAction = VolumeAction.none).thenReturn(VolumeAction.none);
when(() => mockVolumeEventsService.setVolumeHandling(false)).thenAnswer((_) async => false);
expectLater(interactor.setVolumeAction(VolumeAction.none), isA<Future<void>>());
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 {}
void main() {
late _MockTimerInteractor meteringInteractor;
late TimerBloc bloc;
late _MockTimerInteractor timerInteractor;
setUp(() {
meteringInteractor = _MockTimerInteractor();
when(meteringInteractor.quickVibration).thenAnswer((_) async {});
when(meteringInteractor.responseVibration).thenAnswer((_) async {});
bloc = TimerBloc(meteringInteractor, const Duration(seconds: 1));
});
tearDown(() {
bloc.close();
setUpAll(() {
timerInteractor = _MockTimerInteractor();
when(() => timerInteractor.isAutostartTimerEnabled).thenReturn(true);
when(timerInteractor.quickVibration).thenAnswer((_) async {});
when(timerInteractor.responseVibration).thenAnswer((_) async {});
});
blocTest<TimerBloc, TimerState>(
'Autostart',
build: () => TimerBloc(timerInteractor, const Duration(seconds: 1)),
verify: (_) {
verify(() => timerInteractor.quickVibration()).called(1);
},
expect: () => [
isA<TimerResumedState>(),
],
);
blocTest<TimerBloc, TimerState>(
'Start -> wait till the end -> reset',
build: () => bloc,
act: (bloc) async {
build: () => TimerBloc(timerInteractor, const Duration(seconds: 1)),
setUp: () {
when(() => timerInteractor.isAutostartTimerEnabled).thenReturn(false);
},
act: (bloc) {
bloc.add(const StartTimerEvent());
bloc.add(const TimerEndedEvent());
bloc.add(const ResetTimerEvent());
},
verify: (_) {
verify(() => meteringInteractor.quickVibration()).called(1);
verify(() => meteringInteractor.responseVibration()).called(1);
verify(() => timerInteractor.quickVibration()).called(1);
verify(() => timerInteractor.responseVibration()).called(1);
},
expect: () => [
isA<TimerResumedState>(),
@ -45,7 +53,10 @@ void main() {
blocTest<TimerBloc, TimerState>(
'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 {
bloc.add(const StartTimerEvent());
bloc.add(const StopTimerEvent());
@ -53,8 +64,8 @@ void main() {
bloc.add(const TimerEndedEvent());
},
verify: (_) {
verify(() => meteringInteractor.quickVibration()).called(3);
verify(() => meteringInteractor.responseVibration()).called(1);
verify(() => timerInteractor.quickVibration()).called(3);
verify(() => timerInteractor.responseVibration()).called(1);
},
expect: () => [
isA<TimerResumedState>(),

View file

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