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..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 @@ -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); @@ -43,11 +46,13 @@ 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)) { 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; } @@ -58,11 +63,14 @@ 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, 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..f80272a08 100644 --- a/News-Android-App/src/main/res/values/strings.xml +++ b/News-Android-App/src/main/res/values/strings.xml @@ -181,12 +181,16 @@ Title Body + Both + @string/pref_general_search_in_both @string/pref_general_search_in_title @string/pref_general_search_in_body + + 2 0 1 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 +