m3_lightmeter/lib/data/shared_prefs_service.dart

103 lines
4.4 KiB
Dart
Raw Permalink Normal View History

import 'package:flutter/material.dart';
import 'package:lightmeter/data/models/supported_locale.dart';
2022-12-16 08:08:12 +00:00
import 'package:shared_preferences/shared_preferences.dart';
import 'models/ev_source_type.dart';
import 'models/photography_values/iso_value.dart';
import 'models/photography_values/nd_value.dart';
import 'models/theme_type.dart';
2022-12-16 08:08:12 +00:00
class UserPreferencesService {
static const _isoKey = "iso";
static const _ndFilterKey = "ndFilter";
2022-12-16 08:08:12 +00:00
static const _evSourceTypeKey = "evSourceType";
static const _cameraEvCalibrationKey = "cameraEvCalibration";
static const _lightSensorEvCalibrationKey = "lightSensorEvCalibration";
static const _caffeineKey = "caffeine";
static const _hapticsKey = "haptics";
static const _localeKey = "locale";
static const _themeTypeKey = "themeType";
static const _primaryColorKey = "primaryColor";
static const _dynamicColorKey = "dynamicColor";
2022-12-16 08:08:12 +00:00
final SharedPreferences _sharedPreferences;
2023-02-16 19:57:59 +00:00
UserPreferencesService(this._sharedPreferences) {
2023-02-17 17:41:03 +00:00
_migrateOldKeys();
}
Future<void> _migrateOldKeys() async {
2023-02-16 19:57:59 +00:00
final legacyIsoIndex = _sharedPreferences.getInt("curIsoIndex");
if (legacyIsoIndex != null) {
iso = isoValues[legacyIsoIndex];
2023-02-17 17:41:03 +00:00
await _sharedPreferences.remove("curIsoIndex");
2023-02-16 19:57:59 +00:00
}
final legacyNdIndex = _sharedPreferences.getInt("curndIndex");
if (legacyNdIndex != null) {
/// Legacy ND list has 1 extra value at the end, so this check is needed
if (legacyNdIndex < ndValues.length) {
ndFilter = ndValues[legacyNdIndex];
}
2023-02-17 17:41:03 +00:00
await _sharedPreferences.remove("curndIndex");
2023-02-16 19:57:59 +00:00
}
final legacyCameraCalibration = _sharedPreferences.getDouble("cameraCalibr");
if (legacyCameraCalibration != null) {
cameraEvCalibration = legacyCameraCalibration;
2023-02-17 17:41:03 +00:00
await _sharedPreferences.remove("cameraCalibr");
2023-02-16 19:57:59 +00:00
}
final legacyLightSensorCalibration = _sharedPreferences.getDouble("sensorCalibr");
if (legacyLightSensorCalibration != null) {
lightSensorEvCalibration = legacyLightSensorCalibration;
2023-02-17 17:41:03 +00:00
await _sharedPreferences.remove("sensorCalibr");
2023-02-16 19:57:59 +00:00
}
final legacyHaptics = _sharedPreferences.getBool("vibrate");
if (legacyHaptics != null) {
haptics = legacyHaptics;
2023-02-17 17:41:03 +00:00
await _sharedPreferences.remove("vibrate");
2023-02-16 19:57:59 +00:00
}
}
2022-12-16 08:08:12 +00:00
IsoValue get iso => isoValues.firstWhere((v) => v.value == (_sharedPreferences.getInt(_isoKey) ?? 100));
set iso(IsoValue value) => _sharedPreferences.setInt(_isoKey, value.value);
NdValue get ndFilter => ndValues.firstWhere((v) => v.value == (_sharedPreferences.getInt(_ndFilterKey) ?? 0));
set ndFilter(NdValue value) => _sharedPreferences.setInt(_ndFilterKey, value.value);
EvSourceType get evSourceType => EvSourceType.values[_sharedPreferences.getInt(_evSourceTypeKey) ?? 0];
set evSourceType(EvSourceType value) => _sharedPreferences.setInt(_evSourceTypeKey, value.index);
bool get caffeine => _sharedPreferences.getBool(_caffeineKey) ?? false;
set caffeine(bool value) => _sharedPreferences.setBool(_caffeineKey, value);
bool get haptics => _sharedPreferences.getBool(_hapticsKey) ?? true;
set haptics(bool value) => _sharedPreferences.setBool(_hapticsKey, value);
SupportedLocale get locale => SupportedLocale.values.firstWhere(
(e) => e.toString() == _sharedPreferences.getString(_localeKey),
orElse: () => SupportedLocale.en,
);
set locale(SupportedLocale value) => _sharedPreferences.setString(_localeKey, value.toString());
double get cameraEvCalibration => _sharedPreferences.getDouble(_cameraEvCalibrationKey) ?? 0.0;
set cameraEvCalibration(double value) => _sharedPreferences.setDouble(_cameraEvCalibrationKey, value);
double get lightSensorEvCalibration => _sharedPreferences.getDouble(_lightSensorEvCalibrationKey) ?? 0.0;
set lightSensorEvCalibration(double value) => _sharedPreferences.setDouble(_lightSensorEvCalibrationKey, value);
ThemeType get themeType => ThemeType.values[_sharedPreferences.getInt(_themeTypeKey) ?? 0];
set themeType(ThemeType value) => _sharedPreferences.setInt(_themeTypeKey, value.index);
Color get primaryColor => Color(_sharedPreferences.getInt(_primaryColorKey) ?? 0xff2196f3);
set primaryColor(Color value) => _sharedPreferences.setInt(_primaryColorKey, value.value);
bool get dynamicColor => _sharedPreferences.getBool(_dynamicColorKey) ?? false;
set dynamicColor(bool value) => _sharedPreferences.setBool(_dynamicColorKey, value);
2022-12-16 08:08:12 +00:00
}