Обучение Dagger2 все еще сложно, и я пытаюсь учиться. Настройте проект, используя новый андроид кинжала, чтобы избежать инъекций внутри класса активности. Пока это работает, но нужно использовать модификацию, введенную в классе докладчика. Добавлен модуль дооснащения в AppComponent, но метод класса apiService обнуляется при вызове из класса презентатора. Нужно знать, как правильно вводить.
AppComponent.java
@Singleton
@Component(modules = {
/* Use AndroidInjectionModule.class if you're not using support library */
AndroidSupportInjectionModule.class,
AppModule.class,
BuildersModule.class,
RetrofitModule.class})
public interface AppComponent {
@Component.Builder
interface Builder {
@BindsInstance
Builder application(BaseApplication application);
Builder retrofitModule(RetrofitModule retrofitModule);
AppComponent build();
}
void inject(BaseApplication app);
}
AppModule.java
@Module
class AppModule {
@Provides
Context provideContext(BaseApplication application) {
return application.getApplicationContext();
}
}
BuildersModule.java
@Module
public abstract class BuildersModule {
@ContributesAndroidInjector(modules = {SplashViewModule.class, SplashModule.class})
abstract SplashActivity bindSplashActivity();
// Add bindings for other sub-components here
}
BaseApplication.java
public class BaseApplication extends Application implements HasActivityInjector {
@Inject
DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;
@Override
public void onCreate() {
super.onCreate();
//configure timber for logging
if (BuildConfig.DEBUG) {
Timber.plant(new Timber.DebugTree());
}
DaggerAppComponent
.builder()
.application(this)
.retrofitModule(new RetrofitModule())
.build()
.inject(this);
}
@Override
public AndroidInjector<Activity> activityInjector() {
return dispatchingAndroidInjector;
}
}
RetrofitModule.java
@Module
public class RetrofitModule {
//get retrofit instance
@Singleton
@Provides
public UserAuthService getRestService() {
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ApiConstants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(getOkHttpClient())
.build();
return retrofit.create(UserAuthService.class);
}
}
UserAuthService.java
public interface UserAuthService {
@GET("v2/5be345062f00006b00ca22c4")
Observable<Example> getExampleResponse();
}
SplashModule.java
@Module
public class SplashModule {
@Provides
SplashPresenter provideSplashPresenter(SplashView splashView) {
return new SplashPresenter(splashView);
}
}
SplashViewModule.java
@Module
public abstract class SplashViewModule {
@Binds
abstract SplashView provideSplashView(SplashActivity splashActivity);
}
SplashPresenter.java
class SplashPresenter extends BasePresenter<SplashView> {
@Inject
@Named(ValueConstants.MAIN_THREAD)
Scheduler mMainThread;
@Inject
@Named(ValueConstants.NEW_THREAD)
Scheduler mNewThread;
@Inject
UserAuthService userAuthService;
SplashPresenter(SplashView view) {
super(view);
}
//test purpose
public void sayHello() {
userAuthService.getExampleResponse()
.observeOn(mMainThread)
.subscribeOn(mNewThread)
.subscribe(new Observer<Example>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Example example) {
Timber.d("example %s", example.toString());
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onComplete() {
}
});
}
}
здесь "userAuthService.getExampleResponse ()" становится нулевым. Я думаю, что докладчик должен знать о инъекции RetrofitModule. Так что мне нужно это исправить и как?