diff --git a/assets/group1.json b/assets/group1.json index e5aaeda..eadf04c 100644 --- a/assets/group1.json +++ b/assets/group1.json @@ -1,16 +1,15 @@ { "group": "Gruppe 1", "HITT_time": 35, - "relapse categories": ["App stresst mich", "langeweile", "lunge bruacht es"], + "relapse_categories": ["App stresst mich", "langeweile", "lunge braucht es"], "mood_query": { "days": ["Montag", "Freitag"], "hours": 8, "minutes": 30 }, - "sleep_query": { - "days": ["Dienstag", "Samstag"], - "hours": 9, - "minutes": 30 - } - + "sleep_query": { + "days": ["Dienstag", "Samstag"], + "hours": 9, + "minutes": 30 + } } diff --git a/assets/group3.json b/assets/group3.json index e742219..6e1d75c 100644 --- a/assets/group3.json +++ b/assets/group3.json @@ -5,16 +5,15 @@ "hours": 8, "minutes": 30 }, - "relapse categories": ["App stresst mich", "langeweile", "lunge bruacht es"], + "relapse_categories": ["App stresst mich", "langeweile", "lunge braucht es"], "mood_query": { "days": ["Montag", "Freitag"], "hours": 10, "minutes": 30 }, - "sleep_query": { - "days": ["Dienstag", "Samstag"], - "hours": 11, - "minutes": 30 - } - + "sleep_query": { + "days": ["Dienstag", "Samstag"], + "hours": 11, + "minutes": 30 + } } diff --git a/lib/models/settings.dart b/lib/models/settings.dart index 4dced6e..0282083 100644 --- a/lib/models/settings.dart +++ b/lib/models/settings.dart @@ -1,13 +1,44 @@ +import 'package:smoke_cess_app/service/json_service.dart'; + class Settings { final String group; - final String key; - //final List relapseCategories; + final List? relapseCategories; + final QueryConfig moodQuery; + final QueryConfig sleepQuery; + final TimeConfig? chessTime; - Settings(this.group, this.key); //, this.relapseCategories); + Settings(this.group, this.relapseCategories, this.moodQuery, this.sleepQuery, + this.chessTime); Settings.fromJson(Map json) - : group = json['group'].toString(), - key = json['key'].toString() - //relapseCategories = json['relapse_categories'] - ; + : group = json['group'] as String, + relapseCategories = jsonPropertyAsList(json['relapse_categories']), + moodQuery = QueryConfig.fromJson(json['mood_query']), + sleepQuery = QueryConfig.fromJson(json['sleep_query']), + chessTime = + json['chess_time'] ? TimeConfig.fromJson(json['chess_time']) : null; +} + +class QueryConfig { + final int hours; + final int minutes; + final List? days; + + QueryConfig(this.hours, this.minutes, this.days); + + QueryConfig.fromJson(Map json) + : hours = json['hours'] as int, + minutes = json['minutes'] as int, + days = jsonPropertyAsList(json['days']); +} + +class TimeConfig { + final int hours; + final int minutes; + + TimeConfig(this.hours, this.minutes); + + TimeConfig.fromJson(Map json) + : hours = json['hours'] as int, + minutes = json['minutes'] as int; } diff --git a/lib/service/json_service.dart b/lib/service/json_service.dart index d20dacf..ca1b26a 100644 --- a/lib/service/json_service.dart +++ b/lib/service/json_service.dart @@ -1,9 +1,12 @@ import 'package:flutter/services.dart'; import 'dart:convert'; -const String configJSONPath = 'assets/config.json'; +const String configJSONPath = 'assets/group3.json'; Future> loadLocalConfigJSON() async { String content = await rootBundle.loadString(configJSONPath); return jsonDecode(content); } + +List? jsonPropertyAsList(dynamic property) => + property != null ? List.from(property) : null; diff --git a/lib/service/settings_service.dart b/lib/service/settings_service.dart index e42173b..ead6f9b 100644 --- a/lib/service/settings_service.dart +++ b/lib/service/settings_service.dart @@ -3,28 +3,45 @@ import 'package:smoke_cess_app/models/settings.dart'; import 'package:smoke_cess_app/service/json_service.dart'; //access group setting which was saved in local storage -Future getGroup() { - return getStringSetting('group'); -} +Future getGroup() => _getStringSetting('group'); -void setStringSetting(String settingKey, String settingValue) => +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'); + +void _setStringSetting(String settingKey, String settingValue) => SharedPreferences.getInstance() .then((pref) => pref.setString(settingKey, settingValue)); -Future getStringSetting(String settingKey) => +Future _getStringSetting(String settingKey) => SharedPreferences.getInstance().then((pref) => pref.getString(settingKey)); -void setIntSetting(String settingKey, int settingValue) => +void _setIntSetting(String settingKey, int settingValue) => SharedPreferences.getInstance() .then((pref) => pref.setInt(settingKey, settingValue)); -Future getIntSetting(String settingKey) => +Future _getIntSetting(String settingKey) => SharedPreferences.getInstance().then((pref) => pref.getInt(settingKey)); -void setStringList(String settingKey, List list) => +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)); + //Add other setters and getters if needed //other possible SharedPreferences Types: Int, Bool, Double, StringList //see https://pub.dev/packages/shared_preferences @@ -32,13 +49,16 @@ void setStringList(String settingKey, List list) => Future loadSettings() async { Map configJSON = await loadLocalConfigJSON(); Settings settings = Settings.fromJson(configJSON); - print(settings.group); saveSettings(settings); - String gruppe = (await getGroup())!; - print('Gruppe: $gruppe'); } void saveSettings(Settings settings) { - setStringSetting('group', settings.group); - setStringSetting('key', settings.key); + _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); } diff --git a/pubspec.yaml b/pubspec.yaml index 8502294..cf6766f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -64,6 +64,8 @@ flutter: # - images/a_dot_ham.jpeg assets: - config.json + - group1.json + - group3.json # An image asset can refer to one or more resolution-specific "variants", see # https://flutter.dev/assets-and-images/#resolution-aware