add startedAt Settingsproperty

main
Julian Gegner 2023-03-05 20:43:02 +01:00
parent a5596b0934
commit d9272bdfb2
2 changed files with 19 additions and 3 deletions

View File

@ -6,9 +6,10 @@ class Settings {
final QueryConfig? moodQuery;
final QueryConfig? sleepQuery;
final TimeConfig? chessTime;
final DateTime startedAt;
Settings(this.group, this.relapseCategories, this.moodQuery, this.sleepQuery,
this.chessTime);
this.chessTime, this.startedAt);
Settings.fromJson(Map<String, dynamic> json)
: group = json['group'] as int,
@ -17,7 +18,8 @@ class Settings {
sleepQuery = QueryConfig.fromJson(json['sleep_query']),
chessTime = json['chess_time'] != null
? TimeConfig.fromJson(json['chess_time'])
: null;
: null,
startedAt = DateTime.parse(json['startedAt']);
}
class QueryConfig {

View File

@ -23,6 +23,8 @@ Future<int?> getMoodQueryMinutes() => _getIntSetting('mood_query_minutes');
Future<int?> getChessHours() => _getIntSetting('chess_hours');
Future<int?> getChessMinutes() => _getIntSetting('chess_minutes');
Future<String?> getStartDay() => _getStringSetting('startedAt');
void _setIntSetting(String settingKey, int settingValue) =>
SharedPreferences.getInstance()
.then((pref) => pref.setInt(settingKey, settingValue));
@ -30,6 +32,13 @@ void _setIntSetting(String settingKey, int settingValue) =>
Future<int?> _getIntSetting(String settingKey) =>
SharedPreferences.getInstance().then((pref) => pref.getInt(settingKey));
void _setStringSetting(String settingKey, String settingValue) =>
SharedPreferences.getInstance()
.then((pref) => pref.setString(settingKey, settingValue));
Future<String?> _getStringSetting(String settingKey) =>
SharedPreferences.getInstance().then((pref) => pref.getString(settingKey));
void _setStringListSetting(String settingKey, List<String> list) =>
SharedPreferences.getInstance()
.then((pref) => pref.setStringList(settingKey, list));
@ -40,6 +49,7 @@ Future<List<String>?> _getStringListSetting(String settingKey) =>
Future<void> loadSettingsFromLocalJSON() async {
Map<String, dynamic> configJSON = await loadLocalConfigJSON();
configJSON['startedAt'] = DateTime.now().toIso8601String();
Settings settings = Settings.fromJson(configJSON);
saveSettings(settings);
}
@ -53,6 +63,7 @@ void saveSettings(Settings settings) {
_setStringListSetting('sleep_query_days', settings.sleepQuery!.days!);
_setIntSetting('sleep_query_hours', settings.sleepQuery!.hours!);
_setIntSetting('sleep_query_minutes', settings.sleepQuery!.minutes!);
_setStringSetting('startedAt', DateTime.now().toIso8601String());
if (settings.chessTime != null) {
_setIntSetting('chess_hours', settings.chessTime!.hours!);
_setIntSetting('chess_minutes', settings.chessTime!.minutes!);
@ -70,6 +81,8 @@ Future<Settings?> loadSettings() async {
List<String>? sleepDays = await getSleepQueryDaysCategories();
int? chessHours = await getChessHours();
int? chessMinutes = await getChessMinutes();
DateTime startedAt =
DateTime.parse(await getStartDay() ?? DateTime.now().toIso8601String());
if (group != null) {
return Settings(
@ -77,7 +90,8 @@ Future<Settings?> loadSettings() async {
relapseCategories,
QueryConfig(moodHours, moodMinutes, moodDays),
QueryConfig(sleepHours, sleepMinutes, sleepDays),
TimeConfig(chessHours, chessMinutes));
TimeConfig(chessHours, chessMinutes),
startedAt);
}
return null;
}