save/restore equipment profiles

This commit is contained in:
Vadim 2023-03-26 13:14:18 +03:00
parent cb9563ac98
commit 0055b5f628
3 changed files with 53 additions and 25 deletions

View file

@ -1,5 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:lightmeter/data/models/supported_locale.dart'; import 'package:lightmeter/data/models/supported_locale.dart';
import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart';
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
@ -104,4 +105,11 @@ class UserPreferencesService {
bool get dynamicColor => _sharedPreferences.getBool(_dynamicColorKey) ?? false; bool get dynamicColor => _sharedPreferences.getBool(_dynamicColorKey) ?? false;
set dynamicColor(bool value) => _sharedPreferences.setBool(_dynamicColorKey, value); set dynamicColor(bool value) => _sharedPreferences.setBool(_dynamicColorKey, value);
String get selectedEquipmentProfileId => _sharedPreferences.selectedEquipmentProfileId;
set selectedEquipmentProfileId(String id) => _sharedPreferences.selectedEquipmentProfileId = id;
List<EquipmentProfileData> get equipmentProfiles => _sharedPreferences.equipmentProfiles;
set equipmentProfiles(List<EquipmentProfileData> profiles) =>
_sharedPreferences.equipmentProfiles = profiles;
} }

View file

@ -1,5 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:lightmeter/data/shared_prefs_service.dart';
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
import 'package:provider/provider.dart';
import 'package:uuid/uuid.dart'; import 'package:uuid/uuid.dart';
class EquipmentProfileProvider extends StatefulWidget { class EquipmentProfileProvider extends StatefulWidget {
@ -16,23 +18,37 @@ class EquipmentProfileProvider extends StatefulWidget {
} }
class EquipmentProfileProviderState extends State<EquipmentProfileProvider> { class EquipmentProfileProviderState extends State<EquipmentProfileProvider> {
final List<EquipmentProfileData> _profiles = [ static const EquipmentProfileData _defaultProfile = EquipmentProfileData(
const EquipmentProfileData( id: '',
id: '', name: '',
name: '', apertureValues: apertureValues,
apertureValues: apertureValues, ndValues: ndValues,
ndValues: ndValues, shutterSpeedValues: shutterSpeedValues,
shutterSpeedValues: shutterSpeedValues, isoValues: isoValues,
isoValues: isoValues, );
)
];
late EquipmentProfileData _selectedProfile = _profiles.first; List<EquipmentProfileData> _customProfiles = [];
String _selectedId = '';
EquipmentProfileData get _selectedProfile => _customProfiles.firstWhere(
(e) => e.id == _selectedId,
orElse: () {
context.read<UserPreferencesService>().selectedEquipmentProfileId = _defaultProfile.id;
return _defaultProfile;
},
);
@override
void initState() {
super.initState();
_selectedId = context.read<UserPreferencesService>().selectedEquipmentProfileId;
_customProfiles = context.read<UserPreferencesService>().equipmentProfiles;
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return EquipmentProfiles( return EquipmentProfiles(
profiles: _profiles, profiles: [_defaultProfile] + _customProfiles,
child: EquipmentProfile( child: EquipmentProfile(
data: _selectedProfile, data: _selectedProfile,
child: widget.child, child: widget.child,
@ -42,13 +58,14 @@ class EquipmentProfileProviderState extends State<EquipmentProfileProvider> {
void setProfile(EquipmentProfileData data) { void setProfile(EquipmentProfileData data) {
setState(() { setState(() {
_selectedProfile = data; _selectedId = data.id;
}); });
context.read<UserPreferencesService>().selectedEquipmentProfileId = _selectedProfile.id;
} }
/// Creates a default equipment profile /// Creates a default equipment profile
void addProfile(String name) { void addProfile(String name) {
_profiles.add(EquipmentProfileData( _customProfiles.add(EquipmentProfileData(
id: const Uuid().v1(), id: const Uuid().v1(),
name: name, name: name,
apertureValues: apertureValues, apertureValues: apertureValues,
@ -56,25 +73,24 @@ class EquipmentProfileProviderState extends State<EquipmentProfileProvider> {
shutterSpeedValues: shutterSpeedValues, shutterSpeedValues: shutterSpeedValues,
isoValues: isoValues, isoValues: isoValues,
)); ));
setState(() {}); _refreshSavedProfiles();
} }
void updateProdile(EquipmentProfileData data) { void updateProdile(EquipmentProfileData data) {
final indexToUpdate = _profiles.indexWhere((element) => element.id == data.id); final indexToUpdate = _customProfiles.indexWhere((element) => element.id == data.id);
if (indexToUpdate >= 0) { if (indexToUpdate >= 0) {
_profiles[indexToUpdate] = data; _customProfiles[indexToUpdate] = data;
if (data.id == _selectedProfile.id) { _refreshSavedProfiles();
_selectedProfile = _profiles[indexToUpdate];
}
setState(() {});
} }
} }
void deleteProfile(EquipmentProfileData data) { void deleteProfile(EquipmentProfileData data) {
_profiles.remove(data); _customProfiles.remove(data);
if (data.id == _selectedProfile.id) { _refreshSavedProfiles();
_selectedProfile = _profiles.first; }
}
void _refreshSavedProfiles() {
context.read<UserPreferencesService>().equipmentProfiles = _customProfiles;
setState(() {}); setState(() {});
} }
} }

View file

@ -21,6 +21,10 @@ dependencies:
intl_utils: 2.8.1 intl_utils: 2.8.1
light_sensor: 2.0.2 light_sensor: 2.0.2
material_color_utilities: 0.2.0 material_color_utilities: 0.2.0
m3_lightmeter_iap:
git:
url: "https://github.com/vodemn/m3_lightmeter_iap"
ref: main
m3_lightmeter_resources: m3_lightmeter_resources:
git: git:
url: "https://github.com/vodemn/m3_lightmeter_resources" url: "https://github.com/vodemn/m3_lightmeter_resources"