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); subscribedSubredditsListingFragment.changeSearchQuery(searchQuery);
} }
if (followedUsersListingFragment != null) { if (followedUsersListingFragment != null) {
followedUsersListingFragment.changeSearchQuery(searchQuery);
} }
if (multiRedditListingFragment != null) { if (multiRedditListingFragment != null) {
multiRedditListingFragment.changeSearchQuery(searchQuery);
} }
} }
} }

View File

@ -179,4 +179,8 @@ public class FollowedUsersListingFragment extends Fragment implements FragmentCo
mLinearLayoutManager.scrollToPositionWithOffset(0, 0); 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 @Override
public void onAttach(@NonNull Context context) { public void onAttach(@NonNull Context context) {
super.onAttach(context); super.onAttach(context);

View File

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

View File

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

View File

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

View File

@ -19,18 +19,12 @@ public interface SubscribedSubredditDao {
@Query("DELETE FROM subscribed_subreddits") @Query("DELETE FROM subscribed_subreddits")
void deleteAllSubscribedSubreddits(); 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") @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); LiveData<List<SubscribedSubredditData>> getAllSubscribedSubredditsWithSearchQuery(String accountName, String searchQuery);
@Query("SELECT * from subscribed_subreddits WHERE username = :accountName COLLATE NOCASE ORDER BY name COLLATE NOCASE ASC") @Query("SELECT * from subscribed_subreddits WHERE username = :accountName COLLATE NOCASE ORDER BY name COLLATE NOCASE ASC")
List<SubscribedSubredditData> getAllSubscribedSubredditsList(String accountName); 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") @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); LiveData<List<SubscribedSubredditData>> getAllFavoriteSubscribedSubredditsWithSearchQuery(String accountName, String searchQuery);

View File

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

View File

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

View File

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

View File

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