2023-04-05 19:15:11 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2023-01-31 21:24:26 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2023-05-11 12:53:04 +00:00
|
|
|
import 'package:lightmeter/data/models/ev_source_type.dart';
|
|
|
|
import 'package:lightmeter/data/models/film.dart';
|
|
|
|
import 'package:lightmeter/data/models/metering_screen_layout_config.dart';
|
2023-02-11 19:19:18 +00:00
|
|
|
import 'package:lightmeter/data/models/supported_locale.dart';
|
2023-05-11 12:53:04 +00:00
|
|
|
import 'package:lightmeter/data/models/theme_type.dart';
|
2023-03-30 19:24:18 +00:00
|
|
|
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
|
2022-12-16 08:08:12 +00:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
|
|
|
|
class UserPreferencesService {
|
2023-01-21 10:37:49 +00:00
|
|
|
static const _isoKey = "iso";
|
2023-01-29 16:57:47 +00:00
|
|
|
static const _ndFilterKey = "ndFilter";
|
2022-12-16 08:08:12 +00:00
|
|
|
|
2023-01-29 16:57:47 +00:00
|
|
|
static const _evSourceTypeKey = "evSourceType";
|
2023-01-26 09:10:23 +00:00
|
|
|
static const _cameraEvCalibrationKey = "cameraEvCalibration";
|
2023-01-29 16:57:47 +00:00
|
|
|
static const _lightSensorEvCalibrationKey = "lightSensorEvCalibration";
|
2023-04-05 19:15:11 +00:00
|
|
|
static const _meteringScreenLayoutKey = "meteringScreenLayout";
|
2023-04-01 19:04:55 +00:00
|
|
|
static const _filmKey = "film";
|
2023-01-26 09:10:23 +00:00
|
|
|
|
2023-02-11 12:58:47 +00:00
|
|
|
static const _caffeineKey = "caffeine";
|
2023-01-21 10:37:49 +00:00
|
|
|
static const _hapticsKey = "haptics";
|
2023-02-11 19:19:18 +00:00
|
|
|
static const _localeKey = "locale";
|
2023-02-11 12:58:47 +00:00
|
|
|
|
2023-01-21 10:37:49 +00:00
|
|
|
static const _themeTypeKey = "themeType";
|
2023-01-31 21:24:26 +00:00
|
|
|
static const _primaryColorKey = "primaryColor";
|
2023-01-25 10:08:11 +00:00
|
|
|
static const _dynamicColorKey = "dynamicColor";
|
2022-12-16 20:07:05 +00:00
|
|
|
|
2022-12-16 08:08:12 +00:00
|
|
|
final SharedPreferences _sharedPreferences;
|
|
|
|
|
2023-02-17 19:32:25 +00:00
|
|
|
UserPreferencesService(this._sharedPreferences) {
|
|
|
|
_migrateOldKeys();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _migrateOldKeys() async {
|
|
|
|
final legacyIsoIndex = _sharedPreferences.getInt("curIsoIndex");
|
|
|
|
if (legacyIsoIndex != null) {
|
2023-04-05 19:15:11 +00:00
|
|
|
iso = IsoValue.values[legacyIsoIndex];
|
2023-02-17 19:32:25 +00:00
|
|
|
await _sharedPreferences.remove("curIsoIndex");
|
|
|
|
}
|
|
|
|
|
|
|
|
final legacyNdIndex = _sharedPreferences.getInt("curndIndex");
|
|
|
|
if (legacyNdIndex != null) {
|
|
|
|
/// Legacy ND list has 1 extra value at the end, so this check is needed
|
2023-04-05 19:15:11 +00:00
|
|
|
if (legacyNdIndex < NdValue.values.length) {
|
|
|
|
ndFilter = NdValue.values[legacyNdIndex];
|
2023-02-17 19:32:25 +00:00
|
|
|
}
|
|
|
|
await _sharedPreferences.remove("curndIndex");
|
|
|
|
}
|
|
|
|
|
|
|
|
final legacyCameraCalibration = _sharedPreferences.getDouble("cameraCalibr");
|
|
|
|
if (legacyCameraCalibration != null) {
|
|
|
|
cameraEvCalibration = legacyCameraCalibration;
|
|
|
|
await _sharedPreferences.remove("cameraCalibr");
|
|
|
|
}
|
|
|
|
|
|
|
|
final legacyLightSensorCalibration = _sharedPreferences.getDouble("sensorCalibr");
|
|
|
|
if (legacyLightSensorCalibration != null) {
|
|
|
|
lightSensorEvCalibration = legacyLightSensorCalibration;
|
|
|
|
await _sharedPreferences.remove("sensorCalibr");
|
|
|
|
}
|
|
|
|
|
|
|
|
final legacyHaptics = _sharedPreferences.getBool("vibrate");
|
|
|
|
if (legacyHaptics != null) {
|
|
|
|
haptics = legacyHaptics;
|
|
|
|
await _sharedPreferences.remove("vibrate");
|
|
|
|
}
|
|
|
|
}
|
2022-12-16 08:08:12 +00:00
|
|
|
|
2023-03-30 19:24:18 +00:00
|
|
|
IsoValue get iso =>
|
2023-04-05 19:15:11 +00:00
|
|
|
IsoValue.values.firstWhere((v) => v.value == (_sharedPreferences.getInt(_isoKey) ?? 100));
|
2022-12-16 08:08:12 +00:00
|
|
|
set iso(IsoValue value) => _sharedPreferences.setInt(_isoKey, value.value);
|
|
|
|
|
2023-03-30 19:24:18 +00:00
|
|
|
NdValue get ndFilter =>
|
2023-04-05 19:15:11 +00:00
|
|
|
NdValue.values.firstWhere((v) => v.value == (_sharedPreferences.getInt(_ndFilterKey) ?? 0));
|
2022-12-16 08:08:12 +00:00
|
|
|
set ndFilter(NdValue value) => _sharedPreferences.setInt(_ndFilterKey, value.value);
|
2022-12-16 20:07:05 +00:00
|
|
|
|
2023-03-30 19:24:18 +00:00
|
|
|
EvSourceType get evSourceType =>
|
|
|
|
EvSourceType.values[_sharedPreferences.getInt(_evSourceTypeKey) ?? 0];
|
2023-01-29 16:57:47 +00:00
|
|
|
set evSourceType(EvSourceType value) => _sharedPreferences.setInt(_evSourceTypeKey, value.index);
|
|
|
|
|
2023-02-11 12:58:47 +00:00
|
|
|
bool get caffeine => _sharedPreferences.getBool(_caffeineKey) ?? false;
|
|
|
|
set caffeine(bool value) => _sharedPreferences.setBool(_caffeineKey, value);
|
|
|
|
|
2023-04-05 19:15:11 +00:00
|
|
|
MeteringScreenLayoutConfig get meteringScreenLayout {
|
|
|
|
final configJson = _sharedPreferences.getString(_meteringScreenLayoutKey);
|
|
|
|
if (configJson != null) {
|
2023-05-11 12:58:57 +00:00
|
|
|
return MeteringScreenLayoutConfigJson.fromJson(
|
|
|
|
json.decode(configJson) as Map<String, dynamic>,
|
|
|
|
);
|
2023-04-05 19:15:11 +00:00
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
MeteringScreenLayoutFeature.extremeExposurePairs: true,
|
|
|
|
MeteringScreenLayoutFeature.filmPicker: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set meteringScreenLayout(MeteringScreenLayoutConfig value) =>
|
|
|
|
_sharedPreferences.setString(_meteringScreenLayoutKey, json.encode(value.toJson()));
|
|
|
|
|
2023-02-11 12:58:47 +00:00
|
|
|
bool get haptics => _sharedPreferences.getBool(_hapticsKey) ?? true;
|
2023-01-21 10:37:49 +00:00
|
|
|
set haptics(bool value) => _sharedPreferences.setBool(_hapticsKey, value);
|
|
|
|
|
2023-02-11 19:19:18 +00:00
|
|
|
SupportedLocale get locale => SupportedLocale.values.firstWhere(
|
|
|
|
(e) => e.toString() == _sharedPreferences.getString(_localeKey),
|
|
|
|
orElse: () => SupportedLocale.en,
|
|
|
|
);
|
|
|
|
set locale(SupportedLocale value) => _sharedPreferences.setString(_localeKey, value.toString());
|
|
|
|
|
2023-01-26 09:10:23 +00:00
|
|
|
double get cameraEvCalibration => _sharedPreferences.getDouble(_cameraEvCalibrationKey) ?? 0.0;
|
2023-03-30 19:24:18 +00:00
|
|
|
set cameraEvCalibration(double value) =>
|
|
|
|
_sharedPreferences.setDouble(_cameraEvCalibrationKey, value);
|
2023-01-26 09:10:23 +00:00
|
|
|
|
2023-03-30 19:24:18 +00:00
|
|
|
double get lightSensorEvCalibration =>
|
|
|
|
_sharedPreferences.getDouble(_lightSensorEvCalibrationKey) ?? 0.0;
|
|
|
|
set lightSensorEvCalibration(double value) =>
|
|
|
|
_sharedPreferences.setDouble(_lightSensorEvCalibrationKey, value);
|
2023-01-29 16:57:47 +00:00
|
|
|
|
2022-12-16 20:07:05 +00:00
|
|
|
ThemeType get themeType => ThemeType.values[_sharedPreferences.getInt(_themeTypeKey) ?? 0];
|
|
|
|
set themeType(ThemeType value) => _sharedPreferences.setInt(_themeTypeKey, value.index);
|
2023-01-22 19:30:29 +00:00
|
|
|
|
2023-01-31 21:24:26 +00:00
|
|
|
Color get primaryColor => Color(_sharedPreferences.getInt(_primaryColorKey) ?? 0xff2196f3);
|
|
|
|
set primaryColor(Color value) => _sharedPreferences.setInt(_primaryColorKey, value.value);
|
|
|
|
|
2023-01-25 10:08:11 +00:00
|
|
|
bool get dynamicColor => _sharedPreferences.getBool(_dynamicColorKey) ?? false;
|
|
|
|
set dynamicColor(bool value) => _sharedPreferences.setBool(_dynamicColorKey, value);
|
2023-03-30 19:24:18 +00:00
|
|
|
|
2023-04-01 19:04:55 +00:00
|
|
|
Film get film => Film.values.firstWhere(
|
|
|
|
(e) => e.name == _sharedPreferences.getString(_filmKey),
|
|
|
|
orElse: () => Film.values.first,
|
|
|
|
);
|
|
|
|
set film(Film value) => _sharedPreferences.setString(_filmKey, value.name);
|
|
|
|
|
2023-03-30 19:24:18 +00:00
|
|
|
String get selectedEquipmentProfileId => '';
|
|
|
|
set selectedEquipmentProfileId(String id) {}
|
|
|
|
|
|
|
|
List<EquipmentProfileData> get equipmentProfiles => [];
|
|
|
|
set equipmentProfiles(List<EquipmentProfileData> profiles) {}
|
2022-12-16 08:08:12 +00:00
|
|
|
}
|