Search subscribed subreddits.

This commit is contained in:
Alex Ning 2021-12-20 20:25:31 +08:00
parent f8a5dc9d61
commit 053773741e
7 changed files with 140 additions and 8 deletions

View File

@ -1,15 +1,21 @@
package ml.docilealligator.infinityforreddit.activities; package ml.docilealligator.infinityforreddit.activities;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.Window; import android.view.Window;
import android.view.WindowManager; import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -79,6 +85,8 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti
CollapsingToolbarLayout collapsingToolbarLayout; CollapsingToolbarLayout collapsingToolbarLayout;
@BindView(R.id.toolbar_subscribed_thing_listing_activity) @BindView(R.id.toolbar_subscribed_thing_listing_activity)
Toolbar toolbar; Toolbar toolbar;
@BindView(R.id.search_edit_text_subscribed_thing_listing_activity)
EditText searchEditText;
@BindView(R.id.tab_layout_subscribed_thing_listing_activity) @BindView(R.id.tab_layout_subscribed_thing_listing_activity)
TabLayout tabLayout; TabLayout tabLayout;
@BindView(R.id.view_pager_subscribed_thing_listing_activity) @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 mInsertMultiredditSuccess = false;
private boolean showMultiReddits = false; private boolean showMultiReddits = false;
private SectionsPagerAdapter sectionsPagerAdapter; private SectionsPagerAdapter sectionsPagerAdapter;
private Menu mMenu;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -165,6 +174,23 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti
} else { } else {
showMultiReddits = getIntent().getBooleanExtra(EXTRA_SHOW_MULTIREDDITS, false); 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(); initializeViewPagerAndLoadSubscriptions();
} }
@ -184,6 +210,8 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti
applyAppBarLayoutAndCollapsingToolbarLayoutAndToolbarTheme(appBarLayout, collapsingToolbarLayout, toolbar); applyAppBarLayoutAndCollapsingToolbarLayoutAndToolbarTheme(appBarLayout, collapsingToolbarLayout, toolbar);
applyTabLayoutTheme(tabLayout); applyTabLayoutTheme(tabLayout);
applyFABTheme(fab); applyFABTheme(fab);
searchEditText.setTextColor(mCustomThemeWrapper.getToolbarPrimaryTextAndIconColor());
searchEditText.setHintTextColor(mCustomThemeWrapper.getToolbarSecondaryTextColor());
} }
private void initializeViewPagerAndLoadSubscriptions() { private void initializeViewPagerAndLoadSubscriptions() {
@ -222,9 +250,38 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti
loadSubscriptions(false); loadSubscriptions(false);
} }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.subscribed_thing_listing_activity, menu);
mMenu = menu;
applyMenuItemTheme(menu);
return true;
}
@Override @Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) { 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(); finish();
return true; return true;
} }
@ -232,6 +289,23 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti
return false; 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 @Override
protected void onSaveInstanceState(@NonNull Bundle outState) { protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
@ -483,5 +557,17 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti
multiRedditListingFragment.goBackToTop(); multiRedditListingFragment.goBackToTop();
} }
} }
void changeSearchQuery(String searchQuery) {
if (subscribedSubredditsListingFragment != null) {
subscribedSubredditsListingFragment.changeSearchQuery(searchQuery);
}
if (followedUsersListingFragment != null) {
}
if (multiRedditListingFragment != null) {
}
}
} }
} }

View File

@ -198,4 +198,8 @@ public class SubscribedSubredditsListingFragment extends Fragment implements Fra
mLinearLayoutManager.scrollToPositionWithOffset(0, 0); mLinearLayoutManager.scrollToPositionWithOffset(0, 0);
} }
} }
public void changeSearchQuery(String searchQuery) {
mSubscribedSubredditViewModel.setSearchQuery(searchQuery);
}
} }

View File

@ -22,12 +22,18 @@ public interface SubscribedSubredditDao {
@Query("SELECT * from subscribed_subreddits WHERE username = :accountName ORDER BY name COLLATE NOCASE ASC") @Query("SELECT * from subscribed_subreddits WHERE username = :accountName ORDER BY name COLLATE NOCASE ASC")
LiveData<List<SubscribedSubredditData>> getAllSubscribedSubreddits(String accountName); 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") @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") @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); 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") @Query("SELECT * from subscribed_subreddits WHERE name = :subredditName COLLATE NOCASE AND username = :accountName COLLATE NOCASE LIMIT 1")
SubscribedSubredditData getSubscribedSubreddit(String subredditName, String accountName); SubscribedSubredditData getSubscribedSubreddit(String subredditName, String accountName);

View File

@ -12,19 +12,21 @@ public class SubscribedSubredditRepository {
private SubscribedSubredditDao mSubscribedSubredditDao; private SubscribedSubredditDao mSubscribedSubredditDao;
private LiveData<List<SubscribedSubredditData>> mAllSubscribedSubreddits; private LiveData<List<SubscribedSubredditData>> mAllSubscribedSubreddits;
private LiveData<List<SubscribedSubredditData>> mAllFavoriteSubscribedSubreddits; private LiveData<List<SubscribedSubredditData>> mAllFavoriteSubscribedSubreddits;
private String mAccountName;
SubscribedSubredditRepository(RedditDataRoomDatabase redditDataRoomDatabase, String accountName) { SubscribedSubredditRepository(RedditDataRoomDatabase redditDataRoomDatabase, String accountName) {
mAccountName = accountName;
mSubscribedSubredditDao = redditDataRoomDatabase.subscribedSubredditDao(); mSubscribedSubredditDao = redditDataRoomDatabase.subscribedSubredditDao();
mAllSubscribedSubreddits = mSubscribedSubredditDao.getAllSubscribedSubreddits(accountName); mAllSubscribedSubreddits = mSubscribedSubredditDao.getAllSubscribedSubreddits(accountName);
mAllFavoriteSubscribedSubreddits = mSubscribedSubredditDao.getAllFavoriteSubscribedSubreddits(accountName); mAllFavoriteSubscribedSubreddits = mSubscribedSubredditDao.getAllFavoriteSubscribedSubreddits(accountName);
} }
LiveData<List<SubscribedSubredditData>> getAllSubscribedSubreddits() { LiveData<List<SubscribedSubredditData>> getAllSubscribedSubredditsWithSearchQuery(String searchQuery) {
return mAllSubscribedSubreddits; return mSubscribedSubredditDao.getAllSubscribedSubredditsWithSearchQuery(mAccountName, searchQuery);
} }
public LiveData<List<SubscribedSubredditData>> getAllFavoriteSubscribedSubreddits() { public LiveData<List<SubscribedSubredditData>> getAllFavoriteSubscribedSubredditsWithSearchQuery(String searchQuery) {
return mAllFavoriteSubscribedSubreddits; return mSubscribedSubredditDao.getAllFavoriteSubscribedSubredditsWithSearchQuery(mAccountName, searchQuery);
} }
public void insert(SubscribedSubredditData subscribedSubredditData) { public void insert(SubscribedSubredditData subscribedSubredditData) {

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 SubscribedSubredditViewModel extends AndroidViewModel {
private SubscribedSubredditRepository mSubscribedSubredditRepository; private SubscribedSubredditRepository mSubscribedSubredditRepository;
private LiveData<List<SubscribedSubredditData>> mAllSubscribedSubreddits; private LiveData<List<SubscribedSubredditData>> mAllSubscribedSubreddits;
private LiveData<List<SubscribedSubredditData>> mAllFavoriteSubscribedSubreddits; private LiveData<List<SubscribedSubredditData>> mAllFavoriteSubscribedSubreddits;
private MutableLiveData<String> searchQueryLiveData;
public SubscribedSubredditViewModel(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String accountName) { public SubscribedSubredditViewModel(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String accountName) {
super(application); super(application);
mSubscribedSubredditRepository = new SubscribedSubredditRepository(redditDataRoomDatabase, accountName); mSubscribedSubredditRepository = new SubscribedSubredditRepository(redditDataRoomDatabase, accountName);
mAllSubscribedSubreddits = mSubscribedSubredditRepository.getAllSubscribedSubreddits(); searchQueryLiveData = new MutableLiveData<>();
mAllFavoriteSubscribedSubreddits = mSubscribedSubredditRepository.getAllFavoriteSubscribedSubreddits(); searchQueryLiveData.postValue("");
mAllSubscribedSubreddits = Transformations.switchMap(searchQueryLiveData, searchQuery -> mSubscribedSubredditRepository.getAllSubscribedSubredditsWithSearchQuery(searchQuery));
mAllFavoriteSubscribedSubreddits = Transformations.switchMap(searchQueryLiveData, searchQuery -> mSubscribedSubredditRepository.getAllFavoriteSubscribedSubredditsWithSearchQuery(searchQuery));
} }
public LiveData<List<SubscribedSubredditData>> getAllSubscribedSubreddits() { public LiveData<List<SubscribedSubredditData>> getAllSubscribedSubreddits() {
@ -36,6 +42,10 @@ public class SubscribedSubredditViewModel extends AndroidViewModel {
mSubscribedSubredditRepository.insert(subscribedSubredditData); mSubscribedSubredditRepository.insert(subscribedSubredditData);
} }
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

@ -29,7 +29,21 @@
app:layout_collapseMode="pin" app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways" app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay" app:popupTheme="@style/AppTheme.PopupOverlay"
app:navigationIcon="?attr/homeAsUpIndicator" /> app:navigationIcon="?attr/homeAsUpIndicator">
<EditText
android:id="@+id/search_edit_text_subscribed_thing_listing_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
android:gravity="top"
android:hint="@string/search"
android:maxLines="1"
android:textSize="?attr/font_20"
android:fontFamily="?attr/font_family"
android:visibility="gone" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout> </com.google.android.material.appbar.CollapsingToolbarLayout>

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search_subscribed_thing_listing_activity"
android:orderInCategory="1"
android:title="@string/action_search"
android:icon="@drawable/ic_search_toolbar_24dp"
app:showAsAction="ifRoom" />
</menu>