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';
|
|
|
|
|
2020-04-22 20:10:57 +02:00
|
|
|
const String autoPlayKey = 'autoPlay';
|
|
|
|
const String autoAddKey = 'autoAdd';
|
|
|
|
const String audioPositionKey = 'audioposition';
|
|
|
|
const String lastWorkKey = 'lastWork';
|
|
|
|
const String refreshdateKey = 'refreshdate';
|
|
|
|
const String themesKey = 'themes';
|
|
|
|
const String accentsKey = 'accents';
|
|
|
|
const String autoUpdateKey = 'autoupdate';
|
|
|
|
const String updateIntervalKey = 'updateInterval';
|
|
|
|
const String downloadUsingDataKey = 'downloadUsingData';
|
|
|
|
const String introKey = 'intro';
|
|
|
|
const String realDarkKey = 'realDark';
|
2020-04-27 19:26:33 +02:00
|
|
|
const String cacheMaxKey = 'cacheMax';
|
2020-04-22 20:10:57 +02:00
|
|
|
|
2020-02-20 10:09:21 +01:00
|
|
|
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()]
|
2020-04-22 20:10:57 +02:00
|
|
|
}));
|
2020-02-20 10:09:21 +01:00
|
|
|
}
|
2020-04-22 20:10:57 +02:00
|
|
|
print(prefs.getString(key));
|
|
|
|
return json
|
|
|
|
.decode(prefs.getString(key))['groups']
|
|
|
|
.cast<Map<String, Object>>()
|
|
|
|
.map<GroupEntity>(GroupEntity.fromJson)
|
|
|
|
.toList(growable: false);
|
|
|
|
}
|
|
|
|
|
2020-02-20 10:09:21 +01:00
|
|
|
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-04-22 20:10:57 +02:00
|
|
|
Future<bool> saveInt(int setting) async {
|
2020-02-22 13:25:06 +01:00
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
return prefs.setInt(key, setting);
|
|
|
|
}
|
|
|
|
|
2020-04-22 20:10:57 +02:00
|
|
|
Future<int> getInt() async {
|
2020-02-22 13:25:06 +01:00
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
2020-04-22 20:10:57 +02:00
|
|
|
if (prefs.getInt(key) == null) await prefs.setInt(key, 0);
|
2020-02-22 13:25:06 +01:00
|
|
|
return prefs.getInt(key);
|
|
|
|
}
|
2020-02-25 10:57:12 +01:00
|
|
|
|
2020-04-22 20:10:57 +02: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-04-22 20:10:57 +02:00
|
|
|
Future<List<String>> getStringList() async {
|
2020-02-25 10:57:12 +01:00
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
2020-04-22 20:10:57 +02:00
|
|
|
if (prefs.getStringList(key) == null) {
|
|
|
|
await prefs.setStringList(key, []);
|
|
|
|
}
|
2020-02-25 10:57:12 +01:00
|
|
|
return prefs.getStringList(key);
|
|
|
|
}
|
2020-03-01 13:17:06 +01:00
|
|
|
|
2020-04-22 20:10:57 +02:00
|
|
|
Future<bool> saveString(String string) async {
|
2020-03-01 13:17:06 +01:00
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
return prefs.setString(key, string);
|
|
|
|
}
|
|
|
|
|
2020-04-22 20:10:57 +02:00
|
|
|
Future<String> getString() async {
|
2020-03-01 13:17:06 +01:00
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
2020-04-22 20:10:57 +02:00
|
|
|
if (prefs.getString(key) == null) {
|
|
|
|
await prefs.setString(key, '');
|
|
|
|
}
|
2020-03-01 13:17:06 +01:00
|
|
|
return prefs.getString(key);
|
|
|
|
}
|
2020-02-20 10:09:21 +01:00
|
|
|
}
|