diff options
author | Samuel Tardieu <sam@rfc1149.net> | 2015-01-12 13:03:17 +0100 |
---|---|---|
committer | Samuel Tardieu <sam@rfc1149.net> | 2015-01-12 13:03:17 +0100 |
commit | 1e3267fedd82d1d83661d794b3d834390a7e7a18 (patch) | |
tree | 81f40030d4a918371229c8584a6e8f2897227d04 | |
parent | 2682c15758ff8e7cd713feb9572919cb225e8a05 (diff) | |
parent | 4b0a713c91c2a17b2c274f369779618bbb49693d (diff) | |
download | cgeo-1e3267fedd82d1d83661d794b3d834390a7e7a18.zip cgeo-1e3267fedd82d1d83661d794b3d834390a7e7a18.tar.gz cgeo-1e3267fedd82d1d83661d794b3d834390a7e7a18.tar.bz2 |
Merge branch 'issue-4596' into upstream
-rw-r--r-- | main/src/cgeo/geocaching/CacheListActivity.java | 43 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/Geocache.java | 8 |
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<>(); |