From 2dc96e5c17c74cb23b64bb3f1bb06e939d7aef51 Mon Sep 17 00:00:00 2001 From: Vadim <44135514+vodemn@users.noreply.github.com> Date: Sun, 29 Jan 2023 19:01:15 +0300 Subject: [PATCH] added calibration for light sensor --- lib/data/shared_prefs_service.dart | 4 ++ lib/interactors/metering_interactor.dart | 1 + lib/interactors/settings_interactor.dart | 3 ++ lib/l10n/intl_en.arb | 1 + .../bloc_communication_metering.dart | 4 +- .../widget_bottom_controls.dart | 6 ++- .../bloc_container_camera.dart | 2 +- .../provider_container_camera.dart | 2 +- .../widget_container_camera.dart | 1 - .../bloc_container_light_sensor.dart | 6 ++- .../provider_container_light_sensor.dart | 3 +- .../ev_source_base/bloc_base_ev_source.dart | 1 - .../bloc_dialog_calibration.dart | 27 +++++++++-- .../event_dialog_calibration.dart | 10 ++++ .../state_dialog_calibration.dart | 6 ++- .../widget_dialog_calibration.dart | 47 ++++++++++++------- 16 files changed, 95 insertions(+), 29 deletions(-) diff --git a/lib/data/shared_prefs_service.dart b/lib/data/shared_prefs_service.dart index bcfbeae..5665469 100644 --- a/lib/data/shared_prefs_service.dart +++ b/lib/data/shared_prefs_service.dart @@ -11,6 +11,7 @@ class UserPreferencesService { static const _evSourceTypeKey = "evSourceType"; static const _cameraEvCalibrationKey = "cameraEvCalibration"; + static const _lightSensorEvCalibrationKey = "lightSensorEvCalibration"; static const _hapticsKey = "haptics"; static const _themeTypeKey = "themeType"; @@ -35,6 +36,9 @@ class UserPreferencesService { double get cameraEvCalibration => _sharedPreferences.getDouble(_cameraEvCalibrationKey) ?? 0.0; set cameraEvCalibration(double value) => _sharedPreferences.setDouble(_cameraEvCalibrationKey, value); + double get lightSensorEvCalibration => _sharedPreferences.getDouble(_lightSensorEvCalibrationKey) ?? 0.0; + set lightSensorEvCalibration(double value) => _sharedPreferences.setDouble(_lightSensorEvCalibrationKey, value); + ThemeType get themeType => ThemeType.values[_sharedPreferences.getInt(_themeTypeKey) ?? 0]; set themeType(ThemeType value) => _sharedPreferences.setInt(_themeTypeKey, value.index); diff --git a/lib/interactors/metering_interactor.dart b/lib/interactors/metering_interactor.dart index 3536aa5..e58446c 100644 --- a/lib/interactors/metering_interactor.dart +++ b/lib/interactors/metering_interactor.dart @@ -16,6 +16,7 @@ class MeteringInteractor { ); double get cameraEvCalibration => _userPreferencesService.cameraEvCalibration; + double get lightSensorEvCalibration => _userPreferencesService.lightSensorEvCalibration; bool get isHapticsEnabled => _userPreferencesService.haptics; diff --git a/lib/interactors/settings_interactor.dart b/lib/interactors/settings_interactor.dart index 62c7d6d..d19edfe 100644 --- a/lib/interactors/settings_interactor.dart +++ b/lib/interactors/settings_interactor.dart @@ -13,6 +13,9 @@ class SettingsInteractor { double get cameraEvCalibration => _userPreferencesService.cameraEvCalibration; void setCameraEvCalibration(double value) => _userPreferencesService.cameraEvCalibration = value; + double get lightSensorEvCalibration => _userPreferencesService.lightSensorEvCalibration; + void setLightSensorEvCalibration(double value) => _userPreferencesService.lightSensorEvCalibration = value; + bool get isHapticsEnabled => _userPreferencesService.haptics; /// Executes vibration if haptics are enabled in settings diff --git a/lib/l10n/intl_en.arb b/lib/l10n/intl_en.arb index 08d98ef..978035e 100644 --- a/lib/l10n/intl_en.arb +++ b/lib/l10n/intl_en.arb @@ -30,6 +30,7 @@ "calibration": "Calibration", "calibrationMessage": "The accuracy of the readings measured by this application depends entirely on the hardware of the device. Therefore, consider testing this application and setting up an EV calibration value that will give you the desired measurement results.", "camera": "Camera", + "lightSensor": "Light sensor", "general": "General", "haptics": "Haptics", "theme": "Theme", diff --git a/lib/screens/metering/communication/bloc_communication_metering.dart b/lib/screens/metering/communication/bloc_communication_metering.dart index b317c01..14fb5ce 100644 --- a/lib/screens/metering/communication/bloc_communication_metering.dart +++ b/lib/screens/metering/communication/bloc_communication_metering.dart @@ -6,7 +6,9 @@ import 'state_communication_metering.dart'; class MeteringCommunicationBloc extends Bloc { MeteringCommunicationBloc() : super(const InitState()) { - on((_, emit) => emit(const MeasureState())); + // `MeasureState` is not const, so that `Bloc` treats each state as new and updates state stream + // ignore: prefer_const_constructors + on((_, emit) => emit(MeasureState())); on((event, emit) => emit(MeasuredState(event.ev100))); } } diff --git a/lib/screens/metering/components/bottom_controls/widget_bottom_controls.dart b/lib/screens/metering/components/bottom_controls/widget_bottom_controls.dart index 9a9a21c..8b2d7d8 100644 --- a/lib/screens/metering/components/bottom_controls/widget_bottom_controls.dart +++ b/lib/screens/metering/components/bottom_controls/widget_bottom_controls.dart @@ -1,5 +1,7 @@ import 'package:flutter/material.dart'; +import 'package:lightmeter/data/models/ev_source_type.dart'; import 'package:lightmeter/res/dimens.dart'; +import 'package:provider/provider.dart'; import 'components/widget_button_measure.dart'; import 'components/widget_button_secondary.dart'; @@ -36,7 +38,9 @@ class MeteringBottomControls extends StatelessWidget { Expanded( child: MeteringSecondaryButton( onPressed: onSwitchEvSourceType!, - icon: Icons.sync, + icon: context.watch() != EvSourceType.camera + ? Icons.camera_rear + : Icons.wb_incandescent, ), ) else diff --git a/lib/screens/metering/components/camera_container/bloc_container_camera.dart b/lib/screens/metering/components/camera_container/bloc_container_camera.dart index 7b96855..34d4fb5 100644 --- a/lib/screens/metering/components/camera_container/bloc_container_camera.dart +++ b/lib/screens/metering/components/camera_container/bloc_container_camera.dart @@ -34,8 +34,8 @@ class CameraContainerBloc extends EvSourceBlocBase CameraContainerBloc( - context.read(), context.read(), + context.read(), ), child: CameraContainer( fastest: fastest, diff --git a/lib/screens/metering/components/camera_container/widget_container_camera.dart b/lib/screens/metering/components/camera_container/widget_container_camera.dart index 5f54e99..5d46616 100644 --- a/lib/screens/metering/components/camera_container/widget_container_camera.dart +++ b/lib/screens/metering/components/camera_container/widget_container_camera.dart @@ -15,7 +15,6 @@ import 'components/camera_controls/widget_camera_controls.dart'; import 'event_container_camera.dart'; import 'state_container_camera.dart'; -// TODO: add stepHeight calculation based on Text class CameraContainer extends StatelessWidget { final ExposurePair? fastest; final ExposurePair? slowest; diff --git a/lib/screens/metering/components/light_sensor_container/bloc_container_light_sensor.dart b/lib/screens/metering/components/light_sensor_container/bloc_container_light_sensor.dart index c28df2e..f5ea16d 100644 --- a/lib/screens/metering/components/light_sensor_container/bloc_container_light_sensor.dart +++ b/lib/screens/metering/components/light_sensor_container/bloc_container_light_sensor.dart @@ -18,8 +18,8 @@ class LightSensorContainerBloc StreamSubscription? _luxSubscriptions; LightSensorContainerBloc( - MeteringCommunicationBloc communicationBloc, this._meteringInteractor, + MeteringCommunicationBloc communicationBloc, ) : super( communicationBloc, const LightSensorInitState(), @@ -30,7 +30,9 @@ class LightSensorContainerBloc if (communicationState is communication_states.MeasureState) { if (_luxSubscriptions == null) { _luxSubscriptions = _meteringInteractor.luxStream().listen((event) { - communicationBloc.add(communication_event.MeasuredEvent(log2(event.toDouble() / 2.5))); + communicationBloc.add(communication_event.MeasuredEvent( + log2(event.toDouble() / 2.5) + _meteringInteractor.lightSensorEvCalibration, + )); }); } else { _luxSubscriptions?.cancel().then((_) => _luxSubscriptions = null); diff --git a/lib/screens/metering/components/light_sensor_container/provider_container_light_sensor.dart b/lib/screens/metering/components/light_sensor_container/provider_container_light_sensor.dart index 69179ea..5d75647 100644 --- a/lib/screens/metering/components/light_sensor_container/provider_container_light_sensor.dart +++ b/lib/screens/metering/components/light_sensor_container/provider_container_light_sensor.dart @@ -32,9 +32,10 @@ class LightSensorContainerProvider extends StatelessWidget { @override Widget build(BuildContext context) { return BlocProvider( + lazy: false, create: (context) => LightSensorContainerBloc( - context.read(), context.read(), + context.read(), ), child: LightSensorContainer( fastest: fastest, diff --git a/lib/screens/metering/components/shared/ev_source_base/bloc_base_ev_source.dart b/lib/screens/metering/components/shared/ev_source_base/bloc_base_ev_source.dart index 50299a1..5d8f9c2 100644 --- a/lib/screens/metering/components/shared/ev_source_base/bloc_base_ev_source.dart +++ b/lib/screens/metering/components/shared/ev_source_base/bloc_base_ev_source.dart @@ -1,4 +1,3 @@ - import 'dart:async'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:lightmeter/screens/metering/communication/bloc_communication_metering.dart'; diff --git a/lib/screens/settings/components/calibration/components/calibration_dialog/bloc_dialog_calibration.dart b/lib/screens/settings/components/calibration/components/calibration_dialog/bloc_dialog_calibration.dart index 1466d04..854ac7f 100644 --- a/lib/screens/settings/components/calibration/components/calibration_dialog/bloc_dialog_calibration.dart +++ b/lib/screens/settings/components/calibration/components/calibration_dialog/bloc_dialog_calibration.dart @@ -8,26 +8,47 @@ class CalibrationDialogBloc extends Bloc(_onCameraEvCalibrationChanged); on(_onCameraEvCalibrationReset); + on(_onLightSensorEvCalibrationChanged); + on(_onLightSensorEvCalibrationReset); on(_onSaveCalibration); } void _onCameraEvCalibrationChanged(CameraEvCalibrationChangedEvent event, Emitter emit) { _cameraEvCalibration = event.value; - emit(CalibrationDialogState(event.value)); + emit(CalibrationDialogState(_cameraEvCalibration, _lightSensorEvCalibration)); } void _onCameraEvCalibrationReset(CameraEvCalibrationResetEvent event, Emitter emit) { _settingsInteractor.quickVibration(); _cameraEvCalibration = 0; - emit(CalibrationDialogState(_cameraEvCalibration)); + emit(CalibrationDialogState(_cameraEvCalibration, _lightSensorEvCalibration)); + } + + void _onLightSensorEvCalibrationChanged( + LightSensorEvCalibrationChangedEvent event, Emitter emit) { + _lightSensorEvCalibration = event.value; + emit(CalibrationDialogState(_cameraEvCalibration, _lightSensorEvCalibration)); + } + + void _onLightSensorEvCalibrationReset(LightSensorEvCalibrationResetEvent event, Emitter emit) { + _settingsInteractor.quickVibration(); + _lightSensorEvCalibration = 0; + emit(CalibrationDialogState(_cameraEvCalibration, _lightSensorEvCalibration)); } void _onSaveCalibration(SaveCalibrationDialogEvent event, __) { _settingsInteractor.setCameraEvCalibration(_cameraEvCalibration); + _settingsInteractor.setLightSensorEvCalibration(_lightSensorEvCalibration); } } diff --git a/lib/screens/settings/components/calibration/components/calibration_dialog/event_dialog_calibration.dart b/lib/screens/settings/components/calibration/components/calibration_dialog/event_dialog_calibration.dart index e949032..08be80e 100644 --- a/lib/screens/settings/components/calibration/components/calibration_dialog/event_dialog_calibration.dart +++ b/lib/screens/settings/components/calibration/components/calibration_dialog/event_dialog_calibration.dart @@ -12,6 +12,16 @@ class CameraEvCalibrationResetEvent extends CalibrationDialogEvent { const CameraEvCalibrationResetEvent(); } +class LightSensorEvCalibrationChangedEvent extends CalibrationDialogEvent { + final double value; + + const LightSensorEvCalibrationChangedEvent(this.value); +} + +class LightSensorEvCalibrationResetEvent extends CalibrationDialogEvent { + const LightSensorEvCalibrationResetEvent(); +} + class SaveCalibrationDialogEvent extends CalibrationDialogEvent { const SaveCalibrationDialogEvent(); } diff --git a/lib/screens/settings/components/calibration/components/calibration_dialog/state_dialog_calibration.dart b/lib/screens/settings/components/calibration/components/calibration_dialog/state_dialog_calibration.dart index 3561bcc..928618f 100644 --- a/lib/screens/settings/components/calibration/components/calibration_dialog/state_dialog_calibration.dart +++ b/lib/screens/settings/components/calibration/components/calibration_dialog/state_dialog_calibration.dart @@ -1,5 +1,9 @@ class CalibrationDialogState { final double cameraEvCalibration; + final double lightSensorEvCalibration; - const CalibrationDialogState(this.cameraEvCalibration); + const CalibrationDialogState( + this.cameraEvCalibration, + this.lightSensorEvCalibration, + ); } diff --git a/lib/screens/settings/components/calibration/components/calibration_dialog/widget_dialog_calibration.dart b/lib/screens/settings/components/calibration/components/calibration_dialog/widget_dialog_calibration.dart index ed49180..add029d 100644 --- a/lib/screens/settings/components/calibration/components/calibration_dialog/widget_dialog_calibration.dart +++ b/lib/screens/settings/components/calibration/components/calibration_dialog/widget_dialog_calibration.dart @@ -9,16 +9,9 @@ import 'package:lightmeter/utils/to_string_signed.dart'; import 'bloc_dialog_calibration.dart'; import 'state_dialog_calibration.dart'; -class CalibrationDialog extends StatefulWidget { +class CalibrationDialog extends StatelessWidget { const CalibrationDialog({super.key}); - @override - State createState() => _CalibrationDialogState(); -} - -class _CalibrationDialogState extends State { - CalibrationDialogBloc get bloc => context.read(); - @override Widget build(BuildContext context) { return AlertDialog( @@ -30,17 +23,39 @@ class _CalibrationDialogState extends State { ), title: Text(S.of(context).calibration), contentPadding: const EdgeInsets.symmetric(horizontal: Dimens.paddingL), - content: BlocBuilder( - builder: (context, state) => Column( + content: SingleChildScrollView( + child: Column( mainAxisSize: MainAxisSize.min, children: [ Text(S.of(context).calibrationMessage), const SizedBox(height: Dimens.grid16), - _CalibrationUnit( - title: S.of(context).camera, - value: state.cameraEvCalibration, - onChanged: (value) => bloc.add(CameraEvCalibrationChangedEvent(value)), - onReset: () => bloc.add(const CameraEvCalibrationResetEvent()), + BlocBuilder( + buildWhen: (previous, current) => + previous.cameraEvCalibration != current.cameraEvCalibration, + builder: (context, state) => _CalibrationUnit( + title: S.of(context).camera, + value: state.cameraEvCalibration, + onChanged: (value) => context + .read() + .add(CameraEvCalibrationChangedEvent(value)), + onReset: () => context + .read() + .add(const CameraEvCalibrationResetEvent()), + ), + ), + BlocBuilder( + buildWhen: (previous, current) => + previous.lightSensorEvCalibration != current.lightSensorEvCalibration, + builder: (context, state) => _CalibrationUnit( + title: S.of(context).lightSensor, + value: state.lightSensorEvCalibration, + onChanged: (value) => context + .read() + .add(LightSensorEvCalibrationChangedEvent(value)), + onReset: () => context + .read() + .add(const LightSensorEvCalibrationResetEvent()), + ), ), ], ), @@ -58,7 +73,7 @@ class _CalibrationDialogState extends State { ), TextButton( onPressed: () { - bloc.add(const SaveCalibrationDialogEvent()); + context.read().add(const SaveCalibrationDialogEvent()); Navigator.of(context).pop(); }, child: Text(S.of(context).save),