From b86e5284a21271e2cd791538e05b3a353e28d01a Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 25 Feb 2021 23:34:26 +0800 Subject: Multi-selection in subreddit search result for CustomizePostFilterActivity. --- .../activities/SearchActivity.java | 15 ++- .../activities/SearchSubredditsResultActivity.java | 29 ++++++ .../SubredditMultiselectionActivity.java | 6 +- .../SubredditListingRecyclerViewAdapter.java | 108 +++++++++++++-------- .../fragments/SubredditListingFragment.java | 28 +++++- .../infinityforreddit/subreddit/SubredditData.java | 12 +++ 6 files changed, 148 insertions(+), 50 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchActivity.java index 9dd2a891..903f2180 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchActivity.java @@ -53,8 +53,10 @@ public class SearchActivity extends BaseActivity { static final String EXTRA_SEARCH_ONLY_USERS = "ESOU"; static final String EXTRA_RETURN_SUBREDDIT_NAME = "ERSN"; static final String EXTRA_RETURN_SUBREDDIT_ICON_URL = "ERSIU"; + static final String RETURN_EXTRA_SELECTED_SUBREDDIT_NAMES = "RESSN"; static final String EXTRA_RETURN_USER_NAME = "ERUN"; static final String EXTRA_RETURN_USER_ICON_URL = "ERUIU"; + static final String EXTRA_IS_MULTI_SELECTION = "EIMS"; private static final String SUBREDDIT_NAME_STATE = "SNS"; private static final String SUBREDDIT_IS_USER_STATE = "SIUS"; @@ -249,6 +251,7 @@ public class SearchActivity extends BaseActivity { if (searchOnlySubreddits) { Intent intent = new Intent(SearchActivity.this, SearchSubredditsResultActivity.class); intent.putExtra(SearchSubredditsResultActivity.EXTRA_QUERY, query); + intent.putExtra(SearchSubredditsResultActivity.EXTRA_IS_MULTI_SELECTION, getIntent().getBooleanExtra(EXTRA_IS_MULTI_SELECTION, false)); startActivityForResult(intent, SUBREDDIT_SEARCH_REQUEST_CODE); } else if (searchOnlyUsers) { Intent intent = new Intent(this, SearchUsersResultActivity.class); @@ -340,11 +343,15 @@ public class SearchActivity extends BaseActivity { subredditNameTextView.setText(subredditName); } } else if (requestCode == SUBREDDIT_SEARCH_REQUEST_CODE) { - String name = data.getStringExtra(SearchSubredditsResultActivity.EXTRA_RETURN_SUBREDDIT_NAME); - String iconUrl = data.getStringExtra(SearchSubredditsResultActivity.EXTRA_RETURN_SUBREDDIT_ICON_URL); Intent returnIntent = new Intent(); - returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_NAME, name); - returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_ICON_URL, iconUrl); + if (getIntent().getBooleanExtra(EXTRA_IS_MULTI_SELECTION, false)) { + returnIntent.putStringArrayListExtra(RETURN_EXTRA_SELECTED_SUBREDDIT_NAMES, data.getStringArrayListExtra(SearchSubredditsResultActivity.RETURN_EXTRA_SELECTED_SUBREDDIT_NAMES)); + } else { + String name = data.getStringExtra(SearchSubredditsResultActivity.EXTRA_RETURN_SUBREDDIT_NAME); + String iconUrl = data.getStringExtra(SearchSubredditsResultActivity.EXTRA_RETURN_SUBREDDIT_ICON_URL); + returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_NAME, name); + returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_ICON_URL, iconUrl); + } setResult(Activity.RESULT_OK, returnIntent); finish(); } else if (requestCode == USER_SEARCH_REQUEST_CODE) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchSubredditsResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchSubredditsResultActivity.java index b6882196..597c9ca2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchSubredditsResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchSubredditsResultActivity.java @@ -5,6 +5,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; +import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.Window; @@ -21,6 +22,9 @@ import com.r0adkll.slidr.Slidr; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; +import java.util.ArrayList; +import java.util.List; + import javax.inject.Inject; import javax.inject.Named; @@ -32,6 +36,7 @@ import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.fragments.SubredditListingFragment; +import ml.docilealligator.infinityforreddit.subreddit.SubredditData; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; public class SearchSubredditsResultActivity extends BaseActivity implements ActivityToolbarInterface { @@ -39,6 +44,8 @@ public class SearchSubredditsResultActivity extends BaseActivity implements Acti static final String EXTRA_QUERY = "EQ"; static final String EXTRA_RETURN_SUBREDDIT_NAME = "ERSN"; static final String EXTRA_RETURN_SUBREDDIT_ICON_URL = "ERSIU"; + static final String EXTRA_IS_MULTI_SELECTION = "EIMS"; + static final String RETURN_EXTRA_SELECTED_SUBREDDIT_NAMES = "RESS"; private static final String FRAGMENT_OUT_STATE = "FOS"; @@ -113,6 +120,7 @@ public class SearchSubredditsResultActivity extends BaseActivity implements Acti bundle.putBoolean(SubredditListingFragment.EXTRA_IS_GETTING_SUBREDDIT_INFO, true); bundle.putString(SubredditListingFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(SubredditListingFragment.EXTRA_ACCOUNT_NAME, mAccountName); + bundle.putBoolean(SubredditListingFragment.EXTRA_IS_MULTI_SELECTION, getIntent().getBooleanExtra(EXTRA_IS_MULTI_SELECTION, false)); mFragment.setArguments(bundle); getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_search_subreddits_result_activity, mFragment).commit(); } else { @@ -145,11 +153,32 @@ public class SearchSubredditsResultActivity extends BaseActivity implements Acti finish(); } + @Override + public boolean onCreateOptionsMenu(Menu menu) { + if (getIntent().getBooleanExtra(EXTRA_IS_MULTI_SELECTION, false)) { + getMenuInflater().inflate(R.menu.search_subreddits_result_activity, menu); + applyMenuItemTheme(menu); + } + return true; + } + @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { if (item.getItemId() == android.R.id.home) { finish(); return true; + } else if (item.getItemId() == R.id.action_save_search_subreddits_result_activity) { + if (mFragment != null) { + List subreddits = ((SubredditListingFragment) mFragment).getSelectedSubreddits(); + ArrayList subredditNames = new ArrayList<>(); + for (SubredditData s : subreddits) { + subredditNames.add(s.getName()); + } + Intent returnIntent = new Intent(); + returnIntent.putStringArrayListExtra(RETURN_EXTRA_SELECTED_SUBREDDIT_NAMES, subredditNames); + setResult(Activity.RESULT_OK, returnIntent); + finish(); + } } return false; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditMultiselectionActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditMultiselectionActivity.java index 23efef58..69b83579 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditMultiselectionActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditMultiselectionActivity.java @@ -191,6 +191,7 @@ public class SubredditMultiselectionActivity extends BaseActivity implements Act } else if (itemId == R.id.action_search_subreddit_multiselection_activity) { Intent intent = new Intent(this, SearchActivity.class); intent.putExtra(SearchActivity.EXTRA_SEARCH_ONLY_SUBREDDITS, true); + intent.putExtra(SearchActivity.EXTRA_IS_MULTI_SELECTION, true); startActivityForResult(intent, SUBREDDIT_SEARCH_REQUEST_CODE); } @@ -203,7 +204,10 @@ public class SubredditMultiselectionActivity extends BaseActivity implements Act if (requestCode == SUBREDDIT_SEARCH_REQUEST_CODE && resultCode == RESULT_OK && data != null && mAdapter != null) { Intent returnIntent = new Intent(); ArrayList selectedSubreddits = mAdapter.getAllSelectedSubreddits(); - selectedSubreddits.add(data.getStringExtra(SearchActivity.EXTRA_RETURN_SUBREDDIT_NAME)); + ArrayList searchedSubreddits = data.getStringArrayListExtra(SearchActivity.RETURN_EXTRA_SELECTED_SUBREDDIT_NAMES); + if (searchedSubreddits != null) { + selectedSubreddits.addAll(searchedSubreddits); + } returnIntent.putStringArrayListExtra(EXTRA_RETURN_SELECTED_SUBREDDITS, selectedSubreddits); setResult(RESULT_OK, returnIntent); finish(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditListingRecyclerViewAdapter.java index 1d34c29d..69fd6091 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditListingRecyclerViewAdapter.java @@ -22,17 +22,18 @@ import androidx.recyclerview.widget.RecyclerView; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; import com.bumptech.glide.request.RequestOptions; +import com.google.android.material.checkbox.MaterialCheckBox; import java.util.concurrent.Executor; import butterknife.BindView; import butterknife.ButterKnife; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; -import ml.docilealligator.infinityforreddit.asynctasks.CheckIsSubscribedToSubreddit; -import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.asynctasks.CheckIsSubscribedToSubreddit; +import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.subreddit.SubredditData; import ml.docilealligator.infinityforreddit.subreddit.SubredditSubscription; import pl.droidsonroids.gif.GifImageView; @@ -61,6 +62,7 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter - callback.subredditSelected(subredditData.getName(), subredditData.getIconUrl())); + if (subredditData != null) { + if (isMultiSelection) { + ((DataViewHolder) holder).checkBox.setOnCheckedChangeListener((compoundButton, b) -> subredditData.setSelected(b)); + } + ((DataViewHolder) holder).constraintLayout.setOnClickListener(view -> { + if (isMultiSelection) { + ((DataViewHolder) holder).checkBox.performClick(); + } else { + callback.subredditSelected(subredditData.getName(), subredditData.getIconUrl()); + } + }); - if (!subredditData.getIconUrl().equals("")) { - glide.load(subredditData.getIconUrl()) - .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) - .error(glide.load(R.drawable.subreddit_default_icon) - .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) - .into(((DataViewHolder) holder).iconGifImageView); - } else { - glide.load(R.drawable.subreddit_default_icon) - .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) - .into(((DataViewHolder) holder).iconGifImageView); - } + if (!subredditData.getIconUrl().equals("")) { + glide.load(subredditData.getIconUrl()) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .error(glide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) + .into(((DataViewHolder) holder).iconGifImageView); + } else { + glide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .into(((DataViewHolder) holder).iconGifImageView); + } - ((DataViewHolder) holder).subredditNameTextView.setText(subredditData.getName()); + ((DataViewHolder) holder).subredditNameTextView.setText(subredditData.getName()); - CheckIsSubscribedToSubreddit.checkIsSubscribedToSubreddit(executor, new Handler(), - redditDataRoomDatabase, subredditData.getName(), accountName, - new CheckIsSubscribedToSubreddit.CheckIsSubscribedToSubredditListener() { - @Override - public void isSubscribed() { - ((DataViewHolder) holder).subscribeButton.setVisibility(View.GONE); - } + if (!isMultiSelection) { + CheckIsSubscribedToSubreddit.checkIsSubscribedToSubreddit(executor, new Handler(), + redditDataRoomDatabase, subredditData.getName(), accountName, + new CheckIsSubscribedToSubreddit.CheckIsSubscribedToSubredditListener() { + @Override + public void isSubscribed() { + ((DataViewHolder) holder).subscribeButton.setVisibility(View.GONE); + } - @Override - public void isNotSubscribed() { - ((DataViewHolder) holder).subscribeButton.setVisibility(View.VISIBLE); - ((DataViewHolder) holder).subscribeButton.setOnClickListener(view -> { - SubredditSubscription.subscribeToSubreddit(oauthRetrofit, retrofit, - accessToken, subredditData.getName(), accountName, redditDataRoomDatabase, - new SubredditSubscription.SubredditSubscriptionListener() { - @Override - public void onSubredditSubscriptionSuccess() { - ((DataViewHolder) holder).subscribeButton.setVisibility(View.GONE); - Toast.makeText(context, R.string.subscribed, Toast.LENGTH_SHORT).show(); - } + @Override + public void isNotSubscribed() { + ((DataViewHolder) holder).subscribeButton.setVisibility(View.VISIBLE); + ((DataViewHolder) holder).subscribeButton.setOnClickListener(view -> { + SubredditSubscription.subscribeToSubreddit(oauthRetrofit, retrofit, + accessToken, subredditData.getName(), accountName, redditDataRoomDatabase, + new SubredditSubscription.SubredditSubscriptionListener() { + @Override + public void onSubredditSubscriptionSuccess() { + ((DataViewHolder) holder).subscribeButton.setVisibility(View.GONE); + Toast.makeText(context, R.string.subscribed, Toast.LENGTH_SHORT).show(); + } - @Override - public void onSubredditSubscriptionFail() { - Toast.makeText(context, R.string.subscribe_failed, Toast.LENGTH_SHORT).show(); - } - }); + @Override + public void onSubredditSubscriptionFail() { + Toast.makeText(context, R.string.subscribe_failed, Toast.LENGTH_SHORT).show(); + } + }); + }); + } }); - } - }); + } else { + ((DataViewHolder) holder).checkBox.setChecked(subredditData.isSelected()); + } + } } } @@ -227,12 +244,17 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter getSelectedSubreddits() { + if (mSubredditListingViewModel != null) { + List allSubreddits = mSubredditListingViewModel.getSubreddits().getValue(); + if (allSubreddits == null) { + return null; + } + + List selectedSubreddits = new ArrayList<>(); + for (SubredditData s : allSubreddits) { + if (s.isSelected()) { + selectedSubreddits.add(s); + } + } + return selectedSubreddits; + } + + return null; + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditData.java index 21d5d130..9bdada34 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditData.java @@ -3,6 +3,7 @@ package ml.docilealligator.infinityforreddit.subreddit; import androidx.annotation.NonNull; import androidx.room.ColumnInfo; import androidx.room.Entity; +import androidx.room.Ignore; import androidx.room.PrimaryKey; @Entity(tableName = "subreddits") @@ -29,6 +30,8 @@ public class SubredditData { private String suggestedCommentSort; @ColumnInfo(name = "over18") private boolean isNSFW; + @Ignore + private boolean isSelected; public SubredditData(@NonNull String id, String name, String iconUrl, String bannerUrl, String description, String sidebarDescription, int nSubscribers, long createdUTC, @@ -43,6 +46,7 @@ public class SubredditData { this.createdUTC = createdUTC; this.suggestedCommentSort = suggestedCommentSort; this.isNSFW = isNSFW; + this.isSelected = false; } @NonNull @@ -85,4 +89,12 @@ public class SubredditData { public boolean isNSFW() { return isNSFW; } + + public boolean isSelected() { + return isSelected; + } + + public void setSelected(boolean selected) { + isSelected = selected; + } } -- cgit v1.2.3