diff options
author | Scott Jackson <daneren2005@gmail.com> | 2015-12-17 17:10:30 -0800 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2015-12-17 17:10:30 -0800 |
commit | 4c7d1f25ecb6b0c1a1a5245acfad406fc4bffe8e (patch) | |
tree | 4d565d01a9da5d9272fa7ca437fe486959fb55de | |
parent | 9e809b82a743aa8c2140edb2317080f4cdb0224b (diff) | |
download | dsub-4c7d1f25ecb6b0c1a1a5245acfad406fc4bffe8e.tar.gz dsub-4c7d1f25ecb6b0c1a1a5245acfad406fc4bffe8e.tar.bz2 dsub-4c7d1f25ecb6b0c1a1a5245acfad406fc4bffe8e.zip |
Delete/add entries from background refresh
3 files changed, 40 insertions, 18 deletions
diff --git a/app/src/main/java/github/daneren2005/dsub/domain/MusicDirectory.java b/app/src/main/java/github/daneren2005/dsub/domain/MusicDirectory.java index 8983051e..b502bdf4 100644 --- a/app/src/main/java/github/daneren2005/dsub/domain/MusicDirectory.java +++ b/app/src/main/java/github/daneren2005/dsub/domain/MusicDirectory.java @@ -90,11 +90,11 @@ public class MusicDirectory implements Serializable { this.children = children; } - public List<Entry> getChildren() { + public synchronized List<Entry> getChildren() { return getChildren(true, true); } - public List<Entry> getChildren(boolean includeDirs, boolean includeFiles) { + public synchronized List<Entry> getChildren(boolean includeDirs, boolean includeFiles) { if (includeDirs && includeFiles) { return children; } @@ -107,7 +107,7 @@ public class MusicDirectory implements Serializable { } return result; } - public List<Entry> getSongs() { + public synchronized List<Entry> getSongs() { List<Entry> result = new ArrayList<Entry>(); for (Entry child : children) { if (child != null && !child.isDirectory() && !child.isVideo()) { @@ -117,7 +117,7 @@ public class MusicDirectory implements Serializable { return result; } - public int getChildrenSize() { + public synchronized int getChildrenSize() { return children.size(); } @@ -135,7 +135,7 @@ public class MusicDirectory implements Serializable { EntryComparator.sort(children, byYear); } - public void updateDifferences(Context context, int instance, MusicDirectory refreshedDirectory) { + public synchronized void updateMetadata(MusicDirectory refreshedDirectory) { Iterator<Entry> it = children.iterator(); while(it.hasNext()) { Entry entry = it.next(); @@ -155,24 +155,36 @@ public class MusicDirectory implements Serializable { entry.setStarred(refreshed.isStarred()); entry.setRating(refreshed.getRating()); entry.setType(refreshed.getType()); - } else { - // No longer exists in here - // it.remove(); + } + } + } + public synchronized boolean updateEntriesList(Context context, int instance, MusicDirectory refreshedDirectory) { + boolean changed = false; + Iterator<Entry> it = children.iterator(); + while(it.hasNext()) { + Entry entry = it.next(); + // No longer exists in here + if(refreshedDirectory.children.indexOf(entry) == -1) { + it.remove(); + changed = true; } } // Make sure we contain all children from refreshed set - /*boolean resort = false; + boolean resort = false; for(Entry refreshed: refreshedDirectory.children) { if(!this.children.contains(refreshed)) { this.children.add(refreshed); resort = true; + changed = true; } } if(resort) { this.sortChildren(context, instance); - }*/ + } + + return changed; } public static class Entry implements Serializable { diff --git a/app/src/main/java/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java b/app/src/main/java/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java index ef5490d1..d2282117 100644 --- a/app/src/main/java/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java +++ b/app/src/main/java/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java @@ -647,7 +647,7 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section } } - return new Pair<MusicDirectory, Boolean>(dir, licenseValid); + return new Pair<>(dir, licenseValid); } @Override @@ -655,6 +655,13 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section finishLoading(); currentTask = null; } + + @Override + public void updateCache() { + if(entryGridAdapter != null) { + entryGridAdapter.notifyDataSetChanged(); + } + } } @Override diff --git a/app/src/main/java/github/daneren2005/dsub/service/CachedMusicService.java b/app/src/main/java/github/daneren2005/dsub/service/CachedMusicService.java index 256fd6df..e85e73c5 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/CachedMusicService.java +++ b/app/src/main/java/github/daneren2005/dsub/service/CachedMusicService.java @@ -168,23 +168,26 @@ public class CachedMusicService implements MusicService { dir = cached; new SilentBackgroundTask<Void>(context) { + MusicDirectory refreshed; + @Override protected Void doInBackground() throws Throwable { - Util.sleepQuietly(2000L); - MusicDirectory refreshed = musicService.getMusicDirectory(id, name, true, context, null); + refreshed = musicService.getMusicDirectory(id, name, true, context, null); updateAllSongs(context, refreshed); - cached.updateDifferences(context, musicService.getInstance(context), refreshed); + cached.updateMetadata(refreshed); FileUtil.serialize(context, refreshed, getCacheName(context, "directory", id)); return null; } - // TODO: When upgrading to RecyclerView, this should be usable since won't have split entry/album lists - /*@Override + // Update which entries exist + @Override public void done(Void result) { if(progressListener != null) { - progressListener.updateCache(); + if(cached.updateEntriesList(context, musicService.getInstance(context), refreshed)) { + progressListener.updateCache(); + } } - }*/ + } @Override public void error(Throwable error) { |