diff --git a/lib/models/settings.dart b/lib/models/settings.dart index 83fad43..dfa79be 100644 --- a/lib/models/settings.dart +++ b/lib/models/settings.dart @@ -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 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 { diff --git a/lib/services/settings_service.dart b/lib/services/settings_service.dart index f28556a..ec8a656 100644 --- a/lib/services/settings_service.dart +++ b/lib/services/settings_service.dart @@ -23,6 +23,8 @@ Future getMoodQueryMinutes() => _getIntSetting('mood_query_minutes'); Future getChessHours() => _getIntSetting('chess_hours'); Future getChessMinutes() => _getIntSetting('chess_minutes'); +Future 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 _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 _getStringSetting(String settingKey) => + SharedPreferences.getInstance().then((pref) => pref.getString(settingKey)); + void _setStringListSetting(String settingKey, List list) => SharedPreferences.getInstance() .then((pref) => pref.setStringList(settingKey, list)); @@ -40,6 +49,7 @@ Future?> _getStringListSetting(String settingKey) => Future loadSettingsFromLocalJSON() async { Map 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 loadSettings() async { List? 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 loadSettings() async { relapseCategories, QueryConfig(moodHours, moodMinutes, moodDays), QueryConfig(sleepHours, sleepMinutes, sleepDays), - TimeConfig(chessHours, chessMinutes)); + TimeConfig(chessHours, chessMinutes), + startedAt); } return null; }