Skip to content

Commit 4eae345

Browse files
Combine history ordered query methods into one
1 parent 0e422a4 commit 4eae345

2 files changed

Lines changed: 32 additions & 48 deletions

File tree

app/src/main/java/org/schabi/newpipe/database/history/dao/StreamHistoryDAO.java

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
1818
import androidx.room.Delete;
1919
import androidx.room.Insert;
2020
import androidx.room.Query;
21-
import androidx.room.RewriteQueriesToDropUnusedColumns;
21+
import androidx.room.RawQuery;
22+
import androidx.sqlite.db.SimpleSQLiteQuery;
23+
import androidx.sqlite.db.SupportSQLiteQuery;
2224

2325
import org.schabi.newpipe.database.history.model.StreamHistoryEntity;
2426
import org.schabi.newpipe.database.history.model.StreamHistoryEntry;
2527
import org.schabi.newpipe.database.stream.StreamStatisticsEntry;
28+
import org.schabi.newpipe.database.stream.model.StreamEntity;
29+
import org.schabi.newpipe.local.history.SortKey;
2630

2731
import java.util.List;
2832

@@ -31,6 +35,20 @@
3135

3236
@Dao
3337
public interface StreamHistoryDAO {
38+
String ORDERED_HISTORY_QUERY = "SELECT * FROM " + STREAM_TABLE
39+
+ " INNER JOIN "
40+
+ "(SELECT " + JOIN_STREAM_ID + ", "
41+
+ " MAX(" + STREAM_ACCESS_DATE + ") AS " + STREAM_LATEST_DATE + ", "
42+
+ " SUM(" + STREAM_REPEAT_COUNT + ") AS " + STREAM_WATCH_COUNT
43+
+ " FROM " + STREAM_HISTORY_TABLE
44+
+ " GROUP BY " + JOIN_STREAM_ID + ")"
45+
+ " ON " + STREAM_ID + " = " + JOIN_STREAM_ID
46+
+ " LEFT JOIN "
47+
+ "(SELECT " + JOIN_STREAM_ID + " AS " + JOIN_STREAM_ID_ALIAS + ", "
48+
+ STREAM_PROGRESS_MILLIS
49+
+ " FROM " + STREAM_STATE_TABLE + " )"
50+
+ " ON " + STREAM_ID + " = " + JOIN_STREAM_ID_ALIAS;
51+
3452
@Insert
3553
long insert(StreamHistoryEntity entity);
3654

@@ -60,47 +78,16 @@ public interface StreamHistoryDAO {
6078
+ " ORDER BY " + STREAM_ACCESS_DATE + " DESC")
6179
Flowable<List<StreamHistoryEntry>> getHistory();
6280

63-
@RewriteQueriesToDropUnusedColumns
64-
@Query("SELECT * FROM " + STREAM_TABLE
65-
// Select the latest entry and watch count for each stream id on history table
66-
+ " INNER JOIN "
67-
+ "(SELECT " + JOIN_STREAM_ID + ", "
68-
+ " MAX(" + STREAM_ACCESS_DATE + ") AS " + STREAM_LATEST_DATE + ", "
69-
+ " SUM(" + STREAM_REPEAT_COUNT + ") AS " + STREAM_WATCH_COUNT
70-
+ " FROM " + STREAM_HISTORY_TABLE
71-
+ " GROUP BY " + JOIN_STREAM_ID + ")"
72-
73-
+ " ON " + STREAM_ID + " = " + JOIN_STREAM_ID
74-
75-
+ " LEFT JOIN "
76-
+ "(SELECT " + JOIN_STREAM_ID + " AS " + JOIN_STREAM_ID_ALIAS + ", "
77-
+ STREAM_PROGRESS_MILLIS
78-
+ " FROM " + STREAM_STATE_TABLE + " )"
79-
+ " ON " + STREAM_ID + " = " + JOIN_STREAM_ID_ALIAS
80-
81-
+ " ORDER BY " + STREAM_LATEST_DATE + " DESC"
82-
)
83-
PagingSource<Integer, StreamStatisticsEntry> getHistoryOrderedByLastWatched();
84-
85-
@RewriteQueriesToDropUnusedColumns
86-
@Query("SELECT * FROM " + STREAM_TABLE
87-
// Select the latest entry and watch count for each stream id on history table
88-
+ " INNER JOIN "
89-
+ "(SELECT " + JOIN_STREAM_ID + ", "
90-
+ " MAX(" + STREAM_ACCESS_DATE + ") AS " + STREAM_LATEST_DATE + ", "
91-
+ " SUM(" + STREAM_REPEAT_COUNT + ") AS " + STREAM_WATCH_COUNT
92-
+ " FROM " + STREAM_HISTORY_TABLE
93-
+ " GROUP BY " + JOIN_STREAM_ID + ")"
94-
95-
+ " ON " + STREAM_ID + " = " + JOIN_STREAM_ID
96-
97-
+ " LEFT JOIN "
98-
+ "(SELECT " + JOIN_STREAM_ID + " AS " + JOIN_STREAM_ID_ALIAS + ", "
99-
+ STREAM_PROGRESS_MILLIS
100-
+ " FROM " + STREAM_STATE_TABLE + " )"
101-
+ " ON " + STREAM_ID + " = " + JOIN_STREAM_ID_ALIAS
102-
103-
+ " ORDER BY " + STREAM_WATCH_COUNT + " DESC"
104-
)
105-
PagingSource<Integer, StreamStatisticsEntry> getHistoryOrderedByViewCount();
81+
@RawQuery(observedEntities = {StreamStatisticsEntry.class, StreamEntity.class,
82+
StreamHistoryEntity.class})
83+
PagingSource<Integer, StreamStatisticsEntry> getOrderedHistoryByRaw(SupportSQLiteQuery query);
84+
85+
default PagingSource<Integer, StreamStatisticsEntry> getOrderedHistory(SortKey key) {
86+
final String orderBy = switch (key) {
87+
case LAST_PLAYED -> STREAM_LATEST_DATE;
88+
case MOST_PLAYED -> STREAM_WATCH_COUNT;
89+
};
90+
return getOrderedHistoryByRaw(new SimpleSQLiteQuery(ORDERED_HISTORY_QUERY + " ORDER BY "
91+
+ orderBy + " DESC"));
92+
}
10693
}

app/src/main/java/org/schabi/newpipe/local/history/HistoryViewModel.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ class HistoryViewModel(
2525
val historyItems = sortKey
2626
.flatMapLatest {
2727
Pager(PagingConfig(pageSize = 20)) {
28-
when (it) {
29-
SortKey.LAST_PLAYED -> historyDao.getHistoryOrderedByLastWatched()
30-
SortKey.MOST_PLAYED -> historyDao.getHistoryOrderedByViewCount()
31-
}
28+
historyDao.getOrderedHistory(it)
3229
}.flow
3330
}
3431
.map { pagingData -> pagingData.map { Stream(it) } }

0 commit comments

Comments
 (0)