2023-02-26 14:10:06 +01:00
|
|
|
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<int?> getGroup() => _getIntSetting('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');
|
|
|
|
|
|
|
|
Future<List<String>?> getMoodQueryDaysCategories() =>
|
|
|
|
_getStringListSetting('mood_query_days');
|
|
|
|
|
|
|
|
Future<int?> getMoodQueryHours() => _getIntSetting('mood_query_hours');
|
|
|
|
Future<int?> getMoodQueryMinutes() => _getIntSetting('mood_query_minutes');
|
|
|
|
|
|
|
|
Future<int?> getChessHours() => _getIntSetting('chess_hours');
|
|
|
|
Future<int?> getChessMinutes() => _getIntSetting('chess_minutes');
|
|
|
|
|
2023-03-05 20:43:02 +01:00
|
|
|
Future<String?> getStartDay() => _getStringSetting('startedAt');
|
|
|
|
|
2023-02-26 14:10:06 +01:00
|
|
|
void _setIntSetting(String settingKey, int settingValue) =>
|
|
|
|
SharedPreferences.getInstance()
|
|
|
|
.then((pref) => pref.setInt(settingKey, settingValue));
|
|
|
|
|
|
|
|
Future<int?> _getIntSetting(String settingKey) =>
|
|
|
|
SharedPreferences.getInstance().then((pref) => pref.getInt(settingKey));
|
|
|
|
|
2023-03-05 20:43:02 +01:00
|
|
|
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));
|
|
|
|
|
2023-02-26 14:10:06 +01:00
|
|
|
void _setStringListSetting(String settingKey, List<String> list) =>
|
|
|
|
SharedPreferences.getInstance()
|
|
|
|
.then((pref) => pref.setStringList(settingKey, list));
|
|
|
|
|
|
|
|
Future<List<String>?> _getStringListSetting(String settingKey) =>
|
|
|
|
SharedPreferences.getInstance()
|
|
|
|
.then((pref) => pref.getStringList(settingKey));
|
|
|
|
|
|
|
|
Future<void> loadSettingsFromLocalJSON() async {
|
|
|
|
Map<String, dynamic> configJSON = await loadLocalConfigJSON();
|
|
|
|
Settings settings = Settings.fromJson(configJSON);
|
|
|
|
saveSettings(settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
void saveSettings(Settings settings) {
|
2023-02-27 11:40:24 +01:00
|
|
|
_setIntSetting('group', settings.group);
|
2023-02-26 14:10:06 +01:00
|
|
|
_setStringListSetting('relapse_categories', settings.relapseCategories!);
|
2023-02-27 11:21:57 +01:00
|
|
|
_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-03-06 18:39:36 +01:00
|
|
|
_setStringSetting('startedAt', settings.startedAt.toIso8601String());
|
2023-02-26 14:10:06 +01:00
|
|
|
if (settings.chessTime != null) {
|
2023-02-27 11:21:57 +01:00
|
|
|
_setIntSetting('chess_hours', settings.chessTime!.hours!);
|
|
|
|
_setIntSetting('chess_minutes', settings.chessTime!.minutes!);
|
2023-02-26 14:10:06 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-27 11:27:45 +01:00
|
|
|
|
2023-02-27 11:39:42 +01:00
|
|
|
Future<Settings?> loadSettings() async {
|
2023-02-27 11:27:45 +01:00
|
|
|
int? group = await getGroup();
|
|
|
|
List<String>? relapseCategories = await getRelapseCategories();
|
|
|
|
int? moodHours = await getMoodQueryHours();
|
|
|
|
int? moodMinutes = await getMoodQueryMinutes();
|
|
|
|
List<String>? moodDays = await getMoodQueryDaysCategories();
|
|
|
|
int? sleepHours = await getSleepQueryHours();
|
|
|
|
int? sleepMinutes = await getSleepQueryMinutes();
|
|
|
|
List<String>? sleepDays = await getSleepQueryDaysCategories();
|
|
|
|
int? chessHours = await getChessHours();
|
|
|
|
int? chessMinutes = await getChessMinutes();
|
2023-03-05 20:43:02 +01:00
|
|
|
DateTime startedAt =
|
|
|
|
DateTime.parse(await getStartDay() ?? DateTime.now().toIso8601String());
|
2023-02-27 11:27:45 +01:00
|
|
|
|
2023-02-27 11:39:42 +01:00
|
|
|
if (group != null) {
|
|
|
|
return Settings(
|
|
|
|
group,
|
|
|
|
relapseCategories,
|
|
|
|
QueryConfig(moodHours, moodMinutes, moodDays),
|
|
|
|
QueryConfig(sleepHours, sleepMinutes, sleepDays),
|
2023-03-05 20:43:02 +01:00
|
|
|
TimeConfig(chessHours, chessMinutes),
|
|
|
|
startedAt);
|
2023-02-27 11:39:42 +01:00
|
|
|
}
|
|
|
|
return null;
|
2023-02-27 11:27:45 +01:00
|
|
|
}
|