Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion rxandroid/src/main/java/rx/android/view/ViewActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package rx.android.view;

import android.view.View;

import android.widget.TextView;
import rx.functions.Action1;

/**
Expand Down Expand Up @@ -112,4 +112,28 @@ public static Action1<? super Boolean> setVisibility(View view) {
public static Action1<? super Boolean> setVisibility(View view, int visibilityOnFalse) {
return new ViewActionSetVisibility(view, visibilityOnFalse);
}

/**
* Set the text of a {@link TextView} based on values emitted by an Observable.
*/

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that you didn't add @param and @returns which are on every other method in this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dalewking I tend to avoid them when they add nothing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#78

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine with me, just noticed the inconsistency with the rest of the file.

public static Action1<? super CharSequence> setText(final TextView textView) {
return new Action1<CharSequence>() {
@Override
public void call(CharSequence charSequence) {
textView.setText(charSequence);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realized that your implementations are not as robust as the other ViewActions. They all return an instance of ViewAction1 which keeps a weak reference to the view and handles when it is null. The implementation should be like this:

    return new ViewAction1<TextView, CharSequence>(view)
    {
        @Override
        public void call(TextView view, CharSequence text) {
            view.setText(text);
        }
    };

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh. Yeah we should match though I'm not a fan of the whole weak-reference-all-the-things movement.

Also, all these others are not only using explicit top-level types for their actions but they're public... where's the facepalm emoji?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bah I'll fix it to be consistent. Nice catch.

On Tue, Nov 25, 2014, 17:29 Jake Wharton notifications@github.com wrote:

In rxandroid/src/main/java/rx/android/view/ViewActions.java:

@@ -112,4 +112,28 @@ private ViewActions() {
public static Action1<? super Boolean> setVisibility(View view, int visibilityOnFalse) {
return new ViewActionSetVisibility(view, visibilityOnFalse);
}
+

  • /**
  • \* Set the text of a {@link TextView} based on values emitted by an Observable.
    
  • */
    
  • public static Action1<? super CharSequence> setText(final TextView textView) {
  •    return new Action1<CharSequence>() {
    
  •        @Override
    
  •        public void call(CharSequence charSequence) {
    
  •            textView.setText(charSequence);
    

Ugh. Yeah we should match though I'm not a fan of the whole
weak-reference-all-the-things movement.

Also, all these others are not only using explicit top-level types for
their actions but they're public... where's the facepalm emoji?


Reply to this email directly or view it on GitHub
https://github.com/ReactiveX/RxAndroid/pull/85/files#r20902075.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#86 opened

}
};
}

/**
* Set the text of a {@link TextView} based on values emitted by an Observable.
*/
public static Action1<? super Integer> setTextResource(final TextView textView) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this is a return type not a parameter type. Explains all the comments on the issue lol.

return new Action1<Integer>() {
@Override
public void call(Integer integer) {
textView.setText(integer);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setTextResource

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That does not exist in TextView

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, so it doesn't. Was thinking of ImageView. Just use textView.setText(textView.getResources().getString(integer)) then.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, that makes me question the utility of this helper.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setText(int resId) does that exactly. I'd rather not reimplement code I do not have to reimplement

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sweet. I knew I knew what I was talking about, just didn't realize they overloaded in this case. 👍 Android sucks at naming.

}
};
}
}
46 changes: 46 additions & 0 deletions rxandroid/src/test/java/rx/android/view/ViewActionSetTextTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package rx.android.view;

import android.widget.TextView;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.robolectric.RobolectricTestRunner;
import rx.subjects.PublishSubject;

import static org.mockito.Mockito.verify;

@RunWith(RobolectricTestRunner.class)
public class ViewActionSetTextTest {

private TextView textView;

@Before
public void setUp() {
textView = Mockito.mock(TextView.class);
}

@Test
public void testSetsTextViewCharSequence() {
final PublishSubject<String> subject = PublishSubject.create();
subject.subscribe(ViewActions.setText(textView));

subject.onNext("Hello");
verify(textView).setText("Hello");

subject.onNext("World");
verify(textView).setText("World");
}

@Test
public void testSetsTextViewTextResource() {
final PublishSubject<Integer> subject = PublishSubject.create();
subject.subscribe(ViewActions.setTextResource(textView));

subject.onNext(1);
verify(textView).setText(1);

subject.onNext(3);
verify(textView).setText(3);
}
}