write list and nested properties as json

main
Julian Gegner 2023-02-19 17:31:46 +01:00
parent f67f44a030
commit ac994ad6e0
6 changed files with 89 additions and 35 deletions

View File

@ -1,7 +1,7 @@
{
"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,
@ -12,5 +12,4 @@
"hours": 9,
"minutes": 30
}
}

View File

@ -5,7 +5,7 @@
"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,
@ -16,5 +16,4 @@
"hours": 11,
"minutes": 30
}
}

View File

@ -1,13 +1,44 @@
import 'package:smoke_cess_app/service/json_service.dart';
class Settings {
final String group;
final String key;
//final List<String> relapseCategories;
final List<String>? 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<String, dynamic> 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<String>? days;
QueryConfig(this.hours, this.minutes, this.days);
QueryConfig.fromJson(Map<String, dynamic> 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<String, dynamic> json)
: hours = json['hours'] as int,
minutes = json['minutes'] as int;
}

View File

@ -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<Map<String, dynamic>> loadLocalConfigJSON() async {
String content = await rootBundle.loadString(configJSONPath);
return jsonDecode(content);
}
List<String>? jsonPropertyAsList(dynamic property) =>
property != null ? List.from(property) : null;

View File

@ -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<String?> getGroup() {
return getStringSetting('group');
}
Future<String?> getGroup() => _getStringSetting('group');
void setStringSetting(String settingKey, String settingValue) =>
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');
void _setStringSetting(String settingKey, String settingValue) =>
SharedPreferences.getInstance()
.then((pref) => pref.setString(settingKey, settingValue));
Future<String?> getStringSetting(String settingKey) =>
Future<String?> _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<int?> getIntSetting(String settingKey) =>
Future<int?> _getIntSetting(String settingKey) =>
SharedPreferences.getInstance().then((pref) => pref.getInt(settingKey));
void setStringList(String settingKey, List<String> list) =>
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));
//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<String> list) =>
Future<void> loadSettings() async {
Map<String, dynamic> 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);
}

View File

@ -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