使用MVP架构在运行时检查权限的最佳方法是什么? [英] What's the best way to check for permissions at runtime using MVP architecture?

查看:137
本文介绍了使用MVP架构在运行时检查权限的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android应用程序,该应用程序必须在运行时请求权限.我想知道使用Model-View-Presenter架构实现此目标的最佳方法.

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.

我最初的想法是让演示者调用负责权限的组件(例如PermissionHandler),并相应地更新视图.

My initial thought was to have the presenter call a component responsible for permissions(say a PermissionHandler), and update view accordingly.

问题在于,用于检查权限的代码与Activity类紧密相关.这里涉及一些需要活动或上下文的方法:

The issue is that the code to check for permissions is tightly coupled with the Activity class. Here are some of the methods involved that require an Activity or Context:

  • ContextCompat.checkSelfPermission()
  • ActivityCompat.shouldShowRequestPermissionRationale()
  • ActivityCompat.requestPermissions()
  • onRequestPermissionsResult()(回调)
  • ContextCompat.checkSelfPermission()
  • ActivityCompat.shouldShowRequestPermissionRationale()
  • ActivityCompat.requestPermissions()
  • onRequestPermissionsResult()(callback)

这意味着我必须将一个活动对象传递给演示者,我并不太喜欢它,因为我听说让演示者脱离Android代码很容易进行测试.

This means I would have to pass an activity object to the presenter, which I didn't like much because I've heard that keeping your presenter free from Android code is good for testing.

由于此,我然后考虑了在视图级别(在活动中)处理权限,但是我想这将损害使视图仅负责UI更新而没有业务逻辑的目的.

Due to that, I then thought about handling permissions at view level(in an activity), but then I guess this would hurt the purpose of leaving the view responsible only for UI updates, without business logic.

我不确定什么是解决使代码尽可能分离和可维护的最佳方法.有什么想法吗?

I'm not sure what would be the best approach to tackle that keeping the code as decoupled and maintainable as possible. Any ideas?

推荐答案

我要做的是:

该视图将实现:

public Activity getViewActivity();

演示者将实现:

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

requestPermissions内部,演示者将执行以下操作:getViewActivity().checkSelfPermission; getViewActivity.requestPermissions(); etc.

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

该视图将在onRequestPermissionsResult回调内调用presenter.onPermissionsResult();

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).

我听说让您的演示者脱离Android代码很适合进行测试."我不明白这部分.如果代码良好,则可以毫无问题地对其进行测试.

"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.

这篇关于使用MVP架构在运行时检查权限的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