Skip to content

Commit b2d7c07

Browse files
committed
Merge pull request #4096 from wordpress-mobile/feature/reader-search-results
Feature/reader search results
2 parents 605a49f + b64f2a8 commit b2d7c07

21 files changed

+558
-258
lines changed

WordPress/src/main/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,10 @@
389389
android:name=".ui.reader.services.ReaderPostService"
390390
android:exported="false"
391391
android:label="Reader Post Service" />
392+
<service
393+
android:name=".ui.reader.services.ReaderSearchService"
394+
android:exported="false"
395+
android:label="Reader Search Service" />
392396
<service
393397
android:name=".ui.reader.services.ReaderCommentService"
394398
android:exported="false"

WordPress/src/main/java/org/wordpress/android/datasets/ReaderDatabase.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020
public class ReaderDatabase extends SQLiteOpenHelper {
2121
protected static final String DB_NAME = "wpreader.db";
22-
private static final int DB_VERSION = 116;
22+
private static final int DB_VERSION = 118;
2323

2424
/*
2525
* version history
@@ -68,6 +68,8 @@ public class ReaderDatabase extends SQLiteOpenHelper {
6868
* 114 - renamed tag_name to tag_slug in tag tables
6969
* 115 - added ReaderSearchTable
7070
* 116 - added tag_display_name to tag tables
71+
* 117 - changed tbl_posts.timestamp from INTEGER to REAL
72+
* 118 - renamed tbl_search_history to tbl_search_suggestions
7173
*/
7274

7375
/*

WordPress/src/main/java/org/wordpress/android/datasets/ReaderPostTable.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ protected static void createTables(SQLiteDatabase db) {
122122
+ " featured_image TEXT,"
123123
+ " featured_video TEXT,"
124124
+ " post_avatar TEXT,"
125-
+ " timestamp INTEGER DEFAULT 0,"
125+
+ " timestamp REAL DEFAULT 0,"
126126
+ " published TEXT,"
127127
+ " num_replies INTEGER DEFAULT 0,"
128128
+ " num_likes INTEGER DEFAULT 0,"
@@ -180,6 +180,9 @@ protected static int purge(SQLiteDatabase db) {
180180
numDeleted += purgePostsForTag(db, tag);
181181
}
182182

183+
// delete search results
184+
numDeleted += purgeSearchResults(db);
185+
183186
// delete posts in tbl_posts that no longer exist in tbl_post_tags
184187
numDeleted += db.delete("tbl_posts", "pseudo_id NOT IN (SELECT DISTINCT pseudo_id FROM tbl_post_tags)", null);
185188

@@ -211,6 +214,14 @@ private static int purgePostsForTag(SQLiteDatabase db, ReaderTag tag) {
211214
return numDeleted;
212215
}
213216

217+
/*
218+
* purge all posts that were retained from previous searches
219+
*/
220+
private static int purgeSearchResults(SQLiteDatabase db) {
221+
String[] args = {Integer.toString(ReaderTagType.SEARCH.toInt())};
222+
return db.delete("tbl_post_tags", "tag_type=?", args);
223+
}
224+
214225
public static int getNumPostsInBlog(long blogId) {
215226
if (blogId == 0) {
216227
return 0;
@@ -624,7 +635,7 @@ public static void addOrUpdatePosts(final ReaderTag tag, ReaderPostList posts) {
624635
stmtPosts.bindString(16, post.getFeaturedImage());
625636
stmtPosts.bindString(17, post.getFeaturedVideo());
626637
stmtPosts.bindString(18, post.getPostAvatar());
627-
stmtPosts.bindLong (19, post.timestamp);
638+
stmtPosts.bindDouble(19, post.timestamp);
628639
stmtPosts.bindString(20, post.getPublished());
629640
stmtPosts.bindLong (21, post.numReplies);
630641
stmtPosts.bindLong (22, post.numLikes);
@@ -839,7 +850,7 @@ private static ReaderPost getPostFromCursor(Cursor c) {
839850
post.setShortUrl(c.getString(c.getColumnIndex("short_url")));
840851
post.setPostAvatar(c.getString(c.getColumnIndex("post_avatar")));
841852

842-
post.timestamp = c.getLong(c.getColumnIndex("timestamp"));
853+
post.timestamp = c.getDouble(c.getColumnIndex("timestamp"));
843854
post.setPublished(c.getString(c.getColumnIndex("published")));
844855

845856
post.numReplies = c.getInt(c.getColumnIndex("num_replies"));

WordPress/src/main/java/org/wordpress/android/datasets/ReaderSearchTable.java

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,73 +9,71 @@
99
import org.wordpress.android.util.DateTimeUtils;
1010
import org.wordpress.android.util.SqlUtils;
1111

12-
import java.util.ArrayList;
1312
import java.util.Date;
14-
import java.util.List;
1513

1614
/**
17-
* user's reader search history
15+
* search suggestion table - populated by user's reader search history
1816
*/
1917
public class ReaderSearchTable {
2018

19+
public static final String COL_QUERY = "query_string";
20+
2121
protected static void createTables(SQLiteDatabase db) {
22-
db.execSQL("CREATE TABLE tbl_search_history ("
23-
+ " query_string TEXT NOT NULL COLLATE NOCASE PRIMARY KEY,"
24-
+ " date_used TEXT,"
25-
+ " counter INTEGER DEFAULT 1)");
22+
db.execSQL("CREATE TABLE tbl_search_suggestions ("
23+
+ " _id INTEGER PRIMARY KEY AUTOINCREMENT,"
24+
+ " query_string TEXT NOT NULL COLLATE NOCASE,"
25+
+ " date_used TEXT)");
26+
db.execSQL("CREATE UNIQUE INDEX idx_search_suggestions_query ON tbl_search_suggestions(query_string)");
2627
}
2728

2829
protected static void dropTables(SQLiteDatabase db) {
29-
db.execSQL("DROP TABLE IF EXISTS tbl_search_history");
30+
db.execSQL("DROP TABLE IF EXISTS tbl_search_suggestions");
3031
}
3132

3233
/*
33-
* adds the passed query string, updating the usage counter and date
34+
* adds the passed query string, updating the usage date
3435
*/
3536
public static void addOrUpdateQueryString(@NonNull String query) {
3637
String date = DateTimeUtils.javaDateToIso8601(new Date());
37-
int counter = getCounterForQueryString(query) + 1;
3838

3939
SQLiteStatement stmt = ReaderDatabase.getWritableDb().compileStatement(
40-
"INSERT OR REPLACE INTO tbl_search_history (query_string, date_used, counter) VALUES (?1,?2,?3)");
40+
"INSERT OR REPLACE INTO tbl_search_suggestions (query_string, date_used) VALUES (?1,?2)");
4141
try {
4242
stmt.bindString(1, query);
4343
stmt.bindString(2, date);
44-
stmt.bindLong (3, counter);
4544
stmt.execute();
4645
} finally {
4746
SqlUtils.closeStatement(stmt);
4847
}
4948
}
5049

51-
private static int getCounterForQueryString(@NonNull String query) {
52-
String[] args = {query};
53-
return SqlUtils.intForQuery(ReaderDatabase.getReadableDb(),
54-
"SELECT counter FROM tbl_search_history WHERE query_string=?", args);
50+
public static void deleteQueryString(@NonNull String query) {
51+
String[] args = new String[]{query};
52+
ReaderDatabase.getWritableDb().delete("tbl_search_suggestions", "query_string=?", args);
5553
}
5654

57-
public static List<String> getQueryStrings() {
58-
return getQueryStrings(null);
59-
}
60-
public static List<String> getQueryStrings(String filter) {
61-
List<String> queries = new ArrayList<>();
62-
Cursor cursor;
55+
/**
56+
* Returns a cursor containing query strings previously typed by the user
57+
* @param filter - filters the list using LIKE syntax (pass null for no filter)
58+
* @param max - limit the list to this many items (pass zero for no limit)
59+
*/
60+
public static Cursor getQueryStringCursor(String filter, int max) {
61+
String sql;
62+
String[] args;
6363
if (TextUtils.isEmpty(filter)) {
64-
cursor = ReaderDatabase.getReadableDb().rawQuery(
65-
"SELECT query_string FROM tbl_search_history ORDER BY date_used DESC", null);
64+
sql = "SELECT * FROM tbl_search_suggestions";
65+
args = null;
6666
} else {
67-
String likeFilter = filter + "%";
68-
cursor = ReaderDatabase.getReadableDb().rawQuery(
69-
"SELECT query_string FROM tbl_search_history WHERE query_string LIKE ? ORDER BY date_used DESC", new String[]{likeFilter});
67+
sql = "SELECT * FROM tbl_search_suggestions WHERE query_string LIKE ?";
68+
args = new String[]{filter + "%"};
7069
}
7170

72-
try {
73-
while (cursor.moveToNext()) {
74-
queries.add(cursor.getString(0));
75-
}
76-
return queries;
77-
} finally {
78-
SqlUtils.closeCursor(cursor);
71+
sql += " ORDER BY date_used DESC";
72+
73+
if (max > 0) {
74+
sql += " LIMIT " + max;
7975
}
76+
77+
return ReaderDatabase.getReadableDb().rawQuery(sql, args);
8078
}
8179
}

WordPress/src/main/java/org/wordpress/android/models/ReaderPost.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class ReaderPost {
3939
private String primaryTag; // most popular tag on this post based on usage in blog
4040
private String secondaryTag; // second most popular tag on this post based on usage in blog
4141

42-
public long timestamp; // used for sorting
42+
public double timestamp; // used for sorting
4343
private String published;
4444

4545
private String url;
@@ -116,14 +116,19 @@ public static ReaderPost fromJson(JSONObject json) {
116116
post.blogName = JSONUtils.getStringDecoded(json, "site_name");
117117
post.published = JSONUtils.getString(json, "date");
118118

119-
// the date a post was liked is only returned by the read/liked/ endpoint - if this exists,
120-
// set it as the timestamp so posts are sorted by the date they were liked rather than the
121-
// date they were published (the timestamp is used to sort posts when querying)
122-
String likeDate = JSONUtils.getString(json, "date_liked");
123-
if (!TextUtils.isEmpty(likeDate)) {
124-
post.timestamp = DateTimeUtils.iso8601ToTimestamp(likeDate);
119+
// a post's timestamp determines its sort order
120+
if (json.has("score")) {
121+
// search results include a "score" that should be used for sorting
122+
post.timestamp = json.optDouble("score");
125123
} else {
126-
post.timestamp = DateTimeUtils.iso8601ToTimestamp(post.published);
124+
// liked posts should be sorted by the date they were liked, otherwise sort by the
125+
// published date
126+
String likeDate = JSONUtils.getString(json, "date_liked");
127+
if (!TextUtils.isEmpty(likeDate)) {
128+
post.timestamp = DateTimeUtils.iso8601ToTimestamp(likeDate);
129+
} else {
130+
post.timestamp = DateTimeUtils.iso8601ToTimestamp(post.published);
131+
}
127132
}
128133

129134
// if the post is untitled, make up a title from the excerpt

WordPress/src/main/java/org/wordpress/android/models/ReaderTag.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ public static boolean isSameTag(ReaderTag tag1, ReaderTag tag2) {
150150
public boolean isPostsILike() {
151151
return tagType == ReaderTagType.DEFAULT && getEndpoint().endsWith("/read/liked");
152152
}
153+
153154
public boolean isFollowedSites() {
154155
return tagType == ReaderTagType.DEFAULT && getEndpoint().endsWith("/read/following");
155156
}

WordPress/src/main/java/org/wordpress/android/models/ReaderTagType.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ public enum ReaderTagType {
44
FOLLOWED,
55
DEFAULT,
66
RECOMMENDED,
7-
CUSTOM_LIST;
7+
CUSTOM_LIST,
8+
SEARCH;
89

910
private static final int INT_DEFAULT = 0;
1011
private static final int INT_FOLLOWED = 1;
1112
private static final int INT_RECOMMENDED = 2;
1213
private static final int INT_CUSTOM_LIST = 3;
14+
private static final int INT_SEARCH = 4;
1315

1416
public static ReaderTagType fromInt(int value) {
1517
switch (value) {
@@ -19,6 +21,8 @@ public static ReaderTagType fromInt(int value) {
1921
return FOLLOWED;
2022
case INT_CUSTOM_LIST:
2123
return CUSTOM_LIST;
24+
case INT_SEARCH:
25+
return SEARCH;
2226
default :
2327
return DEFAULT;
2428
}
@@ -32,6 +36,8 @@ public int toInt() {
3236
return INT_RECOMMENDED;
3337
case CUSTOM_LIST:
3438
return INT_CUSTOM_LIST;
39+
case SEARCH:
40+
return INT_SEARCH;
3541
default :
3642
return INT_DEFAULT;
3743
}

WordPress/src/main/java/org/wordpress/android/ui/FilteredRecyclerView.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,6 @@ public void removeOnScrollListener(RecyclerView.OnScrollListener listener) {
358358
}
359359
}
360360

361-
public RecyclerView getInternalRecyclerView() {
362-
return mRecyclerView;
363-
}
364-
365361
public void hideToolbar(){
366362
mAppBarLayout.setExpanded(false, true);
367363
}

WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderActivityLauncher.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import org.wordpress.android.R;
1616
import org.wordpress.android.analytics.AnalyticsTracker;
17-
import org.wordpress.android.datasets.ReaderSearchTable;
1817
import org.wordpress.android.models.AccountHelper;
1918
import org.wordpress.android.models.ReaderComment;
2019
import org.wordpress.android.models.ReaderPost;
@@ -134,22 +133,6 @@ public static void showReaderTagPreview(Context context, ReaderTag tag) {
134133
context.startActivity(intent);
135134
}
136135

137-
public static void showReaderSearchResults(Context context, String query) {
138-
if (TextUtils.isEmpty(query)) return;
139-
140-
// record this search query
141-
ReaderSearchTable.addOrUpdateQueryString(query);
142-
143-
// TODO: track analytics
144-
//AnalyticsTracker.track(AnalyticsTracker.Stat.???);
145-
146-
Intent intent = new Intent(context, ReaderPostListActivity.class);
147-
intent.putExtra(ReaderConstants.ARG_SEARCH_QUERY, query);
148-
intent.putExtra(ReaderConstants.ARG_POST_LIST_TYPE, ReaderPostListType.SEARCH_RESULTS);
149-
context.startActivity(intent);
150-
}
151-
152-
153136
/*
154137
* show comments for the passed Ids
155138
*/

WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderConstants.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package org.wordpress.android.ui.reader;
22

33
public class ReaderConstants {
4-
public static final int READER_MAX_POSTS_TO_REQUEST = 20; // max # posts to request when updating posts
5-
public static final int READER_MAX_POSTS_TO_DISPLAY = 200; // max # posts to display
6-
public static final int READER_MAX_COMMENTS_TO_REQUEST = 20; // max # top-level comments to request when updating comments
7-
public static final int READER_MAX_USERS_TO_DISPLAY = 500; // max # users to show in ReaderUserListActivity
8-
public static final long READER_AUTO_UPDATE_DELAY_MINUTES = 10; // 10 minute delay between automatic updates
9-
public static final int READER_MAX_RECOMMENDED_TO_REQUEST = 20; // max # of recommended blogs to request
4+
public static final int READER_MAX_POSTS_TO_REQUEST = 20; // max # posts to request when updating posts
5+
public static final int READER_MAX_SEARCH_POSTS_TO_REQUEST = 10; // max # posts to request when searching posts
6+
public static final int READER_MAX_POSTS_TO_DISPLAY = 200; // max # posts to display
7+
public static final int READER_MAX_COMMENTS_TO_REQUEST = 20; // max # top-level comments to request when updating comments
8+
public static final int READER_MAX_USERS_TO_DISPLAY = 500; // max # users to show in ReaderUserListActivity
9+
public static final long READER_AUTO_UPDATE_DELAY_MINUTES = 10; // 10 minute delay between automatic updates
10+
public static final int READER_MAX_RECOMMENDED_TO_REQUEST = 20; // max # of recommended blogs to request
1011

11-
public static final int MIN_FEATURED_IMAGE_WIDTH = 640; // min width for an image to be suitable featured image
12+
public static final int MIN_FEATURED_IMAGE_WIDTH = 640; // min width for an image to be suitable featured image
1213

13-
public static final String HTTP_REFERER_URL = "https://wordpress.com"; // referrer url for reader posts opened in a browser
14+
public static final String HTTP_REFERER_URL = "https://wordpress.com"; // referrer url for reader posts opened in a browser
1415

1516
// intent arguments / keys
1617
static final String ARG_TAG = "tag";

0 commit comments

Comments
 (0)