2023-03-30 19:24:18 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2023-08-13 19:14:32 +00:00
|
|
|
import 'package:lightmeter/providers/services_provider.dart';
|
2023-03-30 19:24:18 +00:00
|
|
|
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
|
|
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
|
2023-08-13 19:38:42 +00:00
|
|
|
// TODO(@vodemn): This will be removed in #89
|
2023-03-30 19:24:18 +00:00
|
|
|
class EquipmentProfileProvider extends StatefulWidget {
|
|
|
|
final Widget child;
|
|
|
|
|
|
|
|
const EquipmentProfileProvider({required this.child, super.key});
|
|
|
|
|
|
|
|
static EquipmentProfileProviderState of(BuildContext context) {
|
|
|
|
return context.findAncestorStateOfType<EquipmentProfileProviderState>()!;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<EquipmentProfileProvider> createState() => EquipmentProfileProviderState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class EquipmentProfileProviderState extends State<EquipmentProfileProvider> {
|
2023-07-24 07:08:37 +00:00
|
|
|
static const EquipmentProfile _defaultProfile = EquipmentProfile(
|
2023-03-30 19:24:18 +00:00
|
|
|
id: '',
|
|
|
|
name: '',
|
2023-04-05 19:15:11 +00:00
|
|
|
apertureValues: ApertureValue.values,
|
|
|
|
ndValues: NdValue.values,
|
|
|
|
shutterSpeedValues: ShutterSpeedValue.values,
|
|
|
|
isoValues: IsoValue.values,
|
2023-03-30 19:24:18 +00:00
|
|
|
);
|
|
|
|
|
2023-07-24 07:08:37 +00:00
|
|
|
List<EquipmentProfile> _customProfiles = [];
|
2023-03-30 19:24:18 +00:00
|
|
|
String _selectedId = '';
|
|
|
|
|
2023-07-24 07:08:37 +00:00
|
|
|
EquipmentProfile get _selectedProfile => _customProfiles.firstWhere(
|
2023-03-30 19:24:18 +00:00
|
|
|
(e) => e.id == _selectedId,
|
|
|
|
orElse: () {
|
2023-08-13 20:19:14 +00:00
|
|
|
ServicesProvider.of(context).userPreferencesService.selectedEquipmentProfileId =
|
2023-08-13 13:27:43 +00:00
|
|
|
_defaultProfile.id;
|
2023-03-30 19:24:18 +00:00
|
|
|
return _defaultProfile;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2023-08-13 20:19:14 +00:00
|
|
|
_selectedId = ServicesProvider.of(context).userPreferencesService.selectedEquipmentProfileId;
|
|
|
|
_customProfiles = ServicesProvider.of(context).userPreferencesService.equipmentProfiles;
|
2023-03-30 19:24:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-08-13 19:59:37 +00:00
|
|
|
return EquipmentProfiles(
|
|
|
|
profiles: [_defaultProfile] + _customProfiles,
|
|
|
|
selected: _selectedProfile,
|
|
|
|
child: widget.child,
|
2023-03-30 19:24:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-07-24 07:08:37 +00:00
|
|
|
void setProfile(EquipmentProfile data) {
|
2023-03-30 19:24:18 +00:00
|
|
|
setState(() {
|
|
|
|
_selectedId = data.id;
|
|
|
|
});
|
2023-08-13 20:19:14 +00:00
|
|
|
ServicesProvider.of(context).userPreferencesService.selectedEquipmentProfileId =
|
2023-08-13 13:27:43 +00:00
|
|
|
_selectedProfile.id;
|
2023-03-30 19:24:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a default equipment profile
|
|
|
|
void addProfile(String name) {
|
2023-05-11 13:30:18 +00:00
|
|
|
_customProfiles.add(
|
2023-07-24 07:08:37 +00:00
|
|
|
EquipmentProfile(
|
2023-05-11 13:30:18 +00:00
|
|
|
id: const Uuid().v1(),
|
|
|
|
name: name,
|
|
|
|
apertureValues: ApertureValue.values,
|
|
|
|
ndValues: NdValue.values,
|
|
|
|
shutterSpeedValues: ShutterSpeedValue.values,
|
|
|
|
isoValues: IsoValue.values,
|
|
|
|
),
|
|
|
|
);
|
2023-03-30 19:24:18 +00:00
|
|
|
_refreshSavedProfiles();
|
|
|
|
}
|
|
|
|
|
2023-07-24 07:08:37 +00:00
|
|
|
void updateProdile(EquipmentProfile data) {
|
2023-03-30 19:24:18 +00:00
|
|
|
final indexToUpdate = _customProfiles.indexWhere((element) => element.id == data.id);
|
|
|
|
if (indexToUpdate >= 0) {
|
|
|
|
_customProfiles[indexToUpdate] = data;
|
|
|
|
_refreshSavedProfiles();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-24 07:08:37 +00:00
|
|
|
void deleteProfile(EquipmentProfile data) {
|
2023-03-30 19:24:18 +00:00
|
|
|
_customProfiles.remove(data);
|
|
|
|
_refreshSavedProfiles();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _refreshSavedProfiles() {
|
2023-08-13 20:19:14 +00:00
|
|
|
ServicesProvider.of(context).userPreferencesService.equipmentProfiles = _customProfiles;
|
2023-03-30 19:24:18 +00:00
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
}
|
2023-08-13 19:59:37 +00:00
|
|
|
|
|
|
|
// Copied from #89
|
|
|
|
enum EquipmentProfilesAspect { list, selected }
|
|
|
|
|
|
|
|
class EquipmentProfiles extends InheritedModel<EquipmentProfilesAspect> {
|
|
|
|
const EquipmentProfiles({
|
|
|
|
super.key,
|
|
|
|
required this.profiles,
|
|
|
|
required this.selected,
|
|
|
|
required super.child,
|
|
|
|
});
|
|
|
|
|
|
|
|
final List<EquipmentProfile> profiles;
|
|
|
|
final EquipmentProfile selected;
|
|
|
|
|
|
|
|
static List<EquipmentProfile> of(BuildContext context) {
|
|
|
|
return InheritedModel.inheritFrom<EquipmentProfiles>(
|
|
|
|
context,
|
|
|
|
aspect: EquipmentProfilesAspect.list,
|
|
|
|
)!
|
|
|
|
.profiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
static EquipmentProfile selectedOf(BuildContext context) {
|
|
|
|
return InheritedModel.inheritFrom<EquipmentProfiles>(
|
|
|
|
context,
|
|
|
|
aspect: EquipmentProfilesAspect.selected,
|
|
|
|
)!
|
|
|
|
.selected;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool updateShouldNotify(EquipmentProfiles oldWidget) => false;
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool updateShouldNotifyDependent(
|
2023-08-13 20:19:14 +00:00
|
|
|
EquipmentProfiles oldWidget,
|
|
|
|
Set<EquipmentProfilesAspect> dependencies,
|
|
|
|
) =>
|
2023-08-13 19:59:37 +00:00
|
|
|
false;
|
|
|
|
}
|