diff options
author | Scott Jackson <daneren2005@gmail.com> | 2014-09-04 20:30:01 -0700 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2014-09-04 20:30:01 -0700 |
commit | 785272859a776cbb211418bc2910029a4e78b000 (patch) | |
tree | 209a596e7a9b946e5d267832e6de6d573cafae11 | |
parent | 46b4ccb63f90f33214de3a8ba3e12dc7a0c30ac8 (diff) | |
download | dsub-785272859a776cbb211418bc2910029a4e78b000.tar.gz dsub-785272859a776cbb211418bc2910029a4e78b000.tar.bz2 dsub-785272859a776cbb211418bc2910029a4e78b000.zip |
Convert stars to GenericEntryUpdater to simplify logic
7 files changed, 78 insertions, 191 deletions
diff --git a/src/github/daneren2005/dsub/domain/MusicDirectory.java b/src/github/daneren2005/dsub/domain/MusicDirectory.java index 74bc89f5..edcbe77d 100644 --- a/src/github/daneren2005/dsub/domain/MusicDirectory.java +++ b/src/github/daneren2005/dsub/domain/MusicDirectory.java @@ -143,6 +143,9 @@ public class MusicDirectory implements Serializable { public Entry() { } + public Entry(String id) { + this.id = id; + } public Entry(Artist artist) { this.id = artist.getId(); this.title = artist.getName(); diff --git a/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java b/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java index 3da49abd..9ea84d41 100644 --- a/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java +++ b/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java @@ -1047,24 +1047,21 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Adapter @Override
protected Void doInBackground() throws Throwable {
MusicService musicService = MusicServiceFactory.getMusicService(context);
- List<String> ids = new ArrayList<String>();
- List<String> artists = new ArrayList<String>();
- List<String> albums = new ArrayList<String>();
- List<String> parents = new ArrayList<String>();
+ List<MusicDirectory.Entry> entries = new ArrayList<MusicDirectory.Entry>();
+ List<MusicDirectory.Entry> artists = new ArrayList<MusicDirectory.Entry>();
+ List<MusicDirectory.Entry> albums = new ArrayList<MusicDirectory.Entry>();
for(MusicDirectory.Entry entry: unstar) {
if(entry.isDirectory()) {
if(entry.isAlbum()) {
- albums.add(entry.getId());
- parents.add(entry.getArtistId());
+ albums.add(entry);
} else {
- artists.add(entry.getId());
+ artists.add(entry);
}
} else {
- ids.add(entry.getId());
- parents.add(entry.getParent());
+ entries.add(entry);
}
}
- musicService.setStarred(ids, artists, albums, parents, false, this, context);
+ musicService.setStarred(entries, artists, albums, false, this, context);
for(MusicDirectory.Entry entry: unstar) {
setEntryStarred(entry, false);
diff --git a/src/github/daneren2005/dsub/fragments/SubsonicFragment.java b/src/github/daneren2005/dsub/fragments/SubsonicFragment.java index e5b74699..fcef44fb 100644 --- a/src/github/daneren2005/dsub/fragments/SubsonicFragment.java +++ b/src/github/daneren2005/dsub/fragments/SubsonicFragment.java @@ -680,23 +680,12 @@ public class SubsonicFragment extends Fragment implements SwipeRefreshLayout.OnR MusicService musicService = MusicServiceFactory.getMusicService(context);
if(entry.isDirectory() && Util.isTagBrowsing(context) && !Util.isOffline(context)) {
if(entry.isAlbum()) {
- musicService.setStarred(null, null, Arrays.asList(entry.getId()), Arrays.asList(entry.getArtistId()), starred, null, context);
+ musicService.setStarred(null, null, Arrays.asList(entry), starred, null, context);
} else {
- musicService.setStarred(null, Arrays.asList(entry.getId()), null, null, starred, null, context);
+ musicService.setStarred(null, Arrays.asList(entry), null, starred, null, context);
}
} else {
- List<String> parents = null;
- if(Util.isTagBrowsing(context)) {
- if(entry.getAlbumId() != null) {
- parents = Arrays.asList(entry.getAlbumId());
- }
- } else {
- if(entry.getParent() != null) {
- parents = Arrays.asList(entry.getParent());
- }
- }
-
- musicService.setStarred(Arrays.asList(entry.getId()), null, null, parents, starred, null, context);
+ musicService.setStarred(Arrays.asList(entry), null, null, starred, null, context);
}
setEntryStarred(entry, starred);
@@ -755,9 +744,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, null, starred, null, context);
+ musicService.setStarred(null, Arrays.asList(new Entry(entry)), null, starred, null, context);
} else {
- musicService.setStarred(Arrays.asList(entry.getId()), null, null, null, starred, null, context);
+ musicService.setStarred(Arrays.asList(new Entry(entry)), null, null, starred, null, context);
}
return null;
}
diff --git a/src/github/daneren2005/dsub/service/CachedMusicService.java b/src/github/daneren2005/dsub/service/CachedMusicService.java index 4ea8a048..28560bf6 100644 --- a/src/github/daneren2005/dsub/service/CachedMusicService.java +++ b/src/github/daneren2005/dsub/service/CachedMusicService.java @@ -365,56 +365,40 @@ public class CachedMusicService implements MusicService { newList.addAll(dir.getChildren()); final List<Entry> oldList = oldDir.getChildren(); - removeDuplicates(oldList, newList); - - // 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); - - - // Remove non-songs from lists before updating playlists for(Iterator<Entry> it = oldList.iterator(); it.hasNext(); ) { - if(it.next().isDirectory()) { - it.remove(); - } - } - for(Iterator<Entry> it = newList.iterator(); it.hasNext(); ) { - if(it.next().isDirectory()) { + Entry oldEntry = it.next(); + + // Remove entries from newList + if(newList.remove(oldEntry)) { + // If it was removed, then remove it from old list as well it.remove(); + } else { + oldEntry.setStarred(false); } } - - // Only try to update playlists if there was at least one song in new or old set - if(newList.size() > 0 || oldList.size() > 0) { - new PlaylistDirectoryUpdater(context) { - @Override - public boolean checkResult(Entry check) { - for(Entry entry: oldList) { - if(check.getId().equals(entry.getId()) && check.isStarred() != false) { - check.setStarred(false); - return true; - } - } - - for(Entry entry: newList) { - if(check.getId().equals(entry.getId()) && check.isStarred() != true) { - check.setStarred(true); - return true; - } + + List<Entry> totalList = new ArrayList<Entry>(); + totalList.addAll(oldList); + totalList.addAll(newList); + + new GenericEntryUpdater(context, totalList) { + @Override + public boolean checkResult(Entry entry, Entry check) { + if (entry.getId().equals(check.getId())) { + if(entry.isStarred() != check.isStarred()) { + check.setStarred(entry.isStarred()); + return true; } - - return false; } - - @Override - public void updateResult(Entry result) { - - } - }.execute(); - } + + return false; + } + + @Override + public void updateResult(Entry result) { + + } + }.execute(); } FileUtil.serialize(context, dir, "starred"); @@ -555,104 +539,27 @@ public class CachedMusicService implements MusicService { } @Override - public void setStarred(List<String> id, List<String> artistId, List<String> albumId, List<String> parents, final boolean starred, ProgressListener progressListener, Context context) throws Exception { - musicService.setStarred(id, artistId, albumId, parents, starred, progressListener, context); + public void setStarred(List<Entry> entries, List<Entry> artists, List<Entry> albums, final boolean starred, ProgressListener progressListener, Context context) throws Exception { + musicService.setStarred(entries, artists, albums, starred, progressListener, context); // Fuzzy logic to update parents serialization - List<String> ids; - if(artistId != null && artistId.size() > 0) { - ids = artistId; - } else if(albumId != null && albumId.size() > 0) { - ids = albumId; - } else { - ids = id; + List<Entry> allEntries = new ArrayList<Entry>(); + if(artists != null) { + allEntries.addAll(artists); } - - // Make sure list is not somehow null here - if(ids == null) { - Log.w(TAG, "There should never be no ids in setStarred"); - return; + if(albums != null) { + allEntries.addAll(albums); } - - // Define another variable final because Java is retarded - final List<String> 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)) { - String cacheName; - - // If using tag browsing, need to do lookup off of different criteria - if(Util.isTagBrowsing(context, musicService.getInstance(context))) { - // If using id's, we are starring songs and need to use album listings - if(id != null && id.size() > 0) { - cacheName = "album"; - } else { - cacheName = "artist"; - } - } else { - cacheName = "directory"; - } - - for (String parent : parents) { - new MusicDirectoryUpdater(context, cacheName, parent, checkIds.size() == 1) { - @Override - public boolean checkResult(Entry check) { - for (String id : checkIds) { - if(id.equals(check.getId())) { - return true; - } - } - - return false; - } - - @Override - public void updateResult(List<Entry> objects, Entry result) { - result.setStarred(starred); - } - }.execute(); - } - } else { - String name = Util.isTagBrowsing(context, musicService.getInstance(context)) ? "artists" : "indexes"; - new IndexesUpdater(context, name) { - @Override - public boolean checkResult(Artist check) { - for (String id : checkIds) { - if(id.equals(check.getId())) { - return true; - } - } - - return false; - } - - @Override - public void updateResult(List<Artist> objects, Artist result) { - result.setStarred(starred); - } - }.execute(); + if (entries != null) { + allEntries.addAll(entries); } - // Update playlist caches if there is at least one song to be starred - if(ids != null && ids.size() > 0) { - new PlaylistDirectoryUpdater(context) { - @Override - public boolean checkResult(Entry check) { - for (String id : checkIds) { - if (id.equals(check.getId())) { - return true; - } - } - - return false; - } - - @Override - public void updateResult(Entry result) { - result.setStarred(starred); - } - }.execute(); - } + new GenericEntryUpdater(context, allEntries) { + @Override + public void updateResult(Entry result) { + result.setStarred(starred); + } + }.execute(); } @Override @@ -974,16 +881,6 @@ public class CachedMusicService implements MusicService { return name + "-" + s.hashCode() + ".ser"; } - private void removeDuplicates(List<Entry> oldList, List<Entry> newList) { - for(Iterator<Entry> it = oldList.iterator(); it.hasNext(); ) { - // Remove entries from newList - if(newList.remove(it.next())) { - // If it was removed, then remove it from old list as well - it.remove(); - } - } - } - private abstract class SerializeUpdater<T> { final Context context; final String cacheName; @@ -1144,7 +1041,8 @@ public class CachedMusicService implements MusicService { public void execute() { String cacheName, parent; - boolean isTagBrowsing = Util.isTagBrowsing(context, musicService.getInstance(context)); + // Make sure it is up to date + isTagBrowsing = Util.isTagBrowsing(context, musicService.getInstance(context)); // Run through each entry, trying to update the directory it is in final List<Entry> songs = new ArrayList<Entry>(); diff --git a/src/github/daneren2005/dsub/service/MusicService.java b/src/github/daneren2005/dsub/service/MusicService.java index 70672269..02bfae8a 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<String> id, List<String> artistId, List<String> albumId, List<String> parents, boolean starred, ProgressListener progressListener, Context context) throws Exception; + void setStarred(List<MusicDirectory.Entry> entries, List<MusicDirectory.Entry> artists, List<MusicDirectory.Entry> albums, boolean starred, ProgressListener progressListener, Context context) throws Exception; List<Share> 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 3e43de04..41489ee6 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<String> ids, List<String> artistId, List<String> albumId, List<String> parents, boolean starred, ProgressListener progressListener, Context context) throws Exception { + public void setStarred(List<MusicDirectory.Entry> entries, List<MusicDirectory.Entry> artists, List<MusicDirectory.Entry> albums, boolean starred, ProgressListener progressListener, Context context) throws Exception { SharedPreferences prefs = Util.getPreferences(context); String cacheLocn = prefs.getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, null); @@ -604,7 +604,7 @@ public class OfflineMusicService implements MusicService { stars++; SharedPreferences.Editor offlineEditor = offline.edit(); - String id = ids.get(0); + String id = entries.get(0).getId(); if(id.indexOf(cacheLocn) != -1) { String searchCriteria = Util.parseOfflineIDSearch(context, id, cacheLocn); offlineEditor.putString(Constants.OFFLINE_STAR_SEARCH + stars, searchCriteria); diff --git a/src/github/daneren2005/dsub/service/RESTMusicService.java b/src/github/daneren2005/dsub/service/RESTMusicService.java index 0e21b48c..3e59abe3 100644 --- a/src/github/daneren2005/dsub/service/RESTMusicService.java +++ b/src/github/daneren2005/dsub/service/RESTMusicService.java @@ -817,33 +817,33 @@ public class RESTMusicService implements MusicService { } @Override - public void setStarred(List<String> ids, List<String> artistId, List<String> albumId, List<String> parents, boolean starred, ProgressListener progressListener, Context context) throws Exception { + public void setStarred(List<MusicDirectory.Entry> entries, List<MusicDirectory.Entry> artists, List<MusicDirectory.Entry> albums, boolean starred, ProgressListener progressListener, Context context) throws Exception { checkServerVersion(context, "1.8", "Starring is not supported."); List<String> names = new ArrayList<String>(); List<Object> values = new ArrayList<Object>(); - if(ids != null && ids.size() > 0) { - if(ids.size() > 1) { - for (String id : ids) { + if(entries != null && entries.size() > 0) { + if(entries.size() > 1) { + for (MusicDirectory.Entry entry : entries) { names.add("id"); - values.add(id); + values.add(entry.getId()); } } else { names.add("id"); - values.add(getOfflineSongId(ids.get(0), context, progressListener)); + values.add(getOfflineSongId(entries.get(0).getId(), context, progressListener)); } } - if(artistId != null && artistId.size() > 0) { - for (String id : artistId) { + if(artists != null && artists.size() > 0) { + for (MusicDirectory.Entry artist : artists) { names.add("artistId"); - values.add(id); + values.add(artist.getId()); } } - if(albumId != null && albumId.size() > 0) { - for (String id : albumId) { + if(albums != null && albums.size() > 0) { + for (MusicDirectory.Entry album : albums) { names.add("albumId"); - values.add(id); + values.add(album.getId()); } } @@ -1386,7 +1386,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, null, starred, progressListener, context); + setStarred(Arrays.asList(new MusicDirectory.Entry(id)), null, null, starred, progressListener, context); } else { String search = offline.getString(Constants.OFFLINE_STAR_SEARCH + i, ""); try{ @@ -1394,10 +1394,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, null, starred, progressListener, context); + setStarred(Arrays.asList(result.getSongs().get(0)), 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, null, starred, progressListener, context); + setStarred(Arrays.asList(result.getAlbums().get(0)), null, null, starred, progressListener, context); } else{ throw new Exception("Song not found on server"); |