-
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.
Create ViewAction1 to weakly refer to views.
- Loading branch information
1 parent
12e8015
commit 602deb7
Showing
8 changed files
with
146 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* 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.functions; | ||
|
||
import android.view.View; | ||
|
||
import rx.functions.Action1; | ||
|
||
import java.lang.ref.WeakReference; | ||
|
||
/** | ||
* An {@link Action1} implementation specific for {@link View}s. | ||
* | ||
* @param <V> the type of {@link View} upon which to perform the action. | ||
* @param <T> the type being observed | ||
*/ | ||
public abstract class ViewAction1<V extends View, T> implements Action1<T> { | ||
|
||
private final WeakReference<V> viewReference; | ||
|
||
public ViewAction1(V view) { | ||
viewReference = new WeakReference<V>(view); | ||
} | ||
|
||
@Override | ||
public final void call(T t) { | ||
V view = viewReference.get(); | ||
if (view != null) { | ||
call(view, t); | ||
} | ||
} | ||
|
||
/** | ||
* Implement this instead of {@link Action1#call(Object)}. | ||
* @param view the view given in the constructor. | ||
* @param t the object being observed | ||
*/ | ||
public abstract void call(V view, T t); | ||
|
||
} |
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
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
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
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
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
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
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,75 @@ | ||
/** | ||
* 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.functions; | ||
|
||
import android.view.View; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.Robolectric; | ||
import org.robolectric.RobolectricTestRunner; | ||
|
||
import rx.subjects.PublishSubject; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static rx.android.TestUtil.createView; | ||
|
||
@RunWith(RobolectricTestRunner.class) | ||
public class ViewAction1Test { | ||
|
||
@Test | ||
@SuppressWarnings("unchecked") | ||
public void callIsNotExecutedWithAReleasedReference() { | ||
final View view = null; // simulate a released WeakReference | ||
final PublishSubject<Boolean> subject = PublishSubject.create(); | ||
final ViewAction1Impl action = new ViewAction1Impl(view); | ||
subject.subscribe(action); | ||
|
||
assertFalse(action.wasCalled); | ||
subject.onNext(true); | ||
assertFalse(action.wasCalled); | ||
} | ||
|
||
@Test | ||
@SuppressWarnings("unchecked") | ||
public void callIsExecutedWithARetainedReference() { | ||
final View view = createView(); | ||
final PublishSubject<Boolean> subject = PublishSubject.create(); | ||
final ViewAction1Impl action = new ViewAction1Impl(view); | ||
subject.subscribe(action); | ||
|
||
assertFalse(action.wasCalled); | ||
subject.onNext(true); | ||
assertTrue(action.wasCalled); | ||
} | ||
|
||
private static class ViewAction1Impl extends ViewAction1<View, Boolean> { | ||
|
||
boolean wasCalled = false; | ||
|
||
ViewAction1Impl(View view) { | ||
super(view); | ||
} | ||
|
||
@Override | ||
public void call(View view, Boolean aBoolean) { | ||
wasCalled = true; | ||
} | ||
|
||
} | ||
|
||
} |