3

SignUpFragment uses SignUpPresenter and SignUpFragment inplements SignUpView. SingUpPresenter extends BasePresenter where BasePresenter:

public abstract class BasePresenter<V> {

private WeakReference<V> mView;

public void bindView(@NonNull V view) {
    mView = new WeakReference<>(view);
    if (setupDone()) {
        updateView();
    }
}

public void unbindView() {
    mView = null;
}

protected V view() {
    if (mView == null) {
        return null;
    } else {
        return mView.get();
    }
}

protected abstract void updateView();

private boolean setupDone() {
    return view() != null;
}
}

public interface SignUpView extends BaseView {
void showResult(UserInfo result);
}

SignUpPresenter connects with SignUpFragment via view() like:

view().showResult()
view().showError()

I want to know if in SignUpPresenter I want to add validation via RxAndroid:

Observable<CharSequence> loginObservable = RxTextView.textChanges(mEmail);

I mean I want to have access to mEmail of SignUpFragment in SignUpPresenter. Is it ok to add method in SignFramgnet method like:

public EditText getEditTextEmail(){return mEmail;}

Which I could use in SignUpPresenter like mEail = view().getEditTextEmail();

Or I need to add all this part in Activity/Fragment:

 Observable<CharSequence> loginObservable = RxTextView.textChanges(mLogin);
 loginObservable
    .map(this::isValidLogin)
    .subscribe(isValid -> mLogin.setCompoundDrawablesRelativeWithIntrinsicBounds(null,null, (isValid ? mValidField : mInvalidField), null));
Maksim Ostrovidov
  • 10,720
  • 8
  • 42
  • 57
I.S
  • 1,904
  • 2
  • 25
  • 48
  • Normally your Presenter should know nothing about Android, so using `RxTextView` inside a Presenter looks out of place. – Egor Dec 19 '16 at 15:12
  • Great! In such case we need to add all this in Framgnet/Activity ? Observable emailObservable = RxTextView.textChanges(mEmail); emailObservable .map(this::isValidEmail) .subscribe(isValid -> mEmail.setCompoundDrawablesRelativeWithIntrinsicBounds(null,null, (isValid ? mValidField : mInvalidField), null)); – I.S Dec 19 '16 at 15:27
  • Yes, this all looks like UI code to me. If you want your Presenter to validate the email, you can just map against `presenter.isValidEmail()`. – Egor Dec 19 '16 at 15:35
  • @Egor the part of Observable confused me, you mean it is ok to put code of Observable in Activity/Fragment ? – I.S Dec 19 '16 at 16:18
  • Definitely, why not? – Egor Dec 19 '16 at 16:21

1 Answers1

3

Create loginObservable in your View and pass it to Presenter. Observable<CharSequence> is not a part of Android Framework so it can be easily unit-tested.

//View
Observable<CharSequence> loginObservable = RxTextView.textChanges(mEmail);
presenter.setLoginObservable(loginObservable);

//Presenter
void setLoginObservable(Observable<CharSequence> observable) {
    observable
        .map(this::isValidLogin) 
        .subscribe(isValid -> {
            //call appropriate view methods
        });
Maksim Ostrovidov
  • 10,720
  • 8
  • 42
  • 57
  • I see in this video https://www.youtube.com/watch?v=QdmkXL7XikQ 2:40 that as we working with immutable streams like loginObservables= RxTextView.textChanges(mEmail) and then loginObservables.map(this::isValidLogin) and RxTextView.textChanges(mEmail).map(this::isValidLogin) are different,like it is not good idea to separate them What do you think? – I.S Dec 21 '16 at 20:50
  • We have to separate in order to pass observable to Presenter. – Maksim Ostrovidov Dec 21 '16 at 23:49