Skip to content

Commit

Permalink
Merge pull request #25 from Yarikx/shared-preferences-observable
Browse files Browse the repository at this point in the history
Add fromSharedPreferencesChanges Observable
  • Loading branch information
mttkay committed Oct 15, 2014
2 parents c174620 + 6dde4b2 commit 2bb5640
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/java/rx/android/observables/AndroidObservable.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static rx.android.schedulers.AndroidSchedulers.mainThread;

import android.content.SharedPreferences;
import rx.Observable;
import rx.functions.Func1;
import rx.android.operators.OperatorBroadcastRegister;
Expand All @@ -30,6 +31,7 @@
import android.content.IntentFilter;
import android.os.Build;
import android.os.Handler;
import rx.operators.OperatorSharedPreferenceChange;


public final class AndroidObservable {
Expand Down Expand Up @@ -150,4 +152,13 @@ public static Observable<Intent> fromBroadcast(Context context, IntentFilter fil
public static Observable<Intent> fromLocalBroadcast(Context context, IntentFilter filter){
return Observable.create(new OperatorLocalBroadcastRegister(context, filter));
}

/**
* Create Observable that emits String keys whenever it changes in provided SharedPreferences
*
* Items will be observed on the main Android UI thread
*/
public static Observable<String> fromSharedPreferencesChanges(SharedPreferences sharedPreferences){
return Observable.create(new OperatorSharedPreferenceChange(sharedPreferences));
}
}
50 changes: 50 additions & 0 deletions src/main/java/rx/operators/OperatorSharedPreferenceChange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.operators;

import android.content.SharedPreferences;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Action0;
import rx.subscriptions.Subscriptions;

public class OperatorSharedPreferenceChange implements Observable.OnSubscribe<String>{

private final SharedPreferences sharedPreferences;

public OperatorSharedPreferenceChange(SharedPreferences sharedPreferences) {
this.sharedPreferences = sharedPreferences;
}

@Override
public void call(final Subscriber<? super String> subscriber) {
final SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
subscriber.onNext(key);
}
};

subscriber.add(Subscriptions.create(new Action0() {
@Override
public void call() {
sharedPreferences.unregisterOnSharedPreferenceChangeListener(listener);
}
}));

sharedPreferences.registerOnSharedPreferenceChangeListener(listener);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.android.operators;

import android.app.Application;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.android.observables.AndroidObservable;
import rx.observers.TestObserver;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;

@RunWith(RobolectricTestRunner.class)
public class OperatorSharedPreferencesChangeTest {

@Test
public void testSharedPreferences() {
Application application = Robolectric.application;
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(application);
Observable<String> observable = AndroidObservable.fromSharedPreferencesChanges(sharedPreferences);
final Observer<String> observer = mock(Observer.class);
final Subscription subscription = observable.subscribe(new TestObserver<String>(observer));

final InOrder inOrder = inOrder(observer);

inOrder.verify(observer, never()).onNext(any(String.class));

sharedPreferences.edit().putBoolean("a", true).commit();
inOrder.verify(observer, times(1)).onNext("a");

sharedPreferences.edit().putInt("b", 9).commit();
inOrder.verify(observer, times(1)).onNext("b");

subscription.unsubscribe();

sharedPreferences.edit().putInt("c", 42).commit();
inOrder.verify(observer, never()).onNext(any(String.class));
inOrder.verify(observer, never()).onError(any(Throwable.class));
inOrder.verify(observer, never()).onCompleted();
}

}

0 comments on commit 2bb5640

Please sign in to comment.