From c517f41595065e735c6d7bd95d7cd24f0bd93e35 Mon Sep 17 00:00:00 2001 From: Grishka Date: Sun, 17 Sep 2023 11:20:12 +0300 Subject: [PATCH] Paginate search results --- mastodon/build.gradle | 2 +- .../joinmastodon/android/MainActivity.java | 2 +- .../api/requests/search/GetSearchResults.java | 8 ++++- .../ComposeAccountSearchFragment.java | 2 +- .../fragments/discover/SearchFragment.java | 32 +++++++++++++------ .../discover/SearchQueryFragment.java | 4 +-- .../ComposeAutocompleteViewController.java | 7 ++-- 7 files changed, 36 insertions(+), 21 deletions(-) diff --git a/mastodon/build.gradle b/mastodon/build.gradle index 9e81461d3..5d7eaa3fc 100644 --- a/mastodon/build.gradle +++ b/mastodon/build.gradle @@ -9,7 +9,7 @@ android { applicationId "org.joinmastodon.android" minSdk 23 targetSdk 33 - versionCode 64 + versionCode 65 versionName "2.0.4" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" resConfigs "ar-rSA", "be-rBY", "bn-rBD", "bs-rBA", "ca-rES", "cs-rCZ", "da-rDK", "de-rDE", "el-rGR", "es-rES", "eu-rES", "fa-rIR", "fi-rFI", "fil-rPH", "fr-rFR", "ga-rIE", "gd-rGB", "gl-rES", "hi-rIN", "hr-rHR", "hu-rHU", "hy-rAM", "ig-rNG", "in-rID", "is-rIS", "it-rIT", "iw-rIL", "ja-rJP", "kab", "ko-rKR", "my-rMM", "nl-rNL", "no-rNO", "oc-rFR", "pl-rPL", "pt-rBR", "pt-rPT", "ro-rRO", "ru-rRU", "si-rLK", "sl-rSI", "sv-rSE", "th-rTH", "tr-rTR", "uk-rUA", "ur-rIN", "vi-rVN", "zh-rCN", "zh-rTW" diff --git a/mastodon/src/main/java/org/joinmastodon/android/MainActivity.java b/mastodon/src/main/java/org/joinmastodon/android/MainActivity.java index 99cfcc77f..7d25fa056 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/MainActivity.java +++ b/mastodon/src/main/java/org/joinmastodon/android/MainActivity.java @@ -136,7 +136,7 @@ public class MainActivity extends FragmentStackActivity{ } public void openSearchQuery(String q, String accountID, int progressText, boolean fromSearch){ - new GetSearchResults(q, null, true) + new GetSearchResults(q, null, true, null, 0, 0) .setCallback(new Callback<>(){ @Override public void onSuccess(SearchResults result){ diff --git a/mastodon/src/main/java/org/joinmastodon/android/api/requests/search/GetSearchResults.java b/mastodon/src/main/java/org/joinmastodon/android/api/requests/search/GetSearchResults.java index 0407bb702..f368edb21 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/api/requests/search/GetSearchResults.java +++ b/mastodon/src/main/java/org/joinmastodon/android/api/requests/search/GetSearchResults.java @@ -4,13 +4,19 @@ import org.joinmastodon.android.api.MastodonAPIRequest; import org.joinmastodon.android.model.SearchResults; public class GetSearchResults extends MastodonAPIRequest{ - public GetSearchResults(String query, Type type, boolean resolve){ + public GetSearchResults(String query, Type type, boolean resolve, String maxID, int offset, int count){ super(HttpMethod.GET, "/search", SearchResults.class); addQueryParameter("q", query); if(type!=null) addQueryParameter("type", type.name().toLowerCase()); if(resolve) addQueryParameter("resolve", "true"); + if(maxID!=null) + addQueryParameter("max_id", maxID); + if(offset>0) + addQueryParameter("offset", String.valueOf(offset)); + if(count>0) + addQueryParameter("limit", String.valueOf(count)); } public GetSearchResults limit(int limit){ diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/ComposeAccountSearchFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/ComposeAccountSearchFragment.java index 2b55f11e9..1660fcf97 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/ComposeAccountSearchFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/ComposeAccountSearchFragment.java @@ -47,7 +47,7 @@ public class ComposeAccountSearchFragment extends BaseAccountListFragment{ @Override protected void doLoadData(int offset, int count){ refreshing=true; - currentRequest=new GetSearchResults(currentQuery, GetSearchResults.Type.ACCOUNTS, false) + currentRequest=new GetSearchResults(currentQuery, GetSearchResults.Type.ACCOUNTS, false, null, 0, 0) .setCallback(new SimpleCallback<>(this){ @Override public void onSuccess(SearchResults result){ diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/discover/SearchFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/discover/SearchFragment.java index 62deef4ab..fcbb6f19a 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/discover/SearchFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/discover/SearchFragment.java @@ -3,7 +3,6 @@ package org.joinmastodon.android.fragments.discover; import android.app.Activity; import android.os.Build; import android.os.Bundle; -import android.view.View; import android.view.inputmethod.InputMethodManager; import org.joinmastodon.android.R; @@ -20,7 +19,6 @@ import org.joinmastodon.android.model.Status; import org.joinmastodon.android.ui.displayitems.AccountStatusDisplayItem; import org.joinmastodon.android.ui.displayitems.HashtagStatusDisplayItem; import org.joinmastodon.android.ui.displayitems.StatusDisplayItem; -import org.joinmastodon.android.ui.tabs.TabLayout; import org.joinmastodon.android.ui.utils.UiUtils; import org.parceler.Parcels; @@ -28,13 +26,12 @@ import java.util.ArrayList; import java.util.Collections; import java.util.EnumSet; import java.util.List; -import java.util.Objects; +import java.util.Set; import java.util.stream.Collectors; import me.grishka.appkit.Nav; import me.grishka.appkit.api.Callback; import me.grishka.appkit.api.ErrorResponse; -import me.grishka.appkit.utils.V; public class SearchFragment extends BaseStatusListFragment{ private String currentQuery; @@ -110,7 +107,7 @@ public class SearchFragment extends BaseStatusListFragment{ } @Override - protected void doLoadData(int offset, int count){ + protected void doLoadData(int _offset, int count){ GetSearchResults.Type type; if(currentFilter.size()==1){ type=switch(currentFilter.iterator().next()){ @@ -125,7 +122,21 @@ public class SearchFragment extends BaseStatusListFragment{ dataLoaded(); return; } - currentRequest=new GetSearchResults(currentQuery, type, true) + String maxID=null; + // TODO server-side bug + /*int offset=0; + if(_offset>0){ + if(type==GetSearchResults.Type.STATUSES){ + if(!preloadedData.isEmpty()) + maxID=preloadedData.get(preloadedData.size()-1).status.id; + else if(!data.isEmpty()) + maxID=data.get(data.size()-1).status.id; + }else{ + offset=_offset; + } + }*/ + int offset=_offset; + currentRequest=new GetSearchResults(currentQuery, type, type==null, maxID, offset, type==null ? 0 : count) .setCallback(new Callback<>(){ @Override public void onSuccess(SearchResults result){ @@ -139,12 +150,15 @@ public class SearchFragment extends BaseStatusListFragment{ results.add(new SearchResult(tag)); } if(result.statuses!=null){ - for(Status status:result.statuses) - results.add(new SearchResult(status)); + Set alreadyLoadedStatuses=data.stream().filter(r->r.type==SearchResult.Type.STATUS).map(r->r.status.id).collect(Collectors.toSet()); + for(Status status:result.statuses){ + if(!alreadyLoadedStatuses.contains(status.id)) + results.add(new SearchResult(status)); + } } prevDisplayItems=new ArrayList<>(displayItems); unfilteredResults=results; - onDataLoaded(filterSearchResults(results), false); + onDataLoaded(filterSearchResults(results), type!=null && !results.isEmpty()); } @Override diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/discover/SearchQueryFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/discover/SearchQueryFragment.java index ac8940eab..d21073812 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/discover/SearchQueryFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/discover/SearchQueryFragment.java @@ -43,9 +43,7 @@ import org.joinmastodon.android.ui.viewholders.AccountViewHolder; import org.joinmastodon.android.ui.viewholders.SimpleListItemViewHolder; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; -import java.util.List; import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -121,7 +119,7 @@ public class SearchQueryFragment extends MastodonRecyclerFragment(this){ @Override diff --git a/mastodon/src/main/java/org/joinmastodon/android/ui/viewcontrollers/ComposeAutocompleteViewController.java b/mastodon/src/main/java/org/joinmastodon/android/ui/viewcontrollers/ComposeAutocompleteViewController.java index b4983777f..b65730fe9 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/ui/viewcontrollers/ComposeAutocompleteViewController.java +++ b/mastodon/src/main/java/org/joinmastodon/android/ui/viewcontrollers/ComposeAutocompleteViewController.java @@ -15,15 +15,12 @@ import android.widget.TextView; import org.joinmastodon.android.R; import org.joinmastodon.android.api.requests.search.GetSearchResults; import org.joinmastodon.android.api.session.AccountSessionManager; -import org.joinmastodon.android.model.Account; import org.joinmastodon.android.model.Emoji; import org.joinmastodon.android.model.Hashtag; import org.joinmastodon.android.model.SearchResults; import org.joinmastodon.android.model.viewmodel.AccountViewModel; import org.joinmastodon.android.ui.BetterItemAnimator; import org.joinmastodon.android.ui.OutlineProviders; -import org.joinmastodon.android.ui.text.HtmlParser; -import org.joinmastodon.android.ui.utils.CustomEmojiHelper; import org.joinmastodon.android.ui.utils.HideableSingleViewRecyclerAdapter; import org.joinmastodon.android.ui.utils.UiUtils; import org.joinmastodon.android.ui.views.FilterChipView; @@ -240,7 +237,7 @@ public class ComposeAutocompleteViewController{ } private void doSearchUsers(){ - currentRequest=new GetSearchResults(lastText, GetSearchResults.Type.ACCOUNTS, false) + currentRequest=new GetSearchResults(lastText, GetSearchResults.Type.ACCOUNTS, false, null, 0, 0) .setCallback(new Callback<>(){ @Override public void onSuccess(SearchResults result){ @@ -276,7 +273,7 @@ public class ComposeAutocompleteViewController{ } private void doSearchHashtags(){ - currentRequest=new GetSearchResults(lastText, GetSearchResults.Type.HASHTAGS, false) + currentRequest=new GetSearchResults(lastText, GetSearchResults.Type.HASHTAGS, false, null, 0, 0) .setCallback(new Callback<>(){ @Override public void onSuccess(SearchResults result){