import 'package:shared_preferences/shared_preferences.dart'; import 'package:smoke_cess_app/models/settings.dart'; import 'package:smoke_cess_app/services/json_service.dart'; //access group setting which was saved in local storage Future getGroup() => _getIntSetting('group'); Future?> getRelapseCategories() => _getStringListSetting('relapse_categories'); Future?> getSleepQueryDaysCategories() => _getStringListSetting('sleep_query_days'); Future getSleepQueryHours() => _getIntSetting('sleep_query_hours'); Future getSleepQueryMinutes() => _getIntSetting('sleep_query_minutes'); Future?> getMoodQueryDaysCategories() => _getStringListSetting('mood_query_days'); Future getMoodQueryHours() => _getIntSetting('mood_query_hours'); 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)); 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)); Future?> _getStringListSetting(String settingKey) => SharedPreferences.getInstance() .then((pref) => pref.getStringList(settingKey)); Future loadSettingsFromLocalJSON() async { Map configJSON = await loadLocalConfigJSON(); Settings settings = Settings.fromJson(configJSON); saveSettings(settings); } void saveSettings(Settings settings) { _setIntSetting('group', settings.group); _setStringListSetting('relapse_categories', settings.relapseCategories!); _setStringListSetting('mood_query_days', settings.moodQuery!.days!); _setIntSetting('mood_query_hours', settings.moodQuery!.hours!); _setIntSetting('mood_query_minutes', settings.moodQuery!.minutes!); _setStringListSetting('sleep_query_days', settings.sleepQuery!.days!); _setIntSetting('sleep_query_hours', settings.sleepQuery!.hours!); _setIntSetting('sleep_query_minutes', settings.sleepQuery!.minutes!); _setStringSetting('startedAt', settings.startedAt.toIso8601String()); if (settings.chessTime != null) { _setIntSetting('chess_hours', settings.chessTime!.hours!); _setIntSetting('chess_minutes', settings.chessTime!.minutes!); } } Future loadSettings() async { int? group = await getGroup(); List? relapseCategories = await getRelapseCategories(); int? moodHours = await getMoodQueryHours(); int? moodMinutes = await getMoodQueryMinutes(); List? moodDays = await getMoodQueryDaysCategories(); int? sleepHours = await getSleepQueryHours(); int? sleepMinutes = await getSleepQueryMinutes(); 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( group, relapseCategories, QueryConfig(moodHours, moodMinutes, moodDays), QueryConfig(sleepHours, sleepMinutes, sleepDays), TimeConfig(chessHours, chessMinutes), startedAt); } return null; }