2023-02-16 01:59:15 +01:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2023-02-17 13:47:52 +01:00
|
|
|
import 'package:smoke_cess_app/models/settings.dart';
|
|
|
|
import 'package:smoke_cess_app/service/json_service.dart';
|
2023-02-16 01:59:15 +01:00
|
|
|
|
2023-02-19 14:24:39 +01:00
|
|
|
//access group setting which was saved in local storage
|
2023-02-19 17:31:46 +01:00
|
|
|
Future<String?> getGroup() => _getStringSetting('group');
|
|
|
|
|
|
|
|
Future<List<String>?> getRelapseCategories() =>
|
|
|
|
_getStringListSetting('relapse_categories');
|
|
|
|
|
|
|
|
Future<List<String>?> getSleepQueryDaysCategories() =>
|
|
|
|
_getStringListSetting('sleep_query_days');
|
|
|
|
|
|
|
|
Future<int?> getSleepQueryHours() => _getIntSetting('sleep_query_hours');
|
|
|
|
Future<int?> getSleepQueryMinutes() => _getIntSetting('sleep_query_minutes');
|
2023-02-19 14:24:39 +01:00
|
|
|
|
2023-02-19 17:31:46 +01:00
|
|
|
Future<List<String>?> getMoodQueryDaysCategories() =>
|
|
|
|
_getStringListSetting('mood_query_days');
|
|
|
|
|
|
|
|
Future<int?> getMoodQueryHours() => _getIntSetting('mood_query_hours');
|
|
|
|
Future<int?> getMoodQueryMinutes() => _getIntSetting('mood_query_minutes');
|
|
|
|
|
|
|
|
void _setStringSetting(String settingKey, String settingValue) =>
|
2023-02-19 15:06:57 +01:00
|
|
|
SharedPreferences.getInstance()
|
|
|
|
.then((pref) => pref.setString(settingKey, settingValue));
|
2023-02-19 14:24:39 +01:00
|
|
|
|
2023-02-19 17:31:46 +01:00
|
|
|
Future<String?> _getStringSetting(String settingKey) =>
|
2023-02-19 15:06:57 +01:00
|
|
|
SharedPreferences.getInstance().then((pref) => pref.getString(settingKey));
|
2023-02-19 14:24:39 +01:00
|
|
|
|
2023-02-19 17:31:46 +01:00
|
|
|
void _setIntSetting(String settingKey, int settingValue) =>
|
2023-02-19 15:06:57 +01:00
|
|
|
SharedPreferences.getInstance()
|
|
|
|
.then((pref) => pref.setInt(settingKey, settingValue));
|
2023-02-19 14:24:39 +01:00
|
|
|
|
2023-02-19 17:31:46 +01:00
|
|
|
Future<int?> _getIntSetting(String settingKey) =>
|
2023-02-19 15:06:57 +01:00
|
|
|
SharedPreferences.getInstance().then((pref) => pref.getInt(settingKey));
|
2023-02-19 14:24:39 +01:00
|
|
|
|
2023-02-19 17:31:46 +01:00
|
|
|
void _setStringListSetting(String settingKey, List<String> list) =>
|
2023-02-19 15:06:57 +01:00
|
|
|
SharedPreferences.getInstance()
|
|
|
|
.then((pref) => pref.setStringList(settingKey, list));
|
2023-02-19 14:24:39 +01:00
|
|
|
|
2023-02-19 17:31:46 +01:00
|
|
|
Future<List<String>?> _getStringListSetting(String settingKey) =>
|
|
|
|
SharedPreferences.getInstance()
|
|
|
|
.then((pref) => pref.getStringList(settingKey));
|
|
|
|
|
2023-02-19 14:24:39 +01:00
|
|
|
//Add other setters and getters if needed
|
|
|
|
//other possible SharedPreferences Types: Int, Bool, Double, StringList
|
|
|
|
//see https://pub.dev/packages/shared_preferences
|
|
|
|
|
|
|
|
Future<void> loadSettings() async {
|
|
|
|
Map<String, dynamic> configJSON = await loadLocalConfigJSON();
|
|
|
|
Settings settings = Settings.fromJson(configJSON);
|
|
|
|
saveSettings(settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
void saveSettings(Settings settings) {
|
2023-02-19 17:31:46 +01:00
|
|
|
_setStringSetting('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);
|
2023-02-16 01:59:15 +01:00
|
|
|
}
|