import 'dart:async'; import 'dart:convert'; import 'package:shared_preferences/shared_preferences.dart'; import '../state/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'; 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'; class KeyValueStorage { final String key; KeyValueStorage(this.key); Future> 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(GroupEntity.fromJson) .toList(growable: false); } Future saveGroup(List groupList) async { SharedPreferences prefs = await SharedPreferences.getInstance(); return prefs.setString( key, json.encode( {'groups': groupList.map((group) => group.toJson()).toList()})); } Future saveInt(int setting) async { SharedPreferences prefs = await SharedPreferences.getInstance(); return prefs.setInt(key, setting); } Future getInt() async { SharedPreferences prefs = await SharedPreferences.getInstance(); if (prefs.getInt(key) == null) await prefs.setInt(key, 0); return prefs.getInt(key); } Future saveStringList(List playList) async { SharedPreferences prefs = await SharedPreferences.getInstance(); return prefs.setStringList(key, playList); } Future> getStringList() async { SharedPreferences prefs = await SharedPreferences.getInstance(); if (prefs.getStringList(key) == null) { await prefs.setStringList(key, []); } return prefs.getStringList(key); } Future saveString(String string) async { SharedPreferences prefs = await SharedPreferences.getInstance(); return prefs.setString(key, string); } Future getString() async { SharedPreferences prefs = await SharedPreferences.getInstance(); if (prefs.getString(key) == null) { await prefs.setString(key, ''); } return prefs.getString(key); } saveMenu(List list) async { SharedPreferences prefs = await SharedPreferences.getInstance(); await prefs.setStringList(key, list.map((e) => e.toString()).toList()); } Future> getMenu() async { SharedPreferences prefs = await SharedPreferences.getInstance(); if (prefs.getStringList(key) == null) { await prefs.setStringList(key, ['0', '1', '12', '13', '14']); } List list = prefs.getStringList(key); return list.map((e) => int.parse(e)).toList(); } }