convert all callbacks to flows

This commit is contained in:
Christian Schabesberger 2024-08-26 12:27:55 +02:00
parent f3d3ce380f
commit 63334f5893
5 changed files with 48 additions and 25 deletions

View File

@ -23,10 +23,17 @@ package net.newpipe.newplayer
import android.view.View import android.view.View
import androidx.core.view.ViewCompat import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat import androidx.core.view.WindowInsetsCompat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import net.newpipe.newplayer.model.VideoPlayerViewModel import net.newpipe.newplayer.model.VideoPlayerViewModel
class ActivityBrainSlug(val viewModel: VideoPlayerViewModel) { class ActivityBrainSlug(val viewModel: VideoPlayerViewModel) {
val brainSlugScope = CoroutineScope(Dispatchers.Main + Job())
var rootView: View? = null var rootView: View? = null
set(value) { set(value) {
field = value field = value
@ -65,9 +72,9 @@ class ActivityBrainSlug(val viewModel: VideoPlayerViewModel) {
} }
init { init {
viewModel.addCallbackListener(object : VideoPlayerViewModel.Listener { brainSlugScope.launch {
override fun onFullscreenToggle(isFullscreen: Boolean) { viewModel.uiState.collect { uiState ->
if (isFullscreen) { if (uiState.uiMode.fullscreen) {
removeSystemInsets() removeSystemInsets()
viewsToHideOnFullscreen.forEach { it.visibility = View.GONE } viewsToHideOnFullscreen.forEach { it.visibility = View.GONE }
fullscreenPlayerView?.visibility = View.VISIBLE fullscreenPlayerView?.visibility = View.VISIBLE
@ -77,8 +84,7 @@ class ActivityBrainSlug(val viewModel: VideoPlayerViewModel) {
fullscreenPlayerView?.visibility = View.GONE fullscreenPlayerView?.visibility = View.GONE
} }
} }
}
})
} }
fun addViewToHideOnFullscreen(view: View) { fun addViewToHideOnFullscreen(view: View) {

View File

@ -1,3 +1,23 @@
/* NewPlayer
*
* @author Christian Schabesberger
*
* Copyright (C) NewPipe e.V. 2024 <code(at)newpipe-ev.de>
*
* NewPlayer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NewPlayer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NewPlayer. If not, see <http://www.gnu.org/licenses/>.
*/
package net.newpipe.newplayer package net.newpipe.newplayer
class NewPlayerException : Exception { class NewPlayerException : Exception {

View File

@ -22,6 +22,7 @@ package net.newpipe.newplayer.model
import android.os.Bundle import android.os.Bundle
import androidx.media3.common.Player import androidx.media3.common.Player
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import net.newpipe.newplayer.NewPlayer import net.newpipe.newplayer.NewPlayer
import net.newpipe.newplayer.ui.ContentScale import net.newpipe.newplayer.ui.ContentScale
@ -34,8 +35,8 @@ interface VideoPlayerViewModel {
var minContentRatio: Float var minContentRatio: Float
var maxContentRatio: Float var maxContentRatio: Float
var contentFitMode: ContentScale var contentFitMode: ContentScale
val embeddedPlayerDraggedDownBy: SharedFlow<Float>
fun addCallbackListener(listener: Listener)
fun initUIState(instanceState: Bundle) fun initUIState(instanceState: Bundle)
fun play() fun play()
fun pause() fun pause()
@ -52,10 +53,4 @@ interface VideoPlayerViewModel {
fun finishFastSeek() fun finishFastSeek()
fun brightnessChange(changeRate: Float, systemBrightness: Float) fun brightnessChange(changeRate: Float, systemBrightness: Float)
fun volumeChange(changeRate: Float) fun volumeChange(changeRate: Float)
interface Listener {
fun onFullscreenToggle(isFullscreen: Boolean) {}
fun embeddedPlayerDraggedDown(offset: Float) {}
}
} }

View File

@ -26,15 +26,20 @@ import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.collection.mutableFloatFloatMapOf
import androidx.core.content.ContextCompat.getSystemService import androidx.core.content.ContextCompat.getSystemService
import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.media3.common.Player import androidx.media3.common.Player
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import javax.inject.Inject import javax.inject.Inject
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
@ -63,8 +68,6 @@ class VideoPlayerViewModelImpl @Inject constructor(
private val audioManager = private val audioManager =
getSystemService(application.applicationContext, AudioManager::class.java)!! getSystemService(application.applicationContext, AudioManager::class.java)!!
var callbackListeners: MutableList<VideoPlayerViewModel.Listener?> = ArrayList()
init { init {
val soundVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC).toFloat() / val soundVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC).toFloat() /
audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC).toFloat() audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC).toFloat()
@ -118,6 +121,9 @@ class VideoPlayerViewModelImpl @Inject constructor(
} }
} }
var mutableEmbeddedPlayerDraggedDownBy = MutableSharedFlow<Float> ()
override val embeddedPlayerDraggedDownBy = mutableEmbeddedPlayerDraggedDownBy.asSharedFlow()
private fun installNewPlayer() { private fun installNewPlayer() {
internalPlayer?.let { player -> internalPlayer?.let { player ->
Log.d(TAG, "Install player: ${player.videoSize.width}") Log.d(TAG, "Install player: ${player.videoSize.width}")
@ -195,10 +201,6 @@ class VideoPlayerViewModelImpl @Inject constructor(
} }
} }
override fun addCallbackListener(listener: VideoPlayerViewModel.Listener) {
callbackListeners.add(listener)
}
override fun play() { override fun play() {
hideUi() hideUi()
newPlayer?.play() newPlayer?.play()
@ -285,7 +287,9 @@ class VideoPlayerViewModelImpl @Inject constructor(
} }
override fun embeddedDraggedDown(offset: Float) { override fun embeddedDraggedDown(offset: Float) {
callbackListeners.forEach { it?.embeddedPlayerDraggedDown(offset) } viewModelScope.launch {
mutableEmbeddedPlayerDraggedDownBy.emit(offset)
}
} }
override fun fastSeek(count: Int) { override fun fastSeek(count: Int) {
@ -344,14 +348,12 @@ class VideoPlayerViewModelImpl @Inject constructor(
} }
override fun switchToEmbeddedView() { override fun switchToEmbeddedView() {
callbackListeners.forEach { it?.onFullscreenToggle(false) }
uiVisibilityJob?.cancel() uiVisibilityJob?.cancel()
finishFastSeek() finishFastSeek()
updateUiMode(UIModeState.EMBEDDED_VIDEO) updateUiMode(UIModeState.EMBEDDED_VIDEO)
} }
override fun switchToFullscreen() { override fun switchToFullscreen() {
callbackListeners.forEach { it?.onFullscreenToggle(true) }
uiVisibilityJob?.cancel() uiVisibilityJob?.cancel()
finishFastSeek() finishFastSeek()
updateUiMode(UIModeState.FULLSCREEN_VIDEO) updateUiMode(UIModeState.FULLSCREEN_VIDEO)

View File

@ -2,7 +2,10 @@ package net.newpipe.newplayer.model
import android.os.Bundle import android.os.Bundle
import androidx.media3.common.Player import androidx.media3.common.Player
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import net.newpipe.newplayer.NewPlayer import net.newpipe.newplayer.NewPlayer
import net.newpipe.newplayer.ui.ContentScale import net.newpipe.newplayer.ui.ContentScale
@ -13,15 +16,12 @@ open class VideoPlayerViewModelDummy : VideoPlayerViewModel {
override var minContentRatio = 4F / 3F override var minContentRatio = 4F / 3F
override var maxContentRatio = 16F / 9F override var maxContentRatio = 16F / 9F
override var contentFitMode = ContentScale.FIT_INSIDE override var contentFitMode = ContentScale.FIT_INSIDE
override val embeddedPlayerDraggedDownBy = MutableSharedFlow<Float>().asSharedFlow()
override fun initUIState(instanceState: Bundle) { override fun initUIState(instanceState: Bundle) {
println("dummy impl") println("dummy impl")
} }
override fun addCallbackListener(listener: VideoPlayerViewModel.Listener) {
println("dummy impl")
}
override fun play() { override fun play() {
println("dummy impl") println("dummy impl")
} }