RxJava

ReactiveNetwork – new releases & roadmap

2016-06-10 Android, Java, Open source, RxJava No comments

Recent updates

I’ve recently released version 0.3.0 of ReactiveNetwork library for Android. As you can see in the release notes, it contains a lot of updates.
Highlights:

  • Deprecated methods related to monitoring WiFi Access Points and WiFi Singal level in favor of ReactiveWiFi project, which has this functionality extracted from ReactiveNetwork
  • Deprecated methods and enums related to monitoring connectivity with The Internet
  • Added Observables, which allows monitoring Internet connectivity basing on socket connection with a remote host (we can also monitor specific host with given parameters)

Roadmap for the future versions

Updates planned for 0.4.0:

  • Removing deprecated methods
  • Removing unused permissions from AndroidManifest.xml

Track progress of releasing 0.4.0 on GitHub

Updates planned for 0.5.0:

  • Updating library code with respect to the updates in Android 5 and 6 (especially ConnectivityManager) related to monitoring network connectivity mentioned in issue #62 on GitHub
  • Creating strategy interface to keep backward compatibility with Pre-Lollipop devices, so we’ll be able to monitor network in a different way depending on the given Android version

Track progress of releasing 0.5.0 on GitHub

Another future updates (not related to any version):
These updates are also important, but they’re not related to library API

  • Adding example of library usage with Retrofit (without breaking reactive stream chain)

Track progress of resolving this issue on GitHub

Final thoughts

Currently, it’s my most popular open-source project and people rely on it in production apps, so I’m trying to keep it as simple and as good as I can with respect to the recent updates of Android SDK. I’m getting really good feedback from people on GitHub and seriously considering it during the development process. If something bothers you in that project, don’t hesitate to create an Issue or new Pull Request on GitHub.

Handle swipe events in reactive style

2016-03-11 Android, Java, Open source, RxJava No comments

Introduction

Initial Swipe Detector project was created on 24th of December 2014 on GitHub, so more than one year ago. I also wrote an article about that on my blog. I needed that to detect moment when user is swiping horizontally, to block vertical scroll on the ListView while deleting item with “swipe to delete” functionality. This gives better UX and apps from Google works in the same way. Mentioned project was just a simple proof of concept showing how can we detect whether user is swiping on the screen of mobile device and in which direction. Recently, I thought that it would be cool if we had a separate generic library to do the same thing. It would be even cooler if this library will provide RxJava Observable to handle such events in a reactive way. That’s why I’ve decided to take this project, extract generic code to library and write an Observable wrapping listener, which handles swipe events. Please, take a look at the outcome of that work.

swipe

Preparation

New library is called swipe. If we want to use it, we need to add proper dependencies to our build.gradle file:

dependencies {
  compile 'com.github.pwittchen:swipe:0.0.1'
}

Next, we need to create Swipe attribute in our Activity:

private Swipe swipe;

We also need to remember to call dispatchTouchEvent(MotionEvent) method:

We need to perform things described above both for listener and RxJava Observable.

Imperative way

If we are not familiar with RxJava, we can handle swipe events in an imperative way with listener:

Reactive way

If we want to use the power of RxJava and code our app in a reactive style, we need to add Subscription attribute.

private Subscription subscription;

Now, we can subscribe an Observable:

Please note, there’s much less code than in the case of listener.

We need to remember to unsubscribe our Observable, when it’s no longer needed:

That’s it! I hope it will help you to detect and handle swipe events in your apps.

Source code of the library is available on GitHub: https://github.com/pwittchen/swipe.

ReactiveNetwork – release of v. 0.2.0

2016-02-11 Android, Java, Open source, RxJava No comments

I’ve recently released new version of ReactiveNetwork library for Android.

Here are the fresh release notes for version 0.2.0:

  • added possibility to observe WiFi signal level with observeWifiSignalLevel(context, numLevels) and observeWifiSignalLevel(context) method
  • created WifiSignalLevel enum
  • added internet check to parameters of getConnectivityStatus(context, checkInternet) method
  • made getConnectivityStatus(context, checkInternet) method public
  • changed String variable status in ConnectivityStatus enum to description and made it public
  • changed output of the toString() method in ConnectivityStatus to keep consistency with another enum
  • made ReactiveNetwork class non-final
  • bumped Kotlin version in sample app to 1.0.0-rc-1036
  • increased immutability of code of the library
  • updated sample apps and documentation

Thanks to @llp and his Pull Request, we are able to observe WiFi signal level AKA RSSI now! It’s one of the most interesting features in the newest release.

We can do it as follows:

or we can observe an enum value instead of integer:

WifiSignalLevel enum can have the following values:

public enum WifiSignalLevel {
  NO_SIGNAL(0, "no signal"),
  POOR(1, "poor"),
  FAIR(2, "fair"),
  GOOD(3, "good"),
  EXCELLENT(4, "excellent");
  ...
}

Any feedback will be appreciated!

Happy coding!

Reactive Live Coding during GDG DevFest 2015 in Poland

2015-11-29 Android, Conferences, Google, Java, RxJava 2 comments

I was asked to be a speaker during GDG DevFest 2015 conference in Warsaw, Poland. Of course, I accepted this invitation and prepared presentation titled “Reactive Live Coding”. Presentation covered basics of Reactive Programming, RxJava and RxAndroid. Besides my talk I’ve done live coding to show how to use mentioned libraries and basics principles of Reactive Programming in real life. I had only 20 minutes for all of that, so my time-box was very limited. Being a speaker at conference was really interesting and challenging experience, which I haven’t had before. Moreover, I could meet a lot of interesting people and hear very inspiring talks covering different topics. You can check activity from conference by browsing #devfest15pl hashtag on Twitter and Facebook event. You can also check official website of the conference at devfest.pl.

Slides from my presentation are available below.

Source code of the exemplary Android app shown and partially coded during the presentation is available at github.com/pwittchen/guitar-browser.

Picture documenting part of my short talk made by @depodefi can be found below.

CU59bM6W4AABP68

GDG DevFest organizers have done really good job and I can definitely recommend this conference to anyone interested in new IT technologies.

Introducing ReactiveSensors

2015-09-05 Android, Java, Open source, RxJava No comments

Another month, another library. Recently, I’ve released yet another reactive library called ReactiveSensors. It’s an open-source Android library monitoring hardware sensors with RxJava Observables. Library is compatible with RxJava 1.0.+ and RxAndroid 1.0.+ and uses them under the hood.

Library is available at: https://github.com/pwittchen/ReactiveSensors.

In my opinion, hardware sensors are perfect case for applying RxJava, because in fact we’re constantly receiving a stream of events emitted by many sensors. With Reactive Programming approach we have plenty of possibilities and easy API for manipulating received sensor’s data.

Usage of the library is really simple. You just need to subscribe an Observable with RxJava in the same way like in any other reactive library.

Code sample below demonstrates how to observe Gyroscope sensor:

Please note that we are filtering events occuring when sensors reading change with ReactiveSensorEvent.filterSensorChanged() method. There’s also event describing change of sensor’s accuracy, which can be filtered with ReactiveSensorEvent.filterAccuracyChanged() method. When we don’t apply any filter, we will be notified both about sensor readings and accuracy changes.

We can observe any hardware sensor in the same way. You can check list of all sensors in official Android documentation.

I’ve created section about Good Practices regarding working with hardware sensors on Android in README.md file in the GitHub repository.You should also read an article about Best Practices for Accessing and Using Sensors in official Android documentation.

Read more in the README.md file located in the repository of the library at: https://github.com/pwittchen/ReactiveSensors.

You can also find JavaDoc at: http://pwittchen.github.io/ReactiveSensors/.

If you want to use ReactiveSensors in your project, add the following dependency to your build.gradle file:

dependencies {
  compile 'com.github.pwittchen:reactivesensors:0.0.1'
}

Feel free to send me feedback, report an issue or fork the library!

Introducing ReactiveNetwork

2015-08-10 Android, Open source, RxJava No comments

I’ve recently released ReactiveNetwork. It is an open-source Android library listening network connection state and change of the WiFi signal strength with RxJava Observables. It’s a successor of Network Events library rewritten with Reactive Programming approach.

Library is compatible with RxJava 1.0.+ and RxAndroid 1.0.+ and uses them under the hood. Min Android SDK version is 9.
JavaDoc can be found at: http://pwittchen.github.io/ReactiveNetwork.
Repository is available at: https://github.com/pwittchen/ReactiveNetwork.

This library is much simpler and easier to use than NetworkEvents. Even code-base is much smaller, but we have to remember that it utilizes powerful RxJava and RxAndroid. RxJava hase huge API and gives a lot of possibilities. That’s why I was able to obtain desired result with fewer lines of code.

Basic library usage is quite simple. E.g if we want to monitor ConnectivityStatus (WIFI_CONNECTED, MOBILE_CONNECTED or OFFLINE), we can create the following subscription, which is quite familiar for software developers who already know RxJava:

When we want to monitor available WiFi Access Points and we want to get fresh list of them whenever strength of the WiFi Access Points signal changes (e.g. when we are moving with a mobile device around), we can use the following code snippet:

If you want to use ReactiveNetwork in your project, add the following dependency to your build.gradle file:

dependencies {
  compile 'com.github.pwittchen:reactivenetwork:0.0.2'
}

Find more in the GitHub repository of the project at: https://github.com/pwittchen/ReactiveNetwork.

It’s worth mentioning that this library was featured on Android Arsenal, Android Weekly and Android Weekly China websites.

I hope you will find it useful and you will make your apps more reactive!
Feel free to fork the library. Any feedback is welcome as usual.

Introducing Prefser

2015-02-22 Android, RxJava No comments

I’ve recently released Prefser, which is a wrapper for Android SharedPreferences with object serialization and RxJava Observables.

Prefser wraps SharedPreferences and thanks to Java Generics provides you simpler API than classic SharedPreferences with only two methods:

void put(String key, Object value);
<T> T get(String key, Class classOfT, T defaultValue);

Classic SharedPreferences allows you to store only primitive data types and set of strings.

Thanks to Gson serialization, Prefser allows you to store:

  • Primitive data types (boolean, float, int, long, double)
  • Strings
  • Custom Objects
  • Lists
  • Arrays
  • Sets

In addition, Prefser transforms OnSharedPreferenceChangeListener into Observables from RxJava:

Observable<String> observe(final SharedPreferences sharedPreferences);
Observable<String> observeDefaultPreferences();

You can subscribe one of these Observables and monitor updates of SharedPreferences with powerful RxJava.

Moreover, you can observe single preference under a specified key with the following method:

<T> Observable<T> observe(final String key, final Class classOfT, final T defaultValue)

prefser_observable_diagram

For more details, examples and download instructions visit GitHub webiste of the project at: https://github.com/pwittchen/prefser.
I’ve provided sample app using Prefeser in this repository, which you can check to get familiar with the project.
Library is open source, has Unit Tests, Travis CI job and is available on Maven Central Repository. You can depend on it through Maven or Gradle.