rewritten MeteringBloc logic

This commit is contained in:
Vadim 2023-06-09 13:52:16 +02:00
parent 07367afdac
commit abf4d77342
8 changed files with 237 additions and 253 deletions

View file

@ -1,10 +1,8 @@
import 'dart:async'; import 'dart:async';
import 'dart:math';
import 'package:bloc_concurrency/bloc_concurrency.dart'; import 'package:bloc_concurrency/bloc_concurrency.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:lightmeter/data/models/exposure_pair.dart';
import 'package:lightmeter/data/models/film.dart'; import 'package:lightmeter/data/models/film.dart';
import 'package:lightmeter/interactors/metering_interactor.dart'; import 'package:lightmeter/interactors/metering_interactor.dart';
import 'package:lightmeter/screens/metering/communication/bloc_communication_metering.dart'; import 'package:lightmeter/screens/metering/communication/bloc_communication_metering.dart';
@ -21,39 +19,16 @@ class MeteringBloc extends Bloc<MeteringEvent, MeteringState> {
final MeteringInteractor _meteringInteractor; final MeteringInteractor _meteringInteractor;
late final StreamSubscription<communication_states.ScreenState> _communicationSubscription; late final StreamSubscription<communication_states.ScreenState> _communicationSubscription;
List<ApertureValue> get _apertureValues =>
_equipmentProfileData.apertureValues.whereStopType(stopType);
List<ShutterSpeedValue> get _shutterSpeedValues =>
_equipmentProfileData.shutterSpeedValues.whereStopType(stopType);
EquipmentProfileData _equipmentProfileData;
StopType stopType;
@visibleForTesting
late IsoValue iso = _meteringInteractor.iso;
@visibleForTesting
late NdValue nd = _meteringInteractor.ndFilter;
@visibleForTesting
late Film film = _meteringInteractor.film;
@visibleForTesting
double? ev100;
MeteringBloc( MeteringBloc(
this._communicationBloc, this._communicationBloc,
this._meteringInteractor, this._meteringInteractor,
this._equipmentProfileData,
this.stopType,
) : super( ) : super(
MeteringDataState( MeteringDataState(
ev: null, ev100: null,
film: _meteringInteractor.film, film: _meteringInteractor.film,
iso: _meteringInteractor.iso, iso: _meteringInteractor.iso,
nd: _meteringInteractor.ndFilter, nd: _meteringInteractor.ndFilter,
exposurePairs: const [], isMetering: false,
continuousMetering: false,
), ),
) { ) {
_communicationSubscription = _communicationBloc.stream _communicationSubscription = _communicationBloc.stream
@ -62,7 +37,6 @@ class MeteringBloc extends Bloc<MeteringEvent, MeteringState> {
.listen(onCommunicationState); .listen(onCommunicationState);
on<EquipmentProfileChangedEvent>(_onEquipmentProfileChanged); on<EquipmentProfileChangedEvent>(_onEquipmentProfileChanged);
on<StopTypeChangedEvent>(_onStopTypeChanged);
on<FilmChangedEvent>(_onFilmChanged); on<FilmChangedEvent>(_onFilmChanged);
on<IsoChangedEvent>(_onIsoChanged); on<IsoChangedEvent>(_onIsoChanged);
on<NdChangedEvent>(_onNdChanged); on<NdChangedEvent>(_onNdChanged);
@ -71,6 +45,23 @@ class MeteringBloc extends Bloc<MeteringEvent, MeteringState> {
on<MeasureErrorEvent>(_onMeasureError); on<MeasureErrorEvent>(_onMeasureError);
} }
@override
void onTransition(Transition<MeteringEvent, MeteringState> transition) {
super.onTransition(transition);
if (transition.nextState is MeteringDataState) {
final nextState = transition.nextState as MeteringDataState;
if (transition.currentState is LoadingState ||
transition.currentState is MeteringDataState &&
(transition.currentState as MeteringDataState).ev != nextState.ev) {
if (nextState.hasError) {
_meteringInteractor.errorVibration();
} else {
_meteringInteractor.responseVibration();
}
}
}
}
@override @override
Future<void> close() async { Future<void> close() async {
await _communicationSubscription.cancel(); await _communicationSubscription.cancel();
@ -82,60 +73,72 @@ class MeteringBloc extends Bloc<MeteringEvent, MeteringState> {
if (communicationState is communication_states.MeasuredState) { if (communicationState is communication_states.MeasuredState) {
_handleEv100( _handleEv100(
communicationState.ev100, communicationState.ev100,
continuousMetering: communicationState is communication_states.MeteringInProgressState, isMetering: communicationState is communication_states.MeteringInProgressState,
); );
} }
} }
void _onStopTypeChanged(StopTypeChangedEvent event, Emitter emit) {
if (stopType != event.stopType) {
stopType = event.stopType;
_updateMeasurements();
}
}
void _onEquipmentProfileChanged(EquipmentProfileChangedEvent event, Emitter emit) { void _onEquipmentProfileChanged(EquipmentProfileChangedEvent event, Emitter emit) {
_equipmentProfileData = event.equipmentProfileData;
bool willUpdateMeasurements = false; bool willUpdateMeasurements = false;
/// Update selected ISO value, if selected equipment profile /// Update selected ISO value and discard selected film, if selected equipment profile
/// doesn't contain currently selected value /// doesn't contain currently selected value
if (!event.equipmentProfileData.isoValues.any((v) => iso.value == v.value)) { IsoValue iso = state.iso;
Film film = state.film;
if (!event.equipmentProfileData.isoValues.any((v) => state.iso.value == v.value)) {
_meteringInteractor.iso = event.equipmentProfileData.isoValues.first; _meteringInteractor.iso = event.equipmentProfileData.isoValues.first;
iso = event.equipmentProfileData.isoValues.first; iso = event.equipmentProfileData.isoValues.first;
_meteringInteractor.film = Film.values.first;
film = Film.values.first;
willUpdateMeasurements &= true; willUpdateMeasurements &= true;
} }
/// The same for ND filter /// The same for ND filter
if (!event.equipmentProfileData.ndValues.any((v) => nd.value == v.value)) { NdValue nd = state.nd;
if (!event.equipmentProfileData.ndValues.any((v) => state.nd.value == v.value)) {
_meteringInteractor.ndFilter = event.equipmentProfileData.ndValues.first; _meteringInteractor.ndFilter = event.equipmentProfileData.ndValues.first;
nd = event.equipmentProfileData.ndValues.first; nd = event.equipmentProfileData.ndValues.first;
willUpdateMeasurements &= true; willUpdateMeasurements &= true;
} }
if (willUpdateMeasurements) { if (willUpdateMeasurements) {
_updateMeasurements(); emit(
MeteringDataState(
ev100: state.ev100,
film: film,
iso: iso,
nd: nd,
isMetering: state.isMetering,
),
);
} }
} }
void _onFilmChanged(FilmChangedEvent event, Emitter emit) { void _onFilmChanged(FilmChangedEvent event, Emitter emit) {
if (film.name != event.data.name) { if (state.film.name != event.film.name) {
film = event.data; _meteringInteractor.film = event.film;
_meteringInteractor.film = event.data;
_film = event.data; /// Find `IsoValue` with matching value
IsoValue iso = state.iso;
if (state.iso.value != event.film.iso && event.film != const Film.other()) {
iso = IsoValue.values.firstWhere(
(e) => e.value == event.film.iso,
orElse: () => state.iso,
);
_meteringInteractor.iso = iso;
}
/// If user selects 'Other' film we preserve currently selected ISO /// If user selects 'Other' film we preserve currently selected ISO
/// and therefore only discard reciprocity formula /// and therefore only discard reciprocity formula
if (iso.value != event.data.iso && event.data != const Film.other()) { emit(
final newIso = IsoValue.values.firstWhere( MeteringDataState(
(e) => e.value == event.data.iso, ev100: state.ev100,
orElse: () => iso, film: event.film,
); iso: iso,
_meteringInteractor.iso = newIso; nd: state.nd,
iso = newIso; isMetering: state.isMetering,
} ),
);
_updateMeasurements();
} }
} }
@ -144,20 +147,33 @@ class MeteringBloc extends Bloc<MeteringEvent, MeteringState> {
/// because, for example, Fomapan 400 and any Ilford 400 /// because, for example, Fomapan 400 and any Ilford 400
/// have different reciprocity formulas /// have different reciprocity formulas
_meteringInteractor.film = Film.values.first; _meteringInteractor.film = Film.values.first;
_film = Film.values.first;
if (iso != event.isoValue) { if (state.iso != event.isoValue) {
_meteringInteractor.iso = event.isoValue; _meteringInteractor.iso = event.isoValue;
iso = event.isoValue; emit(
_updateMeasurements(); MeteringDataState(
ev100: state.ev100,
film: Film.values.first,
iso: event.isoValue,
nd: state.nd,
isMetering: state.isMetering,
),
);
} }
} }
void _onNdChanged(NdChangedEvent event, Emitter emit) { void _onNdChanged(NdChangedEvent event, Emitter emit) {
if (nd != event.ndValue) { if (state.nd != event.ndValue) {
_meteringInteractor.ndFilter = event.ndValue; _meteringInteractor.ndFilter = event.ndValue;
nd = event.ndValue; emit(
_updateMeasurements(); MeteringDataState(
ev100: state.ev100,
film: state.film,
iso: state.iso,
nd: event.ndValue,
isMetering: state.isMetering,
),
);
} }
} }
@ -166,109 +182,42 @@ class MeteringBloc extends Bloc<MeteringEvent, MeteringState> {
_communicationBloc.add(const communication_events.MeasureEvent()); _communicationBloc.add(const communication_events.MeasureEvent());
emit( emit(
LoadingState( LoadingState(
film: film, film: state.film,
iso: iso, iso: state.iso,
nd: nd, nd: state.nd,
), ),
); );
} }
void _updateMeasurements() => _handleEv100(ev100, continuousMetering: false); void _handleEv100(double? ev100, {required bool isMetering}) {
void _handleEv100(double? ev100, {required bool continuousMetering}) {
if (ev100 == null || ev100.isNaN || ev100.isInfinite) { if (ev100 == null || ev100.isNaN || ev100.isInfinite) {
add(MeasureErrorEvent(continuousMetering: continuousMetering)); add(MeasureErrorEvent(isMetering: isMetering));
} else { } else {
add(MeasuredEvent(ev100, continuousMetering: continuousMetering)); add(MeasuredEvent(ev100, isMetering: isMetering));
} }
} }
void _onMeasured(MeasuredEvent event, Emitter emit) { void _onMeasured(MeasuredEvent event, Emitter emit) {
_meteringInteractor.responseVibration();
ev100 = event.ev100;
final ev = event.ev100 + log2(iso.value / 100) - nd.stopReduction;
emit( emit(
MeteringDataState( MeteringDataState(
ev: ev, ev100: event.ev100,
film: film, film: state.film,
iso: iso, iso: state.iso,
nd: nd, nd: state.nd,
exposurePairs: buildExposureValues(ev), isMetering: event.isMetering,
continuousMetering: event.continuousMetering,
), ),
); );
} }
void _onMeasureError(MeasureErrorEvent event, Emitter emit) { void _onMeasureError(MeasureErrorEvent event, Emitter emit) {
_meteringInteractor.errorVibration();
ev100 = null;
emit( emit(
MeteringDataState( MeteringDataState(
ev: null, ev100: null,
film: film, film: state.film,
iso: iso, iso: state.iso,
nd: nd, nd: state.nd,
exposurePairs: const [], isMetering: event.isMetering,
continuousMetering: event.continuousMetering,
), ),
); );
} }
@visibleForTesting
List<ExposurePair> buildExposureValues(double ev) {
if (ev.isNaN || ev.isInfinite) {
return List.empty();
}
/// Depending on the `stopType` the exposure pairs list length is multiplied by 1,2 or 3
final int evSteps = (ev * (stopType.index + 1)).round();
/// Basically we use 1" shutter speed as an anchor point for building the exposure pairs list.
/// But user can exclude this value from the list using custom equipment profile.
/// So we have to restore the index of the anchor value.
const ShutterSpeedValue anchorShutterSpeed = ShutterSpeedValue(1, false, StopType.full);
int anchorIndex = _shutterSpeedValues.indexOf(anchorShutterSpeed);
if (anchorIndex < 0) {
final filteredFullList = ShutterSpeedValue.values.whereStopType(stopType);
final customListStartIndex = filteredFullList.indexOf(_shutterSpeedValues.first);
final fullListAnchor = filteredFullList.indexOf(anchorShutterSpeed);
if (customListStartIndex < fullListAnchor) {
/// This means, that user excluded anchor value at the end,
/// i.e. all shutter speed values are shorter than 1".
anchorIndex = fullListAnchor - customListStartIndex;
} else {
/// In case user excludes anchor value at the start,
/// we can do no adjustment.
}
}
final int evOffset = anchorIndex - evSteps;
late final int apertureOffset;
late final int shutterSpeedOffset;
if (evOffset >= 0) {
apertureOffset = 0;
shutterSpeedOffset = evOffset;
} else {
apertureOffset = -evOffset;
shutterSpeedOffset = 0;
}
final int itemsCount = min(
_apertureValues.length + shutterSpeedOffset,
_shutterSpeedValues.length + apertureOffset,
) -
max(apertureOffset, shutterSpeedOffset);
if (itemsCount < 0) {
return List.empty();
}
return List.generate(
itemsCount,
(index) => ExposurePair(
_apertureValues[index + apertureOffset],
film.reciprocityFailure(_shutterSpeedValues[index + shutterSpeedOffset]),
),
growable: false,
);
}
} }

