aboutsummaryrefslogtreecommitdiffstats
path: root/main
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2015-01-12 13:00:16 +0100
committerSamuel Tardieu <sam@rfc1149.net>2015-01-12 13:03:06 +0100
commit4b0a713c91c2a17b2c274f369779618bbb49693d (patch)
treeec7867c0043415622655a01074c41dcb1c05e38b /main
parentb31c3fafd8a9e66967c68554ce2d44126528c5d9 (diff)
downloadcgeo-4b0a713c91c2a17b2c274f369779618bbb49693d.zip
cgeo-4b0a713c91c2a17b2c274f369779618bbb49693d.tar.gz
cgeo-4b0a713c91c2a17b2c274f369779618bbb49693d.tar.bz2
fix #4596: UI delay when building the list of caches to refresh
The list of caches to refresh was partitioned into caches already having static maps and others, the later being prioritized in the refresh. This computation was done on the UI thread, which led to a pause for large lists. The list building in now done in the background, with emission of caches to refresh as soon as possible to reduce the user wait.
Diffstat (limited to 'main')
-rw-r--r--main/src/cgeo/geocaching/CacheListActivity.java43
-rw-r--r--main/src/cgeo/geocaching/Geocache.java8
2 files changed, 36 insertions, 15 deletions
diff --git a/main/src/cgeo/geocaching/CacheListActivity.java b/main/src/cgeo/geocaching/CacheListActivity.java
index 43cd304..9090a4e 100644
--- a/main/src/cgeo/geocaching/CacheListActivity.java
+++ b/main/src/cgeo/geocaching/CacheListActivity.java
@@ -63,19 +63,22 @@ import com.github.amlcurran.showcaseview.targets.ActionViewTarget;
import com.github.amlcurran.showcaseview.targets.ActionViewTarget.Type;
import org.apache.commons.collections4.CollectionUtils;
-import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import rx.Observable;
import rx.Observable.OnSubscribe;
+import rx.Scheduler.Worker;
import rx.Subscriber;
import rx.Subscription;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
+import rx.subjects.ReplaySubject;
+import rx.subscriptions.CompositeSubscription;
+import rx.subscriptions.Subscriptions;
import android.app.Activity;
import android.app.AlertDialog;
@@ -1185,11 +1188,37 @@ public class CacheListActivity extends AbstractListActivity implements FilteredA
*/
private void loadDetails(final CancellableHandler handler, final List<Geocache> caches) {
- final List<Geocache> allCaches = Settings.isStoreOfflineMaps() ?
- ListUtils.union(ListUtils.selectRejected(caches, Geocache.hasStaticMap),
- ListUtils.select(caches, Geocache.hasStaticMap)) :
- caches;
- final Observable<Geocache> loaded = Observable.from(allCaches).flatMap(new Func1<Geocache, Observable<Geocache>>() {
+ final Observable<Geocache> allCaches;
+ final Subscription generator;
+ if (Settings.isStoreOfflineMaps()) {
+ // The list of caches will be generated in the background, putting the caches without static maps first.
+ final ReplaySubject<Geocache> withStaticMaps = ReplaySubject.create(caches.size());
+ final ReplaySubject<Geocache> withoutStaticMaps = ReplaySubject.create(caches.size());
+ final Worker worker = Schedulers.io().createWorker();
+ generator = worker.schedule(new Action0() {
+ @Override
+ public void call() {
+ for (final Geocache cache : caches) {
+ if (worker.isUnsubscribed()) {
+ // Do not continue to check for static maps if the user pressed cancel.
+ return;
+ }
+ if (cache.hasStaticMap()) {
+ withStaticMaps.onNext(cache);
+ } else {
+ withoutStaticMaps.onNext(cache);
+ }
+ }
+ withStaticMaps.onCompleted();
+ withoutStaticMaps.onCompleted();
+ }
+ });
+ allCaches = Observable.concat(withoutStaticMaps, withStaticMaps);
+ } else {
+ allCaches = Observable.from(caches);
+ generator = Subscriptions.empty();
+ }
+ final Observable<Geocache> loaded = allCaches.flatMap(new Func1<Geocache, Observable<Geocache>>() {
@Override
public Observable<Geocache> call(final Geocache cache) {
return Observable.create(new OnSubscribe<Geocache>() {
@@ -1208,7 +1237,7 @@ public class CacheListActivity extends AbstractListActivity implements FilteredA
handler.sendEmptyMessage(DownloadProgress.MSG_DONE);
}
});
- handler.unsubscribeIfCancelled(loaded.subscribe());
+ handler.unsubscribeIfCancelled(new CompositeSubscription(generator, loaded.subscribe()));
}
private class DropDetailsTask extends AsyncTaskWithProgress<Geocache, Void> {
diff --git a/main/src/cgeo/geocaching/Geocache.java b/main/src/cgeo/geocaching/Geocache.java
index dda19c8..8e9485e 100644
--- a/main/src/cgeo/geocaching/Geocache.java
+++ b/main/src/cgeo/geocaching/Geocache.java
@@ -38,7 +38,6 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
-import org.apache.commons.collections4.Predicate;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
@@ -1675,13 +1674,6 @@ public class Geocache implements IWaypoint {
return StaticMapsProvider.hasStaticMap(this);
}
- public static final Predicate<Geocache> hasStaticMap = new Predicate<Geocache>() {
- @Override
- public boolean evaluate(final Geocache cache) {
- return cache.hasStaticMap();
- }
- };
-
@NonNull
public Collection<Image> getImages() {
final LinkedList<Image> result = new LinkedList<>();