2020-02-20 10:09:21 +01:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:convert';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import 'package:tsacdop/class/podcast_group.dart';
|
|
|
|
|
|
|
|
class KeyValueStorage {
|
|
|
|
final String key;
|
|
|
|
KeyValueStorage(this.key);
|
|
|
|
|
|
|
|
Future<List<GroupEntity>> getGroups() async {
|
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
if (prefs.getString(key) == null) {
|
|
|
|
PodcastGroup home = PodcastGroup('Home');
|
|
|
|
await prefs.setString(
|
|
|
|
key,
|
|
|
|
json.encode({
|
|
|
|
'groups': [home.toEntity().toJson()]
|
|
|
|
}));}
|
|
|
|
print(prefs.getString(key));
|
|
|
|
return json
|
|
|
|
.decode(prefs.getString(key))['groups']
|
|
|
|
.cast<Map<String, Object>>()
|
|
|
|
.map<GroupEntity>(GroupEntity.fromJson)
|
|
|
|
.toList(growable: false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> saveGroup(List<GroupEntity> groupList) async {
|
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
return prefs.setString(
|
|
|
|
key,
|
|
|
|
json.encode(
|
|
|
|
{'groups': groupList.map((group) => group.toJson()).toList()}));
|
|
|
|
}
|
2020-02-22 13:25:06 +01:00
|
|
|
|
2020-03-01 13:17:06 +01:00
|
|
|
Future<bool> saveInt(int setting) async{
|
2020-02-22 13:25:06 +01:00
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
2020-03-01 13:17:06 +01:00
|
|
|
print(setting.toString());
|
2020-02-22 13:25:06 +01:00
|
|
|
return prefs.setInt(key, setting);
|
|
|
|
}
|
|
|
|
|
2020-03-01 13:17:06 +01:00
|
|
|
Future<int> getInt() async{
|
2020-02-22 13:25:06 +01:00
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
if(prefs.getInt(key) == null) await prefs.setInt(key, 0);
|
|
|
|
return prefs.getInt(key);
|
|
|
|
}
|
2020-02-25 10:57:12 +01:00
|
|
|
|
2020-02-26 10:54:59 +01:00
|
|
|
Future<bool> saveStringlist(List<String> playList) async{
|
2020-02-25 10:57:12 +01:00
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
return prefs.setStringList(key, playList);
|
|
|
|
}
|
|
|
|
|
2020-02-26 10:54:59 +01:00
|
|
|
Future<List<String>> getStringList() async{
|
2020-02-25 10:57:12 +01:00
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
if(prefs.getStringList(key) == null) {await prefs.setStringList(key, []);}
|
|
|
|
return prefs.getStringList(key);
|
|
|
|
}
|
2020-03-01 13:17:06 +01:00
|
|
|
|
|
|
|
Future<bool> saveString(String string) async{
|
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
return prefs.setString(key, string);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<String> getString() async{
|
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
if(prefs.getString(key) == null) {await prefs.setString(key, '');}
|
|
|
|
return prefs.getString(key);
|
|
|
|
}
|
|
|
|
|
2020-02-20 10:09:21 +01:00
|
|
|
}
|