2022-12-14 17:33:38 +00:00
|
|
|
import 'dart:async';
|
2022-10-29 18:02:45 +00:00
|
|
|
|
2023-06-20 06:43:49 +00:00
|
|
|
import 'package:bloc_concurrency/bloc_concurrency.dart';
|
|
|
|
import 'package:flutter/foundation.dart';
|
2022-10-29 18:02:45 +00:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2023-07-09 11:39:33 +00:00
|
|
|
import 'package:lightmeter/data/models/volume_action.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-07-09 11:39:33 +00:00
|
|
|
import 'package:lightmeter/screens/metering/components/shared/volume_keys_notifier/notifier_volume_keys.dart';
|
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> {
|
2023-01-26 09:10:23 +00:00
|
|
|
final MeteringInteractor _meteringInteractor;
|
2023-07-09 11:39:33 +00:00
|
|
|
final VolumeKeysNotifier _volumeKeysNotifier;
|
2023-06-20 06:43:49 +00:00
|
|
|
final MeteringCommunicationBloc _communicationBloc;
|
2022-12-14 17:33:38 +00:00
|
|
|
late final StreamSubscription<communication_states.ScreenState> _communicationSubscription;
|
|
|
|
|
2022-12-16 08:08:12 +00:00
|
|
|
MeteringBloc(
|
2023-01-26 09:10:23 +00:00
|
|
|
this._meteringInteractor,
|
2023-07-09 11:39:33 +00:00
|
|
|
this._volumeKeysNotifier,
|
2023-06-20 06:43:49 +00:00
|
|
|
this._communicationBloc,
|
2022-12-16 08:08:12 +00:00
|
|
|
) : super(
|
2023-05-16 09:47:53 +00:00
|
|
|
MeteringDataState(
|
2023-06-20 06:43:49 +00:00
|
|
|
ev100: null,
|
2023-05-16 09:47:53 +00:00
|
|
|
iso: _meteringInteractor.iso,
|
|
|
|
nd: _meteringInteractor.ndFilter,
|
2023-06-20 06:43:49 +00:00
|
|
|
isMetering: false,
|
2022-10-29 18:02:45 +00:00
|
|
|
),
|
|
|
|
) {
|
2023-07-09 11:39:33 +00:00
|
|
|
_volumeKeysNotifier.addListener(onVolumeKey);
|
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)
|
2023-06-20 06:43:49 +00:00
|
|
|
.listen(onCommunicationState);
|
2022-12-14 17:33:38 +00:00
|
|
|
|
2023-03-30 19:24:18 +00:00
|
|
|
on<EquipmentProfileChangedEvent>(_onEquipmentProfileChanged);
|
2022-12-04 19:00:43 +00:00
|
|
|
on<IsoChangedEvent>(_onIsoChanged);
|
2022-12-04 19:12:52 +00:00
|
|
|
on<NdChangedEvent>(_onNdChanged);
|
2023-06-20 06:43:49 +00:00
|
|
|
on<MeasureEvent>(_onMeasure, transformer: droppable());
|
2022-12-14 17:33:38 +00:00
|
|
|
on<MeasuredEvent>(_onMeasured);
|
2023-05-16 09:47:53 +00:00
|
|
|
on<MeasureErrorEvent>(_onMeasureError);
|
2023-07-09 11:39:33 +00:00
|
|
|
on<SettingsOpenedEvent>(_onSettingsOpened);
|
|
|
|
on<SettingsClosedEvent>(_onSettingsClosed);
|
2022-10-29 18:02:45 +00:00
|
|
|
}
|
|
|
|
|
2023-06-20 06:43:49 +00:00
|
|
|
@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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-14 17:33:38 +00:00
|
|
|
@override
|
|
|
|
Future<void> close() async {
|
2023-07-09 11:39:33 +00:00
|
|
|
_volumeKeysNotifier.removeListener(onVolumeKey);
|
2022-12-14 17:33:38 +00:00
|
|
|
await _communicationSubscription.cancel();
|
|
|
|
return super.close();
|
|
|
|
}
|
|
|
|
|
2023-06-20 06:43:49 +00:00
|
|
|
@visibleForTesting
|
|
|
|
void onCommunicationState(communication_states.ScreenState communicationState) {
|
2022-12-14 17:33:38 +00:00
|
|
|
if (communicationState is communication_states.MeasuredState) {
|
2023-06-20 06:43:49 +00:00
|
|
|
_handleEv100(
|
|
|
|
communicationState.ev100,
|
|
|
|
isMetering: communicationState is communication_states.MeteringInProgressState,
|
|
|
|
);
|
2023-06-04 11:04:04 +00:00
|
|
|
}
|
2022-12-11 14:04:08 +00:00
|
|
|
}
|
|
|
|
|
2023-03-30 19:24:18 +00:00
|
|
|
void _onEquipmentProfileChanged(EquipmentProfileChangedEvent event, Emitter emit) {
|
2023-06-04 11:04:04 +00:00
|
|
|
bool willUpdateMeasurements = false;
|
2023-03-30 19:24:18 +00:00
|
|
|
|
2023-06-20 06:43:49 +00:00
|
|
|
/// Update selected ISO value and discard selected film, if selected equipment profile
|
2023-03-30 19:24:18 +00:00
|
|
|
/// doesn't contain currently selected value
|
2023-06-20 06:43:49 +00:00
|
|
|
IsoValue iso = state.iso;
|
|
|
|
if (!event.equipmentProfileData.isoValues.any((v) => state.iso.value == v.value)) {
|
2023-05-16 09:47:53 +00:00
|
|
|
_meteringInteractor.iso = event.equipmentProfileData.isoValues.first;
|
2023-06-20 06:43:49 +00:00
|
|
|
iso = event.equipmentProfileData.isoValues.first;
|
|
|
|
willUpdateMeasurements = true;
|
2023-03-30 19:24:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The same for ND filter
|
2023-06-20 06:43:49 +00:00
|
|
|
NdValue nd = state.nd;
|
|
|
|
if (!event.equipmentProfileData.ndValues.any((v) => state.nd.value == v.value)) {
|
2023-05-16 09:47:53 +00:00
|
|
|
_meteringInteractor.ndFilter = event.equipmentProfileData.ndValues.first;
|
2023-06-20 06:43:49 +00:00
|
|
|
nd = event.equipmentProfileData.ndValues.first;
|
|
|
|
willUpdateMeasurements = true;
|
2023-03-30 19:24:18 +00:00
|
|
|
}
|
|
|
|
|
2023-06-04 11:04:04 +00:00
|
|
|
if (willUpdateMeasurements) {
|
2023-06-20 06:43:49 +00:00
|
|
|
emit(
|
|
|
|
MeteringDataState(
|
|
|
|
ev100: state.ev100,
|
|
|
|
iso: iso,
|
|
|
|
nd: nd,
|
|
|
|
isMetering: state.isMetering,
|
|
|
|
),
|
|
|
|
);
|
2023-06-04 11:04:04 +00:00
|
|
|
}
|
2023-03-30 19:24:18 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 19:00:43 +00:00
|
|
|
void _onIsoChanged(IsoChangedEvent event, Emitter emit) {
|
2023-06-20 06:43:49 +00:00
|
|
|
if (state.iso != event.isoValue) {
|
2023-06-04 11:04:04 +00:00
|
|
|
_meteringInteractor.iso = event.isoValue;
|
2023-06-20 06:43:49 +00:00
|
|
|
emit(
|
|
|
|
MeteringDataState(
|
|
|
|
ev100: state.ev100,
|
|
|
|
iso: event.isoValue,
|
|
|
|
nd: state.nd,
|
|
|
|
isMetering: state.isMetering,
|
|
|
|
),
|
|
|
|
);
|
2023-04-01 19:04:55 +00:00
|
|
|
}
|
2022-12-04 19:00:43 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 19:12:52 +00:00
|
|
|
void _onNdChanged(NdChangedEvent event, Emitter emit) {
|
2023-06-20 06:43:49 +00:00
|
|
|
if (state.nd != event.ndValue) {
|
2023-06-04 11:04:04 +00:00
|
|
|
_meteringInteractor.ndFilter = event.ndValue;
|
2023-06-20 06:43:49 +00:00
|
|
|
emit(
|
|
|
|
MeteringDataState(
|
|
|
|
ev100: state.ev100,
|
|
|
|
iso: state.iso,
|
|
|
|
nd: event.ndValue,
|
|
|
|
isMetering: state.isMetering,
|
|
|
|
),
|
|
|
|
);
|
2023-06-04 11:04:04 +00:00
|
|
|
}
|
2022-12-04 19:12:52 +00:00
|
|
|
}
|
|
|
|
|
2023-05-16 09:47:53 +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-05-16 09:47:53 +00:00
|
|
|
emit(
|
|
|
|
LoadingState(
|
2023-06-20 06:43:49 +00:00
|
|
|
iso: state.iso,
|
|
|
|
nd: state.nd,
|
2023-05-16 09:47:53 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-20 06:43:49 +00:00
|
|
|
void _handleEv100(double? ev100, {required bool isMetering}) {
|
2023-05-16 09:47:53 +00:00
|
|
|
if (ev100 == null || ev100.isNaN || ev100.isInfinite) {
|
2023-06-20 06:43:49 +00:00
|
|
|
add(MeasureErrorEvent(isMetering: isMetering));
|
2023-05-16 09:47:53 +00:00
|
|
|
} else {
|
2023-06-20 06:43:49 +00:00
|
|
|
add(MeasuredEvent(ev100, isMetering: isMetering));
|
2023-05-16 09:47:53 +00:00
|
|
|
}
|
2023-01-21 10:37:49 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 17:33:38 +00:00
|
|
|
void _onMeasured(MeasuredEvent event, Emitter emit) {
|
2023-05-16 09:47:53 +00:00
|
|
|
emit(
|
|
|
|
MeteringDataState(
|
2023-06-20 06:43:49 +00:00
|
|
|
ev100: event.ev100,
|
|
|
|
iso: state.iso,
|
|
|
|
nd: state.nd,
|
|
|
|
isMetering: event.isMetering,
|
2023-05-16 09:47:53 +00:00
|
|
|
),
|
|
|
|
);
|
2023-02-19 10:26:14 +00:00
|
|
|
}
|
|
|
|
|
2023-06-20 06:43:49 +00:00
|
|
|
void _onMeasureError(MeasureErrorEvent event, Emitter emit) {
|
2023-05-11 13:30:18 +00:00
|
|
|
emit(
|
2023-05-16 09:47:53 +00:00
|
|
|
MeteringDataState(
|
2023-06-20 06:43:49 +00:00
|
|
|
ev100: null,
|
|
|
|
iso: state.iso,
|
|
|
|
nd: state.nd,
|
|
|
|
isMetering: event.isMetering,
|
2022-10-29 18:02:45 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2023-07-09 11:39:33 +00:00
|
|
|
|
|
|
|
@visibleForTesting
|
|
|
|
void onVolumeKey() {
|
|
|
|
if (_meteringInteractor.volumeAction == VolumeAction.shutter) {
|
|
|
|
add(const MeasureEvent());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onSettingsOpened(SettingsOpenedEvent _, Emitter __) {
|
|
|
|
_communicationBloc.add(const communication_events.SettingsOpenedEvent());
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onSettingsClosed(SettingsClosedEvent _, Emitter __) {
|
|
|
|
_communicationBloc.add(const communication_events.SettingsClosedEvent());
|
|
|
|
}
|
2022-10-29 18:02:45 +00:00
|
|
|
}
|