From dcfa67df5cc84ef9b5113c0bc0da68655729af7a Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Tue, 12 Aug 2014 07:04:50 -0700 Subject: Update cache serialization in setStarred --- src/github/daneren2005/dsub/domain/Indexes.java | 6 ++ .../daneren2005/dsub/domain/MusicDirectory.java | 4 + .../dsub/fragments/SelectDirectoryFragment.java | 11 ++- .../dsub/fragments/SubsonicFragment.java | 25 ++++--- .../dsub/service/CachedMusicService.java | 85 +++++++++++++++++++++- .../daneren2005/dsub/service/MusicService.java | 2 +- .../dsub/service/OfflineMusicService.java | 2 +- .../daneren2005/dsub/service/RESTMusicService.java | 8 +- src/github/daneren2005/dsub/util/Util.java | 8 +- src/github/daneren2005/dsub/view/EntryAdapter.java | 2 +- 10 files changed, 124 insertions(+), 29 deletions(-) diff --git a/src/github/daneren2005/dsub/domain/Indexes.java b/src/github/daneren2005/dsub/domain/Indexes.java index 67eb33bc..63c3a8d4 100644 --- a/src/github/daneren2005/dsub/domain/Indexes.java +++ b/src/github/daneren2005/dsub/domain/Indexes.java @@ -67,6 +67,12 @@ public class Indexes implements Serializable { return artists; } + public void setArtists(List artists) { + this.shortcuts = new ArrayList(); + this.artists.clear(); + this.artists.addAll(artists); + } + public List getEntries() { return entries; } diff --git a/src/github/daneren2005/dsub/domain/MusicDirectory.java b/src/github/daneren2005/dsub/domain/MusicDirectory.java index 492726f9..1b3342bd 100644 --- a/src/github/daneren2005/dsub/domain/MusicDirectory.java +++ b/src/github/daneren2005/dsub/domain/MusicDirectory.java @@ -249,6 +249,10 @@ public class MusicDirectory implements Serializable { return album; } + public boolean isAlbum() { + return getParent() != null || getArtist() != null; + } + public String getAlbumDisplay() { if(album != null && title.startsWith("Disc ")) { return album; diff --git a/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java b/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java index 5ab22ae1..bf9e5531 100644 --- a/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java +++ b/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java @@ -1048,18 +1048,21 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Adapter List ids = new ArrayList(); List artists = new ArrayList(); List albums = new ArrayList(); + List parents = new ArrayList(); for(MusicDirectory.Entry entry: unstar) { if(entry.isDirectory()) { - if(entry.getArtist() == null || entry.getParent() == null) { - artists.add(entry.getId()); - } else { + if(entry.isAlbum()) { albums.add(entry.getId()); + parents.add(entry.getArtistId()); + } else { + artists.add(entry.getId()); } } else { ids.add(entry.getId()); + parents.add(entry.getParent()); } } - musicService.setStarred(ids, artists, albums, false, context, this); + musicService.setStarred(ids, artists, albums, parents, false, this, context); return null; } diff --git a/src/github/daneren2005/dsub/fragments/SubsonicFragment.java b/src/github/daneren2005/dsub/fragments/SubsonicFragment.java index 3b691c36..aecaa221 100644 --- a/src/github/daneren2005/dsub/fragments/SubsonicFragment.java +++ b/src/github/daneren2005/dsub/fragments/SubsonicFragment.java @@ -645,20 +645,20 @@ public class SubsonicFragment extends Fragment implements SwipeRefreshLayout.OnR protected Void doInBackground() throws Throwable { MusicService musicService = MusicServiceFactory.getMusicService(context); if(entry.isDirectory() && Util.isTagBrowsing(context) && !Util.isOffline(context)) { - if(entry.getParent() != null || entry.getArtist() != null) { - musicService.setStarred(null, null, Arrays.asList(entry.getId()), starred, context, null); + if(entry.isAlbum()) { + musicService.setStarred(null, null, Arrays.asList(entry.getId()), Arrays.asList(entry.getArtistId()), starred, null, context); } else { - musicService.setStarred(null, Arrays.asList(entry.getId()), null, starred, context, null); + musicService.setStarred(null, Arrays.asList(entry.getId()), null, null, starred, null, context); } } else { - musicService.setStarred(Arrays.asList(entry.getId()), null, null, starred, context, null); + List parents = null; + if(entry.getParent() != null) { + parents = Arrays.asList(entry.getParent()); + } + + musicService.setStarred(Arrays.asList(entry.getId()), null, null, parents, starred, null, context); } - - // Make sure to clear parent cache - String s = Util.getRestUrl(context, null) + entry.getParent(); - String parentCache = "directory-" + s.hashCode() + ".ser"; - File file = new File(context.getCacheDir(), parentCache); - file.delete(); + return null; } @@ -692,9 +692,9 @@ public class SubsonicFragment extends Fragment implements SwipeRefreshLayout.OnR protected Void doInBackground() throws Throwable { MusicService musicService = MusicServiceFactory.getMusicService(context); if(Util.isTagBrowsing(context) && !Util.isOffline(context)) { - musicService.setStarred(null, Arrays.asList(entry.getId()), null, starred, context, null); + musicService.setStarred(null, Arrays.asList(entry.getId()), null, null, starred, null, context); } else { - musicService.setStarred(Arrays.asList(entry.getId()), null, null, starred, context, null); + musicService.setStarred(Arrays.asList(entry.getId()), null, null, null, starred, null, context); } return null; } @@ -707,6 +707,7 @@ public class SubsonicFragment extends Fragment implements SwipeRefreshLayout.OnR @Override protected void error(Throwable error) { + Log.w(TAG, "Failed to star", error); entry.setStarred(!starred); String msg; diff --git a/src/github/daneren2005/dsub/service/CachedMusicService.java b/src/github/daneren2005/dsub/service/CachedMusicService.java index a0992091..6c4822fb 100644 --- a/src/github/daneren2005/dsub/service/CachedMusicService.java +++ b/src/github/daneren2005/dsub/service/CachedMusicService.java @@ -20,7 +20,6 @@ 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; @@ -31,6 +30,7 @@ import android.content.Context; import android.graphics.Bitmap; import android.util.Log; +import github.daneren2005.dsub.domain.Artist; import github.daneren2005.dsub.domain.Bookmark; import github.daneren2005.dsub.domain.ChatMessage; import github.daneren2005.dsub.domain.Genre; @@ -429,8 +429,87 @@ public class CachedMusicService implements MusicService { } @Override - public void setStarred(List id, List artistId, List albumId, boolean starred, Context context, ProgressListener progressListener) throws Exception { - musicService.setStarred(id, artistId, albumId, starred, context, progressListener); + public void setStarred(List id, List artistId, List albumId, List parents, final boolean starred, ProgressListener progressListener, Context context) throws Exception { + musicService.setStarred(id, artistId, albumId, parents, starred, progressListener, context); + + // Fuzzy logic to update parents serialization + List ids; + if(artistId != null && artistId.size() > 0) { + ids = artistId; + } else if(albumId != null && albumId.size() > 0) { + ids = albumId; + } else { + ids = id; + } + + // Make sure list is not somehow null here + if(ids == null) { + Log.w(TAG, "There should never be no ids in setStarred"); + return; + } + + // Define another variable final because Java is retarded + final List checkIds = ids; + + // If parents is null, or artist id's are set, then we are looking at artists + if(parents != null && (artistId == null || artistId.size() == 0)) { + for (String parent : parents) { + new MusicDirectoryUpdater(context, "directory", parent) { + @Override + public boolean checkResult(Entry check) { + for (String id : checkIds) { + if(id.equals(check.getId())) { + return true; + } + } + + return false; + } + + @Override + public void updateResult(List objects, Entry result) { + Log.d(TAG, result.getId()); + result.setStarred(starred); + } + }.execute(); + } + } 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); + } + + @Override + public boolean checkResult(Artist check) { + for (String id : checkIds) { + if(id.equals(check.getId())) { + return true; + } + } + + return false; + } + + @Override + public void updateResult(List objects, Artist result) { + result.setStarred(starred); + } + }.execute(); + } } @Override diff --git a/src/github/daneren2005/dsub/service/MusicService.java b/src/github/daneren2005/dsub/service/MusicService.java index 9f87065c..78c99b73 100644 --- a/src/github/daneren2005/dsub/service/MusicService.java +++ b/src/github/daneren2005/dsub/service/MusicService.java @@ -120,7 +120,7 @@ public interface MusicService { RemoteStatus setJukeboxGain(float gain, Context context, ProgressListener progressListener) throws Exception; - void setStarred(List id, List artistId, List albumId, boolean starred, Context context, ProgressListener progressListener) throws Exception; + void setStarred(List id, List artistId, List albumId, List parents, boolean starred, ProgressListener progressListener, Context context) throws Exception; List getShares(Context context, ProgressListener progressListener) throws Exception; diff --git a/src/github/daneren2005/dsub/service/OfflineMusicService.java b/src/github/daneren2005/dsub/service/OfflineMusicService.java index 3dacaee7..68906fd7 100644 --- a/src/github/daneren2005/dsub/service/OfflineMusicService.java +++ b/src/github/daneren2005/dsub/service/OfflineMusicService.java @@ -595,7 +595,7 @@ public class OfflineMusicService implements MusicService { } @Override - public void setStarred(List ids, List artistId, List albumId, boolean starred, Context context, ProgressListener progressListener) throws Exception { + public void setStarred(List ids, List artistId, List albumId, List parents, boolean starred, ProgressListener progressListener, Context context) throws Exception { SharedPreferences prefs = Util.getPreferences(context); String cacheLocn = prefs.getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, null); diff --git a/src/github/daneren2005/dsub/service/RESTMusicService.java b/src/github/daneren2005/dsub/service/RESTMusicService.java index 51a8a2a6..fe135dc6 100644 --- a/src/github/daneren2005/dsub/service/RESTMusicService.java +++ b/src/github/daneren2005/dsub/service/RESTMusicService.java @@ -794,7 +794,7 @@ public class RESTMusicService implements MusicService { } @Override - public void setStarred(List ids, List artistId, List albumId, boolean starred, Context context, ProgressListener progressListener) throws Exception { + public void setStarred(List ids, List artistId, List albumId, List parents, boolean starred, ProgressListener progressListener, Context context) throws Exception { checkServerVersion(context, "1.8", "Starring is not supported."); List names = new ArrayList(); @@ -1363,7 +1363,7 @@ public class RESTMusicService implements MusicService { String id = offline.getString(Constants.OFFLINE_STAR_ID + i, null); boolean starred = offline.getBoolean(Constants.OFFLINE_STAR_SETTING + i, false); if(id != null) { - setStarred(Arrays.asList(id), null, null, starred, context, progressListener); + setStarred(Arrays.asList(id), null, null, null, starred, progressListener, context); } else { String search = offline.getString(Constants.OFFLINE_STAR_SEARCH + i, ""); try{ @@ -1371,10 +1371,10 @@ public class RESTMusicService implements MusicService { SearchResult result = searchNew(critera, context, progressListener); if(result.getSongs().size() == 1){ Log.i(TAG, "Query '" + search + "' returned song " + result.getSongs().get(0).getTitle() + " by " + result.getSongs().get(0).getArtist() + " with id " + result.getSongs().get(0).getId()); - setStarred(Arrays.asList(result.getSongs().get(0).getId()), null, null, starred, context, progressListener); + setStarred(Arrays.asList(result.getSongs().get(0).getId()), null, null, null, starred, progressListener, context); } else if(result.getAlbums().size() == 1){ Log.i(TAG, "Query '" + search + "' returned song " + result.getAlbums().get(0).getTitle() + " by " + result.getAlbums().get(0).getArtist() + " with id " + result.getAlbums().get(0).getId()); - setStarred(Arrays.asList(result.getAlbums().get(0).getId()), null, null, starred, context, progressListener); + setStarred(Arrays.asList(result.getAlbums().get(0).getId()), null, null, null, starred, progressListener, context); } else{ throw new Exception("Song not found on server"); diff --git a/src/github/daneren2005/dsub/util/Util.java b/src/github/daneren2005/dsub/util/Util.java index edfa5555..1db6e9c9 100644 --- a/src/github/daneren2005/dsub/util/Util.java +++ b/src/github/daneren2005/dsub/util/Util.java @@ -232,10 +232,12 @@ public final class Util { } public static String getSelectedMusicFolderId(Context context) { - SharedPreferences prefs = getPreferences(context); - int instance = getActiveServer(context); - return prefs.getString(Constants.PREFERENCES_KEY_MUSIC_FOLDER_ID + instance, null); + return getSelectedMusicFolderId(context, getActiveServer(context)); } + public static String getSelectedMusicFolderId(Context context, int instance) { + SharedPreferences prefs = getPreferences(context); + return prefs.getString(Constants.PREFERENCES_KEY_MUSIC_FOLDER_ID + instance, null); + } public static String getTheme(Context context) { SharedPreferences prefs = getPreferences(context); diff --git a/src/github/daneren2005/dsub/view/EntryAdapter.java b/src/github/daneren2005/dsub/view/EntryAdapter.java index 2dc14e1b..e9611692 100644 --- a/src/github/daneren2005/dsub/view/EntryAdapter.java +++ b/src/github/daneren2005/dsub/view/EntryAdapter.java @@ -55,7 +55,7 @@ public class EntryAdapter extends ArrayAdapter { MusicDirectory.Entry entry = getItem(position); if (entry.isDirectory()) { - if(entry.getArtist() != null || entry.getParent() != null) { + if(entry.isAlbum()) { AlbumView view; view = new AlbumView(activity); view.setObject(entry, imageLoader); -- cgit v1.2.3