From 492bff3e024d5475f7cd1da921800e5d531d87e9 Mon Sep 17 00:00:00 2001 From: Samuel Tardieu Date: Wed, 26 Mar 2014 23:26:53 +0100 Subject: Allow subscription to either location or direction data The forced union of both information is not appropriate in several activities where only GPS data is needed, or where direction data update less components than location data (such as cache lists). This is part of work on #3680. --- main/src/cgeo/geocaching/CgeoApplication.java | 52 ++++++++++++++++----------- 1 file changed, 32 insertions(+), 20 deletions(-) (limited to 'main/src/cgeo/geocaching/CgeoApplication.java') diff --git a/main/src/cgeo/geocaching/CgeoApplication.java b/main/src/cgeo/geocaching/CgeoApplication.java index 21307d2..eda8420 100644 --- a/main/src/cgeo/geocaching/CgeoApplication.java +++ b/main/src/cgeo/geocaching/CgeoApplication.java @@ -5,9 +5,9 @@ import cgeo.geocaching.sensors.GeoDataProvider; import cgeo.geocaching.sensors.IGeoData; import cgeo.geocaching.utils.Log; -import org.apache.commons.lang3.tuple.ImmutablePair; import rx.Observable; -import rx.functions.Func2; +import rx.functions.Action1; +import rx.observables.ConnectableObservable; import android.app.Application; @@ -17,7 +17,10 @@ public class CgeoApplication extends Application { public boolean showLoginToast = true; //login toast shown just once. private boolean liveMapHintShownInThisSession = false; // livemap hint has been shown private static CgeoApplication instance; - private Observable> geoDir; + private Observable geoDataObservable; + private Observable directionObservable; + private volatile IGeoData currentGeo = null; + private volatile float currentDirection = 0.0f; public CgeoApplication() { setInstance(this); @@ -37,31 +40,40 @@ public class CgeoApplication extends Application { DataStore.removeAllFromCache(); } - public synchronized Observable> geoDirObservable() { - if (geoDir == null) { - final Observable geo = GeoDataProvider.create(this); - final Observable dir = DirectionProvider.create(this); - final Observable> combined = Observable.combineLatest(geo, dir, new Func2>() { - @Override - public ImmutablePair call(final IGeoData geoData, final Float dir) { - return ImmutablePair.of(geoData, dir); - } - }); - geoDir = combined.publish().refCount(); + public synchronized Observable geoDataObservable() { + if (geoDataObservable == null) { + final ConnectableObservable onDemand = GeoDataProvider.create(this).publish(); + onDemand.subscribe(new Action1() { + @Override + public void call(final IGeoData geoData) { + currentGeo = geoData; + } + }); + geoDataObservable = onDemand.refCount(); } - return geoDir; + return geoDataObservable; } - private ImmutablePair currentGeoDir() { - return geoDirObservable().first().toBlockingObservable().single(); + public synchronized Observable directionObservable() { + if (directionObservable == null) { + final ConnectableObservable onDemand = DirectionProvider.create(this).publish(); + onDemand.subscribe(new Action1() { + @Override + public void call(final Float direction) { + currentDirection = direction; + } + }); + directionObservable = onDemand.refCount(); + } + return directionObservable; } public IGeoData currentGeo() { - return currentGeoDir().left; + return currentGeo != null ? currentGeo : geoDataObservable().toBlockingObservable().first(); } - public Float currentDirection() { - return currentGeoDir().right; + public float currentDirection() { + return currentDirection; } public boolean isLiveMapHintShownInThisSession() { -- cgit v1.1