Skip to content

Commit d1f6bad

Browse files
author
Piotr Wittchen
committed
migrating to RxJava2.x
1 parent fcd58a7 commit d1f6bad

File tree

11 files changed

+180
-148
lines changed

11 files changed

+180
-148
lines changed

README.md

+40-34
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
# ReactiveSensors
2-
[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-ReactiveSensors-brightgreen.svg?style=flat)](http://android-arsenal.com/details/1/2451) [![Build Status](https://travis-ci.org/pwittchen/ReactiveSensors.svg?branch=master)](https://travis-ci.org/pwittchen/ReactiveSensors) ![Maven Central](https://img.shields.io/maven-central/v/com.github.pwittchen/reactivesensors.svg?style=flat)
2+
[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-ReactiveSensors-brightgreen.svg?style=flat)](http://android-arsenal.com/details/1/2451)
33

4-
Android library monitoring hardware sensors with RxJava Observables.
4+
Android library monitoring hardware sensors with RxJava.
55

6-
Library is compatible with RxJava 1.+ and RxAndroid 1.+ and uses them under the hood.
6+
| Current Branch | Branch | Artifact Id | Build Status | Coverage | Maven Central |
7+
|:--------------:|:-------:|:-----------:|:-------------:|:--------:|:-------------:|
8+
| | [`RxJava1.x`](https://github.com/pwittchen/ReactiveSensors/tree/RxJava1.x) | `reactivesensors` | [![Build Status for RxJava1.x](https://travis-ci.org/pwittchen/ReactiveSensors.svg?branch=RxJava1.x)](https://travis-ci.org/pwittchen/ReactiveSensors) | [![codecov](https://codecov.io/gh/pwittchen/ReactiveSensors/branch/RxJava1.x/graph/badge.svg)](https://codecov.io/gh/pwittchen/ReactiveSensors/branch/RxJava1.x) | ![Maven Central](https://img.shields.io/maven-central/v/com.github.pwittchen/reactivesensors.svg?style=flat) |
9+
| :ballot_box_with_check: | [`RxJava2.x`](https://github.com/pwittchen/ReactiveSensors/tree/RxJava2.x) | `reactivesensors-rx2` | [![Build Status for RxJava2.x](https://travis-ci.org/pwittchen/ReactiveSensors.svg?branch=RxJava2.x)](https://travis-ci.org/pwittchen/ReactiveSensors) | [![codecov](https://codecov.io/gh/pwittchen/ReactiveSensors/branch/RxJava2.x/graph/badge.svg)](https://codecov.io/gh/pwittchen/ReactiveSensors/branch/RxJava2.x) | ![Maven Central](https://img.shields.io/maven-central/v/com.github.pwittchen/reactivesensors-rx2.svg?style=flat) |
10+
11+
This is **RxJava2.x** branch. To see documentation for RxJava1.x, switch to [RxJava1.x](https://github.com/pwittchen/ReactiveSensors/tree/RxJava1.x) branch.
712

813
min sdk version = 9
914

@@ -17,7 +22,7 @@ Contents
1722
- [Good practices](#good-practices)
1823
- [Checking whether sensor exists](#checking-whether-sensor-exists)
1924
- [Letting it crash](#letting-it-crash)
20-
- [Subscribing and unsubscribing observables](#subscribing-and-unsubscribing-observables)
25+
- [Subscribing and disposing flowables](#subscribing-and-disposing-flowables)
2126
- [Filtering stream](#filtering-stream)
2227
- [Other practices](#other-practices)
2328
- [Download](#download)
@@ -32,14 +37,14 @@ Usage
3237

3338
Code sample below demonstrates how to observe Gyroscope sensor.
3439

35-
Please note that we are filtering events occuring when sensor readings change with `ReactiveSensorFilter.filterSensorChanged()` method. There's also event describing change of sensor's accuracy, which can be filtered with `ReactiveSensorFilter.filterAccuracyChanged()` method. When we don't apply any filter, we will be notified both about sensor readings and accuracy changes.
40+
Please note that we are filtering events occurring when sensor readings change with `ReactiveSensorFilter.filterSensorChanged()` method. There's also event describing change of sensor's accuracy, which can be filtered with `ReactiveSensorFilter.filterAccuracyChanged()` method. When we don't apply any filter, we will be notified both about sensor readings and accuracy changes.
3641

3742
```java
3843
new ReactiveSensors(context).observeSensor(Sensor.TYPE_GYROSCOPE)
3944
.subscribeOn(Schedulers.computation())
4045
.filter(ReactiveSensorFilter.filterSensorChanged())
4146
.observeOn(AndroidSchedulers.mainThread())
42-
.subscribe(new Action1<ReactiveSensorEvent>() {
47+
.subscribe(new Consumer<ReactiveSensorEvent>() {
4348
@Override public void call(ReactiveSensorEvent reactiveSensorEvent) {
4449
SensorEvent event = reactiveSensorEvent.getSensorEvent();
4550

@@ -59,16 +64,16 @@ We can observe any hardware sensor in the same way. You can check [list of all s
5964

6065
### Setting sampling period
6166

62-
Default sampling period for observable below is set to `SensorManager.SENSOR_DELAY_NORMAL`.
67+
Default sampling period for flowable below is set to `SensorManager.SENSOR_DELAY_NORMAL`.
6368

6469
```java
65-
Observable<ReactiveSensorEvent> observeSensor(int sensorType)
70+
Flowable<ReactiveSensorEvent> observeSensor(int sensorType)
6671
```
6772

68-
We can configure sampling period according to our needs with the following observable:
73+
We can configure sampling period according to our needs with the following flowable:
6974

7075
```java
71-
Observable<ReactiveSensorEvent> observeSensor(int sensorType,
76+
Flowable<ReactiveSensorEvent> observeSensor(int sensorType,
7277
final int samplingPeriodInUs)
7378
```
7479

@@ -80,13 +85,20 @@ We can use predefined values available in `SensorManager` class from Android SDK
8085

8186
We can also define our own integer value in microseconds, but it's recommended to use predefined values.
8287

88+
We can customize RxJava 2 Backpressure Strategy for our flowable with method:
89+
90+
```java
91+
Flowable<ReactiveSensorEvent> observeSensor(int sensorType, final int samplingPeriodInUs,
92+
final Handler handler, final BackpressureStrategy strategy)
93+
```
94+
95+
Default Backpressure Strategy is `BUFFER`.
96+
8397
Example
8498
-------
8599

86100
Exemplary application, which gets readings of various sensors is located in `app` directory of this repository. You can easily change `SENSOR_TYPE` variable to read values from a different sensor in a given samples.
87101

88-
If you are interested in library usage with Kotlin, check sample in `app-kotlin` directory.
89-
90102
Good practices
91103
--------------
92104

@@ -113,28 +125,22 @@ new ReactiveSensors(context).observeSensor(Sensor.TYPE_GYROSCOPE)
113125
.subscribeOn(Schedulers.computation())
114126
.filter(ReactiveSensorFilter.filterSensorChanged())
115127
.observeOn(AndroidSchedulers.mainThread())
116-
.subscribe(new Subscriber<ReactiveSensorEvent>() {
117-
@Override public void onCompleted() {
118-
// subscription completed
119-
}
120-
121-
@Override public void onError(Throwable throwable) {
122-
if (throwable instanceof SensorNotFoundException) {
123-
// device does not have given sensor - show error message
124-
} else {
125-
// handle other types of errors
126-
}
127-
}
128-
129-
@Override public void onNext(ReactiveSensorEvent event) {
130-
// handle event
131-
}
132-
});
128+
.subscribe(new Consumer<ReactiveSensorEvent>() {
129+
@Override public void accept(ReactiveSensorEvent reactiveSensorEvent) throws Exception {
130+
// handle reactiveSensorEvent
131+
}
132+
}, new Consumer<Throwable>() {
133+
@Override public void accept(Throwable throwable) throws Exception {
134+
if (throwable instanceof SensorNotFoundException) {
135+
textViewForMessage.setText("Sorry, your device doesn't have required sensor.");
136+
}
137+
}
138+
});
133139
```
134140

135-
### Subscribing and unsubscribing observables
141+
### Subscribing and disposing flowables
136142

137-
When we are using subscriptions in Activity, we should subscribe them in `onResume()` method and unsubscribe them in `onPause()` method.
143+
When we are using Disposables in Activity, we should subscribe them in `onResume()` method and dispose them in `onPause()` method.
138144

139145
### Filtering stream
140146

@@ -156,16 +162,16 @@ You can depend on the library through Maven:
156162
```xml
157163
<dependency>
158164
<groupId>com.github.pwittchen</groupId>
159-
<artifactId>reactivesensors</artifactId>
160-
<version>0.1.2</version>
165+
<artifactId>reactivesensors-rx2</artifactId>
166+
<version>...</version>
161167
</dependency>
162168
```
163169

164170
or through Gradle:
165171

166172
```groovy
167173
dependencies {
168-
compile 'com.github.pwittchen:reactivesensors:0.1.2'
174+
compile 'com.github.pwittchen:reactivesensors-rx2:...'
169175
}
170176
```
171177

app-kotlin/src/main/kotlin/com/github/pwittchen/reactivesensors/kotlinapp/SensorActivity.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import android.os.Bundle
44
import android.support.v7.app.AppCompatActivity
55
import android.widget.TextView
66
import com.github.pwittchen.reactivesensors.library.ReactiveSensors
7-
import rx.Subscription
7+
import io.reactivex.disposables.Disposable
88

99
abstract class SensorActivity : AppCompatActivity() {
10-
private var subscription: Subscription? = null
10+
private var subscription: Disposable? = null
1111
private var sensorHelper: SensorHelper? = null
1212
protected var sensorType: Int = 0
1313
protected var sensorName: String = ""
@@ -32,6 +32,6 @@ abstract class SensorActivity : AppCompatActivity() {
3232
return
3333
}
3434

35-
sensorHelper!!.safelyUnsubscribe(subscription)
35+
sensorHelper!!.safelyDispose(subscription)
3636
}
3737
}

app-kotlin/src/main/kotlin/com/github/pwittchen/reactivesensors/kotlinapp/SensorHelper.kt

+29-10
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,36 @@ import com.github.pwittchen.reactivesensors.library.ReactiveSensorEvent
55
import com.github.pwittchen.reactivesensors.library.ReactiveSensorFilter
66
import com.github.pwittchen.reactivesensors.library.ReactiveSensors
77
import com.github.pwittchen.reactivesensors.library.SensorNotFoundException
8-
import rx.Subscriber
9-
import rx.Subscription
10-
import rx.android.schedulers.AndroidSchedulers
11-
import rx.schedulers.Schedulers
8+
import io.reactivex.android.schedulers.AndroidSchedulers
9+
import io.reactivex.disposables.Disposable
10+
import io.reactivex.functions.Consumer
11+
import io.reactivex.schedulers.Schedulers
12+
import org.reactivestreams.Subscriber
1213

1314
class SensorHelper(private val reactiveSensors: ReactiveSensors, private val sensorType: Int,
1415
private val sensorName: String, private val textView: TextView) {
1516

16-
fun createSubscription(): Subscription {
17+
fun createSubscription(): Disposable {
1718
return reactiveSensors.observeSensor(sensorType)
1819
.subscribeOn(Schedulers.computation())
1920
.filter(ReactiveSensorFilter.filterSensorChanged())
2021
.observeOn(AndroidSchedulers.mainThread())
21-
.subscribe(object : Subscriber<ReactiveSensorEvent>() {
22+
.subscribe({ reactiveSensorEvent ->
23+
run {
24+
if (reactiveSensorEvent != null) {
25+
val event = reactiveSensorEvent.sensorEvent
26+
val x = event.values[0]
27+
val y = event.values[1]
28+
val z = event.values[2]
29+
val message = "%s readings:\n x = %f\n y = %f\n z = %f"
30+
textView.text = message.format(sensorName, x, y, z)
31+
}
32+
}
33+
})
34+
35+
/*
36+
37+
(object : Subscriber<ReactiveSensorEvent>() {
2238
override fun onCompleted() {
2339
}
2440
@@ -38,16 +54,19 @@ class SensorHelper(private val reactiveSensors: ReactiveSensors, private val sen
3854
textView.text = message.format(sensorName, x, y, z)
3955
}
4056
}
41-
})
57+
}
58+
59+
*/
60+
4261
}
4362

44-
fun safelyUnsubscribe(subscription: Subscription?) {
63+
fun safelyDispose(subscription: Disposable?) {
4564
if (!reactiveSensors.hasSensor(sensorType)) {
4665
return
4766
}
4867

49-
if (subscription != null && !subscription.isUnsubscribed) {
50-
subscription.unsubscribe()
68+
if (subscription != null && !subscription.isDisposed) {
69+
subscription.dispose()
5170
}
5271
}
5372
}

app/src/main/java/com/github/pwittchen/reactivesensors/app/SensorActivity.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
import android.widget.TextView;
66
import com.github.pwittchen.reactivesensors.R;
77
import com.github.pwittchen.reactivesensors.library.ReactiveSensors;
8-
import rx.Subscription;
8+
import io.reactivex.disposables.Disposable;
99

1010
public abstract class SensorActivity extends AppCompatActivity {
1111
protected int sensorType;
1212
protected String sensorName;
1313

1414
private TextView tvSensor;
1515
private ReactiveSensors reactiveSensors;
16-
private Subscription subscription;
16+
private Disposable subscription;
1717
private SensorHelper sensorHelper;
1818

1919
@Override protected void onCreate(Bundle savedInstanceState) {
@@ -31,6 +31,6 @@ public abstract class SensorActivity extends AppCompatActivity {
3131

3232
@Override protected void onPause() {
3333
super.onPause();
34-
sensorHelper.safelyUnsubscribe(subscription);
34+
sensorHelper.safelyDispose(subscription);
3535
}
3636
}

app/src/main/java/com/github/pwittchen/reactivesensors/app/SensorHelper.java

+22-27
Original file line numberDiff line numberDiff line change
@@ -6,63 +6,58 @@
66
import com.github.pwittchen.reactivesensors.library.ReactiveSensorFilter;
77
import com.github.pwittchen.reactivesensors.library.ReactiveSensors;
88
import com.github.pwittchen.reactivesensors.library.SensorNotFoundException;
9-
import rx.Subscriber;
10-
import rx.Subscription;
11-
import rx.android.schedulers.AndroidSchedulers;
12-
import rx.functions.Action1;
13-
import rx.schedulers.Schedulers;
9+
import io.reactivex.android.schedulers.AndroidSchedulers;
10+
import io.reactivex.disposables.Disposable;
11+
import io.reactivex.functions.Consumer;
12+
import io.reactivex.schedulers.Schedulers;
13+
import java.util.Locale;
1414

15-
public class SensorHelper {
15+
class SensorHelper {
1616
private ReactiveSensors reactiveSensors;
1717
private int sensorType;
1818
private String sensorName;
1919
private TextView textViewForMessage;
2020

21-
public SensorHelper(ReactiveSensors sensors, int type, String name, TextView textViewForMessage) {
21+
SensorHelper(ReactiveSensors sensors, int type, String name, TextView textViewForMessage) {
2222
this.reactiveSensors = sensors;
2323
this.sensorType = type;
2424
this.sensorName = name;
2525
this.textViewForMessage = textViewForMessage;
2626
}
2727

28-
public Subscription createSubscription() {
29-
Subscription subscription = reactiveSensors.observeSensor(sensorType)
28+
Disposable createSubscription() {
29+
return reactiveSensors.observeSensor(sensorType)
3030
.subscribeOn(Schedulers.computation())
31-
.filter(ReactiveSensorFilter.filterSensorChanged())
3231
.observeOn(AndroidSchedulers.mainThread())
33-
.subscribe(new Subscriber<ReactiveSensorEvent>() {
34-
@Override public void onCompleted() {
35-
}
36-
37-
@Override public void onError(Throwable throwable) {
38-
if (throwable instanceof SensorNotFoundException) {
39-
textViewForMessage.setText("Sorry, your device doesn't have required sensor.");
40-
}
41-
}
42-
43-
@Override public void onNext(ReactiveSensorEvent reactiveSensorEvent) {
32+
.filter(ReactiveSensorFilter.filterSensorChanged())
33+
.subscribe(new Consumer<ReactiveSensorEvent>() {
34+
@Override public void accept(ReactiveSensorEvent reactiveSensorEvent) throws Exception {
4435
SensorEvent event = reactiveSensorEvent.getSensorEvent();
4536

4637
float x = event.values[0];
4738
float y = event.values[1];
4839
float z = event.values[2];
4940

5041
String format = "%s readings:\n x = %f\n y = %f\n z = %f";
51-
String message = String.format(format, sensorName, x, y, z);
42+
String message = String.format(Locale.getDefault(), format, sensorName, x, y, z);
5243
textViewForMessage.setText(message);
5344
}
45+
}, new Consumer<Throwable>() {
46+
@Override public void accept(Throwable throwable) throws Exception {
47+
if (throwable instanceof SensorNotFoundException) {
48+
textViewForMessage.setText("Sorry, your device doesn't have required sensor.");
49+
}
50+
}
5451
});
55-
56-
return subscription;
5752
}
5853

59-
public void safelyUnsubscribe(Subscription subscription) {
54+
void safelyDispose(Disposable disposable) {
6055
if (!reactiveSensors.hasSensor(sensorType)) {
6156
return;
6257
}
6358

64-
if (subscription != null && !subscription.isUnsubscribed()) {
65-
subscription.unsubscribe();
59+
if (disposable != null && !disposable.isDisposed()) {
60+
disposable.dispose();
6661
}
6762
}
6863
}

build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ ext {
88
kotlinVersion = '1.1.51'
99
}
1010

11-
ext.deps = [rxjava : 'io.reactivex:rxjava:1.3.3',
12-
rxandroid : 'io.reactivex:rxandroid:1.2.1',
11+
ext.deps = [rxjava2 : 'io.reactivex.rxjava2:rxjava:2.1.6',
12+
rxandroid2 : 'io.reactivex.rxjava2:rxandroid:2.0.1',
1313
supportannotations: 'com.android.support:support-annotations:25.3.0',
1414
appcompatv7 : 'com.android.support:appcompat-v7:27.0.0',
1515
testingsupportlib : 'com.android.support.test:testing-support-lib:0.1',

library/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ android {
2828
}
2929

3030
dependencies {
31-
api deps.rxjava
32-
api deps.rxandroid
31+
api deps.rxjava2
32+
api deps.rxandroid2
3333

3434
androidTestImplementation deps.testingsupportlib
3535
androidTestImplementation(deps.truth) {

0 commit comments

Comments
 (0)