From 16bd7e7f76870c86c39e8fe8a0dae7f9fb13f090 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Wed, 13 Aug 2014 23:02:14 -0700 Subject: Update serializations when getting list of starred --- .../dsub/service/CachedMusicService.java | 142 ++++++++++++++++++--- 1 file changed, 122 insertions(+), 20 deletions(-) diff --git a/src/github/daneren2005/dsub/service/CachedMusicService.java b/src/github/daneren2005/dsub/service/CachedMusicService.java index 6ba5dc02..b8266913 100644 --- a/src/github/daneren2005/dsub/service/CachedMusicService.java +++ b/src/github/daneren2005/dsub/service/CachedMusicService.java @@ -20,6 +20,7 @@ package github.daneren2005.dsub.service; import java.io.File; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.concurrent.TimeUnit; @@ -355,9 +356,97 @@ public class CachedMusicService implements MusicService { @Override public MusicDirectory getStarredList(Context context, ProgressListener progressListener) throws Exception { - return musicService.getStarredList(context, progressListener); + MusicDirectory dir = musicService.getStarredList(context, progressListener); + + MusicDirectory oldDir = FileUtil.deserialize(context, "starred", MusicDirectory.class); + if(oldDir != null) { + List newList = new ArrayList(); + newList.addAll(dir.getChildren()); + List oldList = oldDir.getChildren(); + + for(Iterator it = oldList.iterator(); it.hasNext(); ) { + // Remove any entries from newList + if(newList.remove(it.next())) { + // If it was removed, then remove from oldList as well + it.remove(); + } + } + + // Left overs in newList need to be starred + boolean isTagBrowsing = Util.isTagBrowsing(context, musicService.getInstance(context)); + updateStarredList(context, newList, true, isTagBrowsing); + + // Left overs in oldList need to be unstarred + updateStarredList(context, oldList, false, isTagBrowsing); + } + FileUtil.serialize(context, dir, "starred"); + + return dir; } + private void updateStarredList(Context context, List list, final boolean starred, final boolean isTagBrowsing) { + for(final Entry entry: list) { + String cacheName, parent = null; + boolean isArtist = false; + if(isTagBrowsing) { + if(entry.isDirectory()) { + if(entry.isAlbum()) { + cacheName = "artist"; + parent = entry.getArtistId(); + } else { + isArtist = true; + cacheName = "artists"; + } + } else { + cacheName = "album"; + parent = entry.getAlbumId(); + } + } else { + if(entry.isDirectory() && !entry.isAlbum()) { + isArtist = true; + cacheName = "indexes"; + } else { + cacheName = "directory"; + parent = entry.getParent(); + } + } + + if(isArtist) { + new IndexesUpdater(context, isTagBrowsing ? "artists" : "indexes") { + @Override + public boolean checkResult(Artist check) { + if(entry.getId().equals(check.getId())) { + return true; + } + + return false; + } + + @Override + public void updateResult(List objects, Artist result) { + result.setStarred(starred); + } + }.execute(); + } else { + new MusicDirectoryUpdater(context, cacheName, parent) { + @Override + public boolean checkResult(Entry check) { + if (entry.getId().equals(check.getId())) { + return true; + } + + return false; + } + + @Override + public void updateResult(List objects, Entry result) { + result.setStarred(true); + } + }.execute(); + } + } + } + @Override public MusicDirectory getRandomSongs(int size, String folder, String genre, String startYear, String endYear, Context context, ProgressListener progressListener) throws Exception { return musicService.getRandomSongs(size, folder, genre, startYear, endYear, context, progressListener); @@ -488,24 +577,7 @@ public class CachedMusicService implements MusicService { } } else { String name = Util.isTagBrowsing(context, musicService.getInstance(context)) ? "artists" : "indexes"; - new SerializeUpdater(context, name, Util.getSelectedMusicFolderId(context, musicService.getInstance(context))) { - Indexes indexes; - - @Override - public ArrayList getArrayList() { - indexes = FileUtil.deserialize(context, cacheName, Indexes.class); - - ArrayList artists = new ArrayList(); - artists.addAll(indexes.getArtists()); - artists.addAll(indexes.getShortcuts()); - return artists; - } - public void save(ArrayList objects) { - indexes.setArtists(objects); - FileUtil.serialize(context, indexes, cacheName); - cachedIndexes.set(indexes); - } - + new IndexesUpdater(context, name) { @Override public boolean checkResult(Artist check) { for (String id : checkIds) { @@ -879,13 +951,43 @@ public class CachedMusicService implements MusicService { @Override public ArrayList getArrayList() { musicDirectory = FileUtil.deserialize(context, cacheName, MusicDirectory.class); - return new ArrayList(musicDirectory.getChildren()); + if(musicDirectory != null) { + return new ArrayList(musicDirectory.getChildren()); + } else { + return null; + } } public void save(ArrayList objects) { musicDirectory.replaceChildren(objects); FileUtil.serialize(context, musicDirectory, cacheName); } } + private abstract class IndexesUpdater extends SerializeUpdater { + Indexes indexes; + + IndexesUpdater(Context context, String name) { + super(context, name, Util.getSelectedMusicFolderId(context, musicService.getInstance(context))); + } + + @Override + public ArrayList getArrayList() { + indexes = FileUtil.deserialize(context, cacheName, Indexes.class); + if(indexes == null) { + return null; + } + + ArrayList artists = new ArrayList(); + artists.addAll(indexes.getArtists()); + artists.addAll(indexes.getShortcuts()); + return artists; + } + + public void save(ArrayList objects) { + indexes.setArtists(objects); + FileUtil.serialize(context, indexes, cacheName); + cachedIndexes.set(indexes); + } + } private void checkSettingsChanged(Context context) { String newUrl = musicService.getRestUrl(context, null, false); -- cgit v1.2.3