From 000598bf01c49129be03aa3e8b2d5be14a9fa2e7 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Tue, 7 Jul 2015 08:39:35 -0700 Subject: Don't show refresh when pulling local data --- .../daneren2005/dsub/adapter/SectionAdapter.java | 11 +++ .../daneren2005/dsub/fragments/MainFragment.java | 1 + .../dsub/fragments/SelectRecyclerFragment.java | 84 ++++++++++++++-------- .../dsub/fragments/SelectYearFragment.java | 7 ++ 4 files changed, 74 insertions(+), 29 deletions(-) (limited to 'app/src/main') diff --git a/app/src/main/java/github/daneren2005/dsub/adapter/SectionAdapter.java b/app/src/main/java/github/daneren2005/dsub/adapter/SectionAdapter.java index b5592520..e8e2bc4a 100644 --- a/app/src/main/java/github/daneren2005/dsub/adapter/SectionAdapter.java +++ b/app/src/main/java/github/daneren2005/dsub/adapter/SectionAdapter.java @@ -71,6 +71,17 @@ public abstract class SectionAdapter extends RecyclerView.Adapter section) { + this.sections = new ArrayList<>(); + this.sections.add(section); + notifyDataSetChanged(); + } + public void replaceExistingData(List headers, List> sections) { + this.headers = headers; + this.sections = sections; + notifyDataSetChanged(); + } + @Override public UpdateViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if(viewType == VIEW_TYPE_HEADER) { diff --git a/app/src/main/java/github/daneren2005/dsub/fragments/MainFragment.java b/app/src/main/java/github/daneren2005/dsub/fragments/MainFragment.java index 5e9da68b..be3fedf7 100644 --- a/app/src/main/java/github/daneren2005/dsub/fragments/MainFragment.java +++ b/app/src/main/java/github/daneren2005/dsub/fragments/MainFragment.java @@ -45,6 +45,7 @@ public class MainFragment extends SelectRecyclerFragment { super(); pullToRefresh = false; serialize = false; + backgroundUpdate = false; } @Override diff --git a/app/src/main/java/github/daneren2005/dsub/fragments/SelectRecyclerFragment.java b/app/src/main/java/github/daneren2005/dsub/fragments/SelectRecyclerFragment.java index 0c3b27f8..f91048b9 100644 --- a/app/src/main/java/github/daneren2005/dsub/fragments/SelectRecyclerFragment.java +++ b/app/src/main/java/github/daneren2005/dsub/fragments/SelectRecyclerFragment.java @@ -15,6 +15,7 @@ package github.daneren2005.dsub.fragments; +import android.content.Context; import android.os.Bundle; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.widget.RecyclerView; @@ -43,12 +44,13 @@ public abstract class SelectRecyclerFragment extends SubsonicFragment impleme private static final String TAG = SelectRecyclerFragment.class.getSimpleName(); protected RecyclerView recyclerView; protected SectionAdapter adapter; - protected BackgroundTask> currentTask; + protected UpdateTask currentTask; protected List objects; protected boolean serialize = true; protected boolean largeAlbums = false; protected int columns; protected boolean pullToRefresh = true; + protected boolean backgroundUpdate = true; @Override public void onCreate(Bundle bundle) { @@ -113,44 +115,30 @@ public abstract class SelectRecyclerFragment extends SubsonicFragment impleme if(titleRes != 0) { setTitle(getTitleResource()); } - recyclerView.setVisibility(View.GONE); + if(backgroundUpdate) { + recyclerView.setVisibility(View.GONE); + } // Cancel current running task before starting another one if(currentTask != null) { currentTask.cancel(); } - currentTask = new TabBackgroundTask>(this) { - @Override - protected List doInBackground() throws Throwable { - MusicService musicService = MusicServiceFactory.getMusicService(context); - - objects = new ArrayList(); + currentTask = new UpdateTask(this, refresh); - try { - objects = getObjects(musicService, refresh, this); - } catch (Exception x) { - Log.e(TAG, "Failed to load", x); - } + if(backgroundUpdate) { + currentTask.execute(); + } else { + objects = new ArrayList(); - return objects; + try { + objects = getObjects(null, refresh, null); + } catch (Exception x) { + Log.e(TAG, "Failed to load", x); } - @Override - protected void done(List result) { - if (result != null && !result.isEmpty()) { - recyclerView.setAdapter(adapter = getAdapter(result)); - - onFinishRefresh(); - recyclerView.setVisibility(View.VISIBLE); - } else { - setEmpty(true); - } - - currentTask = null; - } - }; - currentTask.execute(); + currentTask.done(objects); + } } private void setupLayoutManager() { @@ -165,4 +153,42 @@ public abstract class SelectRecyclerFragment extends SubsonicFragment impleme public void onFinishRefresh() { } + + private class UpdateTask extends TabBackgroundTask> { + private boolean refresh; + + public UpdateTask(SubsonicFragment fragment, boolean refresh) { + super(fragment); + this.refresh = refresh; + } + + @Override + public List doInBackground() throws Exception { + MusicService musicService = MusicServiceFactory.getMusicService(context); + + objects = new ArrayList(); + + try { + objects = getObjects(musicService, refresh, this); + } catch (Exception x) { + Log.e(TAG, "Failed to load", x); + } + + return objects; + } + + @Override + public void done(List result) { + if (result != null && !result.isEmpty()) { + recyclerView.setAdapter(adapter = getAdapter(result)); + + onFinishRefresh(); + recyclerView.setVisibility(View.VISIBLE); + } else { + setEmpty(true); + } + + currentTask = null; + } + } } diff --git a/app/src/main/java/github/daneren2005/dsub/fragments/SelectYearFragment.java b/app/src/main/java/github/daneren2005/dsub/fragments/SelectYearFragment.java index 4e1e51e5..22676d8b 100644 --- a/app/src/main/java/github/daneren2005/dsub/fragments/SelectYearFragment.java +++ b/app/src/main/java/github/daneren2005/dsub/fragments/SelectYearFragment.java @@ -36,6 +36,13 @@ import github.daneren2005.dsub.view.UpdateView; public class SelectYearFragment extends SelectRecyclerFragment { + public SelectYearFragment() { + super(); + pullToRefresh = false; + serialize = false; + backgroundUpdate = false; + } + @Override public int getOptionsMenu() { return R.menu.empty; -- cgit v1.2.3