janishar / android-mvvm-architecture

This repository contains a detailed sample app that implements MVVM architecture using Dagger2, Room, RxJava2, FastAndroidNetworking and PlaceholderView
https://janisharali.com
Apache License 2.0
2.96k stars 934 forks source link

Unable to get ViewModelProvider.Factory for each ViewModel #12

Open prdp89 opened 6 years ago

prdp89 commented 6 years ago

Hi,

I am following same app structure for my app as:

ActivityBuilder.java: `@Module public abstract class ActivityBuilder {

@ContributesAndroidInjector(modules = SplashActivityModule.class)
abstract SplashActivity bindSplashActivity();

@ContributesAndroidInjector(modules = {NavigationActivityModule.class,
                                       CatalogueFragmentProvider.class })
abstract NavigationActivity bindNavigationActivity();

}`

NavigationActivityModule.Java (NavigationActivity which draws Navigation View with NavigationViewModel): `@Module public class NavigationActivityModule {

@Provides
NavigationViewModel provideNavigationViewModel(Application application) {
    return new NavigationViewModel(application);
}

@Provides ViewModelProvider.Factory mainViewModelProvider(NavigationViewModel navigationViewModel) { return new TraderViewModelFactory<>(navigationViewModel); } }`

CatalogueFragmentProvider.Java (CatalogueFragment which shows as the first fragment inside NavigationView container): @Module public abstract class CatalogueFragmentProvider { @ContributesAndroidInjector(modules = CataloguesFragmentModule.class) abstract CataloguesFragment provideCataloguesFragment(); }

Lastly, CataloguesFragmentModule.Java (contain catalogue view such as RecyclerView inside CatalogueFragment): @Module public class CataloguesFragmentModule {

@Provides
CatalogueFragmentViewModel provideAboutViewModel(CataloguesRepository cataloguesRepository, Application application) {
    return new CatalogueFragmentViewModel(cataloguesRepository, application);
}

@Provides
ViewModelProvider.Factory catalogueViewModelProvider(CatalogueFragmentViewModel catalogueFragmentViewModel) {
    return new TraderViewModelFactory<>(catalogueFragmentViewModel);
}

}

The problem is I am getting the same instance of NavigationViewModel inside the CatalogueViewModel. This throws error by ViewModelFactory since modelClass.isAssignableFrom returns false.

Please help. I stuck into this from last three days and couldn't find any solution by googling too.

Thanks.

sunil91 commented 5 years ago

@Provides @Singleton AppDatabase provideAppDatabase(@DatabaseInfo String dbName, Context context) { return Room.databaseBuilder(context, AppDatabase.class, dbName).fallbackToDestructiveMigration() .build(); }

Appdatabase class wrong argument exception @amitshekhariitbhu