Skip to content

Commit

Permalink
Allow searching both title and body at the same time
Browse files Browse the repository at this point in the history
This closes #1263

Signed-off-by: Marcus Nilsson <marcus.nilsson@genarp.com>
  • Loading branch information
mkanilsson committed Sep 18, 2023
1 parent 9cb10f5 commit b5b2e38
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 " +
Expand Down Expand Up @@ -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<String> columns, String searchString) {
String buildSQL = "SELECT " + RssItemDao.Properties.Id.columnName +
" FROM " + RssItemDao.TABLENAME;

Expand All @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<RssItem> PerformSearch(Context context, Long idFolder, Long idFeed, String searchString, SharedPreferences mPrefs) {
DatabaseConnectionOrm.SORT_DIRECTION sortDirection = DatabaseUtilsKt.getSortDirectionFromSettings(mPrefs);
DatabaseConnectionOrm dbConn = new DatabaseConnectionOrm(context);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions News-Android-App/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,17 @@

<string name="pref_general_search_in_title">Title</string>
<string name="pref_general_search_in_body">Body</string>
<string name="pref_general_search_in_both">Both</string>

<string-array name="pref_general_search_in" translatable="false">
<item>@string/pref_general_search_in_title</item>
<item>@string/pref_general_search_in_body</item>
<item>@string/pref_general_search_in_both</item>
</string-array>
<string-array name="pref_general_search_in_values" translatable="false">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>


Expand Down

0 comments on commit b5b2e38

Please sign in to comment.