What's the best way to check for permissions at runtime using MVP architecture?

后端 未结 3 1402
孤独总比滥情好
孤独总比滥情好 2021-02-01 18:41

I\'m developing an android app in which I have to ask for permissions at runtime. I\'m wondering about the best way to implement that using Model-View-Presenter architecture.

3条回答
  •  日久生厌
    2021-02-01 19:28

    What I would do is:

    The view will implement:

    public Activity getViewActivity();
    

    The presenter will implement:

    public void requestPermissions();
    public void onPermissionsResult();
    

    Inside requestPermissions, the presenter will do: getViewActivity().checkSelfPermission; getViewActivity.requestPermissions(); etc.

    The view will call inside the onRequestPermissionsResult callback to presenter.onPermissionsResult();

    With this all the logic will be implemented inside the presenter.

    In my opinion, your presenter is decoupled: it won't depend on any view implementation (it will only depend on the view interface).

    "I've heard that keeping your presenter free from Android code is good for testing." I don't understand this part. If the code is good, it can be tested without any problem.

提交回复
热议问题