sockeqwe / mosby

A Model-View-Presenter / Model-View-Intent library for modern Android apps
http://hannesdorfmann.com/mosby/
Apache License 2.0
5.49k stars 841 forks source link

Access to view from presenter #251

Closed pishguy closed 7 years ago

pishguy commented 7 years ago

Hi there, I would like to access to view widgets such as TextView or EditText from presenter, how can i do that,

in my activity main i want to change scheduleDateTimeNotify text from presenter

public class ActivityChannelContent extends MvpActivity<ActivityChannelContentView, ActivityChannelContentPresenter>
        implements ActivityChannelContentView{

    @BindView(R.id.scheduleDateTimeNotify)
    TextView scheduleDateTimeNotify;

    @NonNull
    @Override
    public ActivityChannelContentPresenter createPresenter() {
        return new ActivityChannelContentPresenterImpl(getApplicationContext());
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_channel_content);
        ButterKnife.bind(this);
    }
}

Presenter:

public interface ActivityChannelContentPresenter extends MvpPresenter<ActivityChannelContentView> {
}

ActivityChannelContentPresenterImpl:

public class ActivityChannelContentPresenterImpl extends MvpBasePresenter<ActivityChannelContentView> implements ActivityChannelContentPresenter {
    private final ActivityChannelContentModel model;

    public ActivityChannelContentPresenterImpl(Context context) {
        model = new ActivityChannelContentModel(context);
    }
}

ActivityChannelContentViewModel:

public class ActivityChannelContentViewModel {
    private final Context context;

    public ActivityChannelContentViewModel(Context mContext) {
        context = mContext;
    }
}

ActivityChannelContentView interface:

public interface ActivityChannelContentView extends MvpView {
}
charbgr commented 7 years ago

You can ask for the instance of the TextView from the ActivityChannelContentView but why do you want something like this?

public interface ActivityChannelContentView extends MvpView {
    TextView getScheduleDateTimeNotify()
}
public class ActivityChannelContent extends MvpActivity<ActivityChannelContentView, ActivityChannelContentPresenter> {
...

@Override
public TextView getScheduleDateTimeNotify() {
    return this.scheduleDateTimeNotify;
}

Why don't you add a function in ActivityChannelContentView like setTextToDateTimeNotify(..) ?

pishguy commented 7 years ago

@charbgr you have right. in my presenter i have more process and i must to access to some widget in the same time

charbgr commented 7 years ago

:) You are welcome! Although, I think those kind of questions should be on stackoverflow.

sockeqwe commented 7 years ago

Thanks @charbgr