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-11-11 20:05:11 +00:00
|
|
|
import 'package:lightmeter/data/models/camera_feature.dart';
|
2023-05-11 13:30:18 +00:00
|
|
|
import 'package:lightmeter/data/models/ev_source_type.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 13:30:18 +00:00
|
|
|
import 'package:lightmeter/data/models/theme_type.dart';
|
2023-07-09 11:39:33 +00:00
|
|
|
import 'package:lightmeter/data/models/volume_action.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-06-23 08:35:33 +00:00
|
|
|
static const isoKey = "iso";
|
|
|
|
static const ndFilterKey = "ndFilter";
|
2022-12-16 08:08:12 +00:00
|
|
|
|
2023-06-23 08:35:33 +00:00
|
|
|
static const evSourceTypeKey = "evSourceType";
|
2023-06-23 08:47:34 +00:00
|
|
|
static const stopTypeKey = "stopType";
|
2024-01-15 19:47:10 +00:00
|
|
|
static const showEv100Key = "showEv100";
|
2023-06-23 08:35:33 +00:00
|
|
|
static const cameraEvCalibrationKey = "cameraEvCalibration";
|
|
|
|
static const lightSensorEvCalibrationKey = "lightSensorEvCalibration";
|
|
|
|
static const meteringScreenLayoutKey = "meteringScreenLayout";
|
2023-11-11 20:05:11 +00:00
|
|
|
static const cameraFeaturesKey = "cameraFeatures";
|
2023-01-26 09:10:23 +00:00
|
|
|
|
2023-06-23 08:35:33 +00:00
|
|
|
static const caffeineKey = "caffeine";
|
|
|
|
static const hapticsKey = "haptics";
|
2024-05-07 17:24:51 +00:00
|
|
|
static const autostartTimerKey = "autostartTimer";
|
2023-07-09 11:39:33 +00:00
|
|
|
static const volumeActionKey = "volumeAction";
|
2023-06-23 08:35:33 +00:00
|
|
|
static const localeKey = "locale";
|
2023-02-11 12:58:47 +00:00
|
|
|
|
2023-06-23 08:35:33 +00:00
|
|
|
static const themeTypeKey = "themeType";
|
|
|
|
static const primaryColorKey = "primaryColor";
|
|
|
|
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) {
|
2023-06-23 08:35:33 +00:00
|
|
|
migrateOldKeys();
|
2023-02-17 19:32:25 +00:00
|
|
|
}
|
|
|
|
|
2023-06-23 08:35:33 +00:00
|
|
|
@visibleForTesting
|
|
|
|
Future<void> migrateOldKeys() async {
|
2023-02-17 19:32:25 +00:00
|
|
|
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-11-11 20:05:11 +00:00
|
|
|
IsoValue get iso => IsoValue.values.firstWhere((v) => v.value == (_sharedPreferences.getInt(isoKey) ?? 100));
|
2023-06-23 08:35:33 +00:00
|
|
|
set iso(IsoValue value) => _sharedPreferences.setInt(isoKey, value.value);
|
2022-12-16 08:08:12 +00:00
|
|
|
|
2023-11-11 20:05:11 +00:00
|
|
|
NdValue get ndFilter => NdValue.values.firstWhere((v) => v.value == (_sharedPreferences.getInt(ndFilterKey) ?? 0));
|
2023-06-23 08:35:33 +00:00
|
|
|
set ndFilter(NdValue value) => _sharedPreferences.setInt(ndFilterKey, value.value);
|
2022-12-16 20:07:05 +00:00
|
|
|
|
2023-11-11 20:05:11 +00:00
|
|
|
EvSourceType get evSourceType => EvSourceType.values[_sharedPreferences.getInt(evSourceTypeKey) ?? 0];
|
2023-06-23 08:35:33 +00:00
|
|
|
set evSourceType(EvSourceType value) => _sharedPreferences.setInt(evSourceTypeKey, value.index);
|
2023-01-29 16:57:47 +00:00
|
|
|
|
2023-06-23 08:47:34 +00:00
|
|
|
StopType get stopType => StopType.values[_sharedPreferences.getInt(stopTypeKey) ?? 2];
|
|
|
|
set stopType(StopType value) => _sharedPreferences.setInt(stopTypeKey, value.index);
|
|
|
|
|
2024-01-15 19:47:10 +00:00
|
|
|
bool get showEv100 => _sharedPreferences.getBool(showEv100Key) ?? false;
|
|
|
|
set showEv100(bool value) => _sharedPreferences.setBool(showEv100Key, value);
|
|
|
|
|
2023-04-05 19:15:11 +00:00
|
|
|
MeteringScreenLayoutConfig get meteringScreenLayout {
|
2023-06-23 08:35:33 +00:00
|
|
|
final configJson = _sharedPreferences.getString(meteringScreenLayoutKey);
|
2023-04-05 19:15:11 +00:00
|
|
|
if (configJson != null) {
|
2023-05-11 13:30:18 +00:00
|
|
|
return MeteringScreenLayoutConfigJson.fromJson(
|
|
|
|
json.decode(configJson) as Map<String, dynamic>,
|
|
|
|
);
|
2023-04-05 19:15:11 +00:00
|
|
|
} else {
|
|
|
|
return {
|
2023-09-02 08:32:08 +00:00
|
|
|
MeteringScreenLayoutFeature.equipmentProfiles: true,
|
2023-04-05 19:15:11 +00:00
|
|
|
MeteringScreenLayoutFeature.extremeExposurePairs: true,
|
|
|
|
MeteringScreenLayoutFeature.filmPicker: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set meteringScreenLayout(MeteringScreenLayoutConfig value) =>
|
2023-06-23 08:35:33 +00:00
|
|
|
_sharedPreferences.setString(meteringScreenLayoutKey, json.encode(value.toJson()));
|
2023-04-05 19:15:11 +00:00
|
|
|
|
2023-11-11 20:05:11 +00:00
|
|
|
CameraFeaturesConfig get cameraFeatures {
|
|
|
|
final configJson = _sharedPreferences.getString(cameraFeaturesKey);
|
|
|
|
if (configJson != null) {
|
|
|
|
return CameraFeaturesConfigJson.fromJson(json.decode(configJson) as Map<String, dynamic>);
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
CameraFeature.spotMetering: false,
|
|
|
|
CameraFeature.histogram: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set cameraFeatures(CameraFeaturesConfig value) =>
|
|
|
|
_sharedPreferences.setString(cameraFeaturesKey, json.encode(value.toJson()));
|
|
|
|
|
2023-07-09 11:39:33 +00:00
|
|
|
bool get caffeine => _sharedPreferences.getBool(caffeineKey) ?? false;
|
|
|
|
set caffeine(bool value) => _sharedPreferences.setBool(caffeineKey, value);
|
|
|
|
|
2023-06-23 08:35:33 +00:00
|
|
|
bool get haptics => _sharedPreferences.getBool(hapticsKey) ?? true;
|
|
|
|
set haptics(bool value) => _sharedPreferences.setBool(hapticsKey, value);
|
2023-01-21 10:37:49 +00:00
|
|
|
|
2024-05-07 17:24:51 +00:00
|
|
|
bool get autostartTimer => _sharedPreferences.getBool(autostartTimerKey) ?? true;
|
|
|
|
set autostartTimer(bool value) => _sharedPreferences.setBool(autostartTimerKey, value);
|
|
|
|
|
2023-07-09 11:39:33 +00:00
|
|
|
VolumeAction get volumeAction => VolumeAction.values.firstWhere(
|
|
|
|
(e) => e.toString() == _sharedPreferences.getString(volumeActionKey),
|
|
|
|
orElse: () => VolumeAction.shutter,
|
|
|
|
);
|
2023-11-11 20:05:11 +00:00
|
|
|
set volumeAction(VolumeAction value) => _sharedPreferences.setString(volumeActionKey, value.toString());
|
2023-07-09 11:39:33 +00:00
|
|
|
|
2023-02-11 19:19:18 +00:00
|
|
|
SupportedLocale get locale => SupportedLocale.values.firstWhere(
|
2023-06-23 08:35:33 +00:00
|
|
|
(e) => e.toString() == _sharedPreferences.getString(localeKey),
|
2023-02-11 19:19:18 +00:00
|
|
|
orElse: () => SupportedLocale.en,
|
|
|
|
);
|
2023-06-23 08:35:33 +00:00
|
|
|
set locale(SupportedLocale value) => _sharedPreferences.setString(localeKey, value.toString());
|
2023-02-11 19:19:18 +00:00
|
|
|
|
2023-06-23 08:35:33 +00:00
|
|
|
double get cameraEvCalibration => _sharedPreferences.getDouble(cameraEvCalibrationKey) ?? 0.0;
|
2023-11-11 20:05:11 +00:00
|
|
|
set cameraEvCalibration(double value) => _sharedPreferences.setDouble(cameraEvCalibrationKey, value);
|
2023-01-26 09:10:23 +00:00
|
|
|
|
2023-11-11 20:05:11 +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
|
|
|
|
2023-06-23 08:35:33 +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-06-23 08:35:33 +00:00
|
|
|
Color get primaryColor => Color(_sharedPreferences.getInt(primaryColorKey) ?? 0xff2196f3);
|
|
|
|
set primaryColor(Color value) => _sharedPreferences.setInt(primaryColorKey, value.value);
|
2023-01-31 21:24:26 +00:00
|
|
|
|
2023-06-23 08:35:33 +00:00
|
|
|
bool get dynamicColor => _sharedPreferences.getBool(dynamicColorKey) ?? false;
|
|
|
|
set dynamicColor(bool value) => _sharedPreferences.setBool(dynamicColorKey, value);
|
2022-12-16 08:08:12 +00:00
|
|
|
}
|