From 21e7dad1b04f7b1753aa50efc2cf760568c9595f Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 20 Dec 2021 21:07:46 +0800 Subject: Search subscribed users and multireddits. --- .../activities/SubscribedThingListingActivity.java | 4 ++-- .../fragments/FollowedUsersListingFragment.java | 4 ++++ .../fragments/MultiRedditListingFragment.java | 4 ++++ .../infinityforreddit/multireddit/MultiRedditDao.java | 8 ++++---- .../multireddit/MultiRedditRepository.java | 17 ++++++++--------- .../multireddit/MultiRedditViewModel.java | 14 ++++++++++++-- .../subscribedsubreddit/SubscribedSubredditDao.java | 6 ------ .../SubscribedSubredditRepository.java | 4 ---- .../subscribeduser/SubscribedUserDao.java | 8 ++++---- .../subscribeduser/SubscribedUserRepository.java | 14 ++++++-------- .../subscribeduser/SubscribedUserViewModel.java | 14 ++++++++++++-- 11 files changed, 56 insertions(+), 41 deletions(-) (limited to 'app/src') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubscribedThingListingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubscribedThingListingActivity.java index 1365603c..1aa679ee 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubscribedThingListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubscribedThingListingActivity.java @@ -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); } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/FollowedUsersListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/FollowedUsersListingFragment.java index b3413209..0fe9a13d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/FollowedUsersListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/FollowedUsersListingFragment.java @@ -179,4 +179,8 @@ public class FollowedUsersListingFragment extends Fragment implements FragmentCo mLinearLayoutManager.scrollToPositionWithOffset(0, 0); } } + + public void changeSearchQuery(String searchQuery) { + mSubscribedUserViewModel.setSearchQuery(searchQuery); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/MultiRedditListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/MultiRedditListingFragment.java index 302b0ea0..17a2fb96 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/MultiRedditListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/MultiRedditListingFragment.java @@ -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); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/MultiRedditDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/MultiRedditDao.java index 4d99e7a4..4e6b5a7b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/MultiRedditDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/MultiRedditDao.java @@ -16,14 +16,14 @@ public interface MultiRedditDao { @Insert(onConflict = OnConflictStrategy.REPLACE) void insertAll(List multiReddits); - @Query("SELECT * FROM multi_reddits WHERE username = :username ORDER BY name COLLATE NOCASE ASC") - LiveData> getAllMultiReddits(String username); + @Query("SELECT * FROM multi_reddits WHERE username = :username AND display_name LIKE '%' || :searchQuery || '%' ORDER BY name COLLATE NOCASE ASC") + LiveData> getAllMultiRedditsWithSearchQuery(String username, String searchQuery); @Query("SELECT * FROM multi_reddits WHERE username = :username ORDER BY name COLLATE NOCASE ASC") List getAllMultiRedditsList(String username); - @Query("SELECT * FROM multi_reddits WHERE username = :username AND is_favorite ORDER BY name COLLATE NOCASE ASC") - LiveData> 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> 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); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/MultiRedditRepository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/MultiRedditRepository.java index 8b8156ae..45b000f9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/MultiRedditRepository.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/MultiRedditRepository.java @@ -7,20 +7,19 @@ import java.util.List; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; public class MultiRedditRepository { - private LiveData> mAllMultiReddits; - private LiveData> 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> getAllMultiReddits() { - return mAllMultiReddits; + LiveData> getAllMultiRedditsWithSearchQuery(String searchQuery) { + return mMultiRedditDao.getAllMultiRedditsWithSearchQuery(mAccountName, searchQuery); } - LiveData> getAllFavoriteMultiReddits() { - return mAllFavoriteMultiReddits; + LiveData> getAllFavoriteMultiRedditsWithSearchQuery(String searchQuery) { + return mMultiRedditDao.getAllFavoriteMultiRedditsWithSearchQuery(mAccountName, searchQuery); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/MultiRedditViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/MultiRedditViewModel.java index 989b4853..efb532de 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/MultiRedditViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/MultiRedditViewModel.java @@ -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> mAllMultiReddits; private LiveData> mAllFavoriteMultiReddits; + private MutableLiveData 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> 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; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/subscribedsubreddit/SubscribedSubredditDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/subscribedsubreddit/SubscribedSubredditDao.java index edcfa4cc..7867d225 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subscribedsubreddit/SubscribedSubredditDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subscribedsubreddit/SubscribedSubredditDao.java @@ -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> getAllSubscribedSubreddits(String accountName); - @Query("SELECT * from subscribed_subreddits WHERE username = :accountName AND name LIKE '%' || :searchQuery || '%' ORDER BY name COLLATE NOCASE ASC") LiveData> getAllSubscribedSubredditsWithSearchQuery(String accountName, String searchQuery); @Query("SELECT * from subscribed_subreddits WHERE username = :accountName COLLATE NOCASE ORDER BY name COLLATE NOCASE ASC") List getAllSubscribedSubredditsList(String accountName); - @Query("SELECT * from subscribed_subreddits WHERE username = :accountName COLLATE NOCASE AND is_favorite = 1 ORDER BY name COLLATE NOCASE ASC") - LiveData> 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> getAllFavoriteSubscribedSubredditsWithSearchQuery(String accountName, String searchQuery); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/subscribedsubreddit/SubscribedSubredditRepository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/subscribedsubreddit/SubscribedSubredditRepository.java index 0901da55..2bb9e247 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subscribedsubreddit/SubscribedSubredditRepository.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subscribedsubreddit/SubscribedSubredditRepository.java @@ -10,15 +10,11 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; public class SubscribedSubredditRepository { private SubscribedSubredditDao mSubscribedSubredditDao; - private LiveData> mAllSubscribedSubreddits; - private LiveData> mAllFavoriteSubscribedSubreddits; private String mAccountName; SubscribedSubredditRepository(RedditDataRoomDatabase redditDataRoomDatabase, String accountName) { mAccountName = accountName; mSubscribedSubredditDao = redditDataRoomDatabase.subscribedSubredditDao(); - mAllSubscribedSubreddits = mSubscribedSubredditDao.getAllSubscribedSubreddits(accountName); - mAllFavoriteSubscribedSubreddits = mSubscribedSubredditDao.getAllFavoriteSubscribedSubreddits(accountName); } LiveData> getAllSubscribedSubredditsWithSearchQuery(String searchQuery) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/subscribeduser/SubscribedUserDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/subscribeduser/SubscribedUserDao.java index 18754a6b..ed7249db 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subscribeduser/SubscribedUserDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subscribeduser/SubscribedUserDao.java @@ -16,14 +16,14 @@ public interface SubscribedUserDao { @Insert(onConflict = OnConflictStrategy.REPLACE) void insertAll(List subscribedUserDataList); - @Query("SELECT * FROM subscribed_users WHERE username = :accountName COLLATE NOCASE ORDER BY name COLLATE NOCASE ASC") - LiveData> getAllSubscribedUsers(String accountName); + @Query("SELECT * FROM subscribed_users WHERE username = :accountName AND name LIKE '%' || :searchQuery || '%' COLLATE NOCASE ORDER BY name COLLATE NOCASE ASC") + LiveData> getAllSubscribedUsersWithSearchQuery(String accountName, String searchQuery); @Query("SELECT * FROM subscribed_users WHERE username = :accountName COLLATE NOCASE ORDER BY name COLLATE NOCASE ASC") List getAllSubscribedUsersList(String accountName); - @Query("SELECT * FROM subscribed_users WHERE username = :accountName COLLATE NOCASE AND is_favorite = 1 ORDER BY name COLLATE NOCASE ASC") - LiveData> 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> 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); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/subscribeduser/SubscribedUserRepository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/subscribeduser/SubscribedUserRepository.java index 1bd0aa71..befdcdb2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subscribeduser/SubscribedUserRepository.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subscribeduser/SubscribedUserRepository.java @@ -10,21 +10,19 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; public class SubscribedUserRepository { private SubscribedUserDao mSubscribedUserDao; - private LiveData> mAllSubscribedUsers; - private LiveData> mAllFavoriteSubscribedUsers; + private String mAccountName; SubscribedUserRepository(RedditDataRoomDatabase redditDataRoomDatabase, String accountName) { mSubscribedUserDao = redditDataRoomDatabase.subscribedUserDao(); - mAllSubscribedUsers = mSubscribedUserDao.getAllSubscribedUsers(accountName); - mAllFavoriteSubscribedUsers = mSubscribedUserDao.getAllFavoriteSubscribedUsers(accountName); + mAccountName = accountName; } - LiveData> getAllSubscribedSubreddits() { - return mAllSubscribedUsers; + LiveData> getAllSubscribedUsersWithSearchQuery(String searchQuery) { + return mSubscribedUserDao.getAllSubscribedUsersWithSearchQuery(mAccountName, searchQuery); } - LiveData> getAllFavoriteSubscribedSubreddits() { - return mAllFavoriteSubscribedUsers; + LiveData> getAllFavoriteSubscribedUsersWithSearchQuery(String searchQuery) { + return mSubscribedUserDao.getAllFavoriteSubscribedUsersWithSearchQuery(mAccountName, searchQuery); } public void insert(SubscribedUserData subscribedUserData) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/subscribeduser/SubscribedUserViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/subscribeduser/SubscribedUserViewModel.java index b19e5906..100703f8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subscribeduser/SubscribedUserViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subscribeduser/SubscribedUserViewModel.java @@ -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> mAllSubscribedUsers; private LiveData> mAllFavoriteSubscribedUsers; + private MutableLiveData 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> 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; -- cgit v1.2.3