所以我是Dagger 2依赖注入的新手。我创建了一个自定义ViewModelFactory类,该类返回了我的ViewModel。
@Singleton
public class CustomViewModelFactory implements ViewModelProvider.Factory {
private final MyCatchesRepository repository;
@Inject
public CustomViewModelFactory(MyCatchesRepository repository) {
this.repository = repository;
}
@NonNull
@Override
@SuppressWarnings("unchecked")
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
if (modelClass.isAssignableFrom(MyCatchViewModel.class)) {
return (T) new MyCatchViewModel(repository);
} else {
throw new IllegalArgumentException("ViewModel Not Found");
}
}
}
CustomViewModel
在构造函数中使用一个MyCatchesRepository
,然后创建MyCatchViewModel
。
我如何更改此类,以便可以使用此ViewModelFactory创建具有不同构造函数参数(存储库)的不同ViewModels
这是创建CustomViewModelFactory的模块
@Module
public class RoomModule {
private final MyDatabase myDatabase;
public RoomModule(Application application) {
this.myDatabase = Room.databaseBuilder(application,
MyDatabase.class, AppConstants.DATABASE_NAME)
.build();
}
@Provides
@Singleton
MyCatchesRepository provideCatchesRepository(MyCatchDao myCatchDao) {
return new MyCatchesRepository(myCatchDao);
}
@Provides
@Singleton
MyCatchDao providesCatchDao(MyDatabase myDatabase) {
return myDatabase.myCatchDao();
}
@Provides
@Singleton
LuresRepository provideLureRepository(LureDao lureDao) {
return new LuresRepository(lureDao);
}
@Provides
@Singleton
LureDao provideLureDao(MyDatabase myDatabase) {
return myDatabase.lureDao();
}
@Provides
@Singleton
MyDatabase provideDatabase(Application application) {
return myDatabase;
}
@Provides
@Singleton
ViewModelProvider.Factory provideCatchesViewModelFactory(MyCatchesRepository catchesRepository) {
return new CustomViewModelFactory(catchesRepository);
}
}
ViewModelModule
@Module
public abstract class ViewModelModule {
@Binds
@IntoMap
@ViewModelKey(MyCatchViewModel.class)
abstract ViewModel myCatchViewModel(MyCatchViewModel myCatchViewModel);
@Binds
@IntoMap
@ViewModelKey(FishingSpotViewModel.class)
abstract ViewModel fishingSpotViewModel(FishingSpotViewModel fishingSpotViewModel);
@Binds
abstract ViewModelProvider.Factory bindCustomViewModelFactory(CustomViewModelFactory customViewModelFactory);
}
答案 0 :(得分:1)
Google团队在架构组件示例中提出的方法是使用自定义注释,以便通过匕首提供ViewModel类。
在Java中,注释看起来如下。
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import androidx.lifecycle.ViewModel;
import dagger.MapKey;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@MapKey
@interface ViewModelKey {
Class<? extends ViewModel> value();
}
这使用了Dagger中的MapKey,其中任何带注释的ViewModel都将组成一个Map,然后可以在ViewModelFactory中使用。
在Google样本中,ViewModelFactory如下所示,在其中使用构造函数注入,您可以访问ViewModel提供程序的地图。
public class ViewModelFactory implements ViewModelProvider.Factory {
private final Map<Class<? extends ViewModel>, Provider<ViewModel>> viewModels;
@Inject
public ViewModelFactory(Map<Class<? extends ViewModel>, Provider<ViewModel>> viewModels) {
this.viewModels = viewModels;
}
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
Provider<ViewModel> viewModelProvider = viewModels.get(modelClass);
if (viewModelProvider == null) {
throw new IllegalArgumentException("model class " + modelClass + " not found");
}
//noinspection unchecked
return (T) viewModelProvider.get();
}
}
在您的示例中,您将最终获得以下内容以提供MyCatchViewModel。然后可以通过遵循相同的模式来提供其他ViewModel。
@Module
public abstract class ViewModelModule {
@Binds
@IntoMap
@ViewModelKey(MyCatchViewModel.class)
abstract ViewModel myCatchViewModel(MyCatchViewModel myCatchViewModel);
}
有关完整示例,您可以从Google查看GithubBrowserSample示例。 https://github.com/googlesamples/android-architecture-components/blob/master/GithubBrowserSample/app/src/main/java/com/android/example/github/di/ViewModelModule.kt