diff options
7 files changed, 245 insertions, 179 deletions
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<String> 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<String> call, @NonNull Response<String> response) { - if (response.isSuccessful()) { - ParseSubredditData.parseSubredditListingData(mExecutor, handler, - response.body(), nsfw, new ParseSubredditData.ParseSubredditListingDataListener() { - @Override - public void onParseSubredditListingDataSuccess(ArrayList<SubredditData> 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<String> call, @NonNull Response<String> response) { + subredditAutocompleteCall = null; + if (response.isSuccessful() && !call.isCanceled()) { + ParseSubredditData.parseSubredditListingData(mExecutor, handler, + response.body(), nsfw, new ParseSubredditData.ParseSubredditListingDataListener() { + @Override + public void onParseSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) { + adapter.setSubreddits(subredditData); + } + + @Override + public void onParseSubredditListingDataFail() { + + } + }); + } + } - @Override - public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + @Override + public void onFailure(@NonNull Call<String> 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<String> 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<String> call, @NonNull Response<String> response) { - if (response.isSuccessful() && !call.isCanceled()) { - ParseSubredditData.parseSubredditListingData(executor, handler, - response.body(), nsfw, new ParseSubredditData.ParseSubredditListingDataListener() { - @Override - public void onParseSubredditListingDataSuccess(ArrayList<SubredditData> 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<String> call, @NonNull Response<String> response) { + subredditAutocompleteCall = null; + if (response.isSuccessful() && !call.isCanceled()) { + ParseSubredditData.parseSubredditListingData(executor, handler, + response.body(), nsfw, new ParseSubredditData.ParseSubredditListingDataListener() { + @Override + public void onParseSubredditListingDataSuccess(ArrayList<SubredditData> 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<String> call, @NonNull Throwable t) { + @Override + public void onFailure(@NonNull Call<String> 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<String> 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<String>() { + @Override + public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { + subredditAutocompleteCall = null; + if (response.isSuccessful()) { + ParseSubredditData.parseSubredditListingData(executor, handler, response.body(), + nsfw, new ParseSubredditData.ParseSubredditListingDataListener() { + @Override + public void onParseSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) { + adapter.setSubreddits(subredditData); + } + + @Override + public void onParseSubredditListingDataFail() { + + } + }); + } + } + + @Override + public void onFailure(@NonNull Call<String> 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<String>() { - @Override - public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { - if (response.isSuccessful()) { - ParseSubredditData.parseSubredditListingData(executor, handler, response.body(), - nsfw, new ParseSubredditData.ParseSubredditListingDataListener() { - @Override - public void onParseSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) { - adapter.setSubreddits(subredditData); - } - - @Override - public void onParseSubredditListingDataFail() { - - } - }); - } - } - - @Override - public void onFailure(@NonNull Call<String> 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<String> 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<String>() { + @Override + public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { + subredditAutocompleteCall = null; + if (response.isSuccessful()) { + ParseSubredditData.parseSubredditListingData(mExecutor, handler, response.body(), + nsfw, new ParseSubredditData.ParseSubredditListingDataListener() { + @Override + public void onParseSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) { + adapter.setSubreddits(subredditData); + } + + @Override + public void onParseSubredditListingDataFail() { + + } + }); + } + } + + @Override + public void onFailure(@NonNull Call<String> 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<String>() { - @Override - public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { - if (response.isSuccessful()) { - ParseSubredditData.parseSubredditListingData(mExecutor, handler, response.body(), - nsfw, new ParseSubredditData.ParseSubredditListingDataListener() { - @Override - public void onParseSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) { - adapter.setSubreddits(subredditData); - } - - @Override - public void onParseSubredditListingDataFail() { - - } - }); - } - } - - @Override - public void onFailure(@NonNull Call<String> 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<String> 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<String> call, @NonNull Response<String> response) { - if (response.isSuccessful()) { - ParseSubredditData.parseSubredditListingData(mExecutor, handler, response.body(), - nsfw, new ParseSubredditData.ParseSubredditListingDataListener() { - @Override - public void onParseSubredditListingDataSuccess(ArrayList<SubredditData> 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<String> call, @NonNull Response<String> response) { + subredditAutocompleteCall = null; + if (response.isSuccessful()) { + ParseSubredditData.parseSubredditListingData(mExecutor, handler, response.body(), + nsfw, new ParseSubredditData.ParseSubredditListingDataListener() { + @Override + public void onParseSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) { + adapter.setSubreddits(subredditData); + } + + @Override + public void onParseSubredditListingDataFail() { + + } + }); + } + } - @Override - public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + @Override + public void onFailure(@NonNull Call<String> 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<String> 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<String>() { - @Override - public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { - if (response.isSuccessful()) { - ParseSubredditData.parseSubredditListingData(mExecutor, handler, - response.body(), nsfw, new ParseSubredditData.ParseSubredditListingDataListener() { - @Override - public void onParseSubredditListingDataSuccess(ArrayList<SubredditData> 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<String> call, @NonNull Response<String> response) { + subredditAutocompleteCall = null; + if (response.isSuccessful()) { + ParseSubredditData.parseSubredditListingData(mExecutor, handler, + response.body(), nsfw, new ParseSubredditData.ParseSubredditListingDataListener() { + @Override + public void onParseSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) { + adapter.setSubreddits(subredditData); + } - @Override - public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + @Override + public void onParseSubredditListingDataFail() { - } - }); + } + }); + } + } + + @Override + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + subredditAutocompleteCall = null; + } + }); + }; + + handler.postDelayed(autoCompleteRunnable, 500); + } } }); new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) diff --git a/app/src/main/res/layout/item_recent_search_query.xml b/app/src/main/res/layout/item_recent_search_query.xml index 2ca122f4..97dc3627 100644 --- a/app/src/main/res/layout/item_recent_search_query.xml +++ b/app/src/main/res/layout/item_recent_search_query.xml @@ -17,7 +17,10 @@ android:id="@+id/recent_search_query_text_view_item_recent_search_query" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_margin="16dp" + android:layout_marginTop="16dp" + android:layout_marginBottom="4dp" + android:layout_marginStart="16dp" + android:layout_marginEnd="16dp" android:fontFamily="?attr/font_family" android:textSize="?attr/font_default" android:singleLine="true" @@ -28,6 +31,7 @@ android:id="@+id/recent_search_query_where_text_view_item_recent_search_query" android:layout_width="match_parent" android:layout_height="wrap_content" + android:layout_marginTop="4dp" android:layout_marginBottom="16dp" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" |