Commit debaa308 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Wrapped nullable String returned to onNext with an Optional to avoid NPE...

Wrapped nullable String returned to onNext with an Optional to avoid NPE eventually happening since RxJava 2.x does not support it
parent 6921a304
...@@ -3,11 +3,13 @@ package chat.rocket.android; ...@@ -3,11 +3,13 @@ package chat.rocket.android;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import io.reactivex.BackpressureStrategy; import com.hadisatrio.optional.Optional;
import io.reactivex.Flowable;
import java.util.UUID; import java.util.UUID;
import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
/** /**
* sharedpreference-based cache. * sharedpreference-based cache.
*/ */
...@@ -51,11 +53,11 @@ public class RocketChatCache { ...@@ -51,11 +53,11 @@ public class RocketChatCache {
return preferences.getString(KEY_PUSH_ID, null); return preferences.getString(KEY_PUSH_ID, null);
} }
public Flowable<String> getSelectedServerHostnamePublisher() { public Flowable<Optional<String>> getSelectedServerHostnamePublisher() {
return getValuePublisher(KEY_SELECTED_SERVER_HOSTNAME); return getValuePublisher(KEY_SELECTED_SERVER_HOSTNAME);
} }
public Flowable<String> getSelectedRoomIdPublisher() { public Flowable<Optional<String>> getSelectedRoomIdPublisher() {
return getValuePublisher(KEY_SELECTED_ROOM_ID); return getValuePublisher(KEY_SELECTED_ROOM_ID);
} }
...@@ -75,12 +77,13 @@ public class RocketChatCache { ...@@ -75,12 +77,13 @@ public class RocketChatCache {
getEditor().putString(key, value).apply(); getEditor().putString(key, value).apply();
} }
private Flowable<String> getValuePublisher(final String key) { private Flowable<Optional<String>> getValuePublisher(final String key) {
return Flowable.create(emitter -> { return Flowable.create(emitter -> {
SharedPreferences.OnSharedPreferenceChangeListener SharedPreferences.OnSharedPreferenceChangeListener
listener = (sharedPreferences, changedKey) -> { listener = (sharedPreferences, changedKey) -> {
if (key.equals(changedKey) && !emitter.isCancelled()) { if (key.equals(changedKey) && !emitter.isCancelled()) {
emitter.onNext(getString(key, null)); String value = getString(key, null);
emitter.onNext(Optional.of(value));
} }
}; };
......
...@@ -4,6 +4,8 @@ import android.content.Intent; ...@@ -4,6 +4,8 @@ import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import com.hadisatrio.optional.Optional;
import java.util.List; import java.util.List;
import chat.rocket.android.LaunchUtil; import chat.rocket.android.LaunchUtil;
...@@ -179,6 +181,7 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity { ...@@ -179,6 +181,7 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
private void subscribeToConfigChanges() { private void subscribeToConfigChanges() {
compositeDisposable.add( compositeDisposable.add(
rocketChatCache.getSelectedServerHostnamePublisher() rocketChatCache.getSelectedServerHostnamePublisher()
.map(Optional::get)
.distinctUntilChanged() .distinctUntilChanged()
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
...@@ -190,6 +193,7 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity { ...@@ -190,6 +193,7 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
compositeDisposable.add( compositeDisposable.add(
rocketChatCache.getSelectedRoomIdPublisher() rocketChatCache.getSelectedRoomIdPublisher()
.map(Optional::get)
.distinctUntilChanged() .distinctUntilChanged()
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
......
...@@ -2,6 +2,8 @@ package chat.rocket.android.service.internal; ...@@ -2,6 +2,8 @@ package chat.rocket.android.service.internal;
import android.content.Context; import android.content.Context;
import com.hadisatrio.optional.Optional;
import chat.rocket.android.log.RCLog; import chat.rocket.android.log.RCLog;
import io.reactivex.disposables.CompositeDisposable; import io.reactivex.disposables.CompositeDisposable;
...@@ -48,6 +50,7 @@ public abstract class AbstractRocketChatCacheObserver implements Registrable { ...@@ -48,6 +50,7 @@ public abstract class AbstractRocketChatCacheObserver implements Registrable {
compositeDisposable.add( compositeDisposable.add(
new RocketChatCache(context) new RocketChatCache(context)
.getSelectedRoomIdPublisher() .getSelectedRoomIdPublisher()
.map(Optional::get)
.subscribe(this::updateRoomIdWith, RCLog::e) .subscribe(this::updateRoomIdWith, RCLog::e)
); );
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment