diff options
author | Alex Ning <chineseperson5@gmail.com> | 2021-06-30 12:06:46 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2021-06-30 12:06:46 +0000 |
commit | ef953a72f73e6b11a5886678c2cd0424a4f5e9f8 (patch) | |
tree | 647b80a76346fe2ec7726f2b26840e633638ab1d /app/src/main/java/ml | |
parent | b0f38f9db44d0b09689db58320c5a568ac31ae17 (diff) | |
download | infinity-for-reddit-ef953a72f73e6b11a5886678c2cd0424a4f5e9f8.tar infinity-for-reddit-ef953a72f73e6b11a5886678c2cd0424a4f5e9f8.tar.gz infinity-for-reddit-ef953a72f73e6b11a5886678c2cd0424a4f5e9f8.tar.bz2 infinity-for-reddit-ef953a72f73e6b11a5886678c2cd0424a4f5e9f8.tar.lz infinity-for-reddit-ef953a72f73e6b11a5886678c2cd0424a4f5e9f8.tar.xz infinity-for-reddit-ef953a72f73e6b11a5886678c2cd0424a4f5e9f8.tar.zst infinity-for-reddit-ef953a72f73e6b11a5886678c2cd0424a4f5e9f8.zip |
Suggest subreddits when searching.
Diffstat (limited to 'app/src/main/java/ml')
5 files changed, 176 insertions, 12 deletions
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 cd6c2c5c..a4f77b95 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchActivity.java @@ -28,6 +28,8 @@ import com.r0adkll.slidr.Slidr; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; +import java.util.ArrayList; + import javax.inject.Inject; import javax.inject.Named; @@ -37,12 +39,21 @@ import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.adapters.SearchActivityRecyclerViewAdapter; +import ml.docilealligator.infinityforreddit.adapters.SubredditAutocompleteRecyclerViewAdapter; +import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.recentsearchquery.DeleteRecentSearchQuery; import ml.docilealligator.infinityforreddit.recentsearchquery.RecentSearchQuery; import ml.docilealligator.infinityforreddit.recentsearchquery.RecentSearchQueryViewModel; +import ml.docilealligator.infinityforreddit.subreddit.ParseSubredditData; +import ml.docilealligator.infinityforreddit.subreddit.SubredditData; +import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; public class SearchActivity extends BaseActivity { @@ -83,11 +94,12 @@ public class SearchActivity extends BaseActivity { TextView subredditNameTextView; @BindView(R.id.divider_search_activity) View divider; - @BindView(R.id.recent_summary_text_view_search_activity) - TextView recentSummaryTextView; @BindView(R.id.recycler_view_search_activity) RecyclerView recyclerView; @Inject + @Named("oauth") + Retrofit mOauthRetrofit; + @Inject RedditDataRoomDatabase mRedditDataRoomDatabase; @Inject @Named("default") @@ -96,14 +108,20 @@ public class SearchActivity extends BaseActivity { @Named("current_account") SharedPreferences mCurrentAccountSharedPreferences; @Inject + @Named("nsfw_and_spoiler") + SharedPreferences mNsfwAndSpoilerSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; private String mAccountName; + private String mAccessToken; private String query; private String subredditName; private boolean subredditIsUser; private boolean searchOnlySubreddits; private boolean searchOnlyUsers; private SearchActivityRecyclerViewAdapter adapter; + private SubredditAutocompleteRecyclerViewAdapter subredditAutocompleteRecyclerViewAdapter; + private Call<String> subredditAutocompleteCall; RecentSearchQueryViewModel mRecentSearchQueryViewModel; @Override @@ -159,6 +177,18 @@ public class SearchActivity extends BaseActivity { } }); + mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); + boolean nsfw = mNsfwAndSpoilerSharedPreferences.getBoolean((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.NSFW_BASE, false); + + subredditAutocompleteRecyclerViewAdapter = new SubredditAutocompleteRecyclerViewAdapter(this, + mCustomThemeWrapper, subredditData -> { + Intent intent = new Intent(SearchActivity.this, ViewSubredditDetailActivity.class); + intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, subredditData.getName()); + startActivity(intent); + finish(); + }); + simpleSearchView.setOnQueryTextListener(new SimpleSearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { @@ -168,6 +198,38 @@ public class SearchActivity extends BaseActivity { @Override public boolean onQueryTextChange(String newText) { + if (!newText.isEmpty()) { + if (subredditAutocompleteCall != null) { + subredditAutocompleteCall.cancel(); + } + subredditAutocompleteCall = mOauthRetrofit.create(RedditAPI.class).subredditAutocomplete(APIUtils.getOAuthHeader(mAccessToken), + newText, nsfw); + subredditAutocompleteCall.enqueue(new Callback<String>() { + @Override + public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { + if (response.isSuccessful()) { + ParseSubredditData.parseSubredditListingData(response.body(), nsfw, new ParseSubredditData.ParseSubredditListingDataListener() { + @Override + public void onParseSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) { + subredditAutocompleteRecyclerViewAdapter.setSubreddits(subredditData); + recyclerView.setAdapter(subredditAutocompleteRecyclerViewAdapter); + } + + @Override + public void onParseSubredditListingDataFail() { + + } + }); + } + } + + @Override + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + + } + }); + return true; + } return false; } @@ -177,8 +239,6 @@ public class SearchActivity extends BaseActivity { } }); - mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); - if (savedInstanceState != null) { subredditName = savedInstanceState.getString(SUBREDDIT_NAME_STATE); subredditIsUser = savedInstanceState.getBoolean(SUBREDDIT_IS_USER_STATE); @@ -238,10 +298,8 @@ public class SearchActivity extends BaseActivity { mRecentSearchQueryViewModel.getAllRecentSearchQueries().observe(this, recentSearchQueries -> { if (recentSearchQueries != null && !recentSearchQueries.isEmpty()) { divider.setVisibility(View.VISIBLE); - recentSummaryTextView.setVisibility(View.VISIBLE); } else { divider.setVisibility(View.GONE); - recentSummaryTextView.setVisibility(View.GONE); } adapter.setRecentSearchQueries(recentSearchQueries); }); @@ -309,7 +367,6 @@ public class SearchActivity extends BaseActivity { searchInTextView.setTextColor(colorAccent); subredditNameTextView.setTextColor(mCustomThemeWrapper.getPrimaryTextColor()); divider.setBackgroundColor(mCustomThemeWrapper.getDividerColor()); - recentSummaryTextView.setTextColor(colorAccent); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditAutocompleteRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditAutocompleteRecyclerViewAdapter.java new file mode 100644 index 00000000..c657f422 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditAutocompleteRecyclerViewAdapter.java @@ -0,0 +1,103 @@ +package ml.docilealligator.infinityforreddit.adapters; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import androidx.annotation.NonNull; +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.List; + +import jp.wasabeef.glide.transformations.RoundedCornersTransformation; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.subreddit.SubredditData; +import pl.droidsonroids.gif.GifImageView; + +public class SubredditAutocompleteRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { + private List<SubredditData> subreddits; + private RequestManager glide; + private CustomThemeWrapper customThemeWrapper; + private ItemOnClickListener itemOnClickListener; + + public SubredditAutocompleteRecyclerViewAdapter(Context context, CustomThemeWrapper customThemeWrapper, + ItemOnClickListener itemOnClickListener) { + glide = Glide.with(context); + this.customThemeWrapper = customThemeWrapper; + this.itemOnClickListener = itemOnClickListener; + } + + @NonNull + @Override + public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + return new SubredditViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_subreddit_listing, parent, false)); + } + + @Override + public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { + if (holder instanceof SubredditViewHolder) { + glide.load(subreddits.get(position).getIconUrl()) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .error(glide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) + .into(((SubredditViewHolder) holder).iconImageView); + ((SubredditViewHolder) holder).subredditNameTextView.setText(subreddits.get(position).getName()); + } + } + + @Override + public int getItemCount() { + return subreddits == null ? 0 : subreddits.size(); + } + + @Override + public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) { + super.onViewRecycled(holder); + if (holder instanceof SubredditViewHolder) { + glide.clear(((SubredditViewHolder) holder).iconImageView); + } + } + + public void setSubreddits(List<SubredditData> subreddits) { + this.subreddits = subreddits; + notifyDataSetChanged(); + } + + class SubredditViewHolder extends RecyclerView.ViewHolder { + + GifImageView iconImageView; + TextView subredditNameTextView; + ImageView subscribeImageView; + MaterialCheckBox checkBox; + + public SubredditViewHolder(@NonNull View itemView) { + super(itemView); + iconImageView = itemView.findViewById(R.id.subreddit_icon_gif_image_view_item_subreddit_listing); + subredditNameTextView = itemView.findViewById(R.id.subreddit_name_text_view_item_subreddit_listing); + subscribeImageView = itemView.findViewById(R.id.subscribe_image_view_item_subreddit_listing); + checkBox = itemView.findViewById(R.id.checkbox_item_subreddit_listing); + + subscribeImageView.setVisibility(View.GONE); + checkBox.setVisibility(View.GONE); + + subredditNameTextView.setTextColor(customThemeWrapper.getPrimaryTextColor()); + + itemView.setOnClickListener(view -> { + itemOnClickListener.onClick(subreddits.get(getBindingAdapterPosition())); + }); + } + } + + public interface ItemOnClickListener { + void onClick(SubredditData subredditData); + } +} 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 f7891958..18268b54 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditListingRecyclerViewAdapter.java @@ -262,7 +262,7 @@ 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) + @BindView(R.id.checkbox_item_subreddit_listing) MaterialCheckBox checkBox; DataViewHolder(View itemView) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java index 5e43ca3d..5269da5f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java @@ -352,4 +352,8 @@ public interface RedditAPI { @FormUrlEncoded @POST("/api/quarantine_optin?raw_json=1") Call<String> optInQuarantinedSubreddit(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params); + + @GET("/api/subreddit_autocomplete_v2?typeahead_active=true&include_profiles=false&raw_json=1") + Call<String> subredditAutocomplete(@HeaderMap Map<String ,String> headers, @Query("query") String query, + @Query("include_over_18") boolean nsfw); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/ParseSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/ParseSubredditData.java index d673647d..694d5900 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/ParseSubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/ParseSubredditData.java @@ -13,12 +13,12 @@ import java.util.ArrayList; import ml.docilealligator.infinityforreddit.utils.JSONUtils; import ml.docilealligator.infinityforreddit.utils.Utils; -class ParseSubredditData { - static void parseSubredditData(String response, ParseSubredditDataListener parseSubredditDataListener) { +public class ParseSubredditData { + public static void parseSubredditData(String response, ParseSubredditDataListener parseSubredditDataListener) { new ParseSubredditDataAsyncTask(response, parseSubredditDataListener).execute(); } - static void parseSubredditListingData(String response, boolean nsfw, ParseSubredditListingDataListener parseSubredditListingDataListener) { + public static void parseSubredditListingData(String response, boolean nsfw, ParseSubredditListingDataListener parseSubredditListingDataListener) { new ParseSubredditListingDataAsyncTask(response, nsfw, parseSubredditListingDataListener).execute(); } @@ -70,7 +70,7 @@ class ParseSubredditData { void onParseSubredditDataFail(); } - interface ParseSubredditListingDataListener { + public interface ParseSubredditListingDataListener { void onParseSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after); void onParseSubredditListingDataFail(); |