0

我已经阅读了有关 Dagger2 的更多信息,我发现它类似于Butterknife,因为当我需要使用它时,我创建了一个模块,并使用:

   ((MyApp) getApplication()).createAppComponent().inject(this);

使用,例如,这个:

@Inject
RegisterPresenter presenter;

但我不明白如何使用构造函数注入。我有一个案例(使用模型视图演示者):

private Register.View view;
private RegisterModel model;
private StorageModel storageModel;
private DatabaseModel databaseModel;
private Uri compressPath;

List<String > stringList;
String uid;

@Inject
public RegisterPresenter() {
    model = new RegisterModel(this);
    storageModel = new StorageModel(this);
    databaseModel = new DatabaseModel(this);

}

这是对的吗?还是我需要这样做?

 @Inject
    public RegisterPresenter(RegisterModel model, StorageModel storageModel, DatabaseModel databaseModel) {
        this.model = model;
        this.storageModel = storageModel;
        this.databaseModel = databaseModel;
    }

因为如果我使用第二个代码,在我的活动(视图)中我需要这样做:

protected void onResume() {
        super.onResume();
        presenter.setView(this);
    }

那么如何构建 Presenter 呢?或者如果我需要在 Activity 中创建它,从视图中传递模块是否正确?

编辑:这些是我的模型:

public class FirebaseModel implements Model, FirebaseAuth.AuthStateListener {


    FirebaseAuth auth;

    Presenter presenter;
    FirebaseUser user;

    @Inject
    public FirebaseModel(Presenter presenter) {
        this.presenter = presenter;
        auth = FirebaseAuth.getInstance();

    }


    @Override
    public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
        user = auth.getCurrentUser();
        presenter.updateUser(user);

    }


    @Override
    public void loadUser() {

        auth.addAuthStateListener(this);
    }
}
public class StorageModel implements Register.Storage,OnCompleteListener <UploadTask.TaskSnapshot>{

Register.Presenter presenter;
StorageReference storage;

@Inject
public StorageModel(Register.Presenter presenter) {
    this.presenter = presenter;
    storage = FirebaseStorage.getInstance().getReferenceFromUrl("gs://firebase-happypark.appspot.com");
}



@Override
public void onTryToUpdateUser(Uri path,String uid) {
    if(path != null) {
        StorageReference riversRef = storage.child("uid").child("ImgProfile/" + path.getLastPathSegment());
        UploadTask uploadTask = riversRef.putFile(path);
        Log.d("ssd", "sto qua");
        uploadTask.addOnCompleteListener(this);
     }
 }


//UNNECESSARY

@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {

    if (task.isSuccessful()) {
        presenter.onUploadCompleted();
    } else {
        presenter.onUploadError(task.getException());
    }
}

}

谢谢

4

1 回答 1

0

演示者将是这样的:

public class RegisterPresenter implements RegisterContract.Presenter {

private Register.View view;
private RegisterModel model;
private StorageModel storageModel;
private DatabaseModel databaseModel;
private Uri compressPath;

List<String > stringList;
String uid;

@Inject
 public RegisterPresenter(Register.View view, RegisterModel model, StorageModel storageModel, DatabaseModel databaseModel) {

    this.view = view;
    this.model = model;
    this.storageModel = storageModel;
    this.databaseModel = databaseModel;
}

@Override
public void someMethodFromContract(){}

//or anything else you have in mind

}

然后像这样在 onResume 中调用它

presenter.someMethodFromContract();
于 2017-05-19T13:18:20.600 回答