From 9a21d5f2f51741376062c722dd651b53520ffbaa Mon Sep 17 00:00:00 2001 From: Vadim Date: Fri, 5 May 2023 23:08:20 +0200 Subject: [PATCH] indicate EV value error --- lib/data/haptics_service.dart | 4 +- lib/interactors/metering_interactor.dart | 13 ++-- lib/screens/metering/bloc_metering.dart | 70 +++++++++++-------- .../event_communication_metering.dart | 2 +- .../state_communication_metering.dart | 2 +- .../measure_button/widget_button_measure.dart | 9 ++- .../provider_bottom_controls.dart | 3 + .../widget_bottom_controls.dart | 3 + lib/screens/metering/event_metering.dart | 4 ++ lib/screens/metering/screen_metering.dart | 4 +- lib/screens/metering/state_metering.dart | 27 ++----- 11 files changed, 81 insertions(+), 60 deletions(-) diff --git a/lib/data/haptics_service.dart b/lib/data/haptics_service.dart index 8d36e32..e27a6aa 100644 --- a/lib/data/haptics_service.dart +++ b/lib/data/haptics_service.dart @@ -7,9 +7,11 @@ class HapticsService { Future responseVibration() async => _tryVibrate(duration: 50, amplitude: 128); + Future errorVibration() async => _tryVibrate(duration: 100, amplitude: 128); + Future _tryVibrate({required int duration, required int amplitude}) async { if (await _canVibrate()) { - Vibration.vibrate( + await Vibration.vibrate( duration: duration, amplitude: amplitude, ); diff --git a/lib/interactors/metering_interactor.dart b/lib/interactors/metering_interactor.dart index 0134b34..3a40ac4 100644 --- a/lib/interactors/metering_interactor.dart +++ b/lib/interactors/metering_interactor.dart @@ -33,13 +33,18 @@ class MeteringInteractor { bool get isHapticsEnabled => _userPreferencesService.haptics; /// Executes vibration if haptics are enabled in settings - void quickVibration() { - if (_userPreferencesService.haptics) _hapticsService.quickVibration(); + Future quickVibration() async { + if (_userPreferencesService.haptics) await _hapticsService.quickVibration(); } /// Executes vibration if haptics are enabled in settings - void responseVibration() { - if (_userPreferencesService.haptics) _hapticsService.responseVibration(); + Future responseVibration() async { + if (_userPreferencesService.haptics) await _hapticsService.responseVibration(); + } + + /// Executes vibration if haptics are enabled in settings + Future errorVibration() async { + if (_userPreferencesService.haptics) await _hapticsService.errorVibration(); } Future checkCameraPermission() async { diff --git a/lib/screens/metering/bloc_metering.dart b/lib/screens/metering/bloc_metering.dart index 902f6af..f4bcf69 100644 --- a/lib/screens/metering/bloc_metering.dart +++ b/lib/screens/metering/bloc_metering.dart @@ -32,7 +32,7 @@ class MeteringBloc extends Bloc { late IsoValue _iso = _userPreferencesService.iso; late NdValue _nd = _userPreferencesService.ndFilter; late Film _film = _userPreferencesService.film; - double _ev100 = 0.0; + double? _ev100 = 0.0; bool _isMeteringInProgress = false; MeteringBloc( @@ -42,12 +42,13 @@ class MeteringBloc extends Bloc { this._equipmentProfileData, this.stopType, ) : super( - MeteringEndedState( + MeteringDataState( ev: 0.0, film: _userPreferencesService.film, iso: _userPreferencesService.iso, nd: _userPreferencesService.ndFilter, exposurePairs: const [], + continuousMetering: false, ), ) { _communicationSubscription = _communicationBloc.stream @@ -62,6 +63,7 @@ class MeteringBloc extends Bloc { on(_onNdChanged); on(_onMeasure); on(_onMeasured); + on(_onMeasureError); } @override @@ -73,13 +75,13 @@ class MeteringBloc extends Bloc { void _onCommunicationState(communication_states.ScreenState communicationState) { if (communicationState is communication_states.MeasuredState) { _isMeteringInProgress = communicationState is communication_states.MeteringInProgressState; - add(MeasuredEvent(communicationState.ev100)); + _handleEv100(communicationState.ev100); } } void _onStopTypeChanged(StopTypeChangedEvent event, Emitter emit) { stopType = event.stopType; - _emitMeasuredState(emit); + _updateMeasurements(); } void _onEquipmentProfileChanged(EquipmentProfileChangedEvent event, Emitter emit) { @@ -98,7 +100,7 @@ class MeteringBloc extends Bloc { _nd = event.equipmentProfileData.ndValues.first; } - _emitMeasuredState(emit); + _updateMeasurements(); } void _onFilmChanged(FilmChangedEvent event, Emitter emit) { @@ -111,7 +113,7 @@ class MeteringBloc extends Bloc { } _film = event.data; _userPreferencesService.film = event.data; - _emitMeasuredState(emit); + _updateMeasurements(); } void _onIsoChanged(IsoChangedEvent event, Emitter emit) { @@ -120,47 +122,57 @@ class MeteringBloc extends Bloc { } _userPreferencesService.iso = event.isoValue; _iso = event.isoValue; - _emitMeasuredState(emit); + _updateMeasurements(); } void _onNdChanged(NdChangedEvent event, Emitter emit) { _userPreferencesService.ndFilter = event.ndValue; _nd = event.ndValue; - _emitMeasuredState(emit); + _updateMeasurements(); } - void _onMeasure(_, Emitter emit) { + void _onMeasure(MeasureEvent _, Emitter emit) { _meteringInteractor.quickVibration(); _communicationBloc.add(const communication_events.MeasureEvent()); _isMeteringInProgress = true; emit(const LoadingState()); } + void _updateMeasurements() => _handleEv100(_ev100); + + void _handleEv100(double? ev100) { + if (ev100 == null || ev100.isNaN || ev100.isInfinite) { + add(const MeasureErrorEvent()); + } else { + add(MeasuredEvent(ev100)); + } + } + void _onMeasured(MeasuredEvent event, Emitter emit) { _meteringInteractor.responseVibration(); _ev100 = event.ev100; - _emitMeasuredState(emit); + final ev = event.ev100 + log2(_iso.value / 100) - _nd.stopReduction; + emit(MeteringDataState( + ev: ev, + film: _film, + iso: _iso, + nd: _nd, + exposurePairs: _buildExposureValues(ev), + continuousMetering: _isMeteringInProgress, + )); } - void _emitMeasuredState(Emitter emit) { - final ev = _ev100 + log2(_iso.value / 100) - _nd.stopReduction; - emit( - _isMeteringInProgress - ? MeteringInProgressState( - ev: ev, - film: _film, - iso: _iso, - nd: _nd, - exposurePairs: _buildExposureValues(ev), - ) - : MeteringEndedState( - ev: ev, - film: _film, - iso: _iso, - nd: _nd, - exposurePairs: _buildExposureValues(ev), - ), - ); + void _onMeasureError(MeasureErrorEvent _, Emitter emit) { + _meteringInteractor.errorVibration(); + _ev100 = null; + emit(MeteringDataState( + ev: null, + film: _film, + iso: _iso, + nd: _nd, + exposurePairs: const [], + continuousMetering: _isMeteringInProgress, + )); } List _buildExposureValues(double ev) { diff --git a/lib/screens/metering/communication/event_communication_metering.dart b/lib/screens/metering/communication/event_communication_metering.dart index 6c4ce11..7c00173 100644 --- a/lib/screens/metering/communication/event_communication_metering.dart +++ b/lib/screens/metering/communication/event_communication_metering.dart @@ -15,7 +15,7 @@ class MeasureEvent extends ScreenEvent { } abstract class MeasuredEvent extends SourceEvent { - final double ev100; + final double? ev100; const MeasuredEvent(this.ev100); } diff --git a/lib/screens/metering/communication/state_communication_metering.dart b/lib/screens/metering/communication/state_communication_metering.dart index e60ed67..06bedaf 100644 --- a/lib/screens/metering/communication/state_communication_metering.dart +++ b/lib/screens/metering/communication/state_communication_metering.dart @@ -19,7 +19,7 @@ class MeasureState extends SourceState { } abstract class MeasuredState extends ScreenState { - final double ev100; + final double? ev100; const MeasuredState(this.ev100); } diff --git a/lib/screens/metering/components/bottom_controls/components/measure_button/widget_button_measure.dart b/lib/screens/metering/components/bottom_controls/components/measure_button/widget_button_measure.dart index 3ea6d1f..4e2ec61 100644 --- a/lib/screens/metering/components/bottom_controls/components/measure_button/widget_button_measure.dart +++ b/lib/screens/metering/components/bottom_controls/components/measure_button/widget_button_measure.dart @@ -6,11 +6,13 @@ import 'package:lightmeter/screens/shared/filled_circle/widget_circle_filled.dar class MeteringMeasureButton extends StatefulWidget { final double? ev; final bool isMetering; + final bool hasError; final VoidCallback onTap; const MeteringMeasureButton({ required this.ev, required this.isMetering, + required this.hasError, required this.onTap, super.key, }); @@ -63,7 +65,12 @@ class _MeteringMeasureButtonState extends State { color: Theme.of(context).colorScheme.onSurface, size: Dimens.grid72 - Dimens.grid8, child: Center( - child: widget.ev != null ? _EvValueText(ev: widget.ev!) : null, + child: widget.hasError + ? Icon( + Icons.error, + color: Theme.of(context).colorScheme.surface, + ) + : (widget.ev != null ? _EvValueText(ev: widget.ev!) : null), ), ), ), diff --git a/lib/screens/metering/components/bottom_controls/provider_bottom_controls.dart b/lib/screens/metering/components/bottom_controls/provider_bottom_controls.dart index dd4a9be..4d07889 100644 --- a/lib/screens/metering/components/bottom_controls/provider_bottom_controls.dart +++ b/lib/screens/metering/components/bottom_controls/provider_bottom_controls.dart @@ -6,6 +6,7 @@ import 'package:lightmeter/screens/metering/components/bottom_controls/widget_bo class MeteringBottomControlsProvider extends StatelessWidget { final double? ev; final bool isMetering; + final bool hasError; final VoidCallback? onSwitchEvSourceType; final VoidCallback onMeasure; final VoidCallback onSettings; @@ -13,6 +14,7 @@ class MeteringBottomControlsProvider extends StatelessWidget { const MeteringBottomControlsProvider({ required this.ev, required this.isMetering, + required this.hasError, required this.onSwitchEvSourceType, required this.onMeasure, required this.onSettings, @@ -36,6 +38,7 @@ class MeteringBottomControlsProvider extends StatelessWidget { child: MeteringBottomControls( ev: ev, isMetering: isMetering, + hasError: hasError, onSwitchEvSourceType: onSwitchEvSourceType, onMeasure: onMeasure, onSettings: onSettings, 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 f059b8f..13a3892 100644 --- a/lib/screens/metering/components/bottom_controls/widget_bottom_controls.dart +++ b/lib/screens/metering/components/bottom_controls/widget_bottom_controls.dart @@ -7,6 +7,7 @@ import 'package:provider/provider.dart'; class MeteringBottomControls extends StatelessWidget { final double? ev; final bool isMetering; + final bool hasError; final VoidCallback? onSwitchEvSourceType; final VoidCallback onMeasure; final VoidCallback onSettings; @@ -14,6 +15,7 @@ class MeteringBottomControls extends StatelessWidget { const MeteringBottomControls({ required this.ev, required this.isMetering, + required this.hasError, required this.onSwitchEvSourceType, required this.onMeasure, required this.onSettings, @@ -54,6 +56,7 @@ class MeteringBottomControls extends StatelessWidget { MeteringMeasureButton( ev: ev, isMetering: isMetering, + hasError: hasError, onTap: onMeasure, ), Expanded( diff --git a/lib/screens/metering/event_metering.dart b/lib/screens/metering/event_metering.dart index 6a9abc6..888bea1 100644 --- a/lib/screens/metering/event_metering.dart +++ b/lib/screens/metering/event_metering.dart @@ -44,3 +44,7 @@ class MeasuredEvent extends MeteringEvent { const MeasuredEvent(this.ev100); } + +class MeasureErrorEvent extends MeteringEvent { + const MeasureErrorEvent(); +} diff --git a/lib/screens/metering/screen_metering.dart b/lib/screens/metering/screen_metering.dart index b1a2d9c..66f2445 100644 --- a/lib/screens/metering/screen_metering.dart +++ b/lib/screens/metering/screen_metering.dart @@ -63,7 +63,9 @@ class _MeteringScreenState extends State { BlocBuilder( builder: (context, state) => MeteringBottomControlsProvider( ev: state is MeteringDataState ? state.ev : null, - isMetering: state is LoadingState || state is MeteringInProgressState, + isMetering: + state is LoadingState || state is MeteringDataState && state.continuousMetering, + hasError: state is MeteringDataState && state.hasError, onSwitchEvSourceType: context.read().hasLightSensor ? EvSourceTypeProvider.of(context).toggleType : null, diff --git a/lib/screens/metering/state_metering.dart b/lib/screens/metering/state_metering.dart index 7016374..17e192b 100644 --- a/lib/screens/metering/state_metering.dart +++ b/lib/screens/metering/state_metering.dart @@ -12,12 +12,13 @@ class LoadingState extends MeteringState { const LoadingState(); } -abstract class MeteringDataState extends MeteringState { - final double ev; +class MeteringDataState extends MeteringState { + final double? ev; final Film film; final IsoValue iso; final NdValue nd; final List exposurePairs; + final bool continuousMetering; const MeteringDataState({ required this.ev, @@ -25,28 +26,10 @@ abstract class MeteringDataState extends MeteringState { required this.iso, required this.nd, required this.exposurePairs, + required this.continuousMetering, }); ExposurePair? get fastest => exposurePairs.isEmpty ? null : exposurePairs.first; ExposurePair? get slowest => exposurePairs.isEmpty ? null : exposurePairs.last; -} - -class MeteringInProgressState extends MeteringDataState { - const MeteringInProgressState({ - required super.ev, - required super.film, - required super.iso, - required super.nd, - required super.exposurePairs, - }); -} - -class MeteringEndedState extends MeteringDataState { - const MeteringEndedState({ - required super.ev, - required super.film, - required super.iso, - required super.nd, - required super.exposurePairs, - }); + bool get hasError => ev == null; }