View file

@ -6,13 +6,11 @@ import 'package:lightmeter/screens/shared/filled_circle/widget_circle_filled.dar
class MeteringMeasureButton extends StatefulWidget { class MeteringMeasureButton extends StatefulWidget {
final double? ev; final double? ev;
final bool isMetering; final bool isMetering;
final bool hasError;
final VoidCallback onTap; final VoidCallback onTap;
const MeteringMeasureButton({ const MeteringMeasureButton({
required this.ev, required this.ev,
required this.isMetering, required this.isMetering,
required this.hasError,
required this.onTap, required this.onTap,
super.key, super.key,
}); });
@ -34,58 +32,49 @@ class _MeteringMeasureButtonState extends State<MeteringMeasureButton> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return IgnorePointer( return GestureDetector(
ignoring: widget.isMetering && widget.ev == null && !widget.hasError, onTap: widget.onTap,
child: GestureDetector( onTapDown: (_) {
onTap: widget.onTap, setState(() {
onTapDown: (_) { _isPressed = true;
setState(() { });
_isPressed = true; },
}); onTapUp: (_) {
}, setState(() {
onTapUp: (_) { _isPressed = false;
setState(() { });
_isPressed = false; },
}); onTapCancel: () {
}, setState(() {
onTapCancel: () { _isPressed = false;
setState(() { });
_isPressed = false; },
}); child: SizedBox.fromSize(
}, size: const Size.square(Dimens.grid72),
child: SizedBox.fromSize( child: Stack(
size: const Size.square(Dimens.grid72), children: [
child: Stack( Center(
children: [ child: AnimatedScale(
Center( duration: Dimens.durationS,
child: AnimatedScale( scale: _isPressed ? 0.9 : 1.0,
duration: Dimens.durationS, child: FilledCircle(
scale: _isPressed ? 0.9 : 1.0, color: Theme.of(context).colorScheme.onSurface,
child: FilledCircle( size: Dimens.grid72 - Dimens.grid8,
color: Theme.of(context).colorScheme.onSurface, child: Center(
size: Dimens.grid72 - Dimens.grid8, child: widget.ev != null ? _EvValueText(ev: widget.ev!) : null,
child: Center(
child: widget.hasError
? Icon(
Icons.error_outline,
color: Theme.of(context).colorScheme.surface,
size: Dimens.grid24,
)
: (widget.ev != null ? _EvValueText(ev: widget.ev!) : null),
),
), ),
), ),
), ),
Positioned.fill( ),
child: CircularProgressIndicator( Positioned.fill(
/// This key is needed to make indicator start from the same point every time child: CircularProgressIndicator(
key: ValueKey(widget.isMetering), /// This key is needed to make indicator start from the same point every time
color: Theme.of(context).colorScheme.onSurface, key: ValueKey(widget.isMetering),
value: widget.isMetering ? null : 1, color: Theme.of(context).colorScheme.onSurface,
), value: widget.isMetering ? null : 1,
), ),
], ),
), ],
), ),
), ),
); );

View file

@ -6,7 +6,6 @@ import 'package:lightmeter/screens/metering/components/bottom_controls/widget_bo
class MeteringBottomControlsProvider extends StatelessWidget { class MeteringBottomControlsProvider extends StatelessWidget {
final double? ev; final double? ev;
final bool isMetering; final bool isMetering;
final bool hasError;
final VoidCallback? onSwitchEvSourceType; final VoidCallback? onSwitchEvSourceType;
final VoidCallback onMeasure; final VoidCallback onMeasure;
final VoidCallback onSettings; final VoidCallback onSettings;
@ -14,7 +13,6 @@ class MeteringBottomControlsProvider extends StatelessWidget {
const MeteringBottomControlsProvider({ const MeteringBottomControlsProvider({
required this.ev, required this.ev,
required this.isMetering, required this.isMetering,
required this.hasError,
required this.onSwitchEvSourceType, required this.onSwitchEvSourceType,
required this.onMeasure, required this.onMeasure,
required this.onSettings, required this.onSettings,
@ -38,7 +36,6 @@ class MeteringBottomControlsProvider extends StatelessWidget {
child: MeteringBottomControls( child: MeteringBottomControls(
ev: ev, ev: ev,
isMetering: isMetering, isMetering: isMetering,
hasError: hasError,
onSwitchEvSourceType: onSwitchEvSourceType, onSwitchEvSourceType: onSwitchEvSourceType,
onMeasure: onMeasure, onMeasure: onMeasure,
onSettings: onSettings, onSettings: onSettings,

View file

@ -7,7 +7,6 @@ import 'package:lightmeter/utils/inherited_generics.dart';
class MeteringBottomControls extends StatelessWidget { class MeteringBottomControls extends StatelessWidget {
final double? ev; final double? ev;
final bool isMetering; final bool isMetering;
final bool hasError;
final VoidCallback? onSwitchEvSourceType; final VoidCallback? onSwitchEvSourceType;
final VoidCallback onMeasure; final VoidCallback onMeasure;
final VoidCallback onSettings; final VoidCallback onSettings;
@ -15,7 +14,6 @@ class MeteringBottomControls extends StatelessWidget {
const MeteringBottomControls({ const MeteringBottomControls({
required this.ev, required this.ev,
required this.isMetering, required this.isMetering,
required this.hasError,
required this.onSwitchEvSourceType, required this.onSwitchEvSourceType,
required this.onMeasure, required this.onMeasure,
required this.onSettings, required this.onSettings,
@ -56,7 +54,6 @@ class MeteringBottomControls extends StatelessWidget {
MeteringMeasureButton( MeteringMeasureButton(
ev: ev, ev: ev,
isMetering: isMetering, isMetering: isMetering,
hasError: hasError,
onTap: onMeasure, onTap: onMeasure,
), ),
Expanded( Expanded(

View file

@ -1,16 +1,10 @@
import 'package:lightmeter/data/models/film.dart'; import 'package:lightmeter/data/models/film.dart';
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
abstract class MeteringEvent { sealed class MeteringEvent {
const MeteringEvent(); const MeteringEvent();
} }
class StopTypeChangedEvent extends MeteringEvent {
final StopType stopType;
const StopTypeChangedEvent(this.stopType);
}
class EquipmentProfileChangedEvent extends MeteringEvent { class EquipmentProfileChangedEvent extends MeteringEvent {
final EquipmentProfileData equipmentProfileData; final EquipmentProfileData equipmentProfileData;
@ -18,9 +12,9 @@ class EquipmentProfileChangedEvent extends MeteringEvent {
} }
class FilmChangedEvent extends MeteringEvent { class FilmChangedEvent extends MeteringEvent {
final Film data; final Film film;
const FilmChangedEvent(this.data); const FilmChangedEvent(this.film);
} }
class IsoChangedEvent extends MeteringEvent { class IsoChangedEvent extends MeteringEvent {
@ -41,13 +35,13 @@ class MeasureEvent extends MeteringEvent {
class MeasuredEvent extends MeteringEvent { class MeasuredEvent extends MeteringEvent {
final double ev100; final double ev100;
final bool continuousMetering; final bool isMetering;
const MeasuredEvent(this.ev100, {required this.continuousMetering}); const MeasuredEvent(this.ev100, {required this.isMetering});
} }
class MeasureErrorEvent extends MeteringEvent { class MeasureErrorEvent extends MeteringEvent {
final bool continuousMetering; final bool isMetering;
const MeasureErrorEvent({required this.continuousMetering}); const MeasureErrorEvent({required this.isMetering});
} }

View file

@ -6,12 +6,10 @@ import 'package:lightmeter/data/light_sensor_service.dart';
import 'package:lightmeter/data/permissions_service.dart'; import 'package:lightmeter/data/permissions_service.dart';
import 'package:lightmeter/data/shared_prefs_service.dart'; import 'package:lightmeter/data/shared_prefs_service.dart';
import 'package:lightmeter/interactors/metering_interactor.dart'; import 'package:lightmeter/interactors/metering_interactor.dart';
import 'package:lightmeter/providers/equipment_profile_provider.dart';
import 'package:lightmeter/screens/metering/bloc_metering.dart'; import 'package:lightmeter/screens/metering/bloc_metering.dart';
import 'package:lightmeter/screens/metering/communication/bloc_communication_metering.dart'; import 'package:lightmeter/screens/metering/communication/bloc_communication_metering.dart';
import 'package:lightmeter/screens/metering/screen_metering.dart'; import 'package:lightmeter/screens/metering/screen_metering.dart';
import 'package:lightmeter/utils/inherited_generics.dart'; import 'package:lightmeter/utils/inherited_generics.dart';
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
class MeteringFlow extends StatefulWidget { class MeteringFlow extends StatefulWidget {
const MeteringFlow({super.key}); const MeteringFlow({super.key});
@ -38,8 +36,6 @@ class _MeteringFlowState extends State<MeteringFlow> {
create: (context) => MeteringBloc( create: (context) => MeteringBloc(
context.read<MeteringCommunicationBloc>(), context.read<MeteringCommunicationBloc>(),
context.get<MeteringInteractor>(), context.get<MeteringInteractor>(),
context.get<EquipmentProfile>(),
context.get<StopType>(),
), ),
), ),
], ],

View file

@ -1,3 +1,5 @@
import 'dart:math';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:lightmeter/data/models/ev_source_type.dart'; import 'package:lightmeter/data/models/ev_source_type.dart';
@ -28,26 +30,30 @@ class MeteringScreen extends StatelessWidget {
children: [ children: [
Expanded( Expanded(
child: BlocBuilder<MeteringBloc, MeteringState>( child: BlocBuilder<MeteringBloc, MeteringState>(
builder: (_, state) => _MeteringContainerBuidler( builder: (_, state) {
fastest: state is MeteringDataState ? state.fastest : null, final exposurePairs = state is MeteringDataState && state.ev != null
slowest: state is MeteringDataState ? state.slowest : null, ? buildExposureValues(context, state.ev!, state.film)
exposurePairs: state is MeteringDataState ? state.exposurePairs : [], : <ExposurePair>[];
film: state.film, return _MeteringContainerBuidler(
iso: state.iso, fastest: exposurePairs.isNotEmpty ? exposurePairs.first : null,
nd: state.nd, slowest: exposurePairs.isNotEmpty ? exposurePairs.last : null,
onFilmChanged: (value) => exposurePairs: exposurePairs,
context.read<MeteringBloc>().add(FilmChangedEvent(value)), film: state.film,
onIsoChanged: (value) => context.read<MeteringBloc>().add(IsoChangedEvent(value)), iso: state.iso,
onNdChanged: (value) => context.read<MeteringBloc>().add(NdChangedEvent(value)), nd: state.nd,
), onFilmChanged: (value) =>
context.read<MeteringBloc>().add(FilmChangedEvent(value)),
onIsoChanged: (value) =>
context.read<MeteringBloc>().add(IsoChangedEvent(value)),
onNdChanged: (value) => context.read<MeteringBloc>().add(NdChangedEvent(value)),
);
},
), ),
), ),
BlocBuilder<MeteringBloc, MeteringState>( BlocBuilder<MeteringBloc, MeteringState>(
builder: (context, state) => MeteringBottomControlsProvider( builder: (context, state) => MeteringBottomControlsProvider(
ev: state is MeteringDataState ? state.ev : null, ev: state is MeteringDataState ? state.ev : null,
isMetering: isMetering: state.isMetering,
state is LoadingState || state is MeteringDataState && state.continuousMetering,
hasError: state is MeteringDataState && state.hasError,
onSwitchEvSourceType: context.get<Environment>().hasLightSensor onSwitchEvSourceType: context.get<Environment>().hasLightSensor
? EvSourceTypeProvider.of(context).toggleType ? EvSourceTypeProvider.of(context).toggleType
: null, : null,
@ -60,6 +66,70 @@ class MeteringScreen extends StatelessWidget {
), ),
); );
} }
List<ExposurePair> buildExposureValues(BuildContext context, double ev, Film film) {
if (ev.isNaN || ev.isInfinite) {
return List.empty();
}
/// Depending on the `stopType` the exposure pairs list length is multiplied by 1,2 or 3
final StopType stopType = context.listen<StopType>();
final int evSteps = (ev * (stopType.index + 1)).round();
final EquipmentProfile equipmentProfile = context.listen<EquipmentProfile>();
final List<ApertureValue> apertureValues =
equipmentProfile.apertureValues.whereStopType(stopType);
final List<ShutterSpeedValue> shutterSpeedValues =
equipmentProfile.shutterSpeedValues.whereStopType(stopType);
/// Basically we use 1" shutter speed as an anchor point for building the exposure pairs list.
/// But user can exclude this value from the list using custom equipment profile.
/// So we have to restore the index of the anchor value.
const ShutterSpeedValue anchorShutterSpeed = ShutterSpeedValue(1, false, StopType.full);
int anchorIndex = shutterSpeedValues.indexOf(anchorShutterSpeed);
if (anchorIndex < 0) {
final filteredFullList = ShutterSpeedValue.values.whereStopType(stopType);
final customListStartIndex = filteredFullList.indexOf(shutterSpeedValues.first);
final fullListAnchor = filteredFullList.indexOf(anchorShutterSpeed);
if (customListStartIndex < fullListAnchor) {
/// This means, that user excluded anchor value at the end,
/// i.e. all shutter speed values are shorter than 1".
anchorIndex = fullListAnchor - customListStartIndex;
} else {
/// In case user excludes anchor value at the start,
/// we can do no adjustment.
}
}
final int evOffset = anchorIndex - evSteps;
late final int apertureOffset;
late final int shutterSpeedOffset;
if (evOffset >= 0) {
apertureOffset = 0;
shutterSpeedOffset = evOffset;
} else {
apertureOffset = -evOffset;
shutterSpeedOffset = 0;
}
final int itemsCount = min(
apertureValues.length + shutterSpeedOffset,
shutterSpeedValues.length + apertureOffset,
) -
max(apertureOffset, shutterSpeedOffset);
if (itemsCount < 0) {
return List.empty();
}
return List.generate(
itemsCount,
(index) => ExposurePair(
apertureValues[index + apertureOffset],
film.reciprocityFailure(shutterSpeedValues[index + shutterSpeedOffset]),
),
growable: false,
);
}
} }
class _InheritedListeners extends StatelessWidget { class _InheritedListeners extends StatelessWidget {
@ -73,17 +143,12 @@ class _InheritedListeners extends StatelessWidget {
onDidChangeDependencies: (value) { onDidChangeDependencies: (value) {
context.read<MeteringBloc>().add(EquipmentProfileChangedEvent(value)); context.read<MeteringBloc>().add(EquipmentProfileChangedEvent(value));
}, },
child: InheritedWidgetListener<StopType>( child: InheritedModelAspectListener<MeteringScreenLayoutFeature, bool>(
aspect: MeteringScreenLayoutFeature.filmPicker,
onDidChangeDependencies: (value) { onDidChangeDependencies: (value) {
context.read<MeteringBloc>().add(StopTypeChangedEvent(value)); if (!value) context.read<MeteringBloc>().add(const FilmChangedEvent(Film.other()));
}, },
child: InheritedModelAspectListener<MeteringScreenLayoutFeature, bool>( child: child,
aspect: MeteringScreenLayoutFeature.filmPicker,
onDidChangeDependencies: (value) {
if (!value) context.read<MeteringBloc>().add(const FilmChangedEvent(Film.other()));
},
child: child,
),
), ),
); );
} }

View file

@ -1,18 +1,21 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:lightmeter/data/models/exposure_pair.dart';
import 'package:lightmeter/data/models/film.dart'; import 'package:lightmeter/data/models/film.dart';
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
@immutable @immutable
abstract class MeteringState { abstract class MeteringState {
final double? ev100;
final Film film; final Film film;
final IsoValue iso; final IsoValue iso;
final NdValue nd; final NdValue nd;
final bool isMetering;
const MeteringState({ const MeteringState({
this.ev100,
required this.film, required this.film,
required this.iso, required this.iso,
required this.nd, required this.nd,
required this.isMetering,
}); });
} }
@ -21,24 +24,18 @@ class LoadingState extends MeteringState {
required super.film, required super.film,
required super.iso, required super.iso,
required super.nd, required super.nd,
}); }) : super(isMetering: true);
} }
class MeteringDataState extends MeteringState { class MeteringDataState extends MeteringState {
final double? ev;
final List<ExposurePair> exposurePairs;
final bool continuousMetering;
const MeteringDataState({ const MeteringDataState({
required this.ev, required super.ev100,
required super.film, required super.film,
required super.iso, required super.iso,
required super.nd, required super.nd,
required this.exposurePairs, required super.isMetering,
required this.continuousMetering,
}); });
ExposurePair? get fastest => exposurePairs.isEmpty ? null : exposurePairs.first; double? get ev => ev100 != null ? ev100! + log2(iso.value / 100) - nd.stopReduction : null;
ExposurePair? get slowest => exposurePairs.isEmpty ? null : exposurePairs.last;
bool get hasError => ev == null; bool get hasError => ev == null;
} }