Skip to content

Commit 169eff8

Browse files
Rewrite history fragment using Compose
1 parent fbafdeb commit 169eff8

19 files changed

Lines changed: 613 additions & 553 deletions

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ dependencies {
221221
implementation libs.androidx.recyclerview
222222
implementation libs.androidx.room.runtime
223223
implementation libs.androidx.room.rxjava3
224+
implementation libs.androidx.room.paging
224225
kapt libs.androidx.room.compiler
225226
implementation libs.androidx.swiperefreshlayout
226227
implementation libs.androidx.work.runtime

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

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
package org.schabi.newpipe.database.history.dao;
22

3+
import static org.schabi.newpipe.database.history.model.StreamHistoryEntity.JOIN_STREAM_ID;
4+
import static org.schabi.newpipe.database.history.model.StreamHistoryEntity.STREAM_ACCESS_DATE;
5+
import static org.schabi.newpipe.database.history.model.StreamHistoryEntity.STREAM_HISTORY_TABLE;
6+
import static org.schabi.newpipe.database.history.model.StreamHistoryEntity.STREAM_REPEAT_COUNT;
7+
import static org.schabi.newpipe.database.stream.StreamStatisticsEntry.STREAM_LATEST_DATE;
8+
import static org.schabi.newpipe.database.stream.StreamStatisticsEntry.STREAM_WATCH_COUNT;
9+
import static org.schabi.newpipe.database.stream.model.StreamEntity.STREAM_ID;
10+
import static org.schabi.newpipe.database.stream.model.StreamEntity.STREAM_TABLE;
11+
import static org.schabi.newpipe.database.stream.model.StreamStateEntity.JOIN_STREAM_ID_ALIAS;
12+
import static org.schabi.newpipe.database.stream.model.StreamStateEntity.STREAM_PROGRESS_MILLIS;
13+
import static org.schabi.newpipe.database.stream.model.StreamStateEntity.STREAM_STATE_TABLE;
14+
315
import androidx.annotation.Nullable;
16+
import androidx.paging.PagingSource;
417
import androidx.room.Dao;
518
import androidx.room.Query;
619
import androidx.room.RewriteQueriesToDropUnusedColumns;
@@ -13,18 +26,6 @@
1326

1427
import io.reactivex.rxjava3.core.Flowable;
1528

16-
import static org.schabi.newpipe.database.history.model.StreamHistoryEntity.JOIN_STREAM_ID;
17-
import static org.schabi.newpipe.database.history.model.StreamHistoryEntity.STREAM_ACCESS_DATE;
18-
import static org.schabi.newpipe.database.history.model.StreamHistoryEntity.STREAM_HISTORY_TABLE;
19-
import static org.schabi.newpipe.database.history.model.StreamHistoryEntity.STREAM_REPEAT_COUNT;
20-
import static org.schabi.newpipe.database.stream.StreamStatisticsEntry.STREAM_LATEST_DATE;
21-
import static org.schabi.newpipe.database.stream.StreamStatisticsEntry.STREAM_WATCH_COUNT;
22-
import static org.schabi.newpipe.database.stream.model.StreamEntity.STREAM_ID;
23-
import static org.schabi.newpipe.database.stream.model.StreamEntity.STREAM_TABLE;
24-
import static org.schabi.newpipe.database.stream.model.StreamStateEntity.JOIN_STREAM_ID_ALIAS;
25-
import static org.schabi.newpipe.database.stream.model.StreamStateEntity.STREAM_PROGRESS_MILLIS;
26-
import static org.schabi.newpipe.database.stream.model.StreamStateEntity.STREAM_STATE_TABLE;
27-
2829
@Dao
2930
public abstract class StreamHistoryDAO implements HistoryDAO<StreamHistoryEntity> {
3031
@Query("SELECT * FROM " + STREAM_HISTORY_TABLE
@@ -70,20 +71,45 @@ public Flowable<List<StreamHistoryEntity>> listByService(final int serviceId) {
7071

7172
@RewriteQueriesToDropUnusedColumns
7273
@Query("SELECT * FROM " + STREAM_TABLE
74+
// Select the latest entry and watch count for each stream id on history table
75+
+ " INNER JOIN "
76+
+ "(SELECT " + JOIN_STREAM_ID + ", "
77+
+ " MAX(" + STREAM_ACCESS_DATE + ") AS " + STREAM_LATEST_DATE + ", "
78+
+ " SUM(" + STREAM_REPEAT_COUNT + ") AS " + STREAM_WATCH_COUNT
79+
+ " FROM " + STREAM_HISTORY_TABLE
80+
+ " GROUP BY " + JOIN_STREAM_ID + ")"
81+
82+
+ " ON " + STREAM_ID + " = " + JOIN_STREAM_ID
83+
84+
+ " LEFT JOIN "
85+
+ "(SELECT " + JOIN_STREAM_ID + " AS " + JOIN_STREAM_ID_ALIAS + ", "
86+
+ STREAM_PROGRESS_MILLIS
87+
+ " FROM " + STREAM_STATE_TABLE + " )"
88+
+ " ON " + STREAM_ID + " = " + JOIN_STREAM_ID_ALIAS
89+
90+
+ " ORDER BY " + STREAM_LATEST_DATE + " DESC"
91+
)
92+
public abstract PagingSource<Integer, StreamStatisticsEntry> getHistoryOrderedByLastWatched();
7393

94+
@RewriteQueriesToDropUnusedColumns
95+
@Query("SELECT * FROM " + STREAM_TABLE
7496
// Select the latest entry and watch count for each stream id on history table
7597
+ " INNER JOIN "
7698
+ "(SELECT " + JOIN_STREAM_ID + ", "
7799
+ " MAX(" + STREAM_ACCESS_DATE + ") AS " + STREAM_LATEST_DATE + ", "
78100
+ " SUM(" + STREAM_REPEAT_COUNT + ") AS " + STREAM_WATCH_COUNT
79-
+ " FROM " + STREAM_HISTORY_TABLE + " GROUP BY " + JOIN_STREAM_ID + ")"
101+
+ " FROM " + STREAM_HISTORY_TABLE
102+
+ " GROUP BY " + JOIN_STREAM_ID + ")"
80103

81104
+ " ON " + STREAM_ID + " = " + JOIN_STREAM_ID
82105

83106
+ " LEFT JOIN "
84107
+ "(SELECT " + JOIN_STREAM_ID + " AS " + JOIN_STREAM_ID_ALIAS + ", "
85108
+ STREAM_PROGRESS_MILLIS
86109
+ " FROM " + STREAM_STATE_TABLE + " )"
87-
+ " ON " + STREAM_ID + " = " + JOIN_STREAM_ID_ALIAS)
88-
public abstract Flowable<List<StreamStatisticsEntry>> getStatistics();
110+
+ " ON " + STREAM_ID + " = " + JOIN_STREAM_ID_ALIAS
111+
112+
+ " ORDER BY " + STREAM_WATCH_COUNT + " DESC"
113+
)
114+
public abstract PagingSource<Integer, StreamStatisticsEntry> getHistoryOrderedByViewCount();
89115
}

app/src/main/java/org/schabi/newpipe/local/history/HistoryEntryAdapter.java

Lines changed: 0 additions & 108 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.schabi.newpipe.local.history
2+
3+
import android.os.Bundle
4+
import android.view.LayoutInflater
5+
import android.view.View
6+
import android.view.ViewGroup
7+
import androidx.appcompat.app.AppCompatActivity
8+
import androidx.compose.material3.MaterialTheme
9+
import androidx.compose.material3.Surface
10+
import androidx.fragment.app.Fragment
11+
import androidx.fragment.compose.content
12+
import org.schabi.newpipe.R
13+
import org.schabi.newpipe.ui.screens.HistoryScreen
14+
import org.schabi.newpipe.ui.theme.AppTheme
15+
16+
class HistoryFragment : Fragment() {
17+
override fun onCreateView(
18+
inflater: LayoutInflater,
19+
container: ViewGroup?,
20+
savedInstanceState: Bundle?,
21+
) = content {
22+
AppTheme {
23+
Surface(color = MaterialTheme.colorScheme.background) {
24+
HistoryScreen()
25+
}
26+
}
27+
}
28+
29+
override fun onViewCreated(
30+
view: View,
31+
savedInstanceState: Bundle?,
32+
) {
33+
(activity as AppCompatActivity).supportActionBar?.setTitle(R.string.title_activity_history)
34+
}
35+
}

app/src/main/java/org/schabi/newpipe/local/history/HistoryRecordManager.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,6 @@ public Flowable<List<StreamHistoryEntry>> getStreamHistorySortedById() {
171171
return streamHistoryTable.getHistorySortedById().subscribeOn(Schedulers.io());
172172
}
173173

174-
public Flowable<List<StreamStatisticsEntry>> getStreamStatistics() {
175-
return streamHistoryTable.getStatistics().subscribeOn(Schedulers.io());
176-
}
177-
178174
private boolean isStreamHistoryEnabled() {
179175
return sharedPreferences.getBoolean(streamHistoryKey, false);
180176
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.schabi.newpipe.local.history
2+
3+
import androidx.lifecycle.SavedStateHandle
4+
import androidx.lifecycle.ViewModel
5+
import androidx.paging.Pager
6+
import androidx.paging.PagingConfig
7+
import kotlinx.coroutines.flow.flatMapLatest
8+
import org.schabi.newpipe.App
9+
import org.schabi.newpipe.NewPipeDatabase
10+
11+
class HistoryViewModel(private val savedStateHandle: SavedStateHandle) : ViewModel() {
12+
private val historyDao = NewPipeDatabase.getInstance(App.instance).streamHistoryDAO()
13+
14+
val sortKey = savedStateHandle.getStateFlow(ORDER_KEY, SortKey.MOST_PLAYED)
15+
val historyItems = sortKey
16+
.flatMapLatest {
17+
Pager(PagingConfig(pageSize = 20)) {
18+
when (it) {
19+
SortKey.LAST_PLAYED -> historyDao.getHistoryOrderedByLastWatched()
20+
SortKey.MOST_PLAYED -> historyDao.getHistoryOrderedByViewCount()
21+
}
22+
}.flow
23+
}
24+
25+
fun updateOrder(sortKey: SortKey) {
26+
savedStateHandle[ORDER_KEY] = sortKey
27+
}
28+
29+
companion object {
30+
private const val ORDER_KEY = "order"
31+
}
32+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.schabi.newpipe.local.history
2+
3+
enum class SortKey {
4+
LAST_PLAYED,
5+
MOST_PLAYED
6+
}

0 commit comments

Comments
 (0)