blob: 4b84c5062be0f2820913018f3ef6871179b469b0 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
package ml.docilealligator.infinityforreddit.subscribeduser;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import java.util.List;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
public class SubscribedUserViewModel extends ViewModel {
private final SubscribedUserRepository mSubscribedUserRepository;
private final LiveData<List<SubscribedUserData>> mAllSubscribedUsers;
private final LiveData<List<SubscribedUserData>> mAllFavoriteSubscribedUsers;
private final MutableLiveData<String> searchQueryLiveData;
public SubscribedUserViewModel(RedditDataRoomDatabase redditDataRoomDatabase, String accountName) {
mSubscribedUserRepository = new SubscribedUserRepository(redditDataRoomDatabase, accountName);
searchQueryLiveData = new MutableLiveData<>("");
mAllSubscribedUsers = Transformations.switchMap(searchQueryLiveData, searchQuery -> mSubscribedUserRepository.getAllSubscribedUsersWithSearchQuery(searchQuery));
mAllFavoriteSubscribedUsers = Transformations.switchMap(searchQueryLiveData, searchQuery -> mSubscribedUserRepository.getAllFavoriteSubscribedUsersWithSearchQuery(searchQuery));
}
public LiveData<List<SubscribedUserData>> getAllSubscribedUsers() {
return mAllSubscribedUsers;
}
public LiveData<List<SubscribedUserData>> getAllFavoriteSubscribedUsers() {
return mAllFavoriteSubscribedUsers;
}
public void insert(SubscribedUserData subscribedUserData) {
mSubscribedUserRepository.insert(subscribedUserData);
}
public void setSearchQuery(String searchQuery) {
searchQueryLiveData.postValue(searchQuery);
}
public static class Factory extends ViewModelProvider.NewInstanceFactory {
private final RedditDataRoomDatabase mRedditDataRoomDatabase;
private final String mAccountName;
public Factory(RedditDataRoomDatabase redditDataRoomDatabase, String accountName) {
mRedditDataRoomDatabase = redditDataRoomDatabase;
mAccountName = accountName;
}
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
return (T) new SubscribedUserViewModel(mRedditDataRoomDatabase, mAccountName);
}
}
}
|