-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from Yarikx/shared-preferences-observable
Add fromSharedPreferencesChanges Observable
- Loading branch information
Showing
3 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/rx/operators/OperatorSharedPreferenceChange.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/test/java/rx/android/operators/OperatorSharedPreferencesChangeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |