依赖注入到MVP模型中

时间:2018-07-25 13:13:08

标签: android dependency-injection dagger-2 mvp

所以我正在开发一个简单的应用,该应用使用了random user Api,试图学习mvp架构和dagger2 DI的基础,

这基本上是我的主要屏幕课程:

public class MainActivity extends AppCompatActivity implements MainScreenViewInterface {

private ImageView randomUserImage;
private TextView randomUserFullName, randomUserEmail;
@Inject MainScreenPresenter presenter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bindViews();
    injectMainActivityComponent();

    presenter.getRandomUser();
}

private void bindViews(){
    randomUserImage = findViewById(R.id.randomUserImage);
    randomUserFullName = findViewById(R.id.randomUserName);
    randomUserEmail = findViewById(R.id.randomUserEmail);
}

private void injectMainActivityComponent(){
    DaggerMainActivityComponent
            .builder()
            .appComponent(((ApplicationEntry)getApplicationContext()).getAppComponent())
            .mainActivityModule(new MainActivityModule(this))
            .contextModule(new ContextModule(this))
            .build()
            .inject(this);
}

@Override
protected void onDestroy() {
    super.onDestroy();
}

@Override
public void updateView(RandomUser randomUser) {
    Picasso.get().load(randomUser.getImageUrl()).into(randomUserImage);
    randomUserFullName.setText(randomUser.getName());
    randomUserEmail.setText(randomUser.getEmail());
}

}

public class MainScreenPresenter implements MainScreenPresenterInterface {

private MainScreenModelInterface model;
private MainScreenViewInterface view;

private final String URL = "https://randomuser.me/api/";
@Inject Context context;

@Inject
public MainScreenPresenter(MainScreenModelInterface model, MainScreenViewInterface view) {
    this.model = model;
    this.view = view;
}

@Override
public void getRandomUser() {
    Response.Listener listener = new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            view.updateView(randomUserData(response));
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    };
    model.performRequest(Request.Method.GET, URL, listener, errorListener, this.context);
}

private RandomUser randomUserData(String response){
    RandomUser randomUser = new RandomUser();
    try {
        JSONObject reponse = (JSONObject) new JSONTokener(response).nextValue();
        JSONArray results = reponse.getJSONArray("results");

        JSONObject name = results.getJSONObject(0).getJSONObject("name");

        String imageUrl = results.getJSONObject(0).getJSONObject("picture").getString("medium");
        String fullName = name.getString("title")
                + " " + name.getString("first")
                + " " + name.getString("last");

        String email = results.getJSONObject(0).getString("email");


        randomUser = new RandomUser(imageUrl, fullName, email);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return randomUser;
}

}

public class MainScreenModel implements MainScreenModelInterface {

@Override
public void performRequest(int type, String url, Response.Listener success, Response.ErrorListener error, Context context) {
    StringRequest stringRequest = new StringRequest(type, url, success, error);
    VolleySingleton.getInstance(context).addToRequestQueue(stringRequest);
}

}

正如您所看到的,我使用Dagger2将演示者注入到视图中,并将上下文注入到演示者中,并且工作正常。

但是我的问题是为什么我不能将上下文直接注入到模型中,所以当我尝试它时,它给了我NPE错误。

您可以检查项目here来查看DI类的定义和接口。

谢谢。

0 个答案:

没有答案