2022-08-13 22:51:09 +02:00
|
|
|
import 'package:hive_flutter/hive_flutter.dart';
|
|
|
|
import 'package:immich_mobile/constants/hive_box.dart';
|
|
|
|
|
2022-08-20 23:19:40 +02:00
|
|
|
enum AppSettingsEnum<T> {
|
|
|
|
threeStageLoading<bool>("threeStageLoading", false),
|
|
|
|
themeMode<String>("themeMode", "system"), // "light","dark","system"
|
|
|
|
tilesPerRow<int>("tilesPerRow", 4),
|
|
|
|
storageIndicator<bool>("storageIndicator", true);
|
|
|
|
|
|
|
|
const AppSettingsEnum(this.hiveKey, this.defaultValue);
|
|
|
|
|
|
|
|
final String hiveKey;
|
|
|
|
final T defaultValue;
|
2022-08-13 22:51:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class AppSettingsService {
|
|
|
|
late final Box hiveBox;
|
|
|
|
|
|
|
|
AppSettingsService() {
|
|
|
|
hiveBox = Hive.box(userSettingInfoBox);
|
|
|
|
}
|
|
|
|
|
2022-08-20 23:19:40 +02:00
|
|
|
T getSetting<T>(AppSettingsEnum<T> settingType) {
|
|
|
|
if (!hiveBox.containsKey(settingType.hiveKey)) {
|
|
|
|
return _setDefault(settingType);
|
2022-08-13 22:51:09 +02:00
|
|
|
}
|
|
|
|
|
2022-08-20 23:19:40 +02:00
|
|
|
var result = hiveBox.get(settingType.hiveKey);
|
2022-08-13 22:51:09 +02:00
|
|
|
|
2022-08-20 23:19:40 +02:00
|
|
|
if (result is! T) {
|
|
|
|
return _setDefault(settingType);
|
2022-08-13 22:51:09 +02:00
|
|
|
}
|
|
|
|
|
2022-08-20 23:19:40 +02:00
|
|
|
return result;
|
2022-08-13 22:51:09 +02:00
|
|
|
}
|
|
|
|
|
2022-08-20 23:19:40 +02:00
|
|
|
setSetting<T>(AppSettingsEnum<T> settingType, T value) {
|
|
|
|
hiveBox.put(settingType.hiveKey, value);
|
2022-08-13 22:51:09 +02:00
|
|
|
}
|
|
|
|
|
2022-08-20 23:19:40 +02:00
|
|
|
T _setDefault<T>(AppSettingsEnum<T> settingType) {
|
|
|
|
hiveBox.put(settingType.hiveKey, settingType.defaultValue);
|
|
|
|
return settingType.defaultValue;
|
2022-08-13 22:51:09 +02:00
|
|
|
}
|
|
|
|
}
|