diff --git a/lib/providers/equipment_profile_provider.dart b/lib/providers/equipment_profile_provider.dart index 747ae72..ceef97e 100644 --- a/lib/providers/equipment_profile_provider.dart +++ b/lib/providers/equipment_profile_provider.dart @@ -37,7 +37,8 @@ class EquipmentProfilesProviderState extends State { final SelectableMap _customProfiles = {}; String _selectedId = ''; - EquipmentProfile get _selectedProfile => _customProfiles[_selectedId] ?? EquipmentProfilesProvider.defaultProfile; + EquipmentProfile get _selectedProfile => + _customProfiles[_selectedId]?.value ?? EquipmentProfilesProvider.defaultProfile; @override void initState() { @@ -57,18 +58,19 @@ class EquipmentProfilesProviderState extends State { Future _init() async { _selectedId = widget.storageService.selectedEquipmentProfileId; _customProfiles.addAll(await widget.storageService.getProfiles()); + _discardSelectedIfNotIncluded(); if (mounted) setState(() {}); widget.onInitialized?.call(); } Future addProfile(EquipmentProfile profile) async { await widget.storageService.addProfile(profile); - _customProfiles[profile.id] = profile; + _customProfiles[profile.id] = (value: profile, isUsed: true); setState(() {}); } Future updateProfile(EquipmentProfile profile) async { - final oldProfile = _customProfiles[profile.id]!; + final oldProfile = _customProfiles[profile.id]!.value; await widget.storageService.updateProfile( id: profile.id, name: oldProfile.name != profile.name ? profile.name : null, @@ -79,7 +81,7 @@ class EquipmentProfilesProviderState extends State { ndValues: oldProfile.ndValues != profile.ndValues ? profile.ndValues : null, lensZoom: oldProfile.lensZoom != profile.lensZoom ? profile.lensZoom : null, ); - _customProfiles[profile.id] = profile; + _customProfiles[profile.id] = (value: profile, isUsed: _customProfiles[profile.id]!.isUsed); setState(() {}); } @@ -90,6 +92,7 @@ class EquipmentProfilesProviderState extends State { widget.storageService.selectedEquipmentProfileId = EquipmentProfilesProvider.defaultProfile.id; } _customProfiles.remove(profile.id); + _discardSelectedIfNotIncluded(); setState(() {}); } @@ -101,15 +104,38 @@ class EquipmentProfilesProviderState extends State { widget.storageService.selectedEquipmentProfileId = _selectedProfile.id; } } + + Future toggleProfile(EquipmentProfile profile, bool enabled) async { + if (_customProfiles.containsKey(profile.id)) { + _customProfiles[profile.id] = (value: profile, isUsed: enabled); + } else { + return; + } + await widget.storageService.updateProfile(id: profile.id, isUsed: enabled); + _discardSelectedIfNotIncluded(); + setState(() {}); + } + + void _discardSelectedIfNotIncluded() { + if (_selectedId == EquipmentProfilesProvider.defaultProfile.id) { + return; + } + final isSelectedUsed = _customProfiles[_selectedId]?.isUsed ?? false; + if (!isSelectedUsed) { + _selectedId = EquipmentProfilesProvider.defaultProfile.id; + widget.storageService.selectedEquipmentProfileId = _selectedId; + } + } } enum _EquipmentProfilesModelAspect { profiles, + profilesInUse, selected, } class EquipmentProfiles extends InheritedModel<_EquipmentProfilesModelAspect> { - final Map profiles; + final SelectableMap profiles; final EquipmentProfile selected; const EquipmentProfiles({ @@ -122,7 +148,23 @@ class EquipmentProfiles extends InheritedModel<_EquipmentProfilesModelAspect> { static List of(BuildContext context) { final model = InheritedModel.inheritFrom(context, aspect: _EquipmentProfilesModelAspect.profiles)!; - return List.from([EquipmentProfilesProvider.defaultProfile, ...model.profiles.values]); + return List.from( + [ + EquipmentProfilesProvider.defaultProfile, + ...model.profiles.values.map((p) => p.value), + ], + ); + } + + static List inUseOf(BuildContext context) { + final model = + InheritedModel.inheritFrom(context, aspect: _EquipmentProfilesModelAspect.profilesInUse)!; + return List.from( + [ + EquipmentProfilesProvider.defaultProfile, + ...model.profiles.values.where((p) => p.isUsed).map((p) => p.value), + ], + ); } static EquipmentProfile selectedOf(BuildContext context) { @@ -137,7 +179,7 @@ class EquipmentProfiles extends InheritedModel<_EquipmentProfilesModelAspect> { bool updateShouldNotifyDependent(EquipmentProfiles oldWidget, Set<_EquipmentProfilesModelAspect> dependencies) { return (dependencies.contains(_EquipmentProfilesModelAspect.selected) && oldWidget.selected != selected) || ((dependencies.contains(_EquipmentProfilesModelAspect.profiles) || - dependencies.contains(_EquipmentProfilesModelAspect.profiles)) && + dependencies.contains(_EquipmentProfilesModelAspect.profilesInUse)) && const DeepCollectionEquality().equals(oldWidget.profiles, profiles)); } } diff --git a/lib/screens/equipment_profiles/screen_equipment_profiles.dart b/lib/screens/equipment_profiles/screen_equipment_profiles.dart index 098e72c..3821cad 100644 --- a/lib/screens/equipment_profiles/screen_equipment_profiles.dart +++ b/lib/screens/equipment_profiles/screen_equipment_profiles.dart @@ -32,6 +32,7 @@ class _EquipmentProfilesScreenState extends State with _EquipmentProfilesListBuilder( values: EquipmentProfiles.of(context).skip(1).toList(), onEdit: _editProfile, + onCheckbox: EquipmentProfilesProvider.of(context).toggleProfile, ) else SliverPlaceholder(onTap: _addProfile), @@ -60,10 +61,12 @@ class _EquipmentProfilesScreenState extends State with class _EquipmentProfilesListBuilder extends StatelessWidget { final List values; final void Function(EquipmentProfile film) onEdit; + final void Function(EquipmentProfile film, bool value) onCheckbox; const _EquipmentProfilesListBuilder({ required this.values, required this.onEdit, + required this.onCheckbox, }); @override @@ -92,8 +95,8 @@ class _EquipmentProfilesListBuilder extends StatelessWidget { child: CheckboxListTile( title: Text(values[index].name), controlAffinity: ListTileControlAffinity.leading, - value: false, - onChanged: (value) {}, + value: EquipmentProfiles.inUseOf(context).contains(values[index]), + onChanged: (value) => onCheckbox(values[index], value ?? false), secondary: IconButton( onPressed: () => onEdit(values[index]), icon: const Icon(Icons.edit), diff --git a/lib/screens/metering/components/shared/readings_container/components/equipment_profile_picker/widget_picker_equipment_profiles.dart b/lib/screens/metering/components/shared/readings_container/components/equipment_profile_picker/widget_picker_equipment_profiles.dart index 4493103..4930c46 100644 --- a/lib/screens/metering/components/shared/readings_container/components/equipment_profile_picker/widget_picker_equipment_profiles.dart +++ b/lib/screens/metering/components/shared/readings_container/components/equipment_profile_picker/widget_picker_equipment_profiles.dart @@ -14,7 +14,7 @@ class EquipmentProfilePicker extends StatelessWidget { icon: Icons.camera_outlined, title: S.of(context).equipmentProfile, selectedValue: EquipmentProfiles.selectedOf(context), - values: EquipmentProfiles.of(context), + values: EquipmentProfiles.inUseOf(context), itemTitleBuilder: (_, value) => Text(value.id.isEmpty ? S.of(context).none : value.name), onChanged: EquipmentProfilesProvider.of(context).selectProfile, closedChild: ReadingValueContainer.singleValue(