From 46b4ccb63f90f33214de3a8ba3e12dc7a0c30ac8 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Thu, 4 Sep 2014 18:10:11 -0700 Subject: Convert bookmarks to GenericEntryUpdater to simplify logic, hit podcasts cache --- .../daneren2005/dsub/domain/MusicDirectory.java | 6 +- .../dsub/fragments/NowPlayingFragment.java | 7 +- .../dsub/fragments/SubsonicFragment.java | 8 +- .../dsub/service/CachedMusicService.java | 180 +++++++-------------- .../daneren2005/dsub/service/DownloadService.java | 9 +- .../daneren2005/dsub/service/MusicService.java | 4 +- .../dsub/service/OfflineMusicService.java | 4 +- .../daneren2005/dsub/service/RESTMusicService.java | 8 +- 8 files changed, 81 insertions(+), 145 deletions(-) (limited to 'src') diff --git a/src/github/daneren2005/dsub/domain/MusicDirectory.java b/src/github/daneren2005/dsub/domain/MusicDirectory.java index 5b6bab26..74bc89f5 100644 --- a/src/github/daneren2005/dsub/domain/MusicDirectory.java +++ b/src/github/daneren2005/dsub/domain/MusicDirectory.java @@ -413,7 +413,11 @@ public class MusicDirectory implements Serializable { return rating == null ? 0 : rating; } public void setRating(Integer rating) { - this.rating = rating; + if(rating == null || rating == 0) { + this.rating = null; + } else { + this.rating = rating; + } } public Bookmark getBookmark() { diff --git a/src/github/daneren2005/dsub/fragments/NowPlayingFragment.java b/src/github/daneren2005/dsub/fragments/NowPlayingFragment.java index e0f37f09..6c2ad126 100644 --- a/src/github/daneren2005/dsub/fragments/NowPlayingFragment.java +++ b/src/github/daneren2005/dsub/fragments/NowPlayingFragment.java @@ -25,7 +25,6 @@ import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; -import android.content.res.TypedArray; import android.graphics.Color; import android.os.Build; import android.os.Bundle; @@ -47,18 +46,15 @@ import android.view.ViewGroup; import android.view.WindowManager; import android.view.animation.AnimationUtils; import android.widget.AdapterView; -import android.widget.Button; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ImageView; -import android.widget.LinearLayout; import android.widget.SeekBar; import android.widget.TextView; import android.widget.ViewFlipper; import github.daneren2005.dsub.R; import github.daneren2005.dsub.activity.SubsonicFragmentActivity; import github.daneren2005.dsub.domain.Bookmark; -import github.daneren2005.dsub.domain.MusicDirectory; import github.daneren2005.dsub.domain.PlayerState; import github.daneren2005.dsub.domain.RepeatMode; import github.daneren2005.dsub.service.DownloadFile; @@ -1378,9 +1374,10 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis Entry currentSong = currentDownload.getSong(); MusicService musicService = MusicServiceFactory.getMusicService(context); int position = getDownloadService().getPlayerPosition(); - musicService.createBookmark(currentSong.getId(), Util.getParentFromEntry(context, currentSong), position, comment, context, null); currentSong.setBookmark(new Bookmark(position)); + musicService.createBookmark(currentSong, position, comment, context, null); + Entry find = UpdateView.findEntry(currentSong); if(find != null && find != currentSong) { find.setBookmark(new Bookmark(position)); diff --git a/src/github/daneren2005/dsub/fragments/SubsonicFragment.java b/src/github/daneren2005/dsub/fragments/SubsonicFragment.java index ce2ce2cd..e5b74699 100644 --- a/src/github/daneren2005/dsub/fragments/SubsonicFragment.java +++ b/src/github/daneren2005/dsub/fragments/SubsonicFragment.java @@ -34,7 +34,6 @@ import android.support.v4.widget.SwipeRefreshLayout; import android.util.Log; import android.view.ContextMenu; import android.view.GestureDetector; -import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; @@ -45,7 +44,6 @@ import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; -import android.widget.LinearLayout; import android.widget.RatingBar; import android.widget.TextView; import github.daneren2005.dsub.R; @@ -1420,7 +1418,7 @@ public class SubsonicFragment extends Fragment implements SwipeRefreshLayout.OnR @Override protected Void doInBackground() throws Throwable { MusicService musicService = MusicServiceFactory.getMusicService(context); - musicService.deleteBookmark(song.getId(), Util.getParentFromEntry(context, song), context, null); + musicService.deleteBookmark(song, context, null); song.setBookmark(null); return null; @@ -1497,9 +1495,9 @@ public class SubsonicFragment extends Fragment implements SwipeRefreshLayout.OnR @Override protected Void doInBackground() throws Throwable { MusicService musicService = MusicServiceFactory.getMusicService(context); - musicService.deleteBookmark(entry.getId(), Util.getParentFromEntry(context, entry), context, null); - entry.setBookmark(null); + musicService.deleteBookmark(entry, context, null); + return null; } diff --git a/src/github/daneren2005/dsub/service/CachedMusicService.java b/src/github/daneren2005/dsub/service/CachedMusicService.java index 783f0b4d..4ea8a048 100644 --- a/src/github/daneren2005/dsub/service/CachedMusicService.java +++ b/src/github/daneren2005/dsub/service/CachedMusicService.java @@ -826,43 +826,16 @@ public class CachedMusicService implements MusicService { // Remove from old regardless of whether position is wrong it.remove(); + } else { + oldEntry.setBookmark(null); } } - - // Remove bookmarks from thinsg still in old list - setBookmarkCache(context, oldList, true); - // Add new bookmarks for things in new list - setBookmarkCache(context, newList, false); - - if(oldList.size() > 0 || newList.size() > 0) { - new PlaylistDirectoryUpdater(context) { - @Override - public boolean checkResult(Entry check) { - for(Entry entry: oldList) { - if(entry.getId().equals(check.getId()) && check.getBookmark() != null) { - check.setBookmark(null); - return true; - } - } - for(Entry entry: newList) { - if(entry.getId().equals(check.getId())) { - int newPosition = entry.getBookmark().getPosition(); - if(check.getBookmark() == null || check.getBookmark().getPosition() != newPosition) { - setBookmarkCache(check, newPosition); - return true; - } - } - } - - return false; - } - - @Override - public void updateResult(Entry result) { - - } - }.execute(); - } + + List totalList = new ArrayList(); + totalList.addAll(oldList); + totalList.addAll(newList); + + new BookmarkUpdater(context, totalList).execute(); } FileUtil.serialize(context, bookmarks, "bookmarks"); @@ -870,96 +843,17 @@ public class CachedMusicService implements MusicService { } @Override - public void createBookmark(String id, String parent, int position, String comment, Context context, ProgressListener progressListener) throws Exception { - musicService.createBookmark(id, null, position, comment, context, progressListener); - // Add to directory cache - setBookmarkCache(context, id, parent, position); - // Add to playlist cache - setBookmarkCache(context, id, position); + public void createBookmark(Entry entry, int position, String comment, Context context, ProgressListener progressListener) throws Exception { + musicService.createBookmark(entry, position, comment, context, progressListener); + + new BookmarkUpdater(context, entry).execute(); } @Override - public void deleteBookmark(String id, String parent, Context context, ProgressListener progressListener) throws Exception { - musicService.deleteBookmark(id, null, context, progressListener); - // Delete from directory cache - setBookmarkCache(context, id, parent, -1); - // Delete from playlist cache - setBookmarkCache(context, id, -1); - } - - private void setBookmarkCache(Context context, List entries, boolean remove) { - for(final Entry entry: entries) { - if(remove) { - setBookmarkCache(context, entry.getId(), Util.getParentFromEntry(context, entry), -1); - } else { - setBookmarkCache(context, entry.getId(), Util.getParentFromEntry(context, entry), entry.getBookmark().getPosition()); - } - } - } - private void setBookmarkCache(Context context, final String id, final String parent, final int position) { - String cacheName; - if(isTagBrowsing) { - cacheName = "album"; - } else { - cacheName = "directory"; - } + public void deleteBookmark(Entry entry, Context context, ProgressListener progressListener) throws Exception { + musicService.deleteBookmark(entry, context, progressListener); - // Update the parent directory with bookmark data - new MusicDirectoryUpdater(context, cacheName, parent) { - @Override - public boolean checkResult(Entry check) { - return shouldBookmarkUpdate(check, id, position); - } - - @Override - public void updateResult(List objects, Entry result) { - setBookmarkCache(result, position); - } - }.execute(); - } - private void setBookmarkCache(Context context, final String id, final int position) { - // Update playlists with bookmark data - new PlaylistDirectoryUpdater(context) { - @Override - public boolean checkResult(Entry check) { - return shouldBookmarkUpdate(check, id, position); - } - - @Override - public void updateResult(Entry result) { - setBookmarkCache(result, position); - } - }.execute(); - } - - private boolean shouldBookmarkUpdate(Entry check, String id, int position) { - if(id.equals(check.getId())) { - if(position == -1 && check.getBookmark() != null) { - return true; - } else if(position >= 0 && (check.getBookmark() == null || check.getBookmark().getPosition() != position)) { - return true; - } - } - - return false; - } - - private void setBookmarkCache(Entry result, int position) { - // If position == -1, then it is a delete - if(result.getBookmark() != null && position == -1) { - result.setBookmark(null); - } else if(position >= 0) { - Bookmark bookmark = result.getBookmark(); - - // Create one if empty - if(bookmark == null) { - bookmark = new Bookmark(); - result.setBookmark(bookmark); - } - - // Update bookmark position no matter what - bookmark.setPosition(position); - } + new BookmarkUpdater(context, entry).execute(); } @Override @@ -1345,6 +1239,50 @@ public class CachedMusicService implements MusicService { } } } + private class BookmarkUpdater extends GenericEntryUpdater { + public BookmarkUpdater(Context context, Entry entry) { + super(context, entry); + } + public BookmarkUpdater(Context context, List entries) { + super(context, entries); + } + + @Override + public boolean checkResult(Entry entry, Entry check) { + if(entry.getId().equals(check.getId())) { + int position; + if(entry.getBookmark() == null) { + position = -1; + } else { + position = entry.getBookmark().getPosition(); + } + + if(position == -1 && check.getBookmark() != null) { + check.setBookmark(null); + return true; + } else if(position >= 0 && (check.getBookmark() == null || check.getBookmark().getPosition() != position)) { + Bookmark bookmark = check.getBookmark(); + + // Create one if empty + if(bookmark == null) { + bookmark = new Bookmark(); + check.setBookmark(bookmark); + } + + // Update bookmark position no matter what + bookmark.setPosition(position); + return true; + } + } + + return false; + } + + @Override + public void updateResult(Entry result) { + + } + } private abstract class IndexesUpdater extends SerializeUpdater { Indexes indexes; diff --git a/src/github/daneren2005/dsub/service/DownloadService.java b/src/github/daneren2005/dsub/service/DownloadService.java index f08c8971..d9c67cdb 100644 --- a/src/github/daneren2005/dsub/service/DownloadService.java +++ b/src/github/daneren2005/dsub/service/DownloadService.java @@ -45,7 +45,6 @@ import github.daneren2005.dsub.util.Constants; import github.daneren2005.dsub.util.MediaRouteManager; import github.daneren2005.dsub.util.ShufflePlayBuffer; import github.daneren2005.dsub.util.SimpleServiceBinder; -import github.daneren2005.dsub.util.SyncUtil; import github.daneren2005.dsub.util.Util; import github.daneren2005.dsub.util.compat.RemoteControlClientHelper; import github.daneren2005.dsub.view.UpdateView; @@ -1802,9 +1801,9 @@ public class DownloadService extends Service { @Override public Void doInBackground() throws Throwable { MusicService musicService = MusicServiceFactory.getMusicService(DownloadService.this); - musicService.deleteBookmark(entry.getId(), Util.getParentFromEntry(DownloadService.this, entry), DownloadService.this, null); - entry.setBookmark(null); + musicService.deleteBookmark(entry, DownloadService.this, null); + MusicDirectory.Entry found = UpdateView.findEntry(entry); if(found != null) { found.setBookmark(null); @@ -1852,9 +1851,9 @@ public class DownloadService extends Service { @Override public Void doInBackground() throws Throwable { MusicService musicService = MusicServiceFactory.getMusicService(context); - musicService.createBookmark(entry.getId(), Util.getParentFromEntry(context, entry), position, "Auto created by DSub", context, null); - entry.setBookmark(new Bookmark(position)); + musicService.createBookmark(entry, position, "Auto created by DSub", context, null); + MusicDirectory.Entry found = UpdateView.findEntry(entry); if(found != null) { found.setBookmark(new Bookmark(position)); diff --git a/src/github/daneren2005/dsub/service/MusicService.java b/src/github/daneren2005/dsub/service/MusicService.java index 466ad477..70672269 100644 --- a/src/github/daneren2005/dsub/service/MusicService.java +++ b/src/github/daneren2005/dsub/service/MusicService.java @@ -156,9 +156,9 @@ public interface MusicService { MusicDirectory getBookmarks(boolean refresh, Context context, ProgressListener progressListener) throws Exception; - void createBookmark(String id, String parent, int position, String comment, Context context, ProgressListener progressListener) throws Exception; + void createBookmark(MusicDirectory.Entry entry, int position, String comment, Context context, ProgressListener progressListener) throws Exception; - void deleteBookmark(String id, String parent, Context context, ProgressListener progressListener) throws Exception; + void deleteBookmark(MusicDirectory.Entry entry, Context context, ProgressListener progressListener) throws Exception; User getUser(boolean refresh, String username, 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 e20a2b41..3e43de04 100644 --- a/src/github/daneren2005/dsub/service/OfflineMusicService.java +++ b/src/github/daneren2005/dsub/service/OfflineMusicService.java @@ -747,12 +747,12 @@ public class OfflineMusicService implements MusicService { } @Override - public void createBookmark(String id, String parent, int position, String comment, Context context, ProgressListener progressListener) throws Exception { + public void createBookmark(MusicDirectory.Entry entry, int position, String comment, Context context, ProgressListener progressListener) throws Exception { throw new OfflineException(ERRORMSG); } @Override - public void deleteBookmark(String id, String parent, Context context, ProgressListener progressListener) throws Exception { + public void deleteBookmark(MusicDirectory.Entry entry, Context context, ProgressListener progressListener) throws Exception { throw new OfflineException(ERRORMSG); } diff --git a/src/github/daneren2005/dsub/service/RESTMusicService.java b/src/github/daneren2005/dsub/service/RESTMusicService.java index 991ad5ee..0e21b48c 100644 --- a/src/github/daneren2005/dsub/service/RESTMusicService.java +++ b/src/github/daneren2005/dsub/service/RESTMusicService.java @@ -1150,10 +1150,10 @@ public class RESTMusicService implements MusicService { } @Override - public void createBookmark(String id, String parent, int position, String comment, Context context, ProgressListener progressListener) throws Exception { + public void createBookmark(MusicDirectory.Entry entry, int position, String comment, Context context, ProgressListener progressListener) throws Exception { checkServerVersion(context, "1.9", "Creating bookmarks not supported."); - Reader reader = getReader(context, progressListener, "createBookmark", null, Arrays.asList("id", "position", "comment"), Arrays.asList(id, position, comment)); + Reader reader = getReader(context, progressListener, "createBookmark", null, Arrays.asList("id", "position", "comment"), Arrays.asList(entry.getId(), position, comment)); try { new ErrorParser(context, getInstance(context)).parse(reader); } finally { @@ -1162,10 +1162,10 @@ public class RESTMusicService implements MusicService { } @Override - public void deleteBookmark(String id, String parent, Context context, ProgressListener progressListener) throws Exception { + public void deleteBookmark(MusicDirectory.Entry entry, Context context, ProgressListener progressListener) throws Exception { checkServerVersion(context, "1.9", "Deleting bookmarks not supported."); - Reader reader = getReader(context, progressListener, "deleteBookmark", null, Arrays.asList("id"), Arrays.asList(id)); + Reader reader = getReader(context, progressListener, "deleteBookmark", null, Arrays.asList("id"), Arrays.asList(entry.getId())); try { new ErrorParser(context, getInstance(context)).parse(reader); } finally { -- cgit v1.2.3