Skip to content

Commit

Permalink
Adding more attributes wrapping NetworkInfo object and builder to the…
Browse files Browse the repository at this point in the history
… Connectivity class and code refactoring (issue #151)
  • Loading branch information
Piotr Wittchen committed Feb 6, 2017
1 parent 8a99391 commit f75b163
Show file tree
Hide file tree
Showing 9 changed files with 382 additions and 197 deletions.
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ ReactiveNetwork.observeNetworkConnectivity(context)

When `Connectivity` changes, subscriber will be notified. `Connectivity` can change its state or type.

We can react on a concrete state, states, type or types changes with the `filter(...)` method from RxJava, `hasState(NetworkInfo.State... states)` and `hasType(int... types)` methods located in `Connectivity` class.
We can react on a concrete state, states, type or types changes with the `filter(...)` method from RxJava, `hasState(NetworkInfo.State... states)` and `hasType(int... types)` methods located in `ConnectivityPredicate` class.

```java
ReactiveNetwork.observeNetworkConnectivity(context)
.subscribeOn(Schedulers.io())
.filter(Connectivity.hasState(NetworkInfo.State.CONNECTED))
.filter(Connectivity.hasType(ConnectivityManager.TYPE_WIFI))
.filter(ConnectivityPredicate.hasState(NetworkInfo.State.CONNECTED))
.filter(ConnectivityPredicate.hasType(ConnectivityManager.TYPE_WIFI))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Connectivity>() {
@Override public void call(Connectivity connectivity) {
Expand All @@ -98,21 +98,22 @@ This method allows you to apply your own network observing strategy and is used
`Connectivity` class is used by `observeNetworkConnectivity(context)` and `observeNetworkConnectivity(context, networkObservingStrategy)` methods. It has the following API:

```java
// factory methods responsible for creating Connectivity object
Connectivity create()
Connectivity create(Context context)
Connectivity create(NetworkInfo.State state, int type, String name)

// methods returning information about connectivity
NetworkInfo.State getState()
NetworkInfo.DetailedState getDetailedState()
int getType()
String getName()
boolean isDefault()
String toString()

// helper methods for filter(...) method from RxJava
Func1<Connectivity, Boolean> hasState(NetworkInfo.State... states)
Func1<Connectivity, Boolean> hasType(int... types)
int getSubType()
boolean isAvailable()
boolean isFailover()
boolean isRoaming()
String getTypeName()
String getSubTypeName()
String getReason()
String getExtraInfo()

class Builder
```

### Observing Internet connectivity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class MainActivity : Activity() {
.subscribe { connectivity ->
Log.d(TAG, connectivity.toString())
val state = connectivity.state
val name = connectivity.name
connectivity_status.text = String.format("state: %s, name: %s", state, name)
val name = connectivity.typeName
connectivity_status.text = String.format("state: %s, typeName: %s", state, name)
}

internetSub = ReactiveNetwork.observeInternetConnectivity()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public class MainActivity extends Activity {
@Override public void call(final Connectivity connectivity) {
Log.d(TAG, connectivity.toString());
final NetworkInfo.State state = connectivity.getState();
final String name = connectivity.getName();
tvConnectivityStatus.setText(String.format("state: %s, name: %s", state, name));
final String name = connectivity.getTypeName();
tvConnectivityStatus.setText(String.format("state: %s, typeName: %s", state, name));
}
});

Expand Down
2 changes: 2 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ coverage:
status:
changes: false
patch: true
default:
threshold: 20%
project:
default:
against: parent
Expand Down
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jacocoAndroidUnitTestReport {
dependencies {
compile 'io.reactivex:rxjava:1.2.3'
compile 'io.reactivex:rxandroid:1.2.1'
compile 'com.android.support:support-annotations:25.1.0'
compile 'com.android.support:support-annotations:25.1.1'

testCompile 'junit:junit:4.10'
testCompile 'com.google.truth:truth:0.28'
Expand Down
Loading

0 comments on commit f75b163

Please sign in to comment.