Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In MVP pattern how to access specified view component in Presenter?

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));
like image 244
I.S Avatar asked Nov 27 '25 21:11

I.S


1 Answers

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
        });
like image 138
Maksim Ostrovidov Avatar answered Nov 30 '25 11:11

Maksim Ostrovidov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!