Search subscribed users and multireddits.

This commit is contained in:
Alex Ning 2021-12-20 21:07:46 +08:00
parent 053773741e
commit 21e7dad1b0
11 changed files with 56 additions and 41 deletions

View File

@ -563,10 +563,10 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti
subscribedSubredditsListingFragment.changeSearchQuery(searchQuery);
}
if (followedUsersListingFragment != null) {
followedUsersListingFragment.changeSearchQuery(searchQuery);
}
if (multiRedditListingFragment != null) {
multiRedditListingFragment.changeSearchQuery(searchQuery);
}
}
}

View File

@ -179,4 +179,8 @@ public class FollowedUsersListingFragment extends Fragment implements FragmentCo
mLinearLayoutManager.scrollToPositionWithOffset(0, 0);
}
}
public void changeSearchQuery(String searchQuery) {
mSubscribedUserViewModel.setSearchQuery(searchQuery);
}
}

View File

@ -200,6 +200,10 @@ public class MultiRedditListingFragment extends Fragment implements FragmentComm
}
}
public void changeSearchQuery(String searchQuery) {
mMultiRedditViewModel.setSearchQuery(searchQuery);
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);

View File

@ -16,14 +16,14 @@ public interface MultiRedditDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAll(List<MultiReddit> multiReddits);
@Query("SELECT * FROM multi_reddits WHERE username = :username ORDER BY name COLLATE NOCASE ASC")
LiveData<List<MultiReddit>> getAllMultiReddits(String username);
@Query("SELECT * FROM multi_reddits WHERE username = :username AND display_name LIKE '%' || :searchQuery || '%' ORDER BY name COLLATE NOCASE ASC")
LiveData<List<MultiReddit>> getAllMultiRedditsWithSearchQuery(String username, String searchQuery);
@Query("SELECT * FROM multi_reddits WHERE username = :username ORDER BY name COLLATE NOCASE ASC")
List<MultiReddit> getAllMultiRedditsList(String username);
@Query("SELECT * FROM multi_reddits WHERE username = :username AND is_favorite ORDER BY name COLLATE NOCASE ASC")
LiveData<List<MultiReddit>> getAllFavoriteMultiReddits(String username);
@Query("SELECT * FROM multi_reddits WHERE username = :username AND is_favorite AND display_name LIKE '%' || :searchQuery || '%' ORDER BY name COLLATE NOCASE ASC")
LiveData<List<MultiReddit>> getAllFavoriteMultiRedditsWithSearchQuery(String username, String searchQuery);
@Query("SELECT * FROM multi_reddits WHERE path = :path AND username = :username COLLATE NOCASE LIMIT 1")
MultiReddit getMultiReddit(String path, String username);

View File

