2024-11-06 14:28:16 +00:00
|
|
|
import 'package:collection/collection.dart';
|
2023-10-20 14:12:43 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-01-15 19:47:10 +00:00
|
|
|
import 'package:lightmeter/utils/context_utils.dart';
|
2023-10-20 14:12:43 +00:00
|
|
|
import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart';
|
|
|
|
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
|
|
|
|
|
2024-11-06 14:28:16 +00:00
|
|
|
class EquipmentProfilesProvider extends StatefulWidget {
|
|
|
|
static const EquipmentProfile defaultProfile = EquipmentProfile(
|
|
|
|
id: '',
|
|
|
|
name: '',
|
|
|
|
apertureValues: ApertureValue.values,
|
|
|
|
ndValues: NdValue.values,
|
|
|
|
shutterSpeedValues: ShutterSpeedValue.values,
|
|
|
|
isoValues: IsoValue.values,
|
|
|
|
);
|
|
|
|
|
2024-11-06 13:04:11 +00:00
|
|
|
final EquipmentProfilesStorageService storageService;
|
|
|
|
final VoidCallback? onInitialized;
|
2023-10-20 14:12:43 +00:00
|
|
|
final Widget child;
|
|
|
|
|
2024-11-06 14:28:16 +00:00
|
|
|
const EquipmentProfilesProvider({
|
2023-10-20 14:12:43 +00:00
|
|
|
required this.storageService,
|
2024-11-06 13:04:11 +00:00
|
|
|
this.onInitialized,
|
2023-10-20 14:12:43 +00:00
|
|
|
required this.child,
|
|
|
|
super.key,
|
|
|
|
});
|
|
|
|
|
2024-11-06 14:28:16 +00:00
|
|
|
static EquipmentProfilesProviderState of(BuildContext context) {
|
|
|
|
return context.findAncestorStateOfType<EquipmentProfilesProviderState>()!;
|
2023-10-20 14:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2024-11-06 14:28:16 +00:00
|
|
|
State<EquipmentProfilesProvider> createState() => EquipmentProfilesProviderState();
|
2023-10-20 14:12:43 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 14:28:16 +00:00
|
|
|
class EquipmentProfilesProviderState extends State<EquipmentProfilesProvider> {
|
2024-11-11 12:00:09 +00:00
|
|
|
final SelectableMap<EquipmentProfile> _customProfiles = {};
|
2023-10-20 14:12:43 +00:00
|
|
|
String _selectedId = '';
|
|
|
|
|
2024-11-06 14:28:16 +00:00
|
|
|
EquipmentProfile get _selectedProfile => _customProfiles[_selectedId] ?? EquipmentProfilesProvider.defaultProfile;
|
2023-10-20 14:12:43 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2024-11-06 13:04:11 +00:00
|
|
|
_init();
|
2023-10-20 14:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return EquipmentProfiles(
|
2024-11-06 14:28:16 +00:00
|
|
|
profiles: context.isPro ? _customProfiles : {},
|
|
|
|
selected: context.isPro ? _selectedProfile : EquipmentProfilesProvider.defaultProfile,
|
2023-10-20 14:12:43 +00:00
|
|
|
child: widget.child,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-11-06 13:04:11 +00:00
|
|
|
Future<void> _init() async {
|
|
|
|
_selectedId = widget.storageService.selectedEquipmentProfileId;
|
|
|
|
_customProfiles.addAll(await widget.storageService.getProfiles());
|
|
|
|
if (mounted) setState(() {});
|
|
|
|
widget.onInitialized?.call();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> addProfile(EquipmentProfile profile) async {
|
|
|
|
await widget.storageService.addProfile(profile);
|
|
|
|
_customProfiles[profile.id] = profile;
|
|
|
|
setState(() {});
|
2023-10-20 14:12:43 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 13:04:11 +00:00
|
|
|
Future<void> updateProfile(EquipmentProfile profile) async {
|
|
|
|
final oldProfile = _customProfiles[profile.id]!;
|
|
|
|
await widget.storageService.updateProfile(
|
|
|
|
id: profile.id,
|
|
|
|
name: oldProfile.name != profile.name ? profile.name : null,
|
|
|
|
apertureValues: oldProfile.apertureValues != profile.apertureValues ? profile.apertureValues : null,
|
|
|
|
shutterSpeedValues:
|
|
|
|
oldProfile.shutterSpeedValues != profile.shutterSpeedValues ? profile.shutterSpeedValues : null,
|
|
|
|
isoValues: oldProfile.isoValues != profile.isoValues ? profile.isoValues : null,
|
|
|
|
ndValues: oldProfile.ndValues != profile.ndValues ? profile.ndValues : null,
|
|
|
|
lensZoom: oldProfile.lensZoom != profile.lensZoom ? profile.lensZoom : null,
|
|
|
|
);
|
|
|
|
_customProfiles[profile.id] = profile;
|
|
|
|
setState(() {});
|
2023-10-20 14:12:43 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 13:04:11 +00:00
|
|
|
Future<void> deleteProfile(EquipmentProfile profile) async {
|
|
|
|
await widget.storageService.deleteProfile(profile.id);
|
|
|
|
if (profile.id == _selectedId) {
|
2024-11-06 14:28:16 +00:00
|
|
|
_selectedId = EquipmentProfilesProvider.defaultProfile.id;
|
|
|
|
widget.storageService.selectedEquipmentProfileId = EquipmentProfilesProvider.defaultProfile.id;
|
2023-10-20 14:12:43 +00:00
|
|
|
}
|
2024-11-06 13:04:11 +00:00
|
|
|
_customProfiles.remove(profile.id);
|
2023-10-20 14:12:43 +00:00
|
|
|
setState(() {});
|
|
|
|
}
|
2024-11-06 14:28:16 +00:00
|
|
|
|
|
|
|
void selectProfile(EquipmentProfile data) {
|
|
|
|
if (_selectedId != data.id) {
|
|
|
|
setState(() {
|
|
|
|
_selectedId = data.id;
|
|
|
|
});
|
|
|
|
widget.storageService.selectedEquipmentProfileId = _selectedProfile.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum _EquipmentProfilesModelAspect {
|
|
|
|
profiles,
|
|
|
|
selected,
|
2023-10-20 14:12:43 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 14:28:16 +00:00
|
|
|
class EquipmentProfiles extends InheritedModel<_EquipmentProfilesModelAspect> {
|
|
|
|
final Map<String, EquipmentProfile> profiles;
|
|
|
|
final EquipmentProfile selected;
|
|
|
|
|
2023-10-20 14:12:43 +00:00
|
|
|
const EquipmentProfiles({
|
2024-11-06 14:28:16 +00:00
|
|
|
required this.profiles,
|
|
|
|
required this.selected,
|
2023-10-20 14:12:43 +00:00
|
|
|
required super.child,
|
|
|
|
});
|
|
|
|
|
2024-11-06 14:28:16 +00:00
|
|
|
/// _default + profiles create by the user
|
2023-10-20 14:12:43 +00:00
|
|
|
static List<EquipmentProfile> of(BuildContext context) {
|
2024-11-06 14:28:16 +00:00
|
|
|
final model =
|
|
|
|
InheritedModel.inheritFrom<EquipmentProfiles>(context, aspect: _EquipmentProfilesModelAspect.profiles)!;
|
|
|
|
return List.from([EquipmentProfilesProvider.defaultProfile, ...model.profiles.values]);
|
2023-10-20 14:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static EquipmentProfile selectedOf(BuildContext context) {
|
2024-11-06 14:28:16 +00:00
|
|
|
return InheritedModel.inheritFrom<EquipmentProfiles>(context, aspect: _EquipmentProfilesModelAspect.selected)!
|
2023-10-20 14:12:43 +00:00
|
|
|
.selected;
|
|
|
|
}
|
2024-11-06 14:28:16 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
bool updateShouldNotify(EquipmentProfiles _) => true;
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool updateShouldNotifyDependent(EquipmentProfiles oldWidget, Set<_EquipmentProfilesModelAspect> dependencies) {
|
|
|
|
return (dependencies.contains(_EquipmentProfilesModelAspect.selected) && oldWidget.selected != selected) ||
|
|
|
|
((dependencies.contains(_EquipmentProfilesModelAspect.profiles) ||
|
|
|
|
dependencies.contains(_EquipmentProfilesModelAspect.profiles)) &&
|
|
|
|
const DeepCollectionEquality().equals(oldWidget.profiles, profiles));
|
|
|
|
}
|
2023-10-20 14:12:43 +00:00
|
|
|
}
|