Sort recent searches by search time.

This commit is contained in:
Alex Ning 2021-10-14 09:16:15 +08:00
parent 31d7592964
commit bc9cfc8183
3 changed files with 14 additions and 2 deletions

View File

@ -351,6 +351,7 @@ public abstract class RedditDataRoomDatabase extends RoomDatabase {
"username TEXT NOT NULL, subreddit_name TEXT NOT NULL, " + "username TEXT NOT NULL, subreddit_name TEXT NOT NULL, " +
"PRIMARY KEY(path, username, subreddit_name), FOREIGN KEY(path) REFERENCES multi_reddits(path) ON DELETE CASCADE ON UPDATE CASCADE, " + "PRIMARY KEY(path, username, subreddit_name), FOREIGN KEY(path) REFERENCES multi_reddits(path) ON DELETE CASCADE ON UPDATE CASCADE, " +
"FOREIGN KEY(username) REFERENCES multi_reddits(username) ON DELETE CASCADE ON UPDATE CASCADE)"); "FOREIGN KEY(username) REFERENCES multi_reddits(username) ON DELETE CASCADE ON UPDATE CASCADE)");
database.execSQL("ALTER TABLE recent_search_queries ADD COLUMN time INTEGER DEFAULT 0 NOT NULL");
} }
}; };
} }

View File

@ -17,10 +17,13 @@ public class RecentSearchQuery {
@NonNull @NonNull
@ColumnInfo(name = "search_query") @ColumnInfo(name = "search_query")
private String searchQuery; private String searchQuery;
@ColumnInfo(name = "time")
private long time;
public RecentSearchQuery(@NonNull String username, @NonNull String searchQuery) { public RecentSearchQuery(@NonNull String username, @NonNull String searchQuery) {
this.username = username; this.username = username;
this.searchQuery = searchQuery; this.searchQuery = searchQuery;
this.time = System.currentTimeMillis();
} }
@NonNull @NonNull
@ -40,4 +43,12 @@ public class RecentSearchQuery {
public void setSearchQuery(@NonNull String searchQuery) { public void setSearchQuery(@NonNull String searchQuery) {
this.searchQuery = searchQuery; this.searchQuery = searchQuery;
} }
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
} }

View File

@ -14,10 +14,10 @@ public interface RecentSearchQueryDao {
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(RecentSearchQuery recentSearchQuery); void insert(RecentSearchQuery recentSearchQuery);
@Query("SELECT * FROM recent_search_queries WHERE username = :username") @Query("SELECT * FROM recent_search_queries WHERE username = :username ORDER BY time DESC")
LiveData<List<RecentSearchQuery>> getAllRecentSearchQueriesLiveData(String username); LiveData<List<RecentSearchQuery>> getAllRecentSearchQueriesLiveData(String username);
@Query("SELECT * FROM recent_search_queries WHERE username = :username") @Query("SELECT * FROM recent_search_queries WHERE username = :username ORDER BY time DESC")
List<RecentSearchQuery> getAllRecentSearchQueries(String username); List<RecentSearchQuery> getAllRecentSearchQueries(String username);
@Delete @Delete