import 'dart:convert'; import 'package:flutter/foundation.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../state/podcast_group.dart'; import '../type/playlist.dart'; const String groupsKey = 'groups'; const String playlistKey = 'playlist'; 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'; const String autoSleepTimerKey = 'autoSleepTimerKey'; const String autoSleepTimerStartKey = 'autoSleepTimerStartKey'; const String autoSleepTimerEndKey = 'autoSleepTimerEndKey'; const String defaultSleepTimerKey = 'defaultSleepTimerKey'; const String autoSleepTimerModeKey = 'autoSleepTimerModeKey'; const String tapToOpenPopupMenuKey = 'tapToOpenPopupMenuKey'; const String fastForwardSecondsKey = 'fastForwardSecondsKey'; const String rewindSecondsKey = 'rewindSecondsKey'; const String playerHeightKey = 'playerHeightKey'; const String speedKey = 'speedKey'; const String skipSilenceKey = 'skipSilenceKey'; const String localeKey = 'localeKey'; const String boostVolumeKey = 'boostVolumeKey'; const String volumeGainKey = 'volumeGainKey'; const String hideListenedKey = 'hideListenedKey'; const String notificationLayoutKey = 'notificationLayoutKey'; const String showNotesFontKey = 'showNotesFontKey'; const String speedListKey = 'speedListKey'; const String searchHistoryKey = 'searchHistoryKey'; const String gpodderApiKey = 'gpodderApiKey'; const String gpodderAddKey = 'gpodderAddKey'; const String gpodderRemoveKey = 'gpodderRemoveKey'; const String gpodderSyncStatusKey = 'gpodderSyncStatusKey'; const String gpodderSyncDateTimeKey = 'gpodderSyncDateTimeKey'; const String gpodderRemoteAddKey = 'gpodderRemoteAddKey'; const String gpodderRemoteRemoveKey = 'gpodderRemoteRemoveKey'; const String hidePodcastDiscoveryKey = 'hidePodcastDiscoveryKey'; const String searchEngineKey = 'searchEngineKey'; const String markListenedAfterSkipKey = 'markListenedAfterSkipKey'; const String downloadPositionKey = 'downloadPositionKey'; const String deleteAfterPlayedKey = 'removeAfterPlayedKey'; const String playlistsAllKey = 'playlistsAllKey'; const String playerStateKey = 'playerStateKey'; const String openPlaylistDefaultKey = 'openPlaylistDefaultKey'; const String openAllPodcastDefaultKey = 'openAllPodcastDefaultKey'; class KeyValueStorage { final String key; KeyValueStorage(this.key); Future> getGroups() async { var prefs = await SharedPreferences.getInstance(); if (prefs.getString(key) == null) { var home = PodcastGroup('Home'); await prefs.setString( key, json.encode({ 'groups': [home.toEntity().toJson()] })); } return json .decode(prefs.getString(key))['groups'] .cast>() .map(GroupEntity.fromJson) .toList(growable: false); } Future saveGroup(List groupList) async { var prefs = await SharedPreferences.getInstance(); return prefs.setString( key, json.encode( {'groups': groupList.map((group) => group.toJson()).toList()})); } Future> getPlaylists() async { var prefs = await SharedPreferences.getInstance(); if (prefs.getString(key) == null) { var episodeList = prefs.getStringList(playlistKey); var playlist = Playlist('Queue', episodeList: episodeList); await prefs.setString( key, json.encode({ 'playlists': [playlist.toEntity().toJson()] })); } print(prefs.getString(key)); return json .decode(prefs.getString(key))['playlists'] .cast>() .map(PlaylistEntity.fromJson) .toList(growable: false); } Future savePlaylists(List playlists) async { var prefs = await SharedPreferences.getInstance(); return prefs.setString( key, json.encode({ 'playlists': playlists.map((playlist) => playlist.toJson()).toList() })); } Future savePlayerState( String playlist, String episode, int position) async { var prefs = await SharedPreferences.getInstance(); return prefs.setStringList(key, [playlist, episode, position.toString()]); } Future> getPlayerState() async { var prefs = await SharedPreferences.getInstance(); if (prefs.getStringList(key) == null) { final position = prefs.getInt(audioPositionKey) ?? 0; await savePlayerState('', '', position); } return prefs.getStringList(key); } Future saveInt(int setting) async { var prefs = await SharedPreferences.getInstance(); return prefs.setInt(key, setting); } Future getInt({int defaultValue = 0}) async { var prefs = await SharedPreferences.getInstance(); if (prefs.getInt(key) == null) await prefs.setInt(key, defaultValue); return prefs.getInt(key); } Future saveStringList(List playList) async { var prefs = await SharedPreferences.getInstance(); return prefs.setStringList(key, playList); } Future> getStringList() async { var prefs = await SharedPreferences.getInstance(); if (prefs.getStringList(key) == null) { await prefs.setStringList(key, []); } return prefs.getStringList(key); } Future saveString(String string) async { var prefs = await SharedPreferences.getInstance(); return prefs.setString(key, string); } Future getString() async { var prefs = await SharedPreferences.getInstance(); if (prefs.getString(key) == null) { await prefs.setString(key, ''); } return prefs.getString(key); } Future saveMenu(List list) async { var prefs = await SharedPreferences.getInstance(); return await prefs.setStringList( key, list.map((e) => e.toString()).toList()); } Future> getMenu() async { var prefs = await SharedPreferences.getInstance(); if (prefs.getStringList(key) == null || prefs.getStringList(key).isEmpty) { await prefs.setStringList(key, ['0', '1', '2', '13', '14', '15']); } var list = prefs.getStringList(key); if (list.length == 5) list = [...list, '15']; return list.map(int.parse).toList(); } /// For player speed settings. Future saveSpeedList(List list) async { var prefs = await SharedPreferences.getInstance(); list.sort(); return await prefs.setStringList( key, list.map((e) => e.toStringAsFixed(1)).toList()); } Future> getSpeedList() async { var prefs = await SharedPreferences.getInstance(); if (prefs.getStringList(key) == null || prefs.getStringList(key).isEmpty) { await prefs.setStringList( key, ['0.5', '0.6', '0.8', '1.0', '1.1', '1.2', '1.5', '2.0']); } var list = prefs.getStringList(key); return list.map(double.parse).toList(); } /// Rreverse is used for compatite bool value save before which set true = 0, false = 1 Future getBool( {@required bool defaultValue, bool reverse = false}) async { var prefs = await SharedPreferences.getInstance(); if (prefs.getInt(key) == null) { reverse ? await prefs.setInt(key, defaultValue ? 0 : 1) : await prefs.setInt(key, defaultValue ? 1 : 0); } var 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 Future saveBool(boo, {reverse = false}) async { var prefs = await SharedPreferences.getInstance(); return reverse ? prefs.setInt(key, boo ? 0 : 1) : prefs.setInt(key, boo ? 1 : 0); } Future saveDouble(double data) async { var prefs = await SharedPreferences.getInstance(); return prefs.setDouble(key, data); } Future getDouble({double defaultValue = 0.0}) async { var prefs = await SharedPreferences.getInstance(); if (prefs.getDouble(key) == null) { await prefs.setDouble(key, defaultValue); } return prefs.getDouble(key); } Future addList(List addList) async { final list = await getStringList(); await saveStringList(list..addAll(addList)); } Future clearList() async { await saveStringList([]); } }