From bc9cfc8183886398f2baa1745311a5ae4970b4e5 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 14 Oct 2021 09:16:15 +0800 Subject: Sort recent searches by search time. --- .../infinityforreddit/RedditDataRoomDatabase.java | 1 + .../recentsearchquery/RecentSearchQuery.java | 11 +++++++++++ .../recentsearchquery/RecentSearchQueryDao.java | 4 ++-- 3 files changed, 14 insertions(+), 2 deletions(-) (limited to 'app') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java index 6df86581..62ad3a81 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java @@ -351,6 +351,7 @@ public abstract class RedditDataRoomDatabase extends RoomDatabase { "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, " + "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"); } }; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/recentsearchquery/RecentSearchQuery.java b/app/src/main/java/ml/docilealligator/infinityforreddit/recentsearchquery/RecentSearchQuery.java index 7cbbe77a..5075842c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/recentsearchquery/RecentSearchQuery.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/recentsearchquery/RecentSearchQuery.java @@ -17,10 +17,13 @@ public class RecentSearchQuery { @NonNull @ColumnInfo(name = "search_query") private String searchQuery; + @ColumnInfo(name = "time") + private long time; public RecentSearchQuery(@NonNull String username, @NonNull String searchQuery) { this.username = username; this.searchQuery = searchQuery; + this.time = System.currentTimeMillis(); } @NonNull @@ -40,4 +43,12 @@ public class RecentSearchQuery { public void setSearchQuery(@NonNull String searchQuery) { this.searchQuery = searchQuery; } + + public long getTime() { + return time; + } + + public void setTime(long time) { + this.time = time; + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/recentsearchquery/RecentSearchQueryDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/recentsearchquery/RecentSearchQueryDao.java index 162b02f7..ae3ec0ee 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/recentsearchquery/RecentSearchQueryDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/recentsearchquery/RecentSearchQueryDao.java @@ -14,10 +14,10 @@ public interface RecentSearchQueryDao { @Insert(onConflict = OnConflictStrategy.REPLACE) 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> 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 getAllRecentSearchQueries(String username); @Delete -- cgit v1.2.3