@ -7,20 +7,19 @@ import java.util.List;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
public class MultiRedditRepository {
private LiveData<List<MultiReddit>> mAllMultiReddits;
private LiveData<List<MultiReddit>> mAllFavoriteMultiReddits;
private MultiRedditDao mMultiRedditDao;
private String mAccountName;
MultiRedditRepository(RedditDataRoomDatabase redditDataRoomDatabase, String accountName) {
MultiRedditDao multiRedditDao = redditDataRoomDatabase.multiRedditDao();
mAllMultiReddits = multiRedditDao.getAllMultiReddits(accountName);
mAllFavoriteMultiReddits = multiRedditDao.getAllFavoriteMultiReddits(accountName);
mMultiRedditDao = redditDataRoomDatabase.multiRedditDao();
mAccountName = accountName;
}
LiveData<List<MultiReddit>> getAllMultiReddits() {
return mAllMultiReddits;
LiveData<List<MultiReddit>> getAllMultiRedditsWithSearchQuery(String searchQuery) {
return mMultiRedditDao.getAllMultiRedditsWithSearchQuery(mAccountName, searchQuery);
}
LiveData<List<MultiReddit>> getAllFavoriteMultiReddits() {
return mAllFavoriteMultiReddits;
LiveData<List<MultiReddit>> getAllFavoriteMultiRedditsWithSearchQuery(String searchQuery) {
return mMultiRedditDao.getAllFavoriteMultiRedditsWithSearchQuery(mAccountName, searchQuery);
}
}

View File

@ -5,6 +5,8 @@ import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
@ -16,12 +18,16 @@ public class MultiRedditViewModel extends AndroidViewModel {
private MultiRedditRepository mMultiRedditRepository;
private LiveData<List<MultiReddit>> mAllMultiReddits;
private LiveData<List<MultiReddit>> mAllFavoriteMultiReddits;
private MutableLiveData<String> searchQueryLiveData;
public MultiRedditViewModel(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String accountName) {
super(application);
mMultiRedditRepository = new MultiRedditRepository(redditDataRoomDatabase, accountName);
mAllMultiReddits = mMultiRedditRepository.getAllMultiReddits();
mAllFavoriteMultiReddits = mMultiRedditRepository.getAllFavoriteMultiReddits();
searchQueryLiveData = new MutableLiveData<>();
searchQueryLiveData.postValue("");
mAllMultiReddits = Transformations.switchMap(searchQueryLiveData, searchQuery -> mMultiRedditRepository.getAllMultiRedditsWithSearchQuery(searchQuery));
mAllFavoriteMultiReddits = Transformations.switchMap(searchQueryLiveData, searchQuery -> mMultiRedditRepository.getAllFavoriteMultiRedditsWithSearchQuery(searchQuery));
}
public LiveData<List<MultiReddit>> getAllMultiReddits() {
@ -32,6 +38,10 @@ public class MultiRedditViewModel extends AndroidViewModel {
return mAllFavoriteMultiReddits;
}
public void setSearchQuery(String searchQuery) {
searchQueryLiveData.postValue(searchQuery);
}
public static class Factory extends ViewModelProvider.NewInstanceFactory {
private Application mApplication;
private RedditDataRoomDatabase mRedditDataRoomDatabase;

View File

@ -19,18 +19,12 @@ public interface SubscribedSubredditDao {
@Query("DELETE FROM subscribed_subreddits")
void deleteAllSubscribedSubreddits();
@Query("SELECT * from subscribed_subreddits WHERE username = :accountName ORDER BY name COLLATE NOCASE ASC")
LiveData<List<SubscribedSubredditData>> getAllSubscribedSubreddits(String accountName);
@Query("SELECT * from subscribed_subreddits WHERE username = :accountName AND name LIKE '%' || :searchQuery || '%' ORDER BY name COLLATE NOCASE ASC")
LiveData<List<SubscribedSubredditData>> getAllSubscribedSubredditsWithSearchQuery(String accountName, String searchQuery);
@Query("SELECT * from subscribed_subreddits WHERE username = :accountName COLLATE NOCASE ORDER BY name COLLATE NOCASE ASC")
List<SubscribedSubredditData> getAllSubscribedSubredditsList(String accountName);
@Query("SELECT * from subscribed_subreddits WHERE username = :accountName COLLATE NOCASE AND is_favorite = 1 ORDER BY name COLLATE NOCASE ASC")
LiveData<List<SubscribedSubredditData>> getAllFavoriteSubscribedSubreddits(String accountName);
@Query("SELECT * from subscribed_subreddits WHERE username = :accountName AND name LIKE '%' || :searchQuery || '%' COLLATE NOCASE AND is_favorite = 1 ORDER BY name COLLATE NOCASE ASC")
LiveData<List<SubscribedSubredditData>> getAllFavoriteSubscribedSubredditsWithSearchQuery(String accountName, String searchQuery);

View File

@ -10,15 +10,11 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
public class SubscribedSubredditRepository {
private SubscribedSubredditDao mSubscribedSubredditDao;
private LiveData<List<SubscribedSubredditData>> mAllSubscribedSubreddits;
private LiveData<List<SubscribedSubredditData>> mAllFavoriteSubscribedSubreddits;
private String mAccountName;
SubscribedSubredditRepository(RedditDataRoomDatabase redditDataRoomDatabase, String accountName) {
mAccountName = accountName;
mSubscribedSubredditDao = redditDataRoomDatabase.subscribedSubredditDao();
mAllSubscribedSubreddits = mSubscribedSubredditDao.getAllSubscribedSubreddits(accountName);
mAllFavoriteSubscribedSubreddits = mSubscribedSubredditDao.getAllFavoriteSubscribedSubreddits(accountName);
}
LiveData<List<SubscribedSubredditData>> getAllSubscribedSubredditsWithSearchQuery(String searchQuery) {

View File

@ -16,14 +16,14 @@ public interface SubscribedUserDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAll(List<SubscribedUserData> subscribedUserDataList);
@Query("SELECT * FROM subscribed_users WHERE username = :accountName COLLATE NOCASE ORDER BY name COLLATE NOCASE ASC")
LiveData<List<SubscribedUserData>> getAllSubscribedUsers(String accountName);
@Query("SELECT * FROM subscribed_users WHERE username = :accountName AND name LIKE '%' || :searchQuery || '%' COLLATE NOCASE ORDER BY name COLLATE NOCASE ASC")
LiveData<List<SubscribedUserData>> getAllSubscribedUsersWithSearchQuery(String accountName, String searchQuery);
@Query("SELECT * FROM subscribed_users WHERE username = :accountName COLLATE NOCASE ORDER BY name COLLATE NOCASE ASC")
List<SubscribedUserData> getAllSubscribedUsersList(String accountName);
@Query("SELECT * FROM subscribed_users WHERE username = :accountName COLLATE NOCASE AND is_favorite = 1 ORDER BY name COLLATE NOCASE ASC")
LiveData<List<SubscribedUserData>> getAllFavoriteSubscribedUsers(String accountName);
@Query("SELECT * FROM subscribed_users WHERE username = :accountName AND name LIKE '%' || :searchQuery || '%' COLLATE NOCASE AND is_favorite = 1 ORDER BY name COLLATE NOCASE ASC")
LiveData<List<SubscribedUserData>> getAllFavoriteSubscribedUsersWithSearchQuery(String accountName, String searchQuery);
@Query("SELECT * FROM subscribed_users WHERE name = :name COLLATE NOCASE AND username = :accountName COLLATE NOCASE LIMIT 1")
SubscribedUserData getSubscribedUser(String name, String accountName);

View File

@ -10,21 +10,19 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
public class SubscribedUserRepository {
private SubscribedUserDao mSubscribedUserDao;
private LiveData<List<SubscribedUserData>> mAllSubscribedUsers;
private LiveData<List<SubscribedUserData>> mAllFavoriteSubscribedUsers;
private String mAccountName;
SubscribedUserRepository(RedditDataRoomDatabase redditDataRoomDatabase, String accountName) {
mSubscribedUserDao = redditDataRoomDatabase.subscribedUserDao();
mAllSubscribedUsers = mSubscribedUserDao.getAllSubscribedUsers(accountName);
mAllFavoriteSubscribedUsers = mSubscribedUserDao.getAllFavoriteSubscribedUsers(accountName);
mAccountName = accountName;
}
LiveData<List<SubscribedUserData>> getAllSubscribedSubreddits() {
return mAllSubscribedUsers;
LiveData<List<SubscribedUserData>> getAllSubscribedUsersWithSearchQuery(String searchQuery) {
return mSubscribedUserDao.getAllSubscribedUsersWithSearchQuery(mAccountName, searchQuery);
}
LiveData<List<SubscribedUserData>> getAllFavoriteSubscribedSubreddits() {
return mAllFavoriteSubscribedUsers;
LiveData<List<SubscribedUserData>> getAllFavoriteSubscribedUsersWithSearchQuery(String searchQuery) {
return mSubscribedUserDao.getAllFavoriteSubscribedUsersWithSearchQuery(mAccountName, searchQuery);
}
public void insert(SubscribedUserData subscribedUserData) {

View File

@ -5,6 +5,8 @@ import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
@ -16,12 +18,16 @@ public class SubscribedUserViewModel extends AndroidViewModel {
private SubscribedUserRepository mSubscribedUserRepository;
private LiveData<List<SubscribedUserData>> mAllSubscribedUsers;
private LiveData<List<SubscribedUserData>> mAllFavoriteSubscribedUsers;
private MutableLiveData<String> searchQueryLiveData;
public SubscribedUserViewModel(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String accountName) {
super(application);
mSubscribedUserRepository = new SubscribedUserRepository(redditDataRoomDatabase, accountName);
mAllSubscribedUsers = mSubscribedUserRepository.getAllSubscribedSubreddits();
mAllFavoriteSubscribedUsers = mSubscribedUserRepository.getAllFavoriteSubscribedSubreddits();
searchQueryLiveData = new MutableLiveData<>();
searchQueryLiveData.postValue("");
mAllSubscribedUsers = Transformations.switchMap(searchQueryLiveData, searchQuery -> mSubscribedUserRepository.getAllSubscribedUsersWithSearchQuery(searchQuery));
mAllFavoriteSubscribedUsers = Transformations.switchMap(searchQueryLiveData, searchQuery -> mSubscribedUserRepository.getAllFavoriteSubscribedUsersWithSearchQuery(searchQuery));
}
public LiveData<List<SubscribedUserData>> getAllSubscribedUsers() {
@ -36,6 +42,10 @@ public class SubscribedUserViewModel extends AndroidViewModel {
mSubscribedUserRepository.insert(subscribedUserData);
}
public void setSearchQuery(String searchQuery) {
searchQueryLiveData.postValue(searchQuery);
}
public static class Factory extends ViewModelProvider.NewInstanceFactory {
private Application mApplication;
private RedditDataRoomDatabase mRedditDataRoomDatabase;