From 053773741e08f4fd0bd8ff006af552b3f6754d92 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 20 Dec 2021 20:25:31 +0800 Subject: [PATCH] Search subscribed subreddits. --- .../SubscribedThingListingActivity.java | 88 ++++++++++++++++++- .../SubscribedSubredditsListingFragment.java | 4 + .../SubscribedSubredditDao.java | 6 ++ .../SubscribedSubredditRepository.java | 10 ++- .../SubscribedSubredditViewModel.java | 14 ++- .../activity_subscribed_thing_listing.xml | 16 +++- .../subscribed_thing_listing_activity.xml | 10 +++ 7 files changed, 140 insertions(+), 8 deletions(-) create mode 100644 app/src/main/res/menu/subscribed_thing_listing_activity.xml 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 dde51105..1365603c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubscribedThingListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubscribedThingListingActivity.java @@ -1,15 +1,21 @@ package ml.docilealligator.infinityforreddit.activities; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; import android.os.Handler; +import android.text.Editable; +import android.text.TextWatcher; +import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; +import android.view.inputmethod.InputMethodManager; +import android.widget.EditText; import android.widget.Toast; import androidx.annotation.NonNull; @@ -79,6 +85,8 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti CollapsingToolbarLayout collapsingToolbarLayout; @BindView(R.id.toolbar_subscribed_thing_listing_activity) Toolbar toolbar; + @BindView(R.id.search_edit_text_subscribed_thing_listing_activity) + EditText searchEditText; @BindView(R.id.tab_layout_subscribed_thing_listing_activity) TabLayout tabLayout; @BindView(R.id.view_pager_subscribed_thing_listing_activity) @@ -107,6 +115,7 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti private boolean mInsertMultiredditSuccess = false; private boolean showMultiReddits = false; private SectionsPagerAdapter sectionsPagerAdapter; + private Menu mMenu; @Override protected void onCreate(Bundle savedInstanceState) { @@ -165,6 +174,23 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti } else { showMultiReddits = getIntent().getBooleanExtra(EXTRA_SHOW_MULTIREDDITS, false); } + + searchEditText.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { + + } + + @Override + public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { + + } + + @Override + public void afterTextChanged(Editable editable) { + sectionsPagerAdapter.changeSearchQuery(editable.toString()); + } + }); initializeViewPagerAndLoadSubscriptions(); } @@ -184,6 +210,8 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti applyAppBarLayoutAndCollapsingToolbarLayoutAndToolbarTheme(appBarLayout, collapsingToolbarLayout, toolbar); applyTabLayoutTheme(tabLayout); applyFABTheme(fab); + searchEditText.setTextColor(mCustomThemeWrapper.getToolbarPrimaryTextAndIconColor()); + searchEditText.setHintTextColor(mCustomThemeWrapper.getToolbarSecondaryTextColor()); } private void initializeViewPagerAndLoadSubscriptions() { @@ -222,9 +250,38 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti loadSubscriptions(false); } + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.subscribed_thing_listing_activity, menu); + mMenu = menu; + applyMenuItemTheme(menu); + return true; + } + @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { - if (item.getItemId() == android.R.id.home) { + if (item.getItemId() == R.id.action_search_subscribed_thing_listing_activity) { + item.setVisible(false); + searchEditText.setVisibility(View.VISIBLE); + searchEditText.requestFocus(); + if (searchEditText.requestFocus()) { + InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); + imm.showSoftInput(searchEditText, InputMethodManager.SHOW_IMPLICIT); + } + return true; + } else if (item.getItemId() == android.R.id.home) { + if (searchEditText.getVisibility() == View.VISIBLE) { + View view = this.getCurrentFocus(); + if (view != null) { + InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); + imm.hideSoftInputFromWindow(view.getWindowToken(), 0); + } + searchEditText.setVisibility(View.GONE); + searchEditText.setText(""); + mMenu.findItem(R.id.action_search_subscribed_thing_listing_activity).setVisible(true); + sectionsPagerAdapter.changeSearchQuery(""); + return true; + } finish(); return true; } @@ -232,6 +289,23 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti return false; } + @Override + public void onBackPressed() { + if (searchEditText.getVisibility() == View.VISIBLE) { + View view = this.getCurrentFocus(); + if (view != null) { + InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); + imm.hideSoftInputFromWindow(view.getWindowToken(), 0); + } + searchEditText.setVisibility(View.GONE); + searchEditText.setText(""); + mMenu.findItem(R.id.action_search_subscribed_thing_listing_activity).setVisible(true); + sectionsPagerAdapter.changeSearchQuery(""); + } else { + super.onBackPressed(); + } + } + @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); @@ -483,5 +557,17 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti multiRedditListingFragment.goBackToTop(); } } + + void changeSearchQuery(String searchQuery) { + if (subscribedSubredditsListingFragment != null) { + subscribedSubredditsListingFragment.changeSearchQuery(searchQuery); + } + if (followedUsersListingFragment != null) { + + } + if (multiRedditListingFragment != null) { + + } + } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SubscribedSubredditsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SubscribedSubredditsListingFragment.java index 11d902fb..755f4833 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SubscribedSubredditsListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SubscribedSubredditsListingFragment.java @@ -198,4 +198,8 @@ public class SubscribedSubredditsListingFragment extends Fragment implements Fra mLinearLayoutManager.scrollToPositionWithOffset(0, 0); } } + + public void changeSearchQuery(String searchQuery) { + mSubscribedSubredditViewModel.setSearchQuery(searchQuery); + } } 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 09d866ec..edcfa4cc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subscribedsubreddit/SubscribedSubredditDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subscribedsubreddit/SubscribedSubredditDao.java @@ -22,12 +22,18 @@ public interface SubscribedSubredditDao { @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); + @Query("SELECT * from subscribed_subreddits WHERE name = :subredditName COLLATE NOCASE AND username = :accountName COLLATE NOCASE LIMIT 1") SubscribedSubredditData getSubscribedSubreddit(String subredditName, String accountName); 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 053ae1ed..0901da55 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subscribedsubreddit/SubscribedSubredditRepository.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subscribedsubreddit/SubscribedSubredditRepository.java @@ -12,19 +12,21 @@ 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> getAllSubscribedSubreddits() { - return mAllSubscribedSubreddits; + LiveData> getAllSubscribedSubredditsWithSearchQuery(String searchQuery) { + return mSubscribedSubredditDao.getAllSubscribedSubredditsWithSearchQuery(mAccountName, searchQuery); } - public LiveData> getAllFavoriteSubscribedSubreddits() { - return mAllFavoriteSubscribedSubreddits; + public LiveData> getAllFavoriteSubscribedSubredditsWithSearchQuery(String searchQuery) { + return mSubscribedSubredditDao.getAllFavoriteSubscribedSubredditsWithSearchQuery(mAccountName, searchQuery); } public void insert(SubscribedSubredditData subscribedSubredditData) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/subscribedsubreddit/SubscribedSubredditViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/subscribedsubreddit/SubscribedSubredditViewModel.java index fa38fcac..6191b680 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subscribedsubreddit/SubscribedSubredditViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subscribedsubreddit/SubscribedSubredditViewModel.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 SubscribedSubredditViewModel extends AndroidViewModel { private SubscribedSubredditRepository mSubscribedSubredditRepository; private LiveData> mAllSubscribedSubreddits; private LiveData> mAllFavoriteSubscribedSubreddits; + private MutableLiveData searchQueryLiveData; public SubscribedSubredditViewModel(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String accountName) { super(application); mSubscribedSubredditRepository = new SubscribedSubredditRepository(redditDataRoomDatabase, accountName); - mAllSubscribedSubreddits = mSubscribedSubredditRepository.getAllSubscribedSubreddits(); - mAllFavoriteSubscribedSubreddits = mSubscribedSubredditRepository.getAllFavoriteSubscribedSubreddits(); + searchQueryLiveData = new MutableLiveData<>(); + searchQueryLiveData.postValue(""); + + mAllSubscribedSubreddits = Transformations.switchMap(searchQueryLiveData, searchQuery -> mSubscribedSubredditRepository.getAllSubscribedSubredditsWithSearchQuery(searchQuery)); + mAllFavoriteSubscribedSubreddits = Transformations.switchMap(searchQueryLiveData, searchQuery -> mSubscribedSubredditRepository.getAllFavoriteSubscribedSubredditsWithSearchQuery(searchQuery)); } public LiveData> getAllSubscribedSubreddits() { @@ -36,6 +42,10 @@ public class SubscribedSubredditViewModel extends AndroidViewModel { mSubscribedSubredditRepository.insert(subscribedSubredditData); } + 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/res/layout/activity_subscribed_thing_listing.xml b/app/src/main/res/layout/activity_subscribed_thing_listing.xml index 10037a07..15e8fd7b 100644 --- a/app/src/main/res/layout/activity_subscribed_thing_listing.xml +++ b/app/src/main/res/layout/activity_subscribed_thing_listing.xml @@ -29,7 +29,21 @@ app:layout_collapseMode="pin" app:layout_scrollFlags="scroll|enterAlways" app:popupTheme="@style/AppTheme.PopupOverlay" - app:navigationIcon="?attr/homeAsUpIndicator" /> + app:navigationIcon="?attr/homeAsUpIndicator"> + + + + diff --git a/app/src/main/res/menu/subscribed_thing_listing_activity.xml b/app/src/main/res/menu/subscribed_thing_listing_activity.xml new file mode 100644 index 00000000..83e6d9a5 --- /dev/null +++ b/app/src/main/res/menu/subscribed_thing_listing_activity.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file