androidx.lifecycle.ViewModel Java Examples

The following examples show how to use androidx.lifecycle.ViewModel. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ViewModelFactory.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull final Class<T> modelClass) {
    Callable<? extends ViewModel> creator = creators.get(modelClass);
    if (creator == null) {
        for (Map.Entry<Class, Callable<? extends ViewModel>> entry : creators.entrySet()) {
            if (modelClass.isAssignableFrom(entry.getKey())) {
                creator = entry.getValue();
                break;
            }
        }
    }
    if (creator == null) {
        throw new IllegalArgumentException("unknown model class " + modelClass);
    }
    try {
        return (T) creator.call();
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #2
Source File: PostViewModel.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 6 votes vote down vote up
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
    if (postType == PostDataSource.TYPE_FRONT_PAGE) {
        return (T) new PostViewModel(retrofit, accessToken, locale, postType, sortType, filter,
                nsfw);
    } else if (postType == PostDataSource.TYPE_SEARCH) {
        return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, query,
                postType, sortType, filter, nsfw);
    } else if (postType == PostDataSource.TYPE_SUBREDDIT || postType == PostDataSource.TYPE_MULTI_REDDIT) {
        return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType,
                sortType, filter, nsfw);
    } else {
        return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType,
                sortType, userWhere, filter, nsfw);
    }
}
 
Example #3
Source File: McuMgrViewModelFactory.java    From mcumgr-android with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull final Class<T> modelClass) {
    Callable<? extends ViewModel> creator = creators.get(modelClass);
    if (creator == null) {
        for (Map.Entry<Class<? extends ViewModel>, Callable<? extends ViewModel>> entry : creators.entrySet()) {
            if (modelClass.isAssignableFrom(entry.getKey())) {
                creator = entry.getValue();
                break;
            }
        }
    }
    if (creator == null) {
        throw new IllegalArgumentException("unknown model class " + modelClass);
    }
    try {
        return (T) creator.call();
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #4
Source File: ViewModelFactory.java    From mcumgr-android with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull final Class<T> modelClass) {
    Callable<? extends ViewModel> creator = creators.get(modelClass);
    if (creator == null) {
        for (Map.Entry<Class<? extends ViewModel>, Callable<? extends ViewModel>> entry : creators.entrySet()) {
            if (modelClass.isAssignableFrom(entry.getKey())) {
                creator = entry.getValue();
                break;
            }
        }
    }
    if (creator == null) {
        throw new IllegalArgumentException("unknown model class " + modelClass);
    }
    try {
        return (T) creator.call();
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #5
Source File: FilesViewModelFactory.java    From ArchPackages with GNU General Public License v3.0 5 votes vote down vote up
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
    if (modelClass.isAssignableFrom(FilesViewModel.class)) {
        return (T) new FilesViewModel(repo, arch, pkgname);
    }
    throw new IllegalArgumentException("Unknown ViewModel class");
}
 
Example #6
Source File: DetailsViewModelFactory.java    From ArchPackages with GNU General Public License v3.0 5 votes vote down vote up
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
    if (modelClass.isAssignableFrom(DetailsViewModel.class)) {
        return (T) new DetailsViewModel(repo, arch, packageName);
    }
    throw new IllegalArgumentException("Unknown ViewModel class");
}
 
Example #7
Source File: ViewModelFactory.java    From PopularMovies with MIT License 5 votes vote down vote up
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
    if (modelClass.isAssignableFrom(DiscoverMoviesViewModel.class)) {
        //noinspection unchecked
        return (T) new DiscoverMoviesViewModel(repository);
    } else if (modelClass.isAssignableFrom(FavoritesViewModel.class)) {
        //noinspection unchecked
        return (T) new FavoritesViewModel(repository);
    } else if (modelClass.isAssignableFrom(MovieDetailsViewModel.class)) {
        //noinspection unchecked
        return (T) new MovieDetailsViewModel(repository);
    }
    throw new IllegalArgumentException("Unknown ViewModel class: " + modelClass.getName());
}
 
Example #8
Source File: PackagesViewModelFactory.java    From ArchPackages with GNU General Public License v3.0 5 votes vote down vote up
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
    if (modelClass.isAssignableFrom(PackagesViewModel.class)) {
        return (T) new PackagesViewModel(keywordsParameter, query, listRepo, listArch, flagged);
    }
    throw new IllegalArgumentException("Unknown ViewModel class");
}
 
Example #9
Source File: BackupManageAppViewModelFactory.java    From SAI with GNU General Public License v3.0 5 votes vote down vote up
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
    try {
        return modelClass.getConstructor(Context.class, String.class).newInstance(mAppContext, mPackage);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #10
Source File: ViewModelFactory.java    From ground-android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an instance of the specified view model, which is scoped to the activity if annotated
 * with {@link SharedViewModel}, or scoped to the Fragment if not.
 */
public <T extends ViewModel> T get(Fragment fragment, Class<T> modelClass) {
  if (modelClass.getAnnotation(SharedViewModel.class) == null) {
    return ViewModelProviders.of(fragment, this).get(modelClass);
  } else {
    return get(fragment.getActivity(), modelClass);
  }
}
 
Example #11
Source File: ViewModelFactory.java    From ground-android with Apache License 2.0 5 votes vote down vote up
/**
 * Instantiates a new instance of the specified view model, injecting required dependencies.
 */
@Override
public <T extends ViewModel> T create(Class<T> modelClass) {
  Provider<? extends ViewModel> creator = creators.get(modelClass);
  if (creator == null) {
    throw new IllegalArgumentException("Unknown model class " + modelClass);
  }
  return (T) creator.get();
}
 
Example #12
Source File: ViewModelModule.java    From ground-android with Apache License 2.0 4 votes vote down vote up
@Binds
@IntoMap
@ViewModelKey(MapContainerViewModel.class)
abstract ViewModel bindMapContainerViewModel(MapContainerViewModel viewModel);
 
Example #13
Source File: ViewModelModule.java    From ground-android with Apache License 2.0 4 votes vote down vote up
@Binds
@IntoMap
@ViewModelKey(ProjectSelectorViewModel.class)
abstract ViewModel bindProjectSelectorViewModel(ProjectSelectorViewModel viewModel);
 
Example #14
Source File: ViewModelModule.java    From ground-android with Apache License 2.0 4 votes vote down vote up
@Binds
@IntoMap
@ViewModelKey(MainViewModel.class)
abstract ViewModel bindMainViewModel(MainViewModel viewModel);
 
Example #15
Source File: ViewModelModule.java    From ground-android with Apache License 2.0 4 votes vote down vote up
@Binds
@IntoMap
@ViewModelKey(FeatureDetailsViewModel.class)
abstract ViewModel bindFeatureDetailsViewModel(FeatureDetailsViewModel viewModel);
 
Example #16
Source File: ViewModelModule.java    From ground-android with Apache License 2.0 4 votes vote down vote up
@Binds
@IntoMap
@ViewModelKey(ObservationListViewModel.class)
abstract ViewModel bindObservationListViewModel(ObservationListViewModel viewModel);
 
Example #17
Source File: ViewModelModule.java    From ground-android with Apache License 2.0 4 votes vote down vote up
@Binds
@IntoMap
@ViewModelKey(TextFieldViewModel.class)
abstract ViewModel bindTextFieldViewModel(TextFieldViewModel viewModel);
 
Example #18
Source File: ViewModelModule.java    From ground-android with Apache License 2.0 4 votes vote down vote up
@Binds
@IntoMap
@ViewModelKey(OfflineAreaViewerViewModel.class)
abstract ViewModel bindOfflineAreaViewerViewModel(OfflineAreaViewerViewModel viewModel);
 
Example #19
Source File: ViewModelModule.java    From ground-android with Apache License 2.0 4 votes vote down vote up
@Binds
@IntoMap
@ViewModelKey(HomeScreenViewModel.class)
abstract ViewModel bindHomeScreenViewModel(HomeScreenViewModel viewModel);
 
Example #20
Source File: MainViewModelFactory.java    From android-popular-movies-app with Apache License 2.0 4 votes vote down vote up
@Override
public <T extends ViewModel> T create(Class<T> modelClass) {
    //noinspection unchecked
    return (T) new MainActivityViewModel(mRepository, mSortCriteria);
}
 
Example #21
Source File: SubscribedSubredditViewModel.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 4 votes vote down vote up
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
    return (T) new SubscribedSubredditViewModel(mApplication, mRedditDataRoomDatabase, mAccountName);
}
 
Example #22
Source File: CustomThemeViewModel.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 4 votes vote down vote up
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
    return (T) new CustomThemeViewModel(mRedditDataRoomDatabase);
}
 
Example #23
Source File: SubredditViewModel.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 4 votes vote down vote up
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
    //noinspection unchecked
    return (T) new SubredditViewModel(mApplication, mRedditDataRoomDatabase, mSubredditName);
}
 
Example #24
Source File: UserViewModel.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public <T extends ViewModel> T create(Class<T> modelClass) {
    //noinspection unchecked
    return (T) new UserViewModel(mApplication, mRedditDataRoomDatabase, mUsername);
}
 
Example #25
Source File: AccountViewModel.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public <T extends ViewModel> T create(Class<T> modelClass) {
    //noinspection unchecked
    return (T) new AccountViewModel(mApplication, mRedditDataRoomDatabase, mUsername);
}
 
Example #26
Source File: UserListingViewModel.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 4 votes vote down vote up
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
    return (T) new UserListingViewModel(retrofit, query, sortType);
}
 
Example #27
Source File: SubscribedUserViewModel.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 4 votes vote down vote up
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
    return (T) new SubscribedUserViewModel(mApplication, mRedditDataRoomDatabase, mAccountName);
}
 
Example #28
Source File: CommentViewModel.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 4 votes vote down vote up
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
    return (T) new CommentViewModel(retrofit, locale, accessToken, username, sortType, areSavedComments);
}
 
Example #29
Source File: MessageViewModel.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 4 votes vote down vote up
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
    return (T) new MessageViewModel(retrofit, locale, accessToken, where);
}
 
Example #30
Source File: SubredditListingViewModel.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 4 votes vote down vote up
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
    return (T) new SubredditListingViewModel(retrofit, query, sortType);
}