diff --git a/app/build.gradle b/app/build.gradle index 8e6c40c26..ef7f5e31d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -88,6 +88,11 @@ android { kotlinOptions { jvmTarget = JavaVersion.VERSION_17 + freeCompilerArgs += [ + // Generate default method implementations for interfaces + // https://kotlinlang.org/docs/java-to-kotlin-interop.html#default-methods-in-interfaces + '-Xjvm-default=all' + ] } sourceSets { diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java index 83acfa4f2..80396860a 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java @@ -103,6 +103,7 @@ import org.schabi.newpipe.util.Constants; import org.schabi.newpipe.util.DeviceUtils; import org.schabi.newpipe.util.ExtractorHelper; import org.schabi.newpipe.util.InfoCache; +import org.schabi.newpipe.util.JavaFlow; import org.schabi.newpipe.util.ListHelper; import org.schabi.newpipe.util.Localization; import org.schabi.newpipe.util.NavigationHelper; @@ -130,6 +131,7 @@ import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers; import io.reactivex.rxjava3.disposables.CompositeDisposable; import io.reactivex.rxjava3.disposables.Disposable; import io.reactivex.rxjava3.schedulers.Schedulers; +import kotlin.Unit; @UnstableApi @AndroidEntryPoint @@ -311,6 +313,11 @@ public final class VideoDetailFragment newPlayerViewModel = new ViewModelProvider(this).get(NewPlayerViewModelImpl.class); newPlayerViewModel.setNewPlayer(this.newPlayer); newPlayerViewModel.setContentFitMode(ContentScale.FIT_INSIDE); + new JavaFlow().collect(newPlayerViewModel.getOnBackPressed(), result -> { + Log.d(TAG, "NewPlayer forwarded backpress"); + newPlayerViewModel.setNewPlayer(null); + newPlayer.pause(); + }); final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity); showComments = prefs.getBoolean(getString(R.string.show_comments_key), true); diff --git a/app/src/main/java/org/schabi/newpipe/util/JavaFlow.kt b/app/src/main/java/org/schabi/newpipe/util/JavaFlow.kt new file mode 100644 index 000000000..b1718b127 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/util/JavaFlow.kt @@ -0,0 +1,36 @@ +package org.schabi.newpipe.util + +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.cancel +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.onCompletion +import kotlinx.coroutines.flow.onStart +import kotlinx.coroutines.launch + +class JavaFlow( + private val coroutineScope: CoroutineScope = CoroutineScope(Dispatchers.Default) +) { + + interface OperatorCallback { + fun onStart() = Unit + fun onCompletion(thr: Throwable?) = Unit + fun onResult(result: T) + } + + fun collect( + flow: Flow, + operatorCallback: OperatorCallback, + ) { + coroutineScope.launch { + flow + .onStart { operatorCallback.onStart() } + .onCompletion { operatorCallback.onCompletion(it) } + .collect { operatorCallback.onResult(it) } + } + } + + fun close() { + coroutineScope.cancel() + } +}