From d3940666346e9874e7fbb5ddafb02f63fbb7c877 Mon Sep 17 00:00:00 2001 From: stonega Date: Wed, 4 Nov 2020 01:54:50 +0800 Subject: [PATCH] Update playlist class. --- lib/local_storage/key_value_storage.dart | 31 +++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/local_storage/key_value_storage.dart b/lib/local_storage/key_value_storage.dart index 1f49029..d87b0f4 100644 --- a/lib/local_storage/key_value_storage.dart +++ b/lib/local_storage/key_value_storage.dart @@ -4,6 +4,7 @@ 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'; @@ -57,6 +58,7 @@ const String searchEngineKey = 'searchEngineKey'; const String markListenedAfterSkipKey = 'markListenedAfterSkipKey'; const String downloadPositionKey = 'downloadPositionKey'; const String deleteAfterPlayedKey = 'removeAfterPlayedKey'; +const String playlistsAllKey = 'playlistsAllKey'; class KeyValueStorage { final String key; @@ -86,6 +88,33 @@ class KeyValueStorage { {'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('Playlist', episodeList: episodeList); + await prefs.setString( + key, + json.encode({ + 'playlists': [playlist.toEntity().toJson()] + })); + } + 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 saveInt(int setting) async { var prefs = await SharedPreferences.getInstance(); return prefs.setInt(key, setting); @@ -135,7 +164,7 @@ class KeyValueStorage { await prefs.setStringList(key, ['0', '1', '2', '13', '14', '15']); } var list = prefs.getStringList(key); - if(list.length == 5) list = [...list,'15']; + if (list.length == 5) list = [...list, '15']; return list.map(int.parse).toList(); }