2022-12-14 17:33:38 +00:00
|
|
|
import 'dart:async';
|
2022-10-29 18:02:45 +00:00
|
|
|
import 'dart:math';
|
|
|
|
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2022-12-16 08:08:12 +00:00
|
|
|
import 'package:lightmeter/data/models/exposure_pair.dart';
|
2023-04-01 19:04:55 +00:00
|
|
|
import 'package:lightmeter/data/models/film.dart';
|
2023-01-26 09:10:23 +00:00
|
|
|
import 'package:lightmeter/interactors/metering_interactor.dart';
|
2023-05-11 13:30:18 +00:00
|
|
|
import 'package:lightmeter/screens/metering/communication/bloc_communication_metering.dart';
|
2023-01-26 15:03:48 +00:00
|
|
|
import 'package:lightmeter/screens/metering/communication/event_communication_metering.dart'
|
|
|
|
as communication_events;
|
|
|
|
import 'package:lightmeter/screens/metering/communication/state_communication_metering.dart'
|
|
|
|
as communication_states;
|
2023-05-11 13:30:18 +00:00
|
|
|
import 'package:lightmeter/screens/metering/event_metering.dart';
|
|
|
|
import 'package:lightmeter/screens/metering/state_metering.dart';
|
2023-03-30 19:24:18 +00:00
|
|
|
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
|
2022-10-29 18:02:45 +00:00
|
|
|
|
|
|
|
class MeteringBloc extends Bloc<MeteringEvent, MeteringState> {
|
2022-12-14 17:33:38 +00:00
|
|
|
final MeteringCommunicationBloc _communicationBloc;
|
2023-01-26 09:10:23 +00:00
|
|
|
final MeteringInteractor _meteringInteractor;
|
2022-12-14 17:33:38 +00:00
|
|
|
late final StreamSubscription<communication_states.ScreenState> _communicationSubscription;
|
|
|
|
|
2023-03-30 19:24:18 +00:00
|
|
|
List<ApertureValue> get _apertureValues =>
|
|
|
|
_equipmentProfileData.apertureValues.whereStopType(stopType);
|
|
|
|
List<ShutterSpeedValue> get _shutterSpeedValues =>
|
|
|
|
_equipmentProfileData.shutterSpeedValues.whereStopType(stopType);
|
2022-10-29 18:02:45 +00:00
|
|
|
|
2023-03-30 19:24:18 +00:00
|
|
|
EquipmentProfileData _equipmentProfileData;
|
2022-12-11 14:04:08 +00:00
|
|
|
StopType stopType;
|
|
|
|
|
2023-05-07 10:37:01 +00:00
|
|
|
late IsoValue _iso = _meteringInteractor.iso;
|
|
|
|
late NdValue _nd = _meteringInteractor.ndFilter;
|
|
|
|
late Film _film = _meteringInteractor.film;
|
2023-05-05 21:08:20 +00:00
|
|
|
double? _ev100 = 0.0;
|
2023-02-19 10:26:14 +00:00
|
|
|
bool _isMeteringInProgress = false;
|
|
|
|
|
2022-12-16 08:08:12 +00:00
|
|
|
MeteringBloc(
|
|
|
|
this._communicationBloc,
|
2023-01-26 09:10:23 +00:00
|
|
|
this._meteringInteractor,
|
2023-03-30 19:24:18 +00:00
|
|
|
this._equipmentProfileData,
|
2022-12-16 08:08:12 +00:00
|
|
|
this.stopType,
|
|
|
|
) : super(
|
2023-05-05 21:08:20 +00:00
|
|
|
MeteringDataState(
|
2022-12-15 11:00:28 +00:00
|
|
|
ev: 0.0,
|
2023-05-07 10:37:01 +00:00
|
|
|
film: _meteringInteractor.film,
|
|
|
|
iso: _meteringInteractor.iso,
|
|
|
|
nd: _meteringInteractor.ndFilter,
|
2023-04-01 19:04:55 +00:00
|
|
|
exposurePairs: const [],
|
2023-05-05 21:08:20 +00:00
|
|
|
continuousMetering: false,
|
2022-10-29 18:02:45 +00:00
|
|
|
),
|
|
|
|
) {
|
2022-12-14 17:33:38 +00:00
|
|
|
_communicationSubscription = _communicationBloc.stream
|
|
|
|
.where((state) => state is communication_states.ScreenState)
|
|
|
|
.map((state) => state as communication_states.ScreenState)
|
|
|
|
.listen(_onCommunicationState);
|
|
|
|
|
2023-03-30 19:24:18 +00:00
|
|
|
on<EquipmentProfileChangedEvent>(_onEquipmentProfileChanged);
|
2022-12-11 14:04:08 +00:00
|
|
|
on<StopTypeChangedEvent>(_onStopTypeChanged);
|
2023-04-01 19:04:55 +00:00
|
|
|
on<FilmChangedEvent>(_onFilmChanged);
|
2022-12-04 19:00:43 +00:00
|
|
|
on<IsoChangedEvent>(_onIsoChanged);
|
2022-12-04 19:12:52 +00:00
|
|
|
on<NdChangedEvent>(_onNdChanged);
|
2023-01-21 10:37:49 +00:00
|
|
|
on<MeasureEvent>(_onMeasure);
|
2022-12-14 17:33:38 +00:00
|
|
|
on<MeasuredEvent>(_onMeasured);
|
2023-05-05 21:08:20 +00:00
|
|
|
on<MeasureErrorEvent>(_onMeasureError);
|
2022-10-29 18:02:45 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 17:33:38 +00:00
|
|
|
@override
|
|
|
|
Future<void> close() async {
|
|
|
|
await _communicationSubscription.cancel();
|
|
|
|
return super.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onCommunicationState(communication_states.ScreenState communicationState) {
|
|
|
|
if (communicationState is communication_states.MeasuredState) {
|
2023-02-19 10:26:14 +00:00
|
|
|
_isMeteringInProgress = communicationState is communication_states.MeteringInProgressState;
|
2023-05-05 21:08:20 +00:00
|
|
|
_handleEv100(communicationState.ev100);
|
2022-12-14 17:33:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-11 14:04:08 +00:00
|
|
|
void _onStopTypeChanged(StopTypeChangedEvent event, Emitter emit) {
|
|
|
|
stopType = event.stopType;
|
2023-05-05 21:08:20 +00:00
|
|
|
_updateMeasurements();
|
2022-12-11 14:04:08 +00:00
|
|
|
}
|
|
|
|
|
2023-03-30 19:24:18 +00:00
|
|
|
void _onEquipmentProfileChanged(EquipmentProfileChangedEvent event, Emitter emit) {
|
|
|
|
_equipmentProfileData = event.equipmentProfileData;
|
|
|
|
|
|
|
|
/// Update selected ISO value, if selected equipment profile
|
|
|
|
/// doesn't contain currently selected value
|
|
|
|
if (!event.equipmentProfileData.isoValues.any((v) => _iso.value == v.value)) {
|
2023-05-07 10:37:01 +00:00
|
|
|
_meteringInteractor.iso = event.equipmentProfileData.isoValues.first;
|
2023-03-30 19:24:18 +00:00
|
|
|
_iso = event.equipmentProfileData.isoValues.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The same for ND filter
|
|
|
|
if (!event.equipmentProfileData.ndValues.any((v) => _nd.value == v.value)) {
|
2023-05-07 10:37:01 +00:00
|
|
|
_meteringInteractor.ndFilter = event.equipmentProfileData.ndValues.first;
|
2023-03-30 19:24:18 +00:00
|
|
|
_nd = event.equipmentProfileData.ndValues.first;
|
|
|
|
}
|
|
|
|
|
2023-05-05 21:08:20 +00:00
|
|
|
_updateMeasurements();
|
2023-03-30 19:24:18 +00:00
|
|
|
}
|
|
|
|
|
2023-04-01 19:04:55 +00:00
|
|
|
void _onFilmChanged(FilmChangedEvent event, Emitter emit) {
|
|
|
|
if (_iso.value != event.data.iso) {
|
2023-04-05 19:15:11 +00:00
|
|
|
final newIso = IsoValue.values.firstWhere(
|
2023-04-01 19:04:55 +00:00
|
|
|
(e) => e.value == event.data.iso,
|
|
|
|
orElse: () => _iso,
|
|
|
|
);
|
|
|
|
add(IsoChangedEvent(newIso));
|
|
|
|
}
|
|
|
|
_film = event.data;
|
2023-05-07 10:37:01 +00:00
|
|
|
_meteringInteractor.film = event.data;
|
2023-05-05 21:08:20 +00:00
|
|
|
_updateMeasurements();
|
2023-04-01 19:04:55 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 19:00:43 +00:00
|
|
|
void _onIsoChanged(IsoChangedEvent event, Emitter emit) {
|
2023-04-01 19:04:55 +00:00
|
|
|
if (event.isoValue.value != _film.iso) {
|
|
|
|
_film = Film.values.first;
|
|
|
|
}
|
2023-05-07 10:37:01 +00:00
|
|
|
_meteringInteractor.iso = event.isoValue;
|
2023-02-19 10:26:14 +00:00
|
|
|
_iso = event.isoValue;
|
2023-05-05 21:08:20 +00:00
|
|
|
_updateMeasurements();
|
2022-12-04 19:00:43 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 19:12:52 +00:00
|
|
|
void _onNdChanged(NdChangedEvent event, Emitter emit) {
|
2023-05-07 10:37:01 +00:00
|
|
|
_meteringInteractor.ndFilter = event.ndValue;
|
2023-02-19 10:26:14 +00:00
|
|
|
_nd = event.ndValue;
|
2023-05-05 21:08:20 +00:00
|
|
|
_updateMeasurements();
|
2022-12-04 19:12:52 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 21:08:20 +00:00
|
|
|
void _onMeasure(MeasureEvent _, Emitter emit) {
|
2023-01-26 09:10:23 +00:00
|
|
|
_meteringInteractor.quickVibration();
|
2023-01-21 10:37:49 +00:00
|
|
|
_communicationBloc.add(const communication_events.MeasureEvent());
|
2023-02-19 10:26:14 +00:00
|
|
|
_isMeteringInProgress = true;
|
2023-05-16 07:42:48 +00:00
|
|
|
emit(
|
|
|
|
LoadingState(
|
|
|
|
film: _film,
|
|
|
|
iso: _iso,
|
|
|
|
nd: _nd,
|
|
|
|
),
|
|
|
|
);
|
2023-01-21 10:37:49 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 21:08:20 +00:00
|
|
|
void _updateMeasurements() => _handleEv100(_ev100);
|
|
|
|
|
|
|
|
void _handleEv100(double? ev100) {
|
|
|
|
if (ev100 == null || ev100.isNaN || ev100.isInfinite) {
|
|
|
|
add(const MeasureErrorEvent());
|
|
|
|
} else {
|
|
|
|
add(MeasuredEvent(ev100));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-14 17:33:38 +00:00
|
|
|
void _onMeasured(MeasuredEvent event, Emitter emit) {
|
2023-01-26 09:10:23 +00:00
|
|
|
_meteringInteractor.responseVibration();
|
2023-05-05 21:32:17 +00:00
|
|
|
_ev100 = event.ev100;
|
2023-05-05 21:08:20 +00:00
|
|
|
final ev = event.ev100 + log2(_iso.value / 100) - _nd.stopReduction;
|
2023-05-16 07:42:48 +00:00
|
|
|
emit(
|
|
|
|
MeteringDataState(
|
|
|
|
ev: ev,
|
|
|
|
film: _film,
|
|
|
|
iso: _iso,
|
|
|
|
nd: _nd,
|
|
|
|
exposurePairs: _buildExposureValues(ev),
|
|
|
|
continuousMetering: _isMeteringInProgress,
|
|
|
|
),
|
|
|
|
);
|
2023-02-19 10:26:14 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 21:08:20 +00:00
|
|
|
void _onMeasureError(MeasureErrorEvent _, Emitter emit) {
|
|
|
|
_meteringInteractor.errorVibration();
|
|
|
|
_ev100 = null;
|
2023-05-16 07:42:48 +00:00
|
|
|
emit(
|
|
|
|
MeteringDataState(
|
|
|
|
ev: null,
|
|
|
|
film: _film,
|
|
|
|
iso: _iso,
|
|
|
|
nd: _nd,
|
|
|
|
exposurePairs: const [],
|
|
|
|
continuousMetering: _isMeteringInProgress,
|
|
|
|
),
|
|
|
|
);
|
2022-10-29 18:02:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
List<ExposurePair> _buildExposureValues(double ev) {
|
2023-05-04 10:49:26 +00:00
|
|
|
if (ev.isNaN || ev.isInfinite) {
|
2023-05-11 13:30:18 +00:00
|
|
|
return List.empty();
|
2023-05-04 10:49:26 +00:00
|
|
|
}
|
|
|
|
|
2023-02-18 17:17:39 +00:00
|
|
|
/// Depending on the `stopType` the exposure pairs list length is multiplied by 1,2 or 3
|
|
|
|
final int evSteps = (ev * (stopType.index + 1)).round();
|
2023-03-30 19:24:18 +00:00
|
|
|
|
|
|
|
/// 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) {
|
2023-04-05 19:15:11 +00:00
|
|
|
final filteredFullList = ShutterSpeedValue.values.whereStopType(stopType);
|
2023-03-30 19:24:18 +00:00
|
|
|
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;
|
2022-10-29 18:02:45 +00:00
|
|
|
|
|
|
|
late final int apertureOffset;
|
|
|
|
late final int shutterSpeedOffset;
|
|
|
|
if (evOffset >= 0) {
|
|
|
|
apertureOffset = 0;
|
|
|
|
shutterSpeedOffset = evOffset;
|
|
|
|
} else {
|
|
|
|
apertureOffset = -evOffset;
|
|
|
|
shutterSpeedOffset = 0;
|
|
|
|
}
|
|
|
|
|
2023-05-11 13:30:18 +00:00
|
|
|
final int itemsCount = min(
|
|
|
|
_apertureValues.length + shutterSpeedOffset,
|
|
|
|
_shutterSpeedValues.length + apertureOffset,
|
|
|
|
) -
|
2022-10-29 18:02:45 +00:00
|
|
|
max(apertureOffset, shutterSpeedOffset);
|
|
|
|
|
2022-10-29 18:04:06 +00:00
|
|
|
if (itemsCount < 0) {
|
2023-05-11 13:30:18 +00:00
|
|
|
return List.empty();
|
2022-10-29 18:04:06 +00:00
|
|
|
}
|
2022-10-29 18:02:45 +00:00
|
|
|
return List.generate(
|
|
|
|
itemsCount,
|
|
|
|
(index) => ExposurePair(
|
|
|
|
_apertureValues[index + apertureOffset],
|
2023-04-01 19:04:55 +00:00
|
|
|
_film.reciprocityFailure(_shutterSpeedValues[index + shutterSpeedOffset]),
|
2022-10-29 18:02:45 +00:00
|
|
|
),
|
|
|
|
growable: false,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|