ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Observe SharedPreferences changes using RxJava

SharedPreferences using RxJava and Kotlin

Hope you have gone through my previous articles RecyclerView item click using RxJava and Auto ViewPager slider using RxJava.

Today I’m back with another article about how you can create a RxJava wrapper over SharedPreferences to observe the changes in your SharedPreference values. I won’t be using any architecture for this sample application as that is out of the scope of this article.

If you just want to have a quick peek at the code and don’t want to read this article then jump over to the same project here. Let's get started…

The sample application looks something like this:

The end result

Now let's jump into the code.

First, let’s look into the NameRepository where all the magic is happening.

This is just a contract interface which SharedPreferencesNameRepository will implement. Now let’s see what is happening in SharedPreferencesNameRepository.

I know its too much to understand in one glance so I’ll try to break it into smaller parts.

Firstly we are creating a static create(context: Context) method which helps in the creation of SharedPreferencesNameRepository. Inside create we are creating the SharedPreferences and passing it over to the constructor of SharedPreferencesNameRepository.

Then I’m creating a BehaviorSubject with the default SharedPreferences we have got from the constructor.

After that, I create a SharedPreferences.OnSharedPreferenceChangeListener which gives me the updated SharedPreferences whenever the value in SharedPreferences changes.

And then I set the PreferenceChangeListener to the default SharedPreferences which was passed in the constructor in the init block.

So now we have completed the setup of SharedPreferencesNameRepository and now we will implement the methods from NameRepository.

The first one is saveName() which takes in a String and returns a Completable. Then we convert our Subject(prefSubject) to a Single using firstOrError() and then I’ve created a Kotlin extension function over it to edit the shared preference value and save it.

Then we have the name() which returns an Observable and whenever the SharedPreference value changes our Subject(prefSubject) emits the updated SharedPreferences and we get the latest saved name from this method.

Then we have clear() at the last. This method like saveName() calls the extension function on the prefSubject and removes the name from SharedPreferences.

That's it for Repository. The main stuff is in the initialization of the repository rest changes as per your requirements.

Now, let's discuss how we are handling the data in MainActivity. This is a simple activity class which takes the view references and set listeners on the views.

The first thing we do is create a reference for NameRepository.

We are observing the fields in handleData() when the Submit button is clicked. Firstly we are getting the text from the EditText and then calling the repository.saveName() with the text and then subscribing to it as its a Completable.

Then we are observing over the saved text by subscribing to repository.name() which will emit a value whenever the value in shared preferences changes.

At last, when the Clear button is clicked we are removing the saved text from SharedPreferences.

If anyone would like to help me to convert this into a library then drop me a message at shivamdev31@gmail.com

So that’s it for this article, find the code on github: RxPrefs.

Constructive feedback is welcomed in the comments.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Written by Shivam Chopra

Android Developer | Self-taught Programmer | Open Source Contributor | Freelancer

Responses (1)

Write a response