From 2d1e63d7223408cd1acbd4b02851acea2881dd52 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 1 Jul 2021 12:48:28 +0800 Subject: Suggest subreddits in go to subreddit dialog in MainActivity. --- .../infinityforreddit/activities/MainActivity.java | 69 +++++++++++++++++++++- .../adapters/CommentsRecyclerViewAdapter.java | 2 +- 2 files changed, 69 insertions(+), 2 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java index bcb42023..6549c44e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java @@ -9,6 +9,8 @@ import android.graphics.Color; import android.os.Build; import android.os.Bundle; import android.os.Handler; +import android.text.Editable; +import android.text.TextWatcher; import android.view.Gravity; import android.view.KeyEvent; import android.view.Menu; @@ -81,6 +83,8 @@ import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; import ml.docilealligator.infinityforreddit.account.AccountViewModel; import ml.docilealligator.infinityforreddit.adapters.NavigationDrawerRecyclerViewAdapter; +import ml.docilealligator.infinityforreddit.adapters.SubredditAutocompleteRecyclerViewAdapter; +import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.asynctasks.InsertSubscribedThings; import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccount; import ml.docilealligator.infinityforreddit.asynctasks.SwitchToAnonymousMode; @@ -106,15 +110,20 @@ import ml.docilealligator.infinityforreddit.multireddit.MultiRedditViewModel; import ml.docilealligator.infinityforreddit.post.Post; import ml.docilealligator.infinityforreddit.post.PostDataSource; import ml.docilealligator.infinityforreddit.readpost.InsertReadPost; +import ml.docilealligator.infinityforreddit.subreddit.ParseSubredditData; import ml.docilealligator.infinityforreddit.subreddit.SubredditData; import ml.docilealligator.infinityforreddit.subscribedsubreddit.SubscribedSubredditData; import ml.docilealligator.infinityforreddit.subscribedsubreddit.SubscribedSubredditViewModel; import ml.docilealligator.infinityforreddit.subscribeduser.SubscribedUserData; import ml.docilealligator.infinityforreddit.user.FetchUserData; import ml.docilealligator.infinityforreddit.user.UserData; +import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.CustomThemeSharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.Utils; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; import retrofit2.Retrofit; import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO; @@ -215,6 +224,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb private SectionsPagerAdapter sectionsPagerAdapter; private AppBarLayout.LayoutParams params; private NavigationDrawerRecyclerViewAdapter adapter; + private Call subredditAutocompleteCall; private String mAccessToken; private String mAccountName; private boolean mFetchUserInfoSuccess = false; @@ -1373,8 +1383,20 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb private void goToSubreddit() { View rootView = getLayoutInflater().inflate(R.layout.dialog_go_to_thing_edit_text, coordinatorLayout, false); TextInputEditText thingEditText = rootView.findViewById(R.id.text_input_edit_text_go_to_thing_edit_text); - thingEditText.requestFocus(); + RecyclerView recyclerView = rootView.findViewById(R.id.recycler_view_go_to_thing_edit_text); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); + SubredditAutocompleteRecyclerViewAdapter adapter = new SubredditAutocompleteRecyclerViewAdapter( + this, mCustomThemeWrapper, subredditData -> { + if (imm != null) { + imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); + } + Intent intent = new Intent(MainActivity.this, ViewSubredditDetailActivity.class); + intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, subredditData.getName()); + startActivity(intent); + }); + recyclerView.setAdapter(adapter); + + thingEditText.requestFocus(); if (imm != null) { imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); } @@ -1390,6 +1412,51 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb } return false; }); + + boolean nsfw = mNsfwAndSpoilerSharedPreferences.getBoolean((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.NSFW_BASE, false); + thingEditText.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) { + if (subredditAutocompleteCall != null) { + subredditAutocompleteCall.cancel(); + } + subredditAutocompleteCall = mOauthRetrofit.create(RedditAPI.class).subredditAutocomplete(APIUtils.getOAuthHeader(mAccessToken), + editable.toString(), 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) { + adapter.setSubreddits(subredditData); + } + + @Override + public void onParseSubredditListingDataFail() { + + } + }); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + + } + }); + } + }); new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) .setTitle(R.string.go_to_subreddit) .setView(rootView) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java index eaaf7d96..f88012b9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java @@ -233,7 +233,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter { - if (activity != null && !activity.isDestroyed() && !activity.isFinishing()) { + if (!activity.isDestroyed() && !activity.isFinishing()) { UrlMenuBottomSheetFragment urlMenuBottomSheetFragment = new UrlMenuBottomSheetFragment(); Bundle bundle = new Bundle(); bundle.putString(UrlMenuBottomSheetFragment.EXTRA_URL, url); -- cgit v1.2.3