improve how the pass list is handled - improves speed and fixes one ANR
This commit is contained in:
parent
60689bf02c
commit
ae83e35108
2 changed files with 30 additions and 15 deletions
|
@ -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<Pass> passList;
|
||||
private List<Pass> 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<String> newIds = Arrays.asList(getPassIDArray());
|
||||
final List<String> oldIds = new ArrayList<>();
|
||||
final List<Pass> 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) {
|
||||
|
|
|
@ -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, Optional<String>, Void> {
|
||||
|
||||
@Override
|
||||
protected void onProgressUpdate(Optional<String>... values) {
|
||||
@SafeVarargs
|
||||
protected final void onProgressUpdate(Optional<String>... 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) {
|
||||
|
|
Loading…
Reference in a new issue