package ml.docilealligator.infinityforreddit.account; import androidx.lifecycle.LiveData; import androidx.lifecycle.ViewModel; import androidx.lifecycle.ViewModelProvider; import java.util.List; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; public class AccountViewModel extends ViewModel { private final AccountRepository mAccountRepository; private final LiveData> mAccountsExceptCurrentAccountLiveData; private final LiveData mCurrentAccountLiveData; private final LiveData> mAllAccountsLiveData; public AccountViewModel(RedditDataRoomDatabase redditDataRoomDatabase) { mAccountRepository = new AccountRepository(redditDataRoomDatabase); mAccountsExceptCurrentAccountLiveData = mAccountRepository.getAccountsExceptCurrentAccountLiveData(); mCurrentAccountLiveData = mAccountRepository.getCurrentAccountLiveData(); mAllAccountsLiveData = mAccountRepository.getAllAccountsLiveData(); } public LiveData> getAccountsExceptCurrentAccountLiveData() { return mAccountsExceptCurrentAccountLiveData; } public LiveData getCurrentAccountLiveData() { return mCurrentAccountLiveData; } public LiveData> getAllAccountsLiveData() { return mAllAccountsLiveData; } public void insert(Account userData) { mAccountRepository.insert(userData); } public static class Factory extends ViewModelProvider.NewInstanceFactory { private final RedditDataRoomDatabase mRedditDataRoomDatabase; public Factory(RedditDataRoomDatabase redditDataRoomDatabase) { mRedditDataRoomDatabase = redditDataRoomDatabase; } @Override public T create(Class modelClass) { //noinspection unchecked return (T) new AccountViewModel(mRedditDataRoomDatabase); } } }