Skip to content

Commit

Permalink
Correctly handle unsubscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
esafirm committed Aug 2, 2016
1 parent 1b2db96 commit 72ed2ca
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -14,7 +14,7 @@ allprojects {
```
```
dependencies {
compile 'com.github.esafirm:android-rxgeofence:v0.1.1'
compile 'com.github.esafirm:android-rxgeofence:latest-version'
}
```

Expand Down
10 changes: 9 additions & 1 deletion app/build.gradle
Expand Up @@ -24,13 +24,21 @@ android {
}
}

repositories{
jcenter()
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'

compile project(':rxgeofence')
compile 'com.android.support:appcompat-v7:24.1.1'
compile 'com.android.support:support-v4:24.1.1'

compile 'com.google.code.gson:gson:2.7'

testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.10.19"
compile 'pl.charmas.android:android-reactive-location:0.9@aar'
compile 'com.google.android.gms:play-services-location:9.0.2'
}
2 changes: 1 addition & 1 deletion rxgeofence/build.gradle
Expand Up @@ -8,7 +8,7 @@ android {
minSdkVersion 16
targetSdkVersion 24
versionCode 1
versionName "0.1.1"
versionName "0.1.2"
}
buildTypes {
release {
Expand Down
Expand Up @@ -18,9 +18,12 @@
import com.google.firebase.database.FirebaseDatabase;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.subscriptions.Subscriptions;

/**
* Created by esafirm on 7/29/16.
Expand All @@ -29,7 +32,8 @@ public class RxGeoFenceOnSubscribe implements Observable.OnSubscribe<GeoFenceEve

private static final String GEO_FIRE_REF = "/_geofire";
private static final String APP_ID = "RxGeoFence";
private static final String USER_KEY = "UserRxGeoFence";

private final String userKey = "UserRxGeoFence" + UUID.randomUUID();

private GeoFire geoFire;
private Observable<List<Place>> geoFenceSource;
Expand Down Expand Up @@ -62,7 +66,7 @@ public void setGeoFenceSource(Observable<List<Place>> geoFenceSource) {

locationSource.forEach(new Action1<LatLng>() {
@Override public void call(LatLng latLng) {
geoFire.setLocation(USER_KEY, latLng.toGeoLocation());
geoFire.setLocation(userKey, latLng.toGeoLocation());
}
});

Expand All @@ -76,6 +80,13 @@ public void setGeoFenceSource(Observable<List<Place>> geoFenceSource) {
}
}
});

subscriber.add(Subscriptions.create(new Action0() {
@Override public void call() {
stopListen();
geoFire.removeLocation(userKey);
}
}));
}

private void listen(GeoQueryWrapper<Place> wrapper,
Expand All @@ -84,7 +95,7 @@ private void listen(GeoQueryWrapper<Place> wrapper,
GeoQuery geoQuery = wrapper.getGeoQuery();
geoQuery.addGeoQueryEventListener(new PlaceGeoQueryEventListener(wrapper.getData()) {
@Override public void onChange(GeoFenceType type, String key, Place place) {
if (key.equalsIgnoreCase(USER_KEY)) {
if (key.equalsIgnoreCase(userKey)) {
subscriber.onNext(new GeoFenceEvent(key, place, type));
}
}
Expand Down
Expand Up @@ -14,6 +14,7 @@
package com.esafirm.rxgeofence.utils;

public final class Preconditions {

public static void checkArgument(boolean assertion, String message) {
if (!assertion) {
throw new IllegalArgumentException(message);
Expand Down

This file was deleted.

@@ -0,0 +1,14 @@
package com.esafirm.rxgeofence;

import com.esafirm.rxgeofence.model.GeoFenceEvent;
import org.junit.Test;
import rx.observers.TestSubscriber;

/**
* To work on unit tests, switch the Test Artifact in the Build Variants view.
*/
public class RxGeoFenceTest {
@Test public void testRaceCondition() throws Exception {
TestSubscriber<GeoFenceEvent> testSubscriber = new TestSubscriber<>();
}
}

0 comments on commit 72ed2ca

Please sign in to comment.