简体   繁体   English

MVP演示者中的上下文相关问题

[英]Context Related Problems inside MVP presenter

I am new to android MVP pattern and working on my project i have some basic problem related to Android Context in the presenter. 我是Android MVP模式的新手并且在我的项目上工作我在演示者中有一些与Android Context相关的基本问题。 Although there are many answers related to this but i didn't get a perfect one which can solve my problem. 虽然有很多与此相关的答案,但我没有得到一个可以解决我的问题的完美答案。

I have following queries: 我有以下问题:

  1. how to access shared preferences inside presenter. 如何在演示者中访问共享首选项。
  2. how to access other system services inside presenter. 如何访问演示者中的其他系统服务。
  3. if i am working on SQLite Databases then during any transaction in my database which is done by call from presenter to my SQLite Helper class need context to access database. 如果我正在使用SQLite数据库,那么在我的数据库中的任何事务期间,通过从演示者到我的SQLite Helper类的调用来完成访问数据库的上下文。

If i will pass my activity context in presenter then it will a problem during unit testing, also it is a violation according to MVP Format. 如果我将在演示者中传递我的活动上下文,那么在单元测试期间会出现问题,根据MVP格式也是违规。 I need a perfect solution so that my code quality is not degraded. 我需要一个完美的解决方案,以便我的代码质量不会降低。

Note : I dont want to use dagger tool so the answer should be dagger independent 注意 :我不想使用匕首工具,所以答案应该是匕首独立

In MVP you dont use Context or anything else from the Android SDK/Framework in the Presenter (P) layer! 在MVP中,您不要在Presenter (P)层中使用Context或Android SDK / Framework中的任何其他内容! This layer is for anything else than Android related stuff. 这个图层不是Android相关的东西。

1) how to access shared preferences inside presenter. 1)如何在演示者中访问共享首选项。

You don't. 你没有。 If you need a value from a SharedPrefences in the Presenter then you could pass the value to the Presenter via a method call. 如果需要Presenter SharedPrefences的值,则可以通过方法调用将值传递给Presenter

Example: 例:

class MainActivity{
 String birthday = SharedPrefence.getString(..);
 presenter.setSavedBirtday(birthday);

 }

2) how to access other system services inside presenter. 2)如何访问演示者内部的其他系统服务。

As metioned before; 如前所述; You don't acesss System services in the Presenter . 你不会在acesss系统服务Presenter What you can do is call the a System Service from the Presenter. 您可以做的是从Presenter调用系统服务。

Example with Vibrator: 振动器示例:

1 - Create an interface: 1 - 创建一个界面:

interface OnSystemServiceCaller{
  onVibratorCall();
}

2 - Implement it in a Activity 2 - 在Activity中实现它

class MainActivity implements OnSystemServiceCaller{

@Override
onVibratorCall(){
  Vibrator v = (Vibrator) getSystemService(VIBRATOR);
  v.vibrate(50);

  } 
}

3 - Call from presenter 3 - 来自演示者的电话

class Presenter{
OnSystemServiceCaller listener;

  public void ifButtonClicked(){
    listener.onVibrateCall();
  }

}

3) if i am working on SQLite Databases then during any transaction in my database which is done by call from presenter to my SQLite Helper class need context to access database. 3)如果我正在使用SQLite数据库,那么在我的数据库中的任何事务期间,通过从演示者到我的SQLite Helper类的调用来完成访问数据库的上下文。

Some wont like this answers other will, this is just a suggestion. 有些人不会喜欢这个答案,这只是一个建议。

You can access your SQLite by using a global ApplicationContext() in your app class (Class that extend Application; see how here since your SQLlite is global for the whole app and not just a particular Activity And when you need to pass data from SQLite to a Activity then you pass it first to the Presenter and from Presenter to your Activity the same way we send a call to our Vibrator method 您可以通过在app类中使用全局ApplicationContext()来访问您的SQLite (扩展Application的类; 请参阅此处,因为您的SQLlite是整个应用程序的全局,而不仅仅是特定的Activity以及何时需要将数据从SQLite传递到a Activity然后将它首先传递给Presenter ,从Presenter传递给Activity ,就像我们向Vibrator方法发送调用一样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM