From b5b2e38a8111c95b2ee04afeb979fd7fe00acac2 Mon Sep 17 00:00:00 2001 From: Marcus Nilsson Date: Mon, 18 Sep 2023 14:13:57 +0200 Subject: [PATCH 1/2] Allow searching both title and body at the same time This closes #1263 Signed-off-by: Marcus Nilsson --- .../database/DatabaseConnectionOrm.java | 25 +++++++++++++++---- .../owncloudnewsreader/helper/Search.java | 14 ++++++++--- .../src/main/res/values/strings.xml | 3 +++ 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/database/DatabaseConnectionOrm.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/database/DatabaseConnectionOrm.java index 7d2eec13e..8ae14d2e5 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/database/DatabaseConnectionOrm.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/database/DatabaseConnectionOrm.java @@ -19,6 +19,7 @@ import java.util.Iterator; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; import javax.inject.Inject; import javax.inject.Named; @@ -550,17 +551,30 @@ else if(onlyStarredItems) public String getAllItemsIdsForFeedSQLFilteredByTitle(final long feedId, boolean onlyUnread, boolean onlyStarredItems, SORT_DIRECTION sortDirection, final String searchString) { String buildSQL = getAllItemsIdsForFeedSQL(feedId, onlyUnread, onlyStarredItems, sortDirection); + return new StringBuilder( - buildSQL).insert(buildSQL.indexOf("ORDER"), " AND " + RssItemDao.Properties.Title.columnName + " LIKE \"%" + searchString + "%\" ").toString(); + buildSQL).insert(buildSQL.indexOf("ORDER"), " AND " + getSearchSQLForColumn(RssItemDao.Properties.Title.columnName, searchString)).toString(); } public String getAllItemsIdsForFeedSQLFilteredByBodySQL(final long feedId, boolean onlyUnread, boolean onlyStarredItems, SORT_DIRECTION sortDirection, final String searchString) { String buildSQL = getAllItemsIdsForFeedSQL(feedId, onlyUnread, onlyStarredItems, sortDirection); - return new StringBuilder(buildSQL).insert( - buildSQL.indexOf("ORDER"), " AND " + RssItemDao.Properties.Body.columnName + " LIKE \"%" + searchString + "%\" ").toString(); + return new StringBuilder( + buildSQL).insert(buildSQL.indexOf("ORDER"), " AND " + getSearchSQLForColumn(RssItemDao.Properties.Body.columnName, searchString)).toString(); + } + + public String getAllItemsIdsForFeedSQLFilteredByTitleAndBodySQL(final long feedId, boolean onlyUnread, boolean onlyStarredItems, SORT_DIRECTION sortDirection, final String searchString) { + String buildSQL = getAllItemsIdsForFeedSQL(feedId, onlyUnread, onlyStarredItems, sortDirection); + String titleQuery = getSearchSQLForColumn(RssItemDao.Properties.Title.columnName, searchString); + String bodyQuery = getSearchSQLForColumn(RssItemDao.Properties.Body.columnName, searchString); + + return new StringBuilder( + buildSQL).insert(buildSQL.indexOf("ORDER"), " AND (" + titleQuery + " OR " + bodyQuery + ")" ).toString(); } + private String getSearchSQLForColumn(String column, String searchString) { + return column + " LIKE \"%" + searchString + "%\""; + } public Long getLowestItemIdByFolder(Long id_folder) { WhereCondition whereCondition = new WhereCondition.StringCondition(RssItemDao.Properties.FeedId.columnName + " IN " + @@ -598,7 +612,7 @@ else if(ID_FOLDER == ALL_STARRED_ITEMS.getValue()) return buildSQL; } - public String getAllItemsIdsForFolderSQLSearch(long ID_FOLDER, SORT_DIRECTION sortDirection, String searchPredicate, String searchString) { + public String getAllItemsIdsForFolderSQLSearch(long ID_FOLDER, SORT_DIRECTION sortDirection, List columns, String searchString) { String buildSQL = "SELECT " + RssItemDao.Properties.Id.columnName + " FROM " + RssItemDao.TABLENAME; @@ -613,7 +627,8 @@ public String getAllItemsIdsForFolderSQLSearch(long ID_FOLDER, SORT_DIRECTION so buildSQL += " WHERE "; } - buildSQL += searchPredicate + " LIKE \"%" + searchString + "%\""; + columns = columns.stream().map(c -> c + " LIKE \"%" + searchString + "%\"").collect(Collectors.toList()); + buildSQL += String.join(" OR ", columns); buildSQL += " ORDER BY " + RssItemDao.Properties.PubDate.columnName + " " + sortDirection.toString(); diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/helper/Search.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/helper/Search.java index bba83a1a0..1f24e1a64 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/helper/Search.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/helper/Search.java @@ -4,6 +4,8 @@ import android.content.SharedPreferences; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.List; import de.luhmer.owncloudnewsreader.SettingsActivity; @@ -15,7 +17,8 @@ public class Search { private static final String SEARCH_IN_TITLE = "0"; private static final String SEARCH_IN_BODY = "1"; - + private static final String SEARCH_IN_BOTH = "2"; + public static List PerformSearch(Context context, Long idFolder, Long idFeed, String searchString, SharedPreferences mPrefs) { DatabaseConnectionOrm.SORT_DIRECTION sortDirection = DatabaseUtilsKt.getSortDirectionFromSettings(mPrefs); DatabaseConnectionOrm dbConn = new DatabaseConnectionOrm(context); @@ -48,6 +51,8 @@ private static String getFeedSQLStatement(final long idFeed, sql = dbConn.getAllItemsIdsForFeedSQLFilteredByTitle(idFeed, false, false, sortDirection, searchString); } else if(searchIn.equals(SEARCH_IN_BODY)) { sql = dbConn.getAllItemsIdsForFeedSQLFilteredByBodySQL(idFeed, false, false, sortDirection, searchString); + } else if (searchIn.equals(SEARCH_IN_BOTH)) { + sql = dbConn.getAllItemsIdsForFeedSQLFilteredByTitleAndBodySQL(idFeed, false, false, sortDirection, searchString); } return sql; } @@ -60,9 +65,12 @@ private static String getFolderSQLStatement(final long ID_FOLDER, String sql = ""; String searchIn = mPrefs.getString(SettingsActivity.SP_SEARCH_IN,"0"); if(searchIn.equals(SEARCH_IN_TITLE)) { - sql = dbConn.getAllItemsIdsForFolderSQLSearch(ID_FOLDER, sortDirection, RssItemDao.Properties.Title.columnName, searchString); + sql = dbConn.getAllItemsIdsForFolderSQLSearch(ID_FOLDER, sortDirection, Collections.singletonList(RssItemDao.Properties.Title.columnName), searchString); } else if(searchIn.equals(SEARCH_IN_BODY)) { - sql = dbConn.getAllItemsIdsForFolderSQLSearch(ID_FOLDER, sortDirection, RssItemDao.Properties.Body.columnName, searchString); + sql = dbConn.getAllItemsIdsForFolderSQLSearch(ID_FOLDER, sortDirection, Collections.singletonList(RssItemDao.Properties.Body.columnName), searchString); + } else if(searchIn.equals(SEARCH_IN_BOTH)) { + var columns = Arrays.asList(RssItemDao.Properties.Body.columnName, RssItemDao.Properties.Title.columnName); + sql = dbConn.getAllItemsIdsForFolderSQLSearch(ID_FOLDER, sortDirection, columns, searchString); } return sql; diff --git a/News-Android-App/src/main/res/values/strings.xml b/News-Android-App/src/main/res/values/strings.xml index f855d3b63..513cf8fc4 100644 --- a/News-Android-App/src/main/res/values/strings.xml +++ b/News-Android-App/src/main/res/values/strings.xml @@ -181,14 +181,17 @@ Title Body + Both @string/pref_general_search_in_title @string/pref_general_search_in_body + @string/pref_general_search_in_both 0 1 + 2 From afc04e75d03d164d1a6b65181a2fc6a139499747 Mon Sep 17 00:00:00 2001 From: Marcus Nilsson Date: Fri, 22 Sep 2023 14:48:32 +0200 Subject: [PATCH 2/2] Change default of `Search In` to `BOTH` Signed-off-by: Marcus Nilsson --- .../java/de/luhmer/owncloudnewsreader/helper/Search.java | 4 ++-- News-Android-App/src/main/res/values/strings.xml | 5 +++-- News-Android-App/src/main/res/xml/pref_general.xml | 7 ++++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/helper/Search.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/helper/Search.java index 1f24e1a64..3373080a5 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/helper/Search.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/helper/Search.java @@ -46,7 +46,7 @@ private static String getFeedSQLStatement(final long idFeed, final DatabaseConnectionOrm dbConn, final SharedPreferences mPrefs) { String sql = ""; - String searchIn = mPrefs.getString(SettingsActivity.SP_SEARCH_IN,"0"); + String searchIn = mPrefs.getString(SettingsActivity.SP_SEARCH_IN, SEARCH_IN_BOTH); if(searchIn.equals(SEARCH_IN_TITLE)) { sql = dbConn.getAllItemsIdsForFeedSQLFilteredByTitle(idFeed, false, false, sortDirection, searchString); } else if(searchIn.equals(SEARCH_IN_BODY)) { @@ -63,7 +63,7 @@ private static String getFolderSQLStatement(final long ID_FOLDER, final DatabaseConnectionOrm dbConn, final SharedPreferences mPrefs) { String sql = ""; - String searchIn = mPrefs.getString(SettingsActivity.SP_SEARCH_IN,"0"); + String searchIn = mPrefs.getString(SettingsActivity.SP_SEARCH_IN, SEARCH_IN_BOTH); if(searchIn.equals(SEARCH_IN_TITLE)) { sql = dbConn.getAllItemsIdsForFolderSQLSearch(ID_FOLDER, sortDirection, Collections.singletonList(RssItemDao.Properties.Title.columnName), searchString); } else if(searchIn.equals(SEARCH_IN_BODY)) { diff --git a/News-Android-App/src/main/res/values/strings.xml b/News-Android-App/src/main/res/values/strings.xml index 513cf8fc4..f80272a08 100644 --- a/News-Android-App/src/main/res/values/strings.xml +++ b/News-Android-App/src/main/res/values/strings.xml @@ -184,14 +184,15 @@ Both + @string/pref_general_search_in_both @string/pref_general_search_in_title @string/pref_general_search_in_body - @string/pref_general_search_in_both + + 2 0 1 - 2 diff --git a/News-Android-App/src/main/res/xml/pref_general.xml b/News-Android-App/src/main/res/xml/pref_general.xml index 4f33746da..68320503e 100644 --- a/News-Android-App/src/main/res/xml/pref_general.xml +++ b/News-Android-App/src/main/res/xml/pref_general.xml @@ -113,9 +113,10 @@ android:key="sp_sort_order" android:title="@string/pref_title_general_sort_order" app:iconSpaceReserved="false"/> - + + - \ No newline at end of file +