2023-03-30 19:24:18 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2023-08-13 13:27:43 +00:00
|
|
|
import 'package:lightmeter/providers/service_providers.dart';
|
2023-06-04 11:04:04 +00:00
|
|
|
import 'package:lightmeter/utils/inherited_generics.dart';
|
2023-03-30 19:24:18 +00:00
|
|
|
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
|
|
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
|
2023-07-24 07:08:37 +00:00
|
|
|
typedef EquipmentProfiles = List<EquipmentProfile>;
|
2023-06-04 11:04:04 +00:00
|
|
|
|
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 13:27:43 +00:00
|
|
|
ServiceProviders.userPreferencesServiceOf(context).selectedEquipmentProfileId =
|
|
|
|
_defaultProfile.id;
|
2023-03-30 19:24:18 +00:00
|
|
|
return _defaultProfile;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2023-08-13 13:27:43 +00:00
|
|
|
_selectedId = ServiceProviders.userPreferencesServiceOf(context).selectedEquipmentProfileId;
|
|
|
|
_customProfiles = ServiceProviders.userPreferencesServiceOf(context).equipmentProfiles;
|
2023-03-30 19:24:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-07-24 07:08:37 +00:00
|
|
|
return InheritedWidgetBase<List<EquipmentProfile>>(
|
2023-06-04 11:04:04 +00:00
|
|
|
data: [_defaultProfile] + _customProfiles,
|
2023-07-24 07:08:37 +00:00
|
|
|
child: InheritedWidgetBase<EquipmentProfile>(
|
2023-03-30 19:24:18 +00:00
|
|
|
data: _selectedProfile,
|
|
|
|
child: widget.child,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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 13:27:43 +00:00
|
|
|
ServiceProviders.userPreferencesServiceOf(context).selectedEquipmentProfileId =
|
|
|
|
_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 13:27:43 +00:00
|
|
|
ServiceProviders.userPreferencesServiceOf(context).equipmentProfiles = _customProfiles;
|
2023-03-30 19:24:18 +00:00
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
}
|