我想将MyRepositiry类注入MyPresenter。在这种情况下,如果@Inject PostRepository mRepository;
足够,或者我需要这样做,我唯一不明白的事情是:
public AddPostPresenter(PostRepository postRepository, AddPostContract.View view) {
this.mRepository = postRepository;
mView = view;
mFireBaseApp.getApplicationComponent().inject(this);
}
@Module public class DataModule {
FireBaseApp mFirebaseApp;
public DataModule(FireBaseApp fireBaseApp) {
this.mFirebaseApp = fireBaseApp;
}
@Provides
@Singleton
FirebaseAuth getFirebaseAuth() {
return FirebaseAuth.getInstance();
}
@Provides
@Singleton
FirebaseUser getCurrentUser(FirebaseAuth firebaseAuth) {
return firebaseAuth.getCurrentUser();
}
我的组件:
@Singleton @Component(modules = DataModule.class)
public interface ApplicationComponent {
void inject(MainActivity mainActivity);
void inject(PostRepository postRepository);
void inject(AddPostPresenter addPostPresenter);
void inject(AddPostActivity addPostActivity);
}
申请类:
public class FireBaseApp extends Application {
private ApplicationComponent component;
@Override
public void onCreate() {
super.onCreate();
if (BuildConfig.DEBUG) {
Timber.plant(new Timber.DebugTree());
}
inject();
}
private void inject() {
component = DaggerApplicationComponent
.builder()
.dataModule(new DataModule(this))
.build();
}
public ApplicationComponent getApplicationComponent(){
return component;
}
}
存储库:
public class PostRepository implements IPosts {
FireBaseApp fireBaseApp;
DatabaseReference mDatabaseReference;
FirebaseAuth mFirebaseAuth;
FirebaseUser mFirebaseUser;
@Inject public PostRepository() {
mFirebaseAuth = FirebaseAuth.getInstance();
mFirebaseUser = mFirebaseAuth.getCurrentUser();
mDatabaseReference = FirebaseDatabase.getInstance().getReference();
}
最后是演示者:
public class AddPostPresenter implements AddPostContract.UserActionListener {
@Inject PostRepository mRepository;
FireBaseApp mFireBaseApp;
private final AddPostContract.View mView;
public AddPostPresenter(PostRepository postRepository, AddPostContract.View view) {
this.mRepository = postRepository;
mView = view;
mFireBaseApp.getApplicationComponent().inject(this);
}
所以我的问题是我需要在MyPresenter构造函数mFireBaseApp.getApplicationComponent().inject(this);
中执行此操作
或者使用@Inject的PostRepostory就足够了? @Inject PostRepository mRepository;