1
0
mirror of https://github.com/stonega/tsacdop synced 2024-12-16 18:30:31 +01:00
tsacdop-podcast-app-android/lib/local_storage/key_value_storage.dart
stonegate c5f2e4bdc1 Add getBool/saveBool methods in key_value_storage.
Change default layout to layout.two.
2020-07-16 15:18:45 +08:00

134 lines
4.7 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../state/podcast_group.dart';
const String autoPlayKey = 'autoPlay';
const String audioPositionKey = 'audioposition';
const String lastWorkKey = 'lastWork';
const String refreshdateKey = 'refreshdate';
const String themesKey = 'themes';
const String accentsKey = 'accents';
const String autoUpdateKey = 'autoAdd';
const String updateIntervalKey = 'updateInterval';
const String downloadUsingDataKey = 'downloadUsingData';
const String introKey = 'intro';
const String realDarkKey = 'realDark';
const String cacheMaxKey = 'cacheMax';
const String podcastLayoutKey = 'podcastLayoutKey';
const String recentLayoutKey = 'recentLayoutKey';
const String favLayoutKey = 'favLayoutKey';
const String downloadLayoutKey = 'downloadLayoutKey';
const String autoDownloadNetworkKey = 'autoDownloadNetwork';
const String episodePopupMenuKey = 'episodePopupMenuKey';
const String autoDeleteKey = 'autoDeleteKey';
//SleepTImer
const String autoSleepTimerKey = 'autoSleepTimerKey';
const String autoSleepTimerStartKey = 'autoSleepTimerStartKey';
const String autoSleepTimerEndKey = 'autoSleepTimerEndKey';
const String defaultSleepTimerKey = 'defaultSleepTimerKey';
const String autoSleepTimerModeKey = 'autoSleepTimerModeKey';
const String tapToOpenPopupMenuKey = 'tapToOpenPopupMenuKey';
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({int defaultValue = 0}) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
if (prefs.getInt(key) == null) await prefs.setInt(key, defaultValue);
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);
}
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);
}
saveMenu(List<int> list) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setStringList(key, list.map((e) => e.toString()).toList());
}
Future<List<int>> getMenu() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
if (prefs.getStringList(key) == null) {
await prefs.setStringList(key, ['0', '1', '12', '13', '14']);
}
List<String> list = prefs.getStringList(key);
return list.map((e) => int.parse(e)).toList();
}
/// Rreverse is used for compatite bool value save before which set true = 0, false = 1
Future<bool> getBool(
{@required bool defaultValue, bool reverse = false}) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
if (prefs.getInt(key) == null)
reverse
? await prefs.setInt(key, defaultValue ? 0 : 1)
: await prefs.setInt(key, defaultValue ? 1 : 0);
int i = prefs.getInt(key);
return reverse ? i == 0 : i == 1;
}
/// Rreverse is used for compatite bool value save before which set true = 0, false = 1
saveBool(bool boo, {bool reverse = false}) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
reverse ? prefs.setInt(key, boo ? 0 : 1) : prefs.setInt(key, boo ? 1 : 0);
}
}