From 8d23571bbaf45062af5bd253281132935a68c0a1 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Wed, 5 Feb 2020 00:06:32 +0100 Subject: [PATCH] Search for feeds separately --- .../antennapod/adapter/SearchlistAdapter.java | 1 + .../antennapod/core/storage/DBTasks.java | 17 ++++++++++ .../antennapod/core/storage/FeedSearcher.java | 31 ++++++++++--------- .../antennapod/core/storage/PodDBAdapter.java | 22 ++++++++++++- 4 files changed, 56 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/adapter/SearchlistAdapter.java b/app/src/main/java/de/danoeh/antennapod/adapter/SearchlistAdapter.java index ad23478d6..08f2a5895 100644 --- a/app/src/main/java/de/danoeh/antennapod/adapter/SearchlistAdapter.java +++ b/app/src/main/java/de/danoeh/antennapod/adapter/SearchlistAdapter.java @@ -93,6 +93,7 @@ public class SearchlistAdapter extends BaseAdapter { } else if (component.getClass() == FeedItem.class) { final FeedItem item = (FeedItem) component; holder.title.setText(item.getTitle()); + holder.subtitle.setVisibility(View.VISIBLE); holder.subtitle.setText(result.getLocation().getDescription()); convertView.setAlpha(item.isPlayed() ? 0.5f : 1.0f); diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java index 91f656bf1..8ebe18dc0 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java @@ -553,6 +553,23 @@ public final class DBTasks { }); } + public static FutureTask> searchFeeds(final Context context, final String query) { + return new FutureTask<>(new QueryTask>(context) { + @Override + public void execute(PodDBAdapter adapter) { + Cursor cursor = adapter.searchFeeds(query); + List items = new ArrayList<>(); + if (cursor.moveToFirst()) { + do { + items.add(Feed.fromCursor(cursor)); + } while (cursor.moveToNext()); + } + setResult(items); + cursor.close(); + } + }); + } + /** * A runnable which should be used for database queries. The onCompletion * method is executed on the database executor to handle Cursors correctly. diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/FeedSearcher.java b/core/src/main/java/de/danoeh/antennapod/core/storage/FeedSearcher.java index 6d6359a42..100d0e910 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/FeedSearcher.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/FeedSearcher.java @@ -2,20 +2,15 @@ package de.danoeh.antennapod.core.storage; import android.content.Context; import androidx.annotation.NonNull; - import de.danoeh.antennapod.core.feed.Chapter; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.FutureTask; - -import de.danoeh.antennapod.core.R; +import de.danoeh.antennapod.core.feed.Feed; import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.SearchResult; -import de.danoeh.antennapod.core.util.comparator.InReverseChronologicalOrder; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.FutureTask; /** * Performs search on Feeds and FeedItems. @@ -40,9 +35,17 @@ public class FeedSearcher { public static List performSearch(final Context context, final String query, final long selectedFeed) { final List result = new ArrayList<>(); try { - FutureTask> searchTask = DBTasks.searchFeedItems(context, selectedFeed, query); - searchTask.run(); - final List items = searchTask.get(); + FutureTask> itemSearchTask = DBTasks.searchFeedItems(context, selectedFeed, query); + FutureTask> feedSearchTask = DBTasks.searchFeeds(context, query); + itemSearchTask.run(); + feedSearchTask.run(); + + final List feeds = feedSearchTask.get(); + for (Feed item : feeds) { + result.add(new SearchResult(item, null)); + } + + final List items = itemSearchTask.get(); for (FeedItem item : items) { SearchLocation location; if (safeContains(item.getTitle(), query)) { diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java b/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java index 48af7def0..749597840 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java @@ -284,10 +284,13 @@ public class PodDBAdapter { * Contains FEEDITEM_SEL_FI_SMALL as comma-separated list. Useful for raw queries. */ private static final String SEL_FI_SMALL_STR; + private static final String FEED_SEL_STD_STR; static { String selFiSmall = Arrays.toString(FEEDITEM_SEL_FI_SMALL); SEL_FI_SMALL_STR = selFiSmall.substring(1, selFiSmall.length() - 1); + String selFeedSmall = Arrays.toString(FEED_SEL_STD); + FEED_SEL_STD_STR = selFeedSmall.substring(1, selFeedSmall.length() - 1); } /** @@ -1283,7 +1286,24 @@ public class PodDBAdapter { + TABLE_NAME_FEED_ITEMS + "." + KEY_TITLE + " LIKE '%" + preparedQuery + "%' OR " + TABLE_NAME_SIMPLECHAPTERS + "." + KEY_TITLE + " LIKE '%" + preparedQuery + "%'" + ") ORDER BY " + KEY_PUBDATE + " DESC " - + "LIMIT 500"; + + "LIMIT 300"; + return db.rawQuery(query, null); + } + + /** + * Searches for the given query in various values of all feeds. + * + * @return A cursor with all search results in SEL_FI_EXTRA selection. + */ + public Cursor searchFeeds(String searchQuery) { + String preparedQuery = prepareSearchQuery(searchQuery); + String query = "SELECT " + FEED_SEL_STD_STR + " FROM " + TABLE_NAME_FEEDS + " WHERE " + + KEY_TITLE + " LIKE '%" + preparedQuery + "%' OR " + + KEY_CUSTOM_TITLE + " LIKE '%" + preparedQuery + "%' OR " + + KEY_AUTHOR + " LIKE '%" + preparedQuery + "%' OR " + + KEY_DESCRIPTION + " LIKE '%" + preparedQuery + "%' " + + "ORDER BY " + KEY_TITLE + " ASC " + + "LIMIT 300"; return db.rawQuery(query, null); }