ViewModelProviders with Dagger 2,无法掌握概念 [英] ViewModelProviders with Dagger 2, not able to grasp the concept
问题描述
我有这样的改造服务
public interface BrandsService {
@GET("listBrand")
Call<List<Brand>> getBrands();
}
然后我有一个存储库来从这样的 api 中获取数据
Then I have a Repository to get data from an api like this
public class BrandsRepository {
public static final String TAG = "BrandsRepository";
MutableLiveData<List<Brand>> mutableLiveData;
Retrofit retrofit;
@Inject
public BrandsRepository(Retrofit retrofit) {
this.retrofit = retrofit;
}
public LiveData<List<Brand>> getListOfBrands() {
// Retrofit retrofit = ApiManager.getAdapter();
final BrandsService brandsService = retrofit.create(BrandsService.class);
Log.d(TAG, "getListOfBrands: 00000000000 "+retrofit);
mutableLiveData = new MutableLiveData<>();
Call<List<Brand>> retrofitCall = brandsService.getBrands();
retrofitCall.enqueue(new Callback<List<Brand>>() {
@Override
public void onResponse(Call<List<Brand>> call, Response<List<Brand>> response) {
mutableLiveData.setValue(response.body());
}
@Override
public void onFailure(Call<List<Brand>> call, Throwable t) {
t.printStackTrace();
}
});
return mutableLiveData;
}
}
我通过像这样注入 Retrofit 来使用 Dagger2 的构造函数注入.然后我有一个这样的 ViewModel
I am using constructor injection of Dagger2 by injecting Retrofit like that. Then I have a ViewModel like this
public class BrandsViewModel extends ViewModel{
BrandsRepository brandsRepository;
LiveData<List<Brand>> brandsLiveData;
@Inject
public BrandsViewModel(BrandsRepository brandsRepository) {
this.brandsRepository = brandsRepository;
}
public void callService(){
brandsLiveData = brandsRepository.getListOfBrands();
}
public LiveData<List<Brand>> getBrandsLiveData() {
return brandsLiveData;
}
}
要在 BrandsRepository 中注入 Retrofit,我必须像这样注入 BrandsRepository.然后我有这样的 MainActivity
To inject Retrofit in BrandsRepository, I have to inject BrandsRepository like that. Then I have MainActivity like this
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Inject
BrandsViewModel brandsViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((MainApplication)getApplication()).getNetComponent().inject(this);
// BrandsViewModel brandsViewModel = ViewModelProviders.of(this).get(BrandsViewModel.class);
brandsViewModel.callService();
LiveData<List<Brand>> brandsLiveData = brandsViewModel.getBrandsLiveData();
brandsLiveData.observe(this, new Observer<List<Brand>>() {
@Override
public void onChanged(@Nullable List<Brand> brands) {
Log.d(TAG, "onCreate: "+brands.get(0).getName());
}
});
}
}
BrandsViewModel 是使用 Dagger2 而不是 ViewModelProviders 注入的.这工作正常,但是当我尝试通过取消注释来使用 ViewModelProviders 时,dagger 给了我一个明显的错误.获取 ViewModel 的正确方法是使用 ViewModelProviders,但我将如何在注入这样的改造的同时实现这一点.
The BrandsViewModel is injected using Dagger2 rather than ViewModelProviders. This is working fine but when I try to use ViewModelProviders by uncommenting it, dagger gives me the error which is obvious. The proper way of getting a ViewModel is by using ViewModelProviders but how will I accomplish this while injecting retrofit like that.
推荐答案
重要说明.要使用 Jetpack ViewModel,您不需要地图多重绑定.继续阅读.
An important note. To use Jetpack ViewModel, you don't need map-multibinding. Read on.
答案可能比 Mumi 的方法更简单,即您在组件上公开 ViewModel:
The answer can be simpler than Mumi's approach, which is that you expose the ViewModel on your component:
@Singleton
@Component(modules={...})
public interface SingletonComponent {
BrandsViewModel brandsViewModel();
}
现在您可以在 ViewModelFactory 内的组件上访问此方法:
And now you can access this method on the component inside the ViewModelFactory:
// @Inject
BrandsViewModel brandsViewModel;
...
brandsViewModel = new ViewModelProvider(this, new ViewModelProvider.Factory() {
@Override
public <T extends ViewModel> create(Class<T> modelClazz) {
if(modelClazz == BrandsViewModel.class) {
return singletonComponent.brandsViewModel();
}
throw new IllegalArgumentException("Unexpected class: [" + modelClazz + "]");
}).get(BrandsViewModel.class);
<小时>
所有这些都可以用 Kotlin 简化和隐藏:
All this can be simplified and hidden with Kotlin:
inline fun <reified T: ViewModel> AppCompatActivity.createViewModel(crossinline factory: () -> T): T = T::class.java.let { clazz ->
ViewModelProvider(this, object: ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
if(modelClass == clazz) {
@Suppress("UNCHECKED_CAST")
return factory() as T
}
throw IllegalArgumentException("Unexpected argument: $modelClass")
}
}).get(clazz)
}
现在让你做
brandsViewModel = createViewModel { singletonComponent.brandsViewModel() }
现在 BrandsViewModel
可以从 Dagger 接收参数的地方:
Where now BrandsViewModel
can receive its parameters from Dagger:
class BrandsViewModel @Inject constructor(
private val appContext: Context,
/* other deps */
): ViewModel() {
...
}
虽然如果 Provider
从 Dagger 中暴露出来,那么意图可能会更清晰
Though the intent might be cleaner if a Provider<BrandsViewModel>
is exposed from Dagger instead
interface SingletonComponent {
fun brandsViewModel(): Provider<BrandsViewModel>
}
brandsViewModel = createViewModel { singletonComponent.brandsViewModel().get() }
<小时>
通过 android-ktx
的一些额外技巧,你甚至可以做到
With some additional trickery coming in from android-ktx
, you could even do
@Suppress("UNCHECKED_CAST")
inline fun <reified T : ViewModel> Fragment.fragmentViewModels(
crossinline creator: () -> T
): Lazy<T> {
return createViewModelLazy(T::class, storeProducer = {
viewModelStore
}, factoryProducer = {
object : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(
modelClass: Class<T>
): T = creator.invoke() as T
}
})
}
然后
class ProfileFragment: Fragment(R.layout.profile_fragment) {
private val viewModel by fragmentViewModels {
singletonComponent.brandsViewModelFactory().get()
}
brandsViewModelFactory()
在哪里
fun brandsViewModelFactory(): Provider<BrandsViewModel>
这篇关于ViewModelProviders with Dagger 2,无法掌握概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!