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:lightmeter/utils/selectable_provider.dart';
|
|
|
|
import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart';
|
|
|
|
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
|
|
|
|
|
|
|
|
class EquipmentProfileProvider extends StatefulWidget {
|
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;
|
|
|
|
|
|
|
|
const EquipmentProfileProvider({
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
|
|
|
static EquipmentProfileProviderState of(BuildContext context) {
|
|
|
|
return context.findAncestorStateOfType<EquipmentProfileProviderState>()!;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<EquipmentProfileProvider> createState() => EquipmentProfileProviderState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class EquipmentProfileProviderState extends State<EquipmentProfileProvider> {
|
|
|
|
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 Map<String, EquipmentProfile> _customProfiles = {};
|
2023-10-20 14:12:43 +00:00
|
|
|
String _selectedId = '';
|
|
|
|
|
2024-11-06 13:04:11 +00:00
|
|
|
EquipmentProfile get _selectedProfile => _customProfiles[_selectedId] ?? _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(
|
|
|
|
values: [
|
|
|
|
_defaultProfile,
|
2024-11-06 13:04:11 +00:00
|
|
|
if (context.isPro) ..._customProfiles.values,
|
2023-10-20 14:12:43 +00:00
|
|
|
],
|
2024-01-15 19:47:10 +00:00
|
|
|
selected: context.isPro ? _selectedProfile : _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();
|
|
|
|
}
|
|
|
|
|
2023-10-20 14:12:43 +00:00
|
|
|
void setProfile(EquipmentProfile data) {
|
|
|
|
if (_selectedId != data.id) {
|
|
|
|
setState(() {
|
|
|
|
_selectedId = data.id;
|
|
|
|
});
|
|
|
|
widget.storageService.selectedEquipmentProfileId = _selectedProfile.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-06 13:04:11 +00:00
|
|
|
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) {
|
2023-10-20 14:12:43 +00:00
|
|
|
_selectedId = _defaultProfile.id;
|
|
|
|
widget.storageService.selectedEquipmentProfileId = _defaultProfile.id;
|
|
|
|
}
|
2024-11-06 13:04:11 +00:00
|
|
|
_customProfiles.remove(profile.id);
|
2023-10-20 14:12:43 +00:00
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class EquipmentProfiles extends SelectableInheritedModel<EquipmentProfile> {
|
|
|
|
const EquipmentProfiles({
|
|
|
|
super.key,
|
|
|
|
required super.values,
|
|
|
|
required super.selected,
|
|
|
|
required super.child,
|
|
|
|
});
|
|
|
|
|
|
|
|
/// [_defaultProfile] + profiles created by the user
|
|
|
|
static List<EquipmentProfile> of(BuildContext context) {
|
2023-11-02 16:40:47 +00:00
|
|
|
return InheritedModel.inheritFrom<EquipmentProfiles>(context, aspect: SelectableAspect.list)!.values;
|
2023-10-20 14:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static EquipmentProfile selectedOf(BuildContext context) {
|
2023-11-02 16:40:47 +00:00
|
|
|
return InheritedModel.inheritFrom<EquipmentProfiles>(
|
|
|
|
context,
|
|
|
|
aspect: SelectableAspect.selected,
|
|
|
|
)!
|
2023-10-20 14:12:43 +00:00
|
|
|
.selected;
|
|
|
|
}
|
|
|
|
}
|