From 2735f0b66f418ba13492da4dae6c11c69db94a5d Mon Sep 17 00:00:00 2001 From: Vadim <44135514+vodemn@users.noreply.github.com> Date: Fri, 23 Jun 2023 10:47:34 +0200 Subject: [PATCH] ML-81 Unsaved fractional stops (#83) * save stop type to sharedPrefs * tests --- lib/data/shared_prefs_service.dart | 4 ++++ lib/providers/stop_type_provider.dart | 20 +++++++++++++------- test/data/shared_prefs_service_test.dart | 19 +++++++++++++++++++ 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/lib/data/shared_prefs_service.dart b/lib/data/shared_prefs_service.dart index e90df0e..8fbb6af 100644 --- a/lib/data/shared_prefs_service.dart +++ b/lib/data/shared_prefs_service.dart @@ -14,6 +14,7 @@ class UserPreferencesService { static const ndFilterKey = "ndFilter"; static const evSourceTypeKey = "evSourceType"; + static const stopTypeKey = "stopType"; static const cameraEvCalibrationKey = "cameraEvCalibration"; static const lightSensorEvCalibrationKey = "lightSensorEvCalibration"; static const meteringScreenLayoutKey = "meteringScreenLayout"; @@ -84,6 +85,9 @@ class UserPreferencesService { bool get caffeine => _sharedPreferences.getBool(caffeineKey) ?? false; set caffeine(bool value) => _sharedPreferences.setBool(caffeineKey, value); + StopType get stopType => StopType.values[_sharedPreferences.getInt(stopTypeKey) ?? 2]; + set stopType(StopType value) => _sharedPreferences.setInt(stopTypeKey, value.index); + MeteringScreenLayoutConfig get meteringScreenLayout { final configJson = _sharedPreferences.getString(meteringScreenLayoutKey); if (configJson != null) { diff --git a/lib/providers/stop_type_provider.dart b/lib/providers/stop_type_provider.dart index 690f65f..3b20dd5 100644 --- a/lib/providers/stop_type_provider.dart +++ b/lib/providers/stop_type_provider.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:lightmeter/data/shared_prefs_service.dart'; import 'package:lightmeter/utils/inherited_generics.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; @@ -16,14 +17,12 @@ class StopTypeProvider extends StatefulWidget { } class StopTypeProviderState extends State { - StopType _stopType = StopType.third; + late StopType _stopType; - StopType get stopType => _stopType; - - void set(StopType type) { - setState(() { - _stopType = type; - }); + @override + void initState() { + super.initState(); + _stopType = context.get().stopType; } @override @@ -33,4 +32,11 @@ class StopTypeProviderState extends State { child: widget.child, ); } + + void set(StopType type) { + setState(() { + _stopType = type; + }); + context.get().stopType = type; + } } diff --git a/test/data/shared_prefs_service_test.dart b/test/data/shared_prefs_service_test.dart index 6143141..1a2d05b 100644 --- a/test/data/shared_prefs_service_test.dart +++ b/test/data/shared_prefs_service_test.dart @@ -164,6 +164,25 @@ void main() { }); }); + group('stopType', () { + test('get default', () { + when(() => sharedPreferences.getInt(UserPreferencesService.stopTypeKey)).thenReturn(null); + expect(service.stopType, StopType.third); + }); + + test('get', () { + when(() => sharedPreferences.getInt(UserPreferencesService.stopTypeKey)).thenReturn(1); + expect(service.stopType, StopType.half); + }); + + test('set', () { + when(() => sharedPreferences.setInt(UserPreferencesService.stopTypeKey, 0)) + .thenAnswer((_) => Future.value(true)); + service.stopType = StopType.full; + verify(() => sharedPreferences.setInt(UserPreferencesService.stopTypeKey, 0)).called(1); + }); + }); + group('meteringScreenLayout', () { test('get default', () { when(