2023-06-20 06:43:49 +00:00
|
|
|
import 'dart:math';
|
|
|
|
|
2022-10-24 20:25:38 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2022-10-29 18:02:45 +00:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2023-01-29 16:57:47 +00:00
|
|
|
import 'package:lightmeter/data/models/ev_source_type.dart';
|
2023-02-19 10:26:14 +00:00
|
|
|
import 'package:lightmeter/data/models/exposure_pair.dart';
|
2023-10-20 14:12:43 +00:00
|
|
|
import 'package:lightmeter/providers/equipment_profile_provider.dart';
|
2023-08-14 10:25:37 +00:00
|
|
|
import 'package:lightmeter/providers/services_provider.dart';
|
|
|
|
import 'package:lightmeter/providers/user_preferences_provider.dart';
|
2023-05-11 13:30:18 +00:00
|
|
|
import 'package:lightmeter/screens/metering/bloc_metering.dart';
|
|
|
|
import 'package:lightmeter/screens/metering/components/bottom_controls/provider_bottom_controls.dart';
|
|
|
|
import 'package:lightmeter/screens/metering/components/camera_container/provider_container_camera.dart';
|
|
|
|
import 'package:lightmeter/screens/metering/components/light_sensor_container/provider_container_light_sensor.dart';
|
|
|
|
import 'package:lightmeter/screens/metering/event_metering.dart';
|
|
|
|
import 'package:lightmeter/screens/metering/state_metering.dart';
|
2023-11-02 16:40:47 +00:00
|
|
|
import 'package:lightmeter/screens/metering/utils/listener_equipment_profiles.dart';
|
2023-03-30 19:24:18 +00:00
|
|
|
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
|
2022-10-24 20:25:38 +00:00
|
|
|
|
2023-06-04 11:04:04 +00:00
|
|
|
class MeteringScreen extends StatelessWidget {
|
2022-12-11 14:09:10 +00:00
|
|
|
const MeteringScreen({super.key});
|
2022-10-24 20:25:38 +00:00
|
|
|
|
2022-11-26 18:14:51 +00:00
|
|
|
@override
|
2023-06-04 11:04:04 +00:00
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return _InheritedListeners(
|
|
|
|
child: Scaffold(
|
|
|
|
backgroundColor: Theme.of(context).colorScheme.background,
|
|
|
|
body: Column(
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
child: BlocBuilder<MeteringBloc, MeteringState>(
|
2023-09-02 08:32:08 +00:00
|
|
|
builder: (_, state) => MeteringContainerBuidler(
|
2023-06-20 06:43:49 +00:00
|
|
|
ev: state is MeteringDataState ? state.ev : null,
|
2023-06-04 11:04:04 +00:00
|
|
|
iso: state.iso,
|
|
|
|
nd: state.nd,
|
|
|
|
onIsoChanged: (value) => context.read<MeteringBloc>().add(IsoChangedEvent(value)),
|
|
|
|
onNdChanged: (value) => context.read<MeteringBloc>().add(NdChangedEvent(value)),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
BlocBuilder<MeteringBloc, MeteringState>(
|
|
|
|
builder: (context, state) => MeteringBottomControlsProvider(
|
|
|
|
ev: state is MeteringDataState ? state.ev : null,
|
2023-06-20 06:43:49 +00:00
|
|
|
isMetering: state.isMetering,
|
2023-08-14 10:25:37 +00:00
|
|
|
onSwitchEvSourceType: ServicesProvider.of(context).environment.hasLightSensor
|
|
|
|
? UserPreferencesProvider.of(context).toggleEvSourceType
|
2023-06-04 11:04:04 +00:00
|
|
|
: null,
|
|
|
|
onMeasure: () => context.read<MeteringBloc>().add(const MeasureEvent()),
|
2023-07-09 11:39:33 +00:00
|
|
|
onSettings: () {
|
|
|
|
context.read<MeteringBloc>().add(const SettingsOpenedEvent());
|
|
|
|
Navigator.pushNamed(context, 'settings').then((value) {
|
|
|
|
context.read<MeteringBloc>().add(const SettingsClosedEvent());
|
|
|
|
});
|
|
|
|
},
|
2023-06-04 11:04:04 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2022-11-26 18:14:51 +00:00
|
|
|
}
|
|
|
|
|
2023-06-04 11:04:04 +00:00
|
|
|
class _InheritedListeners extends StatelessWidget {
|
|
|
|
final Widget child;
|
2023-01-26 15:03:48 +00:00
|
|
|
|
2023-06-04 11:04:04 +00:00
|
|
|
const _InheritedListeners({required this.child});
|
2022-12-11 14:04:08 +00:00
|
|
|
|
2022-10-24 20:25:38 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-08-14 10:25:37 +00:00
|
|
|
return EquipmentProfileListener(
|
2023-06-04 11:04:04 +00:00
|
|
|
onDidChangeDependencies: (value) {
|
|
|
|
context.read<MeteringBloc>().add(EquipmentProfileChangedEvent(value));
|
|
|
|
},
|
2023-10-20 14:12:43 +00:00
|
|
|
child: child,
|
2022-10-24 20:25:38 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-02-19 10:26:14 +00:00
|
|
|
|
2023-09-02 08:32:08 +00:00
|
|
|
class MeteringContainerBuidler extends StatelessWidget {
|
2023-06-20 06:43:49 +00:00
|
|
|
final double? ev;
|
2023-02-19 10:26:14 +00:00
|
|
|
final IsoValue iso;
|
|
|
|
final NdValue nd;
|
|
|
|
final ValueChanged<IsoValue> onIsoChanged;
|
|
|
|
final ValueChanged<NdValue> onNdChanged;
|
|
|
|
|
2023-09-02 08:32:08 +00:00
|
|
|
const MeteringContainerBuidler({
|
2023-06-20 06:43:49 +00:00
|
|
|
required this.ev,
|
2023-02-19 10:26:14 +00:00
|
|
|
required this.iso,
|
|
|
|
required this.nd,
|
|
|
|
required this.onIsoChanged,
|
|
|
|
required this.onNdChanged,
|
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-09-02 08:32:08 +00:00
|
|
|
final exposurePairs = ev != null
|
|
|
|
? buildExposureValues(
|
|
|
|
ev!,
|
|
|
|
UserPreferencesProvider.stopTypeOf(context),
|
|
|
|
EquipmentProfiles.selectedOf(context),
|
|
|
|
)
|
|
|
|
: <ExposurePair>[];
|
2023-06-20 06:43:49 +00:00
|
|
|
final fastest = exposurePairs.isNotEmpty ? exposurePairs.first : null;
|
|
|
|
final slowest = exposurePairs.isNotEmpty ? exposurePairs.last : null;
|
2023-08-14 10:25:37 +00:00
|
|
|
// Doubled build here when switching evSourceType. As new source bloc fires a new state on init
|
|
|
|
return UserPreferencesProvider.evSourceTypeOf(context) == EvSourceType.camera
|
2023-02-19 10:26:14 +00:00
|
|
|
? CameraContainerProvider(
|
|
|
|
fastest: fastest,
|
|
|
|
slowest: slowest,
|
|
|
|
iso: iso,
|
|
|
|
nd: nd,
|
|
|
|
onIsoChanged: onIsoChanged,
|
|
|
|
onNdChanged: onNdChanged,
|
|
|
|
exposurePairs: exposurePairs,
|
|
|
|
)
|
|
|
|
: LightSensorContainerProvider(
|
|
|
|
fastest: fastest,
|
|
|
|
slowest: slowest,
|
|
|
|
iso: iso,
|
|
|
|
nd: nd,
|
|
|
|
onIsoChanged: onIsoChanged,
|
|
|
|
onNdChanged: onNdChanged,
|
|
|
|
exposurePairs: exposurePairs,
|
|
|
|
);
|
|
|
|
}
|
2023-06-20 06:43:49 +00:00
|
|
|
|
2023-09-02 08:32:08 +00:00
|
|
|
@visibleForTesting
|
|
|
|
static List<ExposurePair> buildExposureValues(
|
|
|
|
double ev,
|
|
|
|
StopType stopType,
|
|
|
|
EquipmentProfile equipmentProfile,
|
|
|
|
) {
|
2023-06-20 06:43:49 +00:00
|
|
|
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();
|
|
|
|
|
2023-09-02 08:32:08 +00:00
|
|
|
final apertureValues = ApertureValue.values.whereStopType(stopType);
|
|
|
|
final shutterSpeedValues = ShutterSpeedValue.values.whereStopType(stopType);
|
2023-06-20 06:43:49 +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.
|
2023-09-02 08:32:08 +00:00
|
|
|
const anchorShutterSpeed = ShutterSpeedValue(1, false, StopType.full);
|
|
|
|
final int anchorIndex = shutterSpeedValues.indexOf(anchorShutterSpeed);
|
2023-06-20 06:43:49 +00:00
|
|
|
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);
|
|
|
|
|
2023-09-02 08:32:08 +00:00
|
|
|
if (itemsCount <= 0) {
|
2023-06-20 06:43:49 +00:00
|
|
|
return List.empty();
|
|
|
|
}
|
2023-09-02 08:32:08 +00:00
|
|
|
|
|
|
|
final exposurePairs = List.generate(
|
2023-06-20 06:43:49 +00:00
|
|
|
itemsCount,
|
|
|
|
(index) => ExposurePair(
|
|
|
|
apertureValues[index + apertureOffset],
|
2023-09-14 14:59:16 +00:00
|
|
|
shutterSpeedValues[index + shutterSpeedOffset],
|
2023-06-20 06:43:49 +00:00
|
|
|
),
|
|
|
|
growable: false,
|
|
|
|
);
|
2023-09-02 08:32:08 +00:00
|
|
|
|
|
|
|
/// Full equipment profile, nothing to cut
|
|
|
|
if (equipmentProfile.id == "") {
|
|
|
|
return exposurePairs;
|
|
|
|
}
|
|
|
|
|
|
|
|
final equipmentApertureValues = equipmentProfile.apertureValues.whereStopType(stopType);
|
|
|
|
final equipmentShutterSpeedValues = equipmentProfile.shutterSpeedValues.whereStopType(stopType);
|
|
|
|
|
|
|
|
final startCutEV = max(
|
|
|
|
exposurePairs.first.aperture.difference(equipmentApertureValues.first),
|
|
|
|
exposurePairs.first.shutterSpeed.difference(equipmentShutterSpeedValues.first),
|
|
|
|
);
|
|
|
|
final endCutEV = max(
|
|
|
|
equipmentApertureValues.last.difference(exposurePairs.last.aperture),
|
|
|
|
equipmentShutterSpeedValues.last.difference(exposurePairs.last.shutterSpeed),
|
|
|
|
);
|
|
|
|
|
|
|
|
final startCut = (startCutEV * (stopType.index + 1)).round().clamp(0, itemsCount);
|
|
|
|
final endCut = (endCutEV * (stopType.index + 1)).round().clamp(0, itemsCount);
|
|
|
|
|
|
|
|
if (startCut > itemsCount - endCut) {
|
|
|
|
return const [];
|
|
|
|
}
|
|
|
|
return exposurePairs.sublist(startCut, itemsCount - endCut);
|
2023-06-20 06:43:49 +00:00
|
|
|
}
|
2023-02-19 10:26:14 +00:00
|
|
|
}
|