Skip to content

Commit 2836191

Browse files
Migrate related items fragment to Jetpack Compose (#11383)
* Rename .java to .kt * Migrate related items fragment to Jetpack Compose * Specify mode parameter explicitly * Rm unused class * Fix list item size * Added stream progress bar, separate stream and playlist thumbnails * Display message if no related streams are available * Dispose of related items when closing the video player * Add modifiers for no items message function * Implement remaining stream menu items * Improved stream composables * Use view model lifecycle scope * Make live color solid red * Use nested scroll modifier * Simplify determineItemViewMode()
1 parent 9d04a73 commit 2836191

30 files changed

+940
-395
lines changed

app/build.gradle

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ plugins {
99
id "kotlin-parcelize"
1010
id "checkstyle"
1111
id "org.sonarqube" version "4.0.0.2929"
12+
id "org.jetbrains.kotlin.plugin.compose" version "${kotlin_version}"
1213
}
1314

1415
android {
@@ -104,10 +105,6 @@ android {
104105
'META-INF/COPYRIGHT']
105106
}
106107
}
107-
108-
composeOptions {
109-
kotlinCompilerExtensionVersion = "1.5.3"
110-
}
111108
}
112109

113110
ext {
@@ -267,7 +264,7 @@ dependencies {
267264
implementation "com.github.lisawray.groupie:groupie-viewbinding:${groupieVersion}"
268265

269266
// Image loading
270-
implementation 'io.coil-kt:coil:2.7.0'
267+
implementation 'io.coil-kt:coil-compose:2.7.0'
271268

272269
// Markdown library for Android
273270
implementation "io.noties.markwon:core:${markwonVersion}"
@@ -289,10 +286,18 @@ dependencies {
289286
implementation "org.ocpsoft.prettytime:prettytime:5.0.8.Final"
290287

291288
// Jetpack Compose
292-
implementation(platform('androidx.compose:compose-bom:2024.02.01'))
293-
implementation 'androidx.compose.material3:material3'
289+
implementation(platform('androidx.compose:compose-bom:2024.06.00'))
290+
implementation 'androidx.compose.material3:material3:1.3.0-beta05'
291+
implementation 'androidx.compose.material3.adaptive:adaptive:1.0.0-beta04'
294292
implementation 'androidx.activity:activity-compose'
295293
implementation 'androidx.compose.ui:ui-tooling-preview'
294+
implementation 'androidx.compose.ui:ui-text:1.7.0-beta07' // Needed for parsing HTML to AnnotatedString
295+
implementation 'androidx.lifecycle:lifecycle-viewmodel-compose'
296+
implementation 'androidx.paging:paging-compose:3.3.2'
297+
implementation 'com.github.nanihadesuka:LazyColumnScrollbar:2.2.0'
298+
299+
// Coroutines interop
300+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-rx3:1.8.1'
296301

297302
/** Debugging **/
298303
// Memory leak detection

app/src/main/java/org/schabi/newpipe/database/stream/dao/StreamDAO.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import androidx.room.Query
88
import androidx.room.Transaction
99
import io.reactivex.rxjava3.core.Completable
1010
import io.reactivex.rxjava3.core.Flowable
11+
import io.reactivex.rxjava3.core.Maybe
1112
import org.schabi.newpipe.database.BasicDAO
1213
import org.schabi.newpipe.database.stream.model.StreamEntity
1314
import org.schabi.newpipe.database.stream.model.StreamEntity.Companion.STREAM_ID
@@ -27,7 +28,7 @@ abstract class StreamDAO : BasicDAO<StreamEntity> {
2728
abstract override fun listByService(serviceId: Int): Flowable<List<StreamEntity>>
2829

2930
@Query("SELECT * FROM streams WHERE url = :url AND service_id = :serviceId")
30-
abstract fun getStream(serviceId: Long, url: String): Flowable<List<StreamEntity>>
31+
abstract fun getStream(serviceId: Long, url: String): Maybe<StreamEntity>
3132

3233
@Query("UPDATE streams SET uploader_url = :uploaderUrl WHERE url = :url AND service_id = :serviceId")
3334
abstract fun setUploaderUrl(serviceId: Long, url: String, uploaderUrl: String): Completable

app/src/main/java/org/schabi/newpipe/database/stream/dao/StreamStateDAO.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.schabi.newpipe.database.stream.dao;
22

3+
import static org.schabi.newpipe.database.stream.model.StreamStateEntity.JOIN_STREAM_ID;
4+
import static org.schabi.newpipe.database.stream.model.StreamStateEntity.STREAM_STATE_TABLE;
5+
36
import androidx.room.Dao;
47
import androidx.room.Insert;
58
import androidx.room.OnConflictStrategy;
@@ -12,9 +15,7 @@
1215
import java.util.List;
1316

1417
import io.reactivex.rxjava3.core.Flowable;
15-
16-
import static org.schabi.newpipe.database.stream.model.StreamStateEntity.JOIN_STREAM_ID;
17-
import static org.schabi.newpipe.database.stream.model.StreamStateEntity.STREAM_STATE_TABLE;
18+
import io.reactivex.rxjava3.core.Maybe;
1819

1920
@Dao
2021
public interface StreamStateDAO extends BasicDAO<StreamStateEntity> {
@@ -32,7 +33,7 @@ default Flowable<List<StreamStateEntity>> listByService(final int serviceId) {
3233
}
3334

3435
@Query("SELECT * FROM " + STREAM_STATE_TABLE + " WHERE " + JOIN_STREAM_ID + " = :streamId")
35-
Flowable<List<StreamStateEntity>> getState(long streamId);
36+
Maybe<StreamStateEntity> getState(long streamId);
3637

3738
@Query("DELETE FROM " + STREAM_STATE_TABLE + " WHERE " + JOIN_STREAM_ID + " = :streamId")
3839
int deleteState(long streamId);

app/src/main/java/org/schabi/newpipe/fragments/list/videos/RelatedItemsFragment.java

Lines changed: 0 additions & 176 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.schabi.newpipe.fragments.list.videos
2+
3+
import android.os.Bundle
4+
import android.view.LayoutInflater
5+
import android.view.View
6+
import android.view.ViewGroup
7+
import androidx.compose.material3.MaterialTheme
8+
import androidx.compose.material3.Surface
9+
import androidx.compose.ui.platform.ComposeView
10+
import androidx.compose.ui.platform.ViewCompositionStrategy
11+
import androidx.core.os.bundleOf
12+
import androidx.fragment.app.Fragment
13+
import org.schabi.newpipe.extractor.stream.StreamInfo
14+
import org.schabi.newpipe.ktx.serializable
15+
import org.schabi.newpipe.ui.components.video.RelatedItems
16+
import org.schabi.newpipe.ui.theme.AppTheme
17+
import org.schabi.newpipe.util.KEY_INFO
18+
19+
class RelatedItemsFragment : Fragment() {
20+
override fun onCreateView(
21+
inflater: LayoutInflater,
22+
container: ViewGroup?,
23+
savedInstanceState: Bundle?
24+
): View {
25+
return ComposeView(requireContext()).apply {
26+
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
27+
setContent {
28+
AppTheme {
29+
Surface(color = MaterialTheme.colorScheme.background) {
30+
RelatedItems(requireArguments().serializable<StreamInfo>(KEY_INFO)!!)
31+
}
32+
}
33+
}
34+
}
35+
}
36+
37+
companion object {
38+
@JvmStatic
39+
fun getInstance(info: StreamInfo) = RelatedItemsFragment().apply {
40+
arguments = bundleOf(KEY_INFO to info)
41+
}
42+
}
43+
}

app/src/main/java/org/schabi/newpipe/fragments/list/videos/RelatedItemsInfo.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

app/src/main/java/org/schabi/newpipe/info_list/dialog/StreamDialogDefaultEntry.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@
4141
* </p>
4242
*/
4343
public enum StreamDialogDefaultEntry {
44-
SHOW_CHANNEL_DETAILS(R.string.show_channel_details, (fragment, item) ->
45-
fetchUploaderUrlIfSparse(fragment.requireContext(), item.getServiceId(), item.getUrl(),
46-
item.getUploaderUrl(), url -> openChannelFragment(fragment, item, url))
47-
),
44+
SHOW_CHANNEL_DETAILS(R.string.show_channel_details, (fragment, item) -> {
45+
final var activity = fragment.requireActivity();
46+
fetchUploaderUrlIfSparse(activity, item.getServiceId(), item.getUrl(),
47+
item.getUploaderUrl(), url -> openChannelFragment(activity, item, url));
48+
}),
4849

4950
/**
5051
* Enqueues the stream automatically to the current PlayerType.

app/src/main/java/org/schabi/newpipe/info_list/holder/StreamMiniInfoItemHolder.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ public void updateFromItem(final InfoItem infoItem,
6464
StreamStateEntity state2 = null;
6565
if (DependentPreferenceHelper
6666
.getPositionsInListsEnabled(itemProgressView.getContext())) {
67-
state2 = historyRecordManager.loadStreamState(infoItem)
68-
.blockingGet()[0];
67+
state2 = historyRecordManager.loadStreamState(infoItem).blockingGet();
6968
}
7069
if (state2 != null) {
7170
itemProgressView.setVisibility(View.VISIBLE);
@@ -120,7 +119,7 @@ public void updateState(final InfoItem infoItem,
120119
if (DependentPreferenceHelper.getPositionsInListsEnabled(itemProgressView.getContext())) {
121120
state = historyRecordManager
122121
.loadStreamState(infoItem)
123-
.blockingGet()[0];
122+
.blockingGet();
124123
}
125124
if (state != null && item.getDuration() > 0
126125
&& !StreamTypeUtil.isLiveStream(item.getStreamType())) {

app/src/main/java/org/schabi/newpipe/ktx/Bundle.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ package org.schabi.newpipe.ktx
33
import android.os.Bundle
44
import android.os.Parcelable
55
import androidx.core.os.BundleCompat
6+
import java.io.Serializable
67

78
inline fun <reified T : Parcelable> Bundle.parcelableArrayList(key: String?): ArrayList<T>? {
89
return BundleCompat.getParcelableArrayList(this, key, T::class.java)
910
}
11+
12+
inline fun <reified T : Serializable> Bundle.serializable(key: String?): T? {
13+
return BundleCompat.getSerializable(this, key, T::class.java)
14+
}

0 commit comments

Comments
 (0)