2023-05-04 07:13:24 -04:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2023-05-03 17:24:27 -04:00
|
|
|
class SettingsEntry<T> {
|
|
|
|
final T? value;
|
|
|
|
final T deflt;
|
2023-05-03 11:51:18 -04:00
|
|
|
|
2023-05-03 17:24:27 -04:00
|
|
|
const SettingsEntry({this.value, required this.deflt});
|
|
|
|
|
|
|
|
factory SettingsEntry.fromMap(Map map) {
|
|
|
|
return SettingsEntry<T>(
|
2023-05-04 07:13:24 -04:00
|
|
|
value: jsonDecode(map["value"]) as T?,
|
2023-05-03 17:24:27 -04:00
|
|
|
deflt: map["default"],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Map toMap() {
|
|
|
|
return {
|
|
|
|
"value": value.toString(),
|
|
|
|
"default": deflt,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
T get valueOrDefault => value ?? deflt;
|
|
|
|
|
|
|
|
SettingsEntry<T> withValue({required T newValue}) => SettingsEntry(value: newValue, deflt: deflt);
|
|
|
|
|
|
|
|
SettingsEntry<T> passThrough(T? newValue) {
|
|
|
|
return newValue == null ? this : this.withValue(newValue: newValue);
|
|
|
|
}
|
|
|
|
}
|
2023-05-03 11:51:18 -04:00
|
|
|
|
2023-05-03 17:24:27 -04:00
|
|
|
class Settings {
|
|
|
|
final SettingsEntry<bool> notificationsDenied;
|
|
|
|
final SettingsEntry<int> unreadCheckIntervalMinutes;
|
2023-05-03 15:55:34 -04:00
|
|
|
|
2023-05-03 17:24:27 -04:00
|
|
|
const Settings({
|
|
|
|
this.notificationsDenied = const SettingsEntry(deflt: false),
|
|
|
|
this.unreadCheckIntervalMinutes = const SettingsEntry(deflt: 60),
|
|
|
|
});
|
2023-05-03 15:55:34 -04:00
|
|
|
|
|
|
|
factory Settings.fromMap(Map map) {
|
|
|
|
return Settings(
|
2023-05-03 17:24:27 -04:00
|
|
|
notificationsDenied: SettingsEntry.fromMap(map["notificationsDenied"]),
|
|
|
|
unreadCheckIntervalMinutes: SettingsEntry.fromMap(map["unreadCheckIntervalMinutes"]),
|
2023-05-03 15:55:34 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Map toMap() {
|
|
|
|
return {
|
2023-05-03 17:24:27 -04:00
|
|
|
"notificationsDenied": notificationsDenied.toMap(),
|
|
|
|
"unreadCheckIntervalMinutes": unreadCheckIntervalMinutes.toMap(),
|
2023-05-03 15:55:34 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Settings copy() => copyWith();
|
|
|
|
|
|
|
|
Settings copyWith({bool? notificationsDenied, int? unreadCheckIntervalMinutes}) {
|
|
|
|
return Settings(
|
2023-05-03 17:24:27 -04:00
|
|
|
notificationsDenied: this.notificationsDenied.passThrough(notificationsDenied),
|
|
|
|
unreadCheckIntervalMinutes: this.unreadCheckIntervalMinutes.passThrough(unreadCheckIntervalMinutes),
|
2023-05-03 15:55:34 -04:00
|
|
|
);
|
|
|
|
}
|
2023-05-03 17:24:27 -04:00
|
|
|
|
|
|
|
|
2023-05-03 11:51:18 -04:00
|
|
|
}
|