m3_lightmeter/lib/screens/metering/bloc_metering.dart

158 lines
5.3 KiB
Dart
Raw Permalink Normal View History

import 'dart:async';
2022-10-29 18:02:45 +00:00
import 'dart:math';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:lightmeter/data/models/photography_values/aperture_value.dart';
2022-12-16 08:08:12 +00:00
import 'package:lightmeter/data/models/exposure_pair.dart';
2023-02-19 10:24:42 +00:00
import 'package:lightmeter/data/models/photography_values/iso_value.dart';
import 'package:lightmeter/data/models/photography_values/nd_value.dart';
import 'package:lightmeter/data/models/photography_values/photography_value.dart';
import 'package:lightmeter/data/models/photography_values/shutter_speed_value.dart';
2022-12-16 08:08:12 +00:00
import 'package:lightmeter/data/shared_prefs_service.dart';
import 'package:lightmeter/interactors/metering_interactor.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;
2022-12-04 19:00:43 +00:00
import 'package:lightmeter/utils/log_2.dart';
2022-10-29 18:02:45 +00:00
import 'communication/bloc_communication_metering.dart';
import 'event_metering.dart';
import 'state_metering.dart';
2022-10-29 18:02:45 +00:00
class MeteringBloc extends Bloc<MeteringEvent, MeteringState> {
final MeteringCommunicationBloc _communicationBloc;
2022-12-16 08:08:12 +00:00
final UserPreferencesService _userPreferencesService;
final MeteringInteractor _meteringInteractor;
late final StreamSubscription<communication_states.ScreenState> _communicationSubscription;
List<ApertureValue> get _apertureValues => apertureValues.whereStopType(stopType);
List<ShutterSpeedValue> get _shutterSpeedValues => shutterSpeedValues.whereStopType(stopType);
2022-10-29 18:02:45 +00:00
StopType stopType;
2023-02-19 10:24:42 +00:00
late IsoValue _iso = _userPreferencesService.iso;
late NdValue _nd = _userPreferencesService.ndFilter;
double _ev = 0.0;
bool _isMeteringInProgress = false;
2022-12-16 08:08:12 +00:00
MeteringBloc(
this._communicationBloc,
this._userPreferencesService,
this._meteringInteractor,
2022-12-16 08:08:12 +00:00
this.stopType,
) : super(
2023-02-19 10:24:42 +00:00
MeteringEndedState(
2022-12-16 08:08:12 +00:00
iso: _userPreferencesService.iso,
2022-12-15 11:00:28 +00:00
ev: 0.0,
2022-12-16 08:08:12 +00:00
nd: _userPreferencesService.ndFilter,
2022-10-29 18:02:45 +00:00
exposurePairs: [],
),
) {
_communicationSubscription = _communicationBloc.stream
.where((state) => state is communication_states.ScreenState)
.map((state) => state as communication_states.ScreenState)
.listen(_onCommunicationState);
on<StopTypeChangedEvent>(_onStopTypeChanged);
2022-12-04 19:00:43 +00:00
on<IsoChangedEvent>(_onIsoChanged);
on<NdChangedEvent>(_onNdChanged);
on<MeasureEvent>(_onMeasure);
on<MeasuredEvent>(_onMeasured);
2022-10-29 18:02:45 +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:24:42 +00:00
_isMeteringInProgress = communicationState is communication_states.MeteringInProgressState;
add(MeasuredEvent(communicationState.ev100));
}
}
void _onStopTypeChanged(StopTypeChangedEvent event, Emitter emit) {
stopType = event.stopType;
2023-02-19 10:24:42 +00:00
_emitMeasuredState(emit);
}
2022-12-04 19:00:43 +00:00
void _onIsoChanged(IsoChangedEvent event, Emitter emit) {
2022-12-16 08:08:12 +00:00
_userPreferencesService.iso = event.isoValue;
2023-02-19 10:24:42 +00:00
_ev = _ev + log2(event.isoValue.value / _iso.value);
_iso = event.isoValue;
_emitMeasuredState(emit);
2022-12-04 19:00:43 +00:00
}
void _onNdChanged(NdChangedEvent event, Emitter emit) {
2022-12-16 08:08:12 +00:00
_userPreferencesService.ndFilter = event.ndValue;
2023-02-19 10:24:42 +00:00
_ev = _ev - event.ndValue.stopReduction + _nd.stopReduction;
_nd = event.ndValue;
_emitMeasuredState(emit);
}
2023-02-19 10:24:42 +00:00
void _onMeasure(_, Emitter emit) {
_meteringInteractor.quickVibration();
_communicationBloc.add(const communication_events.MeasureEvent());
2023-02-19 10:24:42 +00:00
_isMeteringInProgress = true;
emit(const LoadingState());
}
void _onMeasured(MeasuredEvent event, Emitter emit) {
_meteringInteractor.responseVibration();
2023-02-19 10:24:42 +00:00
_ev = event.ev100 + log2(_iso.value / 100);
_emitMeasuredState(emit);
}
void _emitMeasuredState(Emitter emit) {
emit(_isMeteringInProgress
? MeteringInProgressState(
iso: _iso,
ev: _ev,
nd: _nd,
exposurePairs: _buildExposureValues(_ev),
)
: MeteringEndedState(
iso: _iso,
ev: _ev,
nd: _nd,
exposurePairs: _buildExposureValues(_ev),
));
2022-10-29 18:02:45 +00:00
}
List<ExposurePair> _buildExposureValues(double ev) {
/// Depending on the `stopType` the exposure pairs list length is multiplied by 1,2 or 3
final int evSteps = (ev * (stopType.index + 1)).round();
final int evOffset =
2023-01-26 15:03:48 +00:00
_shutterSpeedValues.indexOf(const ShutterSpeedValue(1, false, StopType.full)) - 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;
}
final int itemsCount = min(_apertureValues.length + shutterSpeedOffset,
2023-01-26 15:03:48 +00:00
_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) {
return List.empty(growable: false);
}
2022-10-29 18:02:45 +00:00
return List.generate(
itemsCount,
(index) => ExposurePair(
_apertureValues[index + apertureOffset],
_shutterSpeedValues[index + shutterSpeedOffset],
),
growable: false,
);
}
}