From ef953a72f73e6b11a5886678c2cd0424a4f5e9f8 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 30 Jun 2021 20:06:46 +0800 Subject: Suggest subreddits when searching. --- .../activities/SearchActivity.java | 71 ++++++++++++-- .../SubredditAutocompleteRecyclerViewAdapter.java | 103 +++++++++++++++++++++ .../SubredditListingRecyclerViewAdapter.java | 2 +- .../infinityforreddit/apis/RedditAPI.java | 4 + .../subreddit/ParseSubredditData.java | 8 +- app/src/main/res/layout/activity_search.xml | 9 -- app/src/main/res/layout/item_subreddit_listing.xml | 2 +- app/src/main/res/values-de/strings.xml | 1 - app/src/main/res/values-es/strings.xml | 1 - app/src/main/res/values-fr/strings.xml | 1 - app/src/main/res/values-hi/strings.xml | 1 - app/src/main/res/values-hr/strings.xml | 1 - app/src/main/res/values-hu/strings.xml | 1 - app/src/main/res/values-it/strings.xml | 1 - app/src/main/res/values-ja/strings.xml | 1 - app/src/main/res/values-nl/strings.xml | 1 - app/src/main/res/values-pl/strings.xml | 1 - app/src/main/res/values-pt-rBR/strings.xml | 1 - app/src/main/res/values-ru/strings.xml | 1 - app/src/main/res/values-tr-rTR/strings.xml | 1 - app/src/main/res/values-vi/strings.xml | 1 - app/src/main/res/values-zh-rCN/strings.xml | 1 - app/src/main/res/values/strings.xml | 2 - 23 files changed, 177 insertions(+), 39 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditAutocompleteRecyclerViewAdapter.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 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 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() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + ParseSubredditData.parseSubredditListingData(response.body(), nsfw, new ParseSubredditData.ParseSubredditListingDataListener() { + @Override + public void onParseSubredditListingDataSuccess(ArrayList subredditData, String after) { + subredditAutocompleteRecyclerViewAdapter.setSubreddits(subredditData); + recyclerView.setAdapter(subredditAutocompleteRecyclerViewAdapter); + } + + @Override + public void onParseSubredditListingDataFail() { + + } + }); + } + } + + @Override + public void onFailure(@NonNull Call 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 { + private List 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 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 optInQuarantinedSubreddit(@HeaderMap Map headers, @FieldMap Map params); + + @GET("/api/subreddit_autocomplete_v2?typeahead_active=true&include_profiles=false&raw_json=1") + Call subredditAutocomplete(@HeaderMap Map 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, String after); void onParseSubredditListingDataFail(); diff --git a/app/src/main/res/layout/activity_search.xml b/app/src/main/res/layout/activity_search.xml index a3997671..7de8bd69 100644 --- a/app/src/main/res/layout/activity_search.xml +++ b/app/src/main/res/layout/activity_search.xml @@ -91,15 +91,6 @@ android:layout_height="1dp" android:visibility="gone" /> - - "Code: %1$d/ Nachricht: %2$s" "Auszeichnung verliehen" "Fehlgeschlagen" - "Kürzliche Suchanfragen" "Warnung" "Dieses ist ein NSFW-Subreddit" "Dieser Nutzer hat NSFW-Inhalte" diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index b23da6fa..963956b6 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -863,7 +863,6 @@ Videos de Reddit están en menor resolución" "Anónimo" "Premio entregado" "Falló" - "Búsquedas recientes" "Precaución" "Este es un Subreddit NSFW" "Este usuario tiene contenido NSFW" diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 485bc042..f3a8c7cf 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -918,7 +918,6 @@ Les onglets pourraient perdre tout leur contenu quand vous passez d'un onglet à "Erreur: %2$s, code: %1$d/" "Succès ! " "Échec de la récompense. " - "Recherches récentes" "Avertissement " "Cette Subreddit est PSPT (Pas Sécurisé Pour le Travail) " "Cet utilisateur poste du contenu NSFW" diff --git a/app/src/main/res/values-hi/strings.xml b/app/src/main/res/values-hi/strings.xml index 10be455b..eba23820 100644 --- a/app/src/main/res/values-hi/strings.xml +++ b/app/src/main/res/values-hi/strings.xml @@ -901,7 +901,6 @@ https://play.google.com/store/apps/details?id=ml.docilealligator.infinityforredd संदेश: %2$s" "पुरस्कार दे दिया गया " "असफल " - "हाल ही की खोजें " "चेतावनी " "यह एक NSFW सबरैडिट है " "इस यूजर के पास NSFW सामग्री है। " diff --git a/app/src/main/res/values-hr/strings.xml b/app/src/main/res/values-hr/strings.xml index 0eeb98e3..4c960a3e 100644 --- a/app/src/main/res/values-hr/strings.xml +++ b/app/src/main/res/values-hr/strings.xml @@ -864,7 +864,6 @@ Reddit videi su niže rezolucije." Poruka: %2$s" "Nagrađeno" "Nagrađivanje nije uspjelo" - "Nedavne pretrage" "Upozorenje" "Ovo je NSFW podreddit." "Ovaj korisnik ima NSFW sadržaj" diff --git a/app/src/main/res/values-hu/strings.xml b/app/src/main/res/values-hu/strings.xml index f138fc36..45e2d0ed 100644 --- a/app/src/main/res/values-hu/strings.xml +++ b/app/src/main/res/values-hu/strings.xml @@ -835,7 +835,6 @@ Videó autómatikus lejátszása le van tiltva." Üzenet %2$s" "Kitüntetés megadva" "Nem sikerült" - "Utóbbi keresések" "Figyelem" "Ez egy NSFW subreddit." "Ennek a felhasználónak van NSFW tartlama" diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index 192ff9da..db6d702f 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -865,7 +865,6 @@ La riproduzione automatica video è disabilitata" Messaggio: %2$s" "Premio conferito" "Fallito" - "Ricerche recenti" "Avvertimento" "Questo è un subreddit marcato NSFW" "Questo utente è marcato NSFW" diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index d30e63d4..a7da9071 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -912,7 +912,6 @@ https://play.google.com/store/apps/details?id=ml.docilealligator.infinityforredd メッセージ: %2$s" "アワードを贈りました" "失敗しました" - "検索履歴" "警告" "このSubredditはNSFWです" "このユーザーはNSFWの投稿を含んでいます" diff --git a/app/src/main/res/values-nl/strings.xml b/app/src/main/res/values-nl/strings.xml index 2473472a..0f3857fd 100644 --- a/app/src/main/res/values-nl/strings.xml +++ b/app/src/main/res/values-nl/strings.xml @@ -838,7 +838,6 @@ Automatisch afspelen van video is uitgeschakeld." Bericht: %2$s" "Award gegeven" "Mislukt" - "Recente zoekopdrachten" "Waarschuwing" "Dit is een NSFW subreddit." "Deze gebruiker heeft NSFW inhoud" diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index 2b733d50..e4b9ec39 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -840,7 +840,6 @@ Autoodtwarzanie wideo jest wyłączone." Wiadomość: %2$s" "Nagroda przyznana" "Niepowodzenie" - "Ostatnie wyszukiwania" "Ostrzeżenie " "To jest subreddit NSFW" "Ten użytkownik posiada treści NSFW" diff --git a/app/src/main/res/values-pt-rBR/strings.xml b/app/src/main/res/values-pt-rBR/strings.xml index 1b5e47dd..5bdd7a6e 100644 --- a/app/src/main/res/values-pt-rBR/strings.xml +++ b/app/src/main/res/values-pt-rBR/strings.xml @@ -841,7 +841,6 @@ Os vídeos do Reddit ficam em menor resolução." Mensagem: %2$s" "Prêmio concedido" "Falhou" - "Buscas recentes" "Aviso" "Este é um subreddit NSFW." "Este usuário tem conteúdo NSFW." diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index cc780a20..74e9adda 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -833,7 +833,6 @@ Reddit видео - уменьшенное разрешение. Сообщение: %2$s" "Награда выдана" "Ошибка" - "Недавние запросы" "Предупреждение" "Это NSFW сабреддит" "У пользователь есть NSFW контент" diff --git a/app/src/main/res/values-tr-rTR/strings.xml b/app/src/main/res/values-tr-rTR/strings.xml index d3258339..db64fe86 100644 --- a/app/src/main/res/values-tr-rTR/strings.xml +++ b/app/src/main/res/values-tr-rTR/strings.xml @@ -847,7 +847,6 @@ Sekmeler, diğerlerine geçtikten sonra tüm içeriği kaybedebilir. Bu, sayfay Mesaj: %2$s" "Ödül verildi" "İşlem Başarısız Oldu" - "Son aramalar" "Uyarı" "Bu bir NSFW subredditidir." "Bu kullanıcının NSFW içeriği var" diff --git a/app/src/main/res/values-vi/strings.xml b/app/src/main/res/values-vi/strings.xml index 37e80774..10f23c53 100644 --- a/app/src/main/res/values-vi/strings.xml +++ b/app/src/main/res/values-vi/strings.xml @@ -832,7 +832,6 @@ Tự động phát video bị tắt." Thông báo: %2$s" "Giải thưởng đã được cho" "Thất bại" - "Tìm kiếm gần đây" "Cảnh báo" "Đây là một subreddit NSFW." "Người dùng này có nội dung NSFW" diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 92cabf83..6301761c 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -892,7 +892,6 @@ https://play.google.com/store/apps/details?id=ml.docilealligator.infinityforredd 错误信息:%2$s" "奖励已赠送" "失败" - "最近搜索" "警告" "这是一个 NSFW 版块。" "该用户有 NSFW 内容" diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index abb5c5b7..ac7f028e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1002,8 +1002,6 @@ Award given Failed - Recent searches - Warning This is a NSFW subreddit. This user has NSFW content -- cgit v1.2.3