tsacdop-podcast-app-android/lib/local_storage/key_value_storage.dart

86 lines
2.7 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:tsacdop/class/podcast_group.dart';
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';
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()}));
}
Future<bool> saveInt(int setting) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.setInt(key, setting);
}
Future<int> getInt() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
if (prefs.getInt(key) == null) await prefs.setInt(key, 0);
return prefs.getInt(key);
}
Future<bool> saveStringList(List<String> playList) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.setStringList(key, playList);
}
Future<List<String>> getStringList() async {
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 {
2020-03-01 13:17:06 +01:00
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.setString(key, string);
}
Future<String> getString() async {
2020-03-01 13:17:06 +01:00
SharedPreferences prefs = await SharedPreferences.getInstance();
if (prefs.getString(key) == null) {
await prefs.setString(key, '');
}
2020-03-01 13:17:06 +01:00
return prefs.getString(key);
}
}