Multi-selection in subreddit search result for CustomizePostFilterActivity.

This commit is contained in:
Alex Ning 2021-02-25 23:34:26 +08:00
parent abb37707fb
commit b86e5284a2
8 changed files with 166 additions and 50 deletions

View File

@ -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) {

View File

@ -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<SubredditData> subreddits = ((SubredditListingFragment) mFragment).getSelectedSubreddits();
ArrayList<String> 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;
}

View File

@ -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<String> selectedSubreddits = mAdapter.getAllSelectedSubreddits();
selectedSubreddits.add(data.getStringExtra(SearchActivity.EXTRA_RETURN_SUBREDDIT_NAME));
ArrayList<String> 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();

View File

@ -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<Subred
private String accessToken;
private String accountName;
private RedditDataRoomDatabase redditDataRoomDatabase;
private boolean isMultiSelection;
private int colorPrimaryLightTheme;
private int primaryTextColor;
private int secondaryTextColor;
@ -75,7 +77,7 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter<Subred
CustomThemeWrapper customThemeWrapper,
String accessToken, String accountName,
RedditDataRoomDatabase redditDataRoomDatabase,
Callback callback) {
boolean isMultiSelection, Callback callback) {
super(DIFF_CALLBACK);
this.context = context;
this.executor = executor;
@ -84,6 +86,7 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter<Subred
this.accessToken = accessToken;
this.accountName = accountName;
this.redditDataRoomDatabase = redditDataRoomDatabase;
this.isMultiSelection = isMultiSelection;
this.callback = callback;
glide = Glide.with(context);
colorPrimaryLightTheme = customThemeWrapper.getColorPrimaryLightTheme();
@ -113,52 +116,66 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter<Subred
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder instanceof DataViewHolder) {
SubredditData subredditData = getItem(position);
((DataViewHolder) holder).constraintLayout.setOnClickListener(view ->
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<Subred
TextView subredditNameTextView;
@BindView(R.id.subscribe_image_view_item_subreddit_listing)
ImageView subscribeButton;
@BindView(R.id.checkbox__item_subreddit_listing)
MaterialCheckBox checkBox;
DataViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
subredditNameTextView.setTextColor(primaryTextColor);
subscribeButton.setColorFilter(unsubscribed, android.graphics.PorterDuff.Mode.SRC_IN);
if (isMultiSelection) {
checkBox.setVisibility(View.VISIBLE);
}
}
}

View File

@ -25,6 +25,8 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import javax.inject.Inject;
@ -35,8 +37,8 @@ import butterknife.ButterKnife;
import ml.docilealligator.infinityforreddit.FragmentCommunicator;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.NetworkState;
import ml.docilealligator.infinityforreddit.RecyclerViewContentScrollingInterface;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RecyclerViewContentScrollingInterface;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.SortType;
import ml.docilealligator.infinityforreddit.activities.BaseActivity;
@ -44,6 +46,7 @@ import ml.docilealligator.infinityforreddit.activities.SearchSubredditsResultAct
import ml.docilealligator.infinityforreddit.activities.ViewSubredditDetailActivity;
import ml.docilealligator.infinityforreddit.adapters.SubredditListingRecyclerViewAdapter;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.subreddit.SubredditData;
import ml.docilealligator.infinityforreddit.subreddit.SubredditListingViewModel;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
import retrofit2.Retrofit;
@ -58,6 +61,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
public static final String EXTRA_IS_GETTING_SUBREDDIT_INFO = "EIGSI";
public static final String EXTRA_ACCESS_TOKEN = "EAT";
public static final String EXTRA_ACCOUNT_NAME = "EAN";
public static final String EXTRA_IS_MULTI_SELECTION = "EIMS";
@BindView(R.id.coordinator_layout_subreddit_listing_fragment)
CoordinatorLayout mCoordinatorLayout;
@ -140,7 +144,8 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
boolean nsfw = mNsfwAndSpoilerSharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.NSFW_BASE, false);
mAdapter = new SubredditListingRecyclerViewAdapter(mActivity, mExecutor, mOauthRetrofit, mRetrofit,
mCustomThemeWrapper, accessToken, accountName, mRedditDataRoomDatabase,
mCustomThemeWrapper, accessToken, accountName,
mRedditDataRoomDatabase, getArguments().getBoolean(EXTRA_IS_MULTI_SELECTION, false),
new SubredditListingRecyclerViewAdapter.Callback() {
@Override
public void retryLoadingMore() {
@ -254,4 +259,23 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
public SortType getSortType() {
return sortType;
}
public List<SubredditData> getSelectedSubreddits() {
if (mSubredditListingViewModel != null) {
List<SubredditData> allSubreddits = mSubredditListingViewModel.getSubreddits().getValue();
if (allSubreddits == null) {
return null;
}
List<SubredditData> selectedSubreddits = new ArrayList<>();
for (SubredditData s : allSubreddits) {
if (s.isSelected()) {
selectedSubreddits.add(s);
}
}
return selectedSubreddits;
}
return null;
}
}

View File

@ -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;
}
}

View File

@ -44,4 +44,12 @@
android:background="?attr/selectableItemBackground"
android:visibility="gone"/>
<com.google.android.material.checkbox.MaterialCheckBox
android:id="@+id/checkbox__item_subreddit_listing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

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_save_search_subreddits_result_activity"
android:orderInCategory="1"
android:title="@string/action_save"
android:icon="@drawable/ic_check_circle_toolbar_24dp"
app:showAsAction="ifRoom" />
</menu>