From ae83e351087e7052079b839333279dd2a37bfe41 Mon Sep 17 00:00:00 2001 From: ligi Date: Sat, 11 Oct 2014 01:25:07 +0200 Subject: [PATCH] improve how the pass list is handled - improves speed and fixes one ANR --- .../model/AndroidFileSystemPassStore.java | 26 ++++++++++++++----- .../ligi/passandroid/ui/PassListActivity.java | 19 +++++++------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/ligi/passandroid/model/AndroidFileSystemPassStore.java b/src/main/java/org/ligi/passandroid/model/AndroidFileSystemPassStore.java index 00c6033c..cecf7257 100644 --- a/src/main/java/org/ligi/passandroid/model/AndroidFileSystemPassStore.java +++ b/src/main/java/org/ligi/passandroid/model/AndroidFileSystemPassStore.java @@ -10,6 +10,7 @@ import org.ligi.passandroid.helper.DirectoryFileFilter; import java.io.File; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; @@ -21,7 +22,7 @@ public class AndroidFileSystemPassStore implements PassStore { private final Context context; private String path; - private List passList; + private List passList = new ArrayList<>(); private Pass actPass; public AndroidFileSystemPassStore(Context context) { @@ -42,18 +43,31 @@ public class AndroidFileSystemPassStore implements PassStore { public void refreshPassesList() { path = App.getPassesDir(context); - passList = new ArrayList<>(); + final List newIds = Arrays.asList(getPassIDArray()); + final List oldIds = new ArrayList<>(); + final List toRemove = new ArrayList<>(); - for (String id : getPassIDArray()) { - passList.add(getCachedPassOrLoad(id)); + for (Pass pass : passList) { + oldIds.add(pass.getId()); + if (!newIds.contains(pass.getId())) { + toRemove.add(pass); + } } + for (String newId : newIds) { + if (!oldIds.contains(newId)) { + passList.add(getCachedPassOrLoad(newId)); + } + } + + passList.removeAll(toRemove); } private String[] getPassIDArray() { return getPassesDirSafely().list(new DirectoryFileFilter()); } + private Pass getCachedPassOrLoad(String id) { final File cachedFile = getCacheFile(id); try { @@ -88,10 +102,10 @@ public class AndroidFileSystemPassStore implements PassStore { return passList.get(pos); } - public Pass getPassbookForId(final String id,final String language) { + public Pass getPassbookForId(final String id, final String language) { final String mPath = path + "/" + id; // TODO read from cache - return AppleStylePassReader.read(mPath,language); + return AppleStylePassReader.read(mPath, language); } public void sort(final SortOrder order) { diff --git a/src/main/java/org/ligi/passandroid/ui/PassListActivity.java b/src/main/java/org/ligi/passandroid/ui/PassListActivity.java index 9ce5bc0d..0c68b371 100644 --- a/src/main/java/org/ligi/passandroid/ui/PassListActivity.java +++ b/src/main/java/org/ligi/passandroid/ui/PassListActivity.java @@ -51,7 +51,6 @@ import fr.nicolaspomepuy.discreetapprate.RetryPolicy; import static org.ligi.passandroid.ui.UnzipPassController.InputStreamUnzipControllerSpec; import static org.ligi.passandroid.ui.UnzipPassController.SilentFail; -import static org.ligi.passandroid.ui.UnzipPassController.SilentWin; import static org.ligi.passandroid.ui.UnzipPassController.processInputStream; public class PassListActivity extends ActionBarActivity { @@ -198,7 +197,7 @@ public class PassListActivity extends ActionBarActivity { } private void prepareRefreshLayout(SwipeRefreshLayout swipeRefreshLayout) { - swipeRefreshLayout.setColorScheme(R.color.icon_blue, R.color.icon_green, R.color.icon_lila, R.color.icon_orange); + swipeRefreshLayout.setColorSchemeResources(R.color.icon_blue, R.color.icon_green, R.color.icon_lila, R.color.icon_orange); swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { @@ -328,7 +327,13 @@ public class PassListActivity extends ActionBarActivity { @Override protected InputStreamWithSource doInBackground(Void... params) { final InputStreamWithSource ins = super.doInBackground(params); - final InputStreamUnzipControllerSpec spec = new InputStreamUnzipControllerSpec(ins, passImportActivity, new SilentWin(), new SilentFail()); + final InputStreamUnzipControllerSpec spec = new InputStreamUnzipControllerSpec(ins, passImportActivity, + new UnzipPassController.SuccessCallback() { + @Override + public void call(String pathToPassbook) { + refreshPasses(); + } + }, new SilentFail()); processInputStream(spec); return ins; } @@ -343,7 +348,8 @@ public class PassListActivity extends ActionBarActivity { class ScanForPassesTask extends AsyncTask, Void> { @Override - protected void onProgressUpdate(Optional... values) { + @SafeVarargs + protected final void onProgressUpdate(Optional... values) { super.onProgressUpdate(values); setActionbarToProgress(values); } @@ -371,11 +377,6 @@ public class PassListActivity extends ActionBarActivity { publishProgress(Optional.of(path.toString())); - if (path == null) { - Log.w("trying to search in null path"); - return; - } - final File[] files = path.listFiles(); if (files == null || files.length == 0) {