我正在重构项目,我想从依赖注入开始,并让Dagger在整个项目中正常工作。下面有一个我到目前为止所做的示例,它可以正常工作,但是我想查看其是否正确实现,以及是否有一种方法可以在组件的BindsInstance中具有可选字段。
我的示例遵循以下路径:
片段-> ServiceUtil->服务
“我的片段”创建ServiceUtil类的实例,并将“回调”属性之一设置为片段中存在的函数。然后,我的ServiceUtil类使用Retrofit和我的Service接口的实例进行API调用并返回回调。
问题:
片段:
public class Fragment1 extends BaseFragment
{
@Inject
ForgotPasswordServiceImpl forgotPasswordServiceImpl;
public static Fragment1 newInstance()
{
return new Fragment1();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment1, container, false);
ComponentA daggerComponentA = DaggerComponentA.builder().apiCallback(apiCallback).baseUrl(Urls.basePreauthUrl).build();
daggerComponentA.inject(this);
}
private Callback<String> apiCallback = new CustomCallback<String>()
{
@Override
public void onResponseReceived(Call<String> call, Response<String> response)
{
int statusCode = response.code();
if (statusCode == HttpCodes.OK && response.body() != null && response.body().getStatus().equals(SUCCESS_INDICATOR) && !response.body().getParticipantID().isEmpty())
{
//Update some stuff
}else{
//Update some stuff
}
}
@Override
public void onSessionExpired(Call<String> call, Response<String> response)
{
//Do something different
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t)
{
//Do Something
}
};
}
ServiceImpl类
public class MyServiceImpl
{
MyServiceImpl myService;
Callback<String> apiCallback;
public MyServiceImpl(MyServiceImpl myService, Callback<String> apiCallback){
this.myService = myService;
this.apiCallback = apiCallback;
}
public void callApi(String test){
Call<String> api = myService.findUser();
api.enqueue(apiCallback);
}
}
服务等级
public interface MyService
{
@POST(Urls.exampleUrl)
Call<String> findUser();
}
组件类
@Component(modules = {TestModule.class})
public interface TestComponent
{
@Component.Builder
interface Builder {
@BindsInstance Builder baseUrl(@Named("baseUrl") String baseUrl);
@BindsInstance Builder apiCallback(@Named("apiCallback") Callback<String> apiCallback);
ForgotPasswordComponent build();
}
void inject(Fragment1 fragment1);
}
TestModule类
@Module
public class TestModule extends PreAuthBaseModule
{
@Provides
public MyService myService(Retrofit retrofit){
return retrofit.create(MyService.class);
}
@Provides
public Callback<String> apiCallback(@Named("apiCallback") Callback<String> apiCallback){
return apiCallback;
}
@Provides
public MyServiceImpl myServiceImpl(MyService myService, Callback<String> apiCallback){
return new MyServiceImpl(myService, apiCallback);
}
}
PreAuthBaseModule类
@Module(includes = {PreAuthOkHttpClientModule.class, JacksonConverterFactoryModule.class})
public class PreAuthBaseModule
{
@Provides
public Retrofit retrofit(@Named("baseUrl") String baseUrl, OkHttpClient okHttpClient, JacksonConverterFactory factory){
return new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(factory)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build();
}
}