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';
|
2024-05-03 09:26:00 +00:00
|
|
|
import 'package:lightmeter/screens/metering/components/bottom_controls/widget_bottom_controls.dart';
|
2023-05-11 13:30:18 +00:00
|
|
|
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)),
|
2024-05-03 10:28:10 +00:00
|
|
|
onExposurePairTap: (value) => pushNamed(context, 'timer', arguments: value),
|
2023-06-04 11:04:04 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
BlocBuilder<MeteringBloc, MeteringState>(
|
2024-05-03 09:26:00 +00:00
|
|
|
builder: (context, state) => MeteringBottomControls(
|
2023-06-04 11:04:04 +00:00
|
|
|
ev: state is MeteringDataState ? state.ev : null,
|
2024-01-15 19:47:10 +00:00
|
|
|
ev100: state is MeteringDataState ? state.ev100 : 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()),
|
2024-05-03 10:28:10 +00:00
|
|
|
onSettings: () => pushNamed(context, 'settings'),
|
2023-06-04 11:04:04 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2024-05-03 10:28:10 +00:00
|
|
|
|
|
|
|
void pushNamed(BuildContext context, String routeName, {Object? arguments}) {
|
|
|
|
context.read<MeteringBloc>().add(const ScreenOnTopOpenedEvent());
|
|
|
|
Navigator.pushNamed(context, routeName, arguments: arguments).then((_) {
|
|
|
|
context.read<MeteringBloc>().add(const ScreenOnTopClosedEvent());
|
|
|
|
});
|
|
|
|
}
|
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;
|
2024-05-02 15:11:53 +00:00
|
|
|
final ValueChanged<ExposurePair> onExposurePairTap;
|
2023-02-19 10:26:14 +00:00
|
|
|
|
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,
|
2024-05-02 14:54:37 +00:00
|
|
|
required this.onExposurePairTap,
|
2023-02-19 10:26:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
@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,
|
2024-05-02 14:54:37 +00:00
|
|
|
onExposurePairTap: onExposurePairTap,
|
2023-02-19 10:26:14 +00:00
|
|
|
)
|
|
|
|
: LightSensorContainerProvider(
|
|
|
|
fastest: fastest,
|
|
|
|
slowest: slowest,
|
|
|
|
iso: iso,
|
|
|
|
nd: nd,
|
|
|
|
onIsoChanged: onIsoChanged,
|
|
|
|
onNdChanged: onNdChanged,
|
|
|
|
exposurePairs: exposurePairs,
|
2024-05-02 14:54:37 +00:00
|
|
|
onExposurePairTap: onExposurePairTap,
|
2023-02-19 10:26:14 +00:00
|
|
|
);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2024-04-30 10:44:01 +00:00
|
|
|
int itemsCount = min(
|
2023-06-20 06:43:49 +00:00
|
|
|
apertureValues.length + shutterSpeedOffset,
|
|
|
|
shutterSpeedValues.length + apertureOffset,
|
|
|
|
) -
|
|
|
|
max(apertureOffset, shutterSpeedOffset);
|
|
|
|
|
2024-04-30 10:44:01 +00:00
|
|
|
if (apertureOffset == apertureValues.length) {
|
2023-06-20 06:43:49 +00:00
|
|
|
return List.empty();
|
|
|
|
}
|
2023-09-02 08:32:08 +00:00
|
|
|
|
2024-04-30 10:44:01 +00:00
|
|
|
final lastPreCalcShutterSpeed =
|
|
|
|
shutterSpeedValues.elementAtOrNull(itemsCount - 1 + shutterSpeedOffset) ?? shutterSpeedValues.last;
|
|
|
|
final preCalculatedItemsCount = itemsCount;
|
|
|
|
if (itemsCount <= 0) {
|
|
|
|
itemsCount = apertureValues.length;
|
|
|
|
} else {
|
|
|
|
itemsCount += (apertureValues.length - 1) - (itemsCount - 1 + apertureOffset);
|
|
|
|
}
|
|
|
|
|
2023-09-02 08:32:08 +00:00
|
|
|
final exposurePairs = List.generate(
|
2023-06-20 06:43:49 +00:00
|
|
|
itemsCount,
|
2024-04-30 10:44:01 +00:00
|
|
|
(index) {
|
|
|
|
final stopDifference = (index - (preCalculatedItemsCount - 1)) / (stopType.index + 1);
|
|
|
|
final newShutterSpeed = log2(lastPreCalcShutterSpeed.rawValue) + stopDifference;
|
|
|
|
return ExposurePair(
|
|
|
|
apertureValues[index + apertureOffset],
|
|
|
|
shutterSpeedValues.elementAtOrNull(index + shutterSpeedOffset) ??
|
|
|
|
ShutterSpeedValue(
|
|
|
|
calcShutterSpeed(newShutterSpeed),
|
|
|
|
false,
|
|
|
|
stopDifference == stopDifference.roundToDouble() ? StopType.full : stopType,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
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),
|
2024-04-30 10:44:01 +00:00
|
|
|
equipmentShutterSpeedValues.last != ShutterSpeedValue.values.last
|
|
|
|
? equipmentShutterSpeedValues.last.difference(exposurePairs.last.shutterSpeed)
|
|
|
|
: double.negativeInfinity,
|
2023-09-02 08:32:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2024-04-30 10:44:01 +00:00
|
|
|
|
|
|
|
double calcShutterSpeed(double stopValue) {
|
|
|
|
final shutterSpeed = pow(2, stopValue);
|
|
|
|
if (stopValue < 1.5) {
|
|
|
|
return (shutterSpeed * 10).round() / 10;
|
|
|
|
} else {
|
|
|
|
return shutterSpeed.roundToDouble();
|
|
|
|
}
|
|
|
|
}
|