diff --git a/lib/application.dart b/lib/application.dart index 79055d5..8f88746 100644 --- a/lib/application.dart +++ b/lib/application.dart @@ -18,7 +18,7 @@ import 'environment.dart'; import 'generated/l10n.dart'; import 'providers/equipment_profile_provider.dart'; import 'providers/ev_source_type_provider.dart'; -import 'providers/features_provider.dart'; +import 'providers/metering_screen_layout_provider.dart'; import 'providers/theme_provider.dart'; import 'screens/metering/flow_metering.dart'; import 'screens/settings/flow_settings.dart'; diff --git a/lib/data/models/metering_screen_layout_config.dart b/lib/data/models/metering_screen_layout_config.dart new file mode 100644 index 0000000..68ff40e --- /dev/null +++ b/lib/data/models/metering_screen_layout_config.dart @@ -0,0 +1,10 @@ +enum MeteringScreenLayoutFeature { extremeExposurePairs, filmPicker } + +typedef MeteringScreenLayoutConfig = Map; + +extension MeteringScreenLayoutConfigJson on MeteringScreenLayoutConfig { + static MeteringScreenLayoutConfig fromJson(Map data) => data.map( + (key, value) => MapEntry(MeteringScreenLayoutFeature.values[int.parse(key)], value as bool)); + + Map toJson() => map((key, value) => MapEntry(key.index.toString(), value)); +} diff --git a/lib/data/shared_prefs_service.dart b/lib/data/shared_prefs_service.dart index c5054fb..56e05bd 100644 --- a/lib/data/shared_prefs_service.dart +++ b/lib/data/shared_prefs_service.dart @@ -1,3 +1,5 @@ +import 'dart:convert'; + import 'package:flutter/material.dart'; import 'package:lightmeter/data/models/supported_locale.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; @@ -5,6 +7,7 @@ import 'package:shared_preferences/shared_preferences.dart'; import 'models/ev_source_type.dart'; import 'models/film.dart'; +import 'models/metering_screen_layout_config.dart'; import 'models/theme_type.dart'; class UserPreferencesService { @@ -14,6 +17,7 @@ class UserPreferencesService { static const _evSourceTypeKey = "evSourceType"; static const _cameraEvCalibrationKey = "cameraEvCalibration"; static const _lightSensorEvCalibrationKey = "lightSensorEvCalibration"; + static const _meteringScreenLayoutKey = "meteringScreenLayout"; static const _filmKey = "film"; static const _caffeineKey = "caffeine"; @@ -80,6 +84,20 @@ class UserPreferencesService { bool get caffeine => _sharedPreferences.getBool(_caffeineKey) ?? false; set caffeine(bool value) => _sharedPreferences.setBool(_caffeineKey, value); + MeteringScreenLayoutConfig get meteringScreenLayout { + final configJson = _sharedPreferences.getString(_meteringScreenLayoutKey); + if (configJson != null) { + return MeteringScreenLayoutConfigJson.fromJson(json.decode(configJson)); + } else { + return { + MeteringScreenLayoutFeature.extremeExposurePairs: true, + MeteringScreenLayoutFeature.filmPicker: true, + }; + } + } + set meteringScreenLayout(MeteringScreenLayoutConfig value) => + _sharedPreferences.setString(_meteringScreenLayoutKey, json.encode(value.toJson())); + bool get haptics => _sharedPreferences.getBool(_hapticsKey) ?? true; set haptics(bool value) => _sharedPreferences.setBool(_hapticsKey, value); diff --git a/lib/providers/features_provider.dart b/lib/providers/metering_screen_layout_provider.dart similarity index 66% rename from lib/providers/features_provider.dart rename to lib/providers/metering_screen_layout_provider.dart index 2c11b25..83e3773 100644 --- a/lib/providers/features_provider.dart +++ b/lib/providers/metering_screen_layout_provider.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; - -enum MeteringScreenLayoutFeature { extremeExposurePairs, filmPicker } +import 'package:lightmeter/data/models/metering_screen_layout_config.dart'; +import 'package:lightmeter/data/shared_prefs_service.dart'; +import 'package:provider/provider.dart'; class MeteringScreenLayoutProvider extends StatefulWidget { final Widget child; @@ -16,52 +17,51 @@ class MeteringScreenLayoutProvider extends StatefulWidget { } class MeteringScreenLayoutProviderState extends State { - final Map _features = { - MeteringScreenLayoutFeature.extremeExposurePairs: true, - MeteringScreenLayoutFeature.filmPicker: true, - }; + late final MeteringScreenLayoutConfig _config = + context.read().meteringScreenLayout; @override Widget build(BuildContext context) { return MeteringScreenLayout( - features: Map.from(_features), + config: MeteringScreenLayoutConfig.from(_config), child: widget.child, ); } - void updateFeatures(Map features) { + void updateFeatures(MeteringScreenLayoutConfig config) { setState(() { - features.forEach((key, value) { - _features.update( + config.forEach((key, value) { + _config.update( key, (_) => value, ifAbsent: () => value, ); }); }); + context.read().meteringScreenLayout = _config; } } class MeteringScreenLayout extends InheritedModel { - final Map features; + final MeteringScreenLayoutConfig config; const MeteringScreenLayout({ - required this.features, + required this.config, required super.child, super.key, }); - static Map of(BuildContext context, {bool listen = true}) { + static MeteringScreenLayoutConfig of(BuildContext context, {bool listen = true}) { if (listen) { - return context.dependOnInheritedWidgetOfExactType()!.features; + return context.dependOnInheritedWidgetOfExactType()!.config; } else { - return context.findAncestorWidgetOfExactType()!.features; + return context.findAncestorWidgetOfExactType()!.config; } } static bool featureStatusOf(BuildContext context, MeteringScreenLayoutFeature feature) { return InheritedModel.inheritFrom(context, aspect: feature)! - .features[feature]!; + .config[feature]!; } @override @@ -73,7 +73,7 @@ class MeteringScreenLayout extends InheritedModel { Set dependencies, ) { for (final dependecy in dependencies) { - if (oldWidget.features[dependecy] != features[dependecy]) { + if (oldWidget.config[dependecy] != config[dependecy]) { return true; } } diff --git a/lib/screens/metering/components/camera_container/widget_container_camera.dart b/lib/screens/metering/components/camera_container/widget_container_camera.dart index 0a62cd6..0362b5f 100644 --- a/lib/screens/metering/components/camera_container/widget_container_camera.dart +++ b/lib/screens/metering/components/camera_container/widget_container_camera.dart @@ -2,9 +2,10 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:lightmeter/data/models/exposure_pair.dart'; import 'package:lightmeter/data/models/film.dart'; +import 'package:lightmeter/data/models/metering_screen_layout_config.dart'; import 'package:lightmeter/features.dart'; import 'package:lightmeter/platform_config.dart'; -import 'package:lightmeter/providers/features_provider.dart'; +import 'package:lightmeter/providers/metering_screen_layout_provider.dart'; import 'package:lightmeter/res/dimens.dart'; import 'package:lightmeter/screens/metering/components/camera_container/components/camera_view/widget_camera_view.dart'; import 'package:lightmeter/screens/metering/components/camera_container/models/camera_error_type.dart'; diff --git a/lib/screens/metering/components/shared/readings_container/widget_container_readings.dart b/lib/screens/metering/components/shared/readings_container/widget_container_readings.dart index d443add..313b408 100644 --- a/lib/screens/metering/components/shared/readings_container/widget_container_readings.dart +++ b/lib/screens/metering/components/shared/readings_container/widget_container_readings.dart @@ -1,10 +1,11 @@ import 'package:flutter/material.dart'; import 'package:lightmeter/data/models/exposure_pair.dart'; import 'package:lightmeter/data/models/film.dart'; +import 'package:lightmeter/data/models/metering_screen_layout_config.dart'; import 'package:lightmeter/features.dart'; import 'package:lightmeter/generated/l10n.dart'; import 'package:lightmeter/providers/equipment_profile_provider.dart'; -import 'package:lightmeter/providers/features_provider.dart'; +import 'package:lightmeter/providers/metering_screen_layout_provider.dart'; import 'package:lightmeter/res/dimens.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; diff --git a/lib/screens/settings/components/metering/components/metering_screen_layout/components/meterins_screen_layout_features_dialog/widget_dialog_metering_screen_layout_features.dart b/lib/screens/settings/components/metering/components/metering_screen_layout/components/meterins_screen_layout_features_dialog/widget_dialog_metering_screen_layout_features.dart index c37b72d..654a6c4 100644 --- a/lib/screens/settings/components/metering/components/metering_screen_layout/components/meterins_screen_layout_features_dialog/widget_dialog_metering_screen_layout_features.dart +++ b/lib/screens/settings/components/metering/components/metering_screen_layout/components/meterins_screen_layout_features_dialog/widget_dialog_metering_screen_layout_features.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; +import 'package:lightmeter/data/models/metering_screen_layout_config.dart'; import 'package:lightmeter/generated/l10n.dart'; -import 'package:lightmeter/providers/features_provider.dart'; +import 'package:lightmeter/providers/metering_screen_layout_provider.dart'; import 'package:lightmeter/res/dimens.dart'; class MeteringScreenLayoutFeaturesDialog extends StatefulWidget { @@ -13,7 +14,7 @@ class MeteringScreenLayoutFeaturesDialog extends StatefulWidget { class _MeteringScreenLayoutFeaturesDialogState extends State { late final _features = - Map.from(MeteringScreenLayout.of(context, listen: false)); + MeteringScreenLayoutConfig.from(MeteringScreenLayout.of(context, listen: false)); @override Widget build(BuildContext context) {