From 0a0d7e95b44a9c5c4926f3d566035caee3862838 Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Fri, 16 Aug 2024 23:09:01 -0400 Subject: Optimize subreddit auto complete. --- .../infinityforreddit/activities/MainActivity.java | 67 ++++++++++-------- .../activities/SearchActivity.java | 79 ++++++++++++---------- .../activities/SearchResultActivity.java | 69 +++++++++++-------- .../activities/ViewMultiRedditDetailActivity.java | 69 +++++++++++-------- .../activities/ViewSubredditDetailActivity.java | 67 ++++++++++-------- .../activities/ViewUserDetailActivity.java | 67 ++++++++++-------- 6 files changed, 240 insertions(+), 178 deletions(-) (limited to 'app/src/main/java/ml') 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 feece4f1..60bfabdc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java @@ -186,6 +186,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb private SectionsPagerAdapter sectionsPagerAdapter; private NavigationDrawerRecyclerViewMergedAdapter adapter; private NavigationWrapper navigationWrapper; + private Runnable autoCompleteRunnable; private Call subredditAutocompleteCall; private boolean mFetchUserInfoSuccess = false; private boolean mFetchSubscriptionsSuccess = false; @@ -1400,40 +1401,50 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { - + if (subredditAutocompleteCall != null && subredditAutocompleteCall.isExecuted()) { + subredditAutocompleteCall.cancel(); + } + if (autoCompleteRunnable != null) { + handler.removeCallbacks(autoCompleteRunnable); + } } @Override public void afterTextChanged(Editable editable) { - if (subredditAutocompleteCall != null) { - subredditAutocompleteCall.cancel(); - } - subredditAutocompleteCall = mOauthRetrofit.create(RedditAPI.class).subredditAutocomplete(APIUtils.getOAuthHeader(accessToken), - editable.toString(), nsfw); - subredditAutocompleteCall.enqueue(new Callback<>() { - @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { - if (response.isSuccessful()) { - ParseSubredditData.parseSubredditListingData(mExecutor, handler, - response.body(), nsfw, new ParseSubredditData.ParseSubredditListingDataListener() { - @Override - public void onParseSubredditListingDataSuccess(ArrayList subredditData, String after) { - adapter.setSubreddits(subredditData); - } - - @Override - public void onParseSubredditListingDataFail() { - - } - }); - } - } + String currentQuery = editable.toString().trim(); + if (!currentQuery.isEmpty()) { + autoCompleteRunnable = () -> { + subredditAutocompleteCall = mOauthRetrofit.create(RedditAPI.class).subredditAutocomplete(APIUtils.getOAuthHeader(accessToken), + currentQuery, nsfw); + subredditAutocompleteCall.enqueue(new Callback<>() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + subredditAutocompleteCall = null; + if (response.isSuccessful() && !call.isCanceled()) { + ParseSubredditData.parseSubredditListingData(mExecutor, handler, + 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) { + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + subredditAutocompleteCall = null; + } + }); + }; - } - }); + handler.postDelayed(autoCompleteRunnable, 500); + } } }); new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) 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 052ec4ab..37ef5cf3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchActivity.java @@ -105,6 +105,8 @@ public class SearchActivity extends BaseActivity { private boolean searchOnlyUsers; private SearchActivityRecyclerViewAdapter adapter; private SubredditAutocompleteRecyclerViewAdapter subredditAutocompleteRecyclerViewAdapter; + private Handler handler; + private Runnable autoCompleteRunnable; private Call subredditAutocompleteCall; RecentSearchQueryViewModel mRecentSearchQueryViewModel; private ActivitySearchBinding binding; @@ -169,7 +171,8 @@ public class SearchActivity extends BaseActivity { binding.searchEditTextSearchActivity.setImeOptions(binding.searchEditTextSearchActivity.getImeOptions() | EditorInfoCompat.IME_FLAG_NO_PERSONALIZED_LEARNING); } - Handler handler = new Handler(); + handler = new Handler(); + binding.searchEditTextSearchActivity.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { @@ -178,50 +181,54 @@ public class SearchActivity extends BaseActivity { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { - + if (subredditAutocompleteCall != null && subredditAutocompleteCall.isExecuted()) { + subredditAutocompleteCall.cancel(); + } + if (autoCompleteRunnable != null) { + handler.removeCallbacks(autoCompleteRunnable); + } } @Override public void afterTextChanged(Editable s) { - if (!s.toString().trim().isEmpty()) { - if (subredditAutocompleteCall != null) { - subredditAutocompleteCall.cancel(); - } + String currentQuery = s.toString().trim(); + if (!currentQuery.isEmpty()) { + binding.clearSearchEditViewSearchActivity.setVisibility(View.VISIBLE); - subredditAutocompleteCall = mOauthRetrofit.create(RedditAPI.class).subredditAutocomplete(APIUtils.getOAuthHeader(accessToken), - s.toString(), nsfw); - subredditAutocompleteCall.enqueue(new Callback<>() { - @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { - if (response.isSuccessful() && !call.isCanceled()) { - ParseSubredditData.parseSubredditListingData(executor, handler, - response.body(), nsfw, new ParseSubredditData.ParseSubredditListingDataListener() { - @Override - public void onParseSubredditListingDataSuccess(ArrayList subredditData, String after) { - binding.recentSearchQueryRecyclerViewSearchActivity.setVisibility(View.GONE); - binding.subredditAutocompleteRecyclerViewSearchActivity.setVisibility(View.VISIBLE); - subredditAutocompleteRecyclerViewAdapter.setSubreddits(subredditData); - } - - @Override - public void onParseSubredditListingDataFail() { - - } - }); + autoCompleteRunnable = () -> { + subredditAutocompleteCall = mOauthRetrofit.create(RedditAPI.class).subredditAutocomplete(APIUtils.getOAuthHeader(accessToken), + currentQuery, nsfw); + subredditAutocompleteCall.enqueue(new Callback<>() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + subredditAutocompleteCall = null; + if (response.isSuccessful() && !call.isCanceled()) { + ParseSubredditData.parseSubredditListingData(executor, handler, + response.body(), nsfw, new ParseSubredditData.ParseSubredditListingDataListener() { + @Override + public void onParseSubredditListingDataSuccess(ArrayList subredditData, String after) { + binding.recentSearchQueryRecyclerViewSearchActivity.setVisibility(View.GONE); + binding.subredditAutocompleteRecyclerViewSearchActivity.setVisibility(View.VISIBLE); + subredditAutocompleteRecyclerViewAdapter.setSubreddits(subredditData); + } + + @Override + public void onParseSubredditListingDataFail() { + + } + }); + } } - } - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + subredditAutocompleteCall = null; + } + }); + }; - } - }); - binding.clearSearchEditViewSearchActivity.setVisibility(View.VISIBLE); + handler.postDelayed(autoCompleteRunnable, 500); } else { - if (subredditAutocompleteCall != null) { - subredditAutocompleteCall.cancel(); - } - binding.recentSearchQueryRecyclerViewSearchActivity.setVisibility(View.VISIBLE); binding.subredditAutocompleteRecyclerViewSearchActivity.setVisibility(View.GONE); binding.clearSearchEditViewSearchActivity.setVisibility(View.GONE); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java index 0ce65f0a..b0f78fff 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java @@ -115,6 +115,7 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect CustomThemeWrapper mCustomThemeWrapper; @Inject Executor executor; + private Runnable autoCompleteRunnable; private Call subredditAutocompleteCall; private String mQuery; private String mSearchInSubredditOrUserName; @@ -604,40 +605,50 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { - + if (subredditAutocompleteCall != null && subredditAutocompleteCall.isExecuted()) { + subredditAutocompleteCall.cancel(); + } + if (autoCompleteRunnable != null) { + handler.removeCallbacks(autoCompleteRunnable); + } } @Override public void afterTextChanged(Editable editable) { - if (subredditAutocompleteCall != null) { - subredditAutocompleteCall.cancel(); + String currentQuery = editable.toString().trim(); + if (!currentQuery.isEmpty()) { + autoCompleteRunnable = () -> { + subredditAutocompleteCall = mOauthRetrofit.create(RedditAPI.class).subredditAutocomplete(APIUtils.getOAuthHeader(accessToken), + currentQuery, nsfw); + subredditAutocompleteCall.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + subredditAutocompleteCall = null; + if (response.isSuccessful()) { + ParseSubredditData.parseSubredditListingData(executor, handler, 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) { + subredditAutocompleteCall = null; + } + }); + }; + + handler.postDelayed(autoCompleteRunnable, 500); } - subredditAutocompleteCall = mOauthRetrofit.create(RedditAPI.class).subredditAutocomplete(APIUtils.getOAuthHeader(accessToken), - editable.toString(), nsfw); - subredditAutocompleteCall.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { - if (response.isSuccessful()) { - ParseSubredditData.parseSubredditListingData(executor, handler, 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) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java index cf582fa9..48e40ee1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java @@ -122,6 +122,7 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT private boolean hideFab; private boolean showBottomAppBar; private boolean lockBottomAppBar; + private Runnable autoCompleteRunnable; private Call subredditAutocompleteCall; private NavigationWrapper navigationWrapper; private ActivityViewMultiRedditDetailBinding binding; @@ -604,40 +605,50 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { - + if (subredditAutocompleteCall != null && subredditAutocompleteCall.isExecuted()) { + subredditAutocompleteCall.cancel(); + } + if (autoCompleteRunnable != null) { + handler.removeCallbacks(autoCompleteRunnable); + } } @Override public void afterTextChanged(Editable editable) { - if (subredditAutocompleteCall != null) { - subredditAutocompleteCall.cancel(); + String currentQuery = editable.toString().trim(); + if (!currentQuery.isEmpty()) { + autoCompleteRunnable = () -> { + subredditAutocompleteCall = mOauthRetrofit.create(RedditAPI.class).subredditAutocomplete(APIUtils.getOAuthHeader(accessToken), + currentQuery, nsfw); + subredditAutocompleteCall.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + subredditAutocompleteCall = null; + if (response.isSuccessful()) { + ParseSubredditData.parseSubredditListingData(mExecutor, handler, 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) { + subredditAutocompleteCall = null; + } + }); + }; + + handler.postDelayed(autoCompleteRunnable, 500); } - subredditAutocompleteCall = mOauthRetrofit.create(RedditAPI.class).subredditAutocomplete(APIUtils.getOAuthHeader(accessToken), - editable.toString(), nsfw); - subredditAutocompleteCall.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { - if (response.isSuccessful()) { - ParseSubredditData.parseSubredditListingData(mExecutor, handler, 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) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java index b0526981..002e775d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java @@ -167,6 +167,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp private FragmentManager fragmentManager; private SectionsPagerAdapter sectionsPagerAdapter; private NavigationWrapper navigationWrapper; + private Runnable autoCompleteRunnable; private Call subredditAutocompleteCall; private String subredditName; private String description; @@ -1409,40 +1410,50 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { - + if (subredditAutocompleteCall != null && subredditAutocompleteCall.isExecuted()) { + subredditAutocompleteCall.cancel(); + } + if (autoCompleteRunnable != null) { + handler.removeCallbacks(autoCompleteRunnable); + } } @Override public void afterTextChanged(Editable editable) { - if (subredditAutocompleteCall != null) { - subredditAutocompleteCall.cancel(); - } - subredditAutocompleteCall = mOauthRetrofit.create(RedditAPI.class).subredditAutocomplete(APIUtils.getOAuthHeader(accessToken), - editable.toString(), nsfw); - subredditAutocompleteCall.enqueue(new Callback<>() { - @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { - if (response.isSuccessful()) { - ParseSubredditData.parseSubredditListingData(mExecutor, handler, response.body(), - nsfw, new ParseSubredditData.ParseSubredditListingDataListener() { - @Override - public void onParseSubredditListingDataSuccess(ArrayList subredditData, String after) { - adapter.setSubreddits(subredditData); - } - - @Override - public void onParseSubredditListingDataFail() { - - } - }); - } - } + String currentQuery = editable.toString().trim(); + if (!currentQuery.isEmpty()) { + autoCompleteRunnable = () -> { + subredditAutocompleteCall = mOauthRetrofit.create(RedditAPI.class).subredditAutocomplete(APIUtils.getOAuthHeader(accessToken), + currentQuery, nsfw); + subredditAutocompleteCall.enqueue(new Callback<>() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + subredditAutocompleteCall = null; + if (response.isSuccessful()) { + ParseSubredditData.parseSubredditListingData(mExecutor, handler, 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) { + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + subredditAutocompleteCall = null; + } + }); + }; - } - }); + handler.postDelayed(autoCompleteRunnable, 500); + } } }); new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java index bf2cfaf8..cc179b2a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java @@ -166,6 +166,7 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele private SectionsPagerAdapter sectionsPagerAdapter; private RequestManager glide; private NavigationWrapper navigationWrapper; + private Runnable autoCompleteRunnable; private Call subredditAutocompleteCall; private String username; private String description; @@ -1353,40 +1354,50 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { - + if (subredditAutocompleteCall != null && subredditAutocompleteCall.isExecuted()) { + subredditAutocompleteCall.cancel(); + } + if (autoCompleteRunnable != null) { + handler.removeCallbacks(autoCompleteRunnable); + } } @Override public void afterTextChanged(Editable editable) { - if (subredditAutocompleteCall != null) { - subredditAutocompleteCall.cancel(); - } - subredditAutocompleteCall = mOauthRetrofit.create(RedditAPI.class).subredditAutocomplete(APIUtils.getOAuthHeader(accessToken), - editable.toString(), nsfw); - subredditAutocompleteCall.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { - if (response.isSuccessful()) { - ParseSubredditData.parseSubredditListingData(mExecutor, handler, - response.body(), nsfw, new ParseSubredditData.ParseSubredditListingDataListener() { - @Override - public void onParseSubredditListingDataSuccess(ArrayList subredditData, String after) { - adapter.setSubreddits(subredditData); - } - - @Override - public void onParseSubredditListingDataFail() { - - } - }); - } - } + String currentQuery = editable.toString().trim(); + if (!currentQuery.isEmpty()) { + autoCompleteRunnable = () -> { + subredditAutocompleteCall = mOauthRetrofit.create(RedditAPI.class).subredditAutocomplete(APIUtils.getOAuthHeader(accessToken), + currentQuery, nsfw); + subredditAutocompleteCall.enqueue(new Callback<>() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + subredditAutocompleteCall = null; + if (response.isSuccessful()) { + ParseSubredditData.parseSubredditListingData(mExecutor, handler, + response.body(), nsfw, new ParseSubredditData.ParseSubredditListingDataListener() { + @Override + public void onParseSubredditListingDataSuccess(ArrayList subredditData, String after) { + adapter.setSubreddits(subredditData); + } - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { + @Override + public void onParseSubredditListingDataFail() { - } - }); + } + }); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + subredditAutocompleteCall = null; + } + }); + }; + + handler.postDelayed(autoCompleteRunnable, 500); + } } }); new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) -- cgit v1.2.3