Skip to content

Commit

Permalink
Merge pull request #1265 from mkanilsson/search
Browse files Browse the repository at this point in the history
Allow searching both title and body at the same time
  • Loading branch information
David-Development authored Sep 26, 2023
2 parents 21e7846 + afc04e7 commit 6ab7aeb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 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 @@ -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;
}
Expand All @@ -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;
Expand Down
4 changes: 4 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,12 +181,16 @@

<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_both</item>
<item>@string/pref_general_search_in_title</item>
<item>@string/pref_general_search_in_body</item>
</string-array>
<string-array name="pref_general_search_in_values" translatable="false">
<!-- The ordering here is weird as to not break users current setting -->
<item>2</item>
<item>0</item>
<item>1</item>
</string-array>
Expand Down
7 changes: 4 additions & 3 deletions News-Android-App/src/main/res/xml/pref_general.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,10 @@
android:key="sp_sort_order"
android:title="@string/pref_title_general_sort_order"
app:iconSpaceReserved="false"/>


<!-- Default is BOTH(2) -->
<ListPreference
android:defaultValue="0"
android:defaultValue="2"
android:entries="@array/pref_general_search_in"
android:entryValues="@array/pref_general_search_in_values"
android:key="sp_search_in"
Expand All @@ -140,4 +141,4 @@

</PreferenceCategory>

</androidx.preference.PreferenceScreen>
</androidx.preference.PreferenceScreen>

0 comments on commit 6ab7aeb

Please sign in to comment.