Skip to content

Commit

Permalink
Fixed wrong search results if changing query
Browse files Browse the repository at this point in the history
  • Loading branch information
devgianlu committed Apr 7, 2020
1 parent 139aca5 commit 99e89fa
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.snackbar.Snackbar;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -75,10 +77,10 @@ protected void onCreate(Bundle savedInstanceState) {
rmv.linearLayoutManager(RecyclerView.VERTICAL, false);
rmv.dividerDecoration(RecyclerView.VERTICAL);

final Button messageMore = findViewById(R.id.search_messageMore);
Button messageMore = findViewById(R.id.search_messageMore);
messageMore.setOnClickListener(view -> {
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://gianlu.xyz/projects/TorrentSearchEngine")));
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://swaggerhub.com/apis/devgianlu/torrent-search-engine")));
} catch (ActivityNotFoundException ex) {
messageMore.setVisibility(View.GONE);
}
Expand All @@ -87,7 +89,7 @@ protected void onCreate(Bundle savedInstanceState) {
searchApi = SearchApi.get();
}

private void showEnginesDialog(final List<SearchEngine> engines) {
private void showEnginesDialog(@NotNull List<SearchEngine> engines) {
CharSequence[] enginesNames = new CharSequence[engines.size()];

for (int i = 0; i < engines.size(); i++) {
Expand All @@ -99,7 +101,7 @@ private void showEnginesDialog(final List<SearchEngine> engines) {
Set<String> checkedEnginesSet = Prefs.getSet(PK.A2_SEARCH_ENGINES, null);

if (checkedEnginesSet == null) {
for (int i = 0; i < checkedEngines.length; i++) checkedEngines[i] = true;
Arrays.fill(checkedEngines, true);
} else {
for (String checkedEngine : checkedEnginesSet)
for (int i = 0; i < engines.size(); i++)
Expand Down Expand Up @@ -146,7 +148,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
}

@Override
public boolean onQueryTextSubmit(final String query) {
public boolean onQueryTextSubmit(@NotNull String query) {
message.setVisibility(View.GONE);
rmv.startLoading();
this.query = query;
Expand All @@ -169,12 +171,14 @@ public boolean onClose() {
rmv.hideList();
rmv.hideMessage();
searchView.setQuery(null, false);
this.query = null;
return false;
}

@Override
public void onResult(List<SearchResult> results, List<MissingSearchEngine> missingEngines, @Nullable String nextPageToken) {
public void onResult(@Nullable String query, @NotNull List<SearchResult> results, List<MissingSearchEngine> missingEngines, @Nullable String nextPageToken) {
if (!Objects.equals(this.query, query))
return;

message.setVisibility(View.GONE);

rmv.loadListData(new SearchResultsAdapter(this, results, nextPageToken, this));
Expand All @@ -189,7 +193,7 @@ public void onException(@NonNull Exception ex) {
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
public boolean onOptionsItemSelected(@NotNull MenuItem item) {
if (item.getItemId() == R.id.search_engines) {
showProgress(R.string.gathering_information);
searchApi.listSearchEngines(this, new SearchApi.OnResult<List<SearchEngine>>() {
Expand Down Expand Up @@ -248,7 +252,7 @@ private void showMissingEnginesDialog(List<MissingSearchEngine> missingEngines)
showDialog(builder);
}

private void showTorrentDialog(final Torrent torrent) {
private void showTorrentDialog(@NotNull Torrent torrent) {
LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.dialog_torrent, null, false);
SuperTextView engine = layout.findViewById(R.id.torrentDialog_engine);
SuperTextView size = layout.findViewById(R.id.torrentDialog_size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.gianlu.aria2app.R;
import com.gianlu.aria2app.api.search.MissingSearchEngine;
import com.gianlu.aria2app.api.search.SearchApi;
import com.gianlu.aria2app.api.search.SearchEngine;
import com.gianlu.aria2app.api.search.SearchResult;
import com.gianlu.aria2app.R;
import com.gianlu.commonutils.misc.InfiniteRecyclerView;

import java.util.ArrayList;
Expand Down Expand Up @@ -75,7 +75,7 @@ protected void moreContent(int page, @NonNull ContentProvider<SearchResult> prov
} else {
searchApi.search(token, SearchApi.RESULTS_PER_REQUEST, null, new SearchApi.OnSearch() {
@Override
public void onResult(List<SearchResult> results, List<MissingSearchEngine> missingEngines, @Nullable String nextPageToken) {
public void onResult(@Nullable String query, @NonNull List<SearchResult> results, List<MissingSearchEngine> missingEngines, @Nullable String nextPageToken) {
token = nextPageToken;
provider.onMoreContent(results);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void run() {
missingEngines.add(new MissingSearchEngine(SearchApi.this, missingEnginesArray.getJSONObject(i)));

String newToken = CommonUtils.optString(obj, "token");
post(() -> listener.onResult(results, missingEngines, newToken));
post(() -> listener.onResult(query, results, missingEngines, newToken));
} catch (IOException | StatusCodeException | JSONException ex) {
post(() -> listener.onException(ex));
}
Expand Down Expand Up @@ -197,7 +197,7 @@ public void cacheSearchEngines() {

@UiThread
public interface OnSearch {
void onResult(List<SearchResult> results, List<MissingSearchEngine> missingEngines, @Nullable String nextPageToken);
void onResult(@Nullable String query, @NonNull List<SearchResult> results, List<MissingSearchEngine> missingEngines, @Nullable String nextPageToken);

void onException(@NonNull Exception ex);
}
Expand Down

0 comments on commit 99e89fa

Please sign in to comment.