diff options
author | Alex Ning <chineseperson5@gmail.com> | 2021-12-20 12:25:31 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2021-12-20 12:25:31 +0000 |
commit | 053773741e08f4fd0bd8ff006af552b3f6754d92 (patch) | |
tree | 1cd7ab80bb52dcc00c9148a0cfab54b9740d9902 /app/src/main/java | |
parent | f8a5dc9d6100175dec6ea6535152a28a3b685cea (diff) | |
download | infinity-for-reddit-053773741e08f4fd0bd8ff006af552b3f6754d92.tar infinity-for-reddit-053773741e08f4fd0bd8ff006af552b3f6754d92.tar.gz infinity-for-reddit-053773741e08f4fd0bd8ff006af552b3f6754d92.tar.bz2 infinity-for-reddit-053773741e08f4fd0bd8ff006af552b3f6754d92.tar.lz infinity-for-reddit-053773741e08f4fd0bd8ff006af552b3f6754d92.tar.xz infinity-for-reddit-053773741e08f4fd0bd8ff006af552b3f6754d92.tar.zst infinity-for-reddit-053773741e08f4fd0bd8ff006af552b3f6754d92.zip |
Search subscribed subreddits.
Diffstat (limited to '')
5 files changed, 115 insertions, 7 deletions
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() { @@ -223,8 +251,37 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti } @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; } @@ -233,6 +290,23 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti } @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); outState.putBoolean(INSERT_SUBSCRIBED_SUBREDDIT_STATE, mInsertSuccess); @@ -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<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); + @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<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>> getAllSubscribedSubreddits() { - return mAllSubscribedSubreddits; + LiveData<List<SubscribedSubredditData>> getAllSubscribedSubredditsWithSearchQuery(String searchQuery) { + return mSubscribedSubredditDao.getAllSubscribedSubredditsWithSearchQuery(mAccountName, searchQuery); } - public LiveData<List<SubscribedSubredditData>> getAllFavoriteSubscribedSubreddits() { - return mAllFavoriteSubscribedSubreddits; + public LiveData<List<SubscribedSubredditData>> 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<List<SubscribedSubredditData>> mAllSubscribedSubreddits; private LiveData<List<SubscribedSubredditData>> mAllFavoriteSubscribedSubreddits; + private MutableLiveData<String> 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<List<SubscribedSubredditData>> 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; |