rip out view init foo again

This commit is contained in:
Christian Schabesberger 2024-07-19 14:32:06 +02:00
parent d526527e94
commit be54682b93
5 changed files with 10 additions and 30 deletions

View File

@ -45,9 +45,6 @@ class VideoPlayerView : FrameLayout {
get() = videoPlayerFragment.minLayoutRatio get() = videoPlayerFragment.minLayoutRatio
set(value) {videoPlayerFragment.minLayoutRatio = value} set(value) {videoPlayerFragment.minLayoutRatio = value}
var newPlayer:NewPlayer?
set(value) {videoPlayerFragment.newPlayer = value}
get() = videoPlayerFragment.newPlayer
@JvmOverloads @JvmOverloads
constructor( constructor(

View File

@ -41,24 +41,15 @@ import net.newpipe.newplayer.internal.model.VideoPlayerViewModelImpl
import net.newpipe.newplayer.internal.ui.VideoPlayerUI import net.newpipe.newplayer.internal.ui.VideoPlayerUI
import net.newpipe.newplayer.internal.ui.theme.VideoPlayerTheme import net.newpipe.newplayer.internal.ui.theme.VideoPlayerTheme
private const val TAG = "VideoPlayerFragment"
@AndroidEntryPoint @AndroidEntryPoint
class VideoPlayerFragment() : Fragment() { class VideoPlayerFragment() : Fragment() {
private val TAG = "VideoPlayerFragment"
private val viewModel: VideoPlayerViewModel by viewModels<VideoPlayerViewModelImpl>() private val viewModel: VideoPlayerViewModel by viewModels<VideoPlayerViewModelImpl>()
private var currentVideoRatio = 0F private var currentVideoRatio = 0F
private lateinit var composeView: ComposeView private lateinit var composeView: ComposeView
var newPlayer: NewPlayer? = null
set(value) {
if(context != null) {
viewModel.newPlayer = value
} else {
field = value
}
}
get() = viewModel.newPlayer ?: field
var minLayoutRatio = 4F / 3F var minLayoutRatio = 4F / 3F
set(value) { set(value) {
if (value <= 0 && maxLayoutRatio < minLayoutRatio) if (value <= 0 && maxLayoutRatio < minLayoutRatio)
@ -98,11 +89,6 @@ class VideoPlayerFragment() : Fragment() {
val view = inflater.inflate(R.layout.video_player_framgent, container, false) val view = inflater.inflate(R.layout.video_player_framgent, container, false)
composeView = view.findViewById(R.id.player_copose_view) composeView = view.findViewById(R.id.player_copose_view)
// late init player in case player was set before fragment was attached to a context
if (viewModel.newPlayer == null) {
viewModel.newPlayer = newPlayer
}
viewModel.listener = object : VideoPlayerViewModel.Listener { viewModel.listener = object : VideoPlayerViewModel.Listener {
override fun requestUpdateLayoutRatio(videoRatio: Float) { override fun requestUpdateLayoutRatio(videoRatio: Float) {
currentVideoRatio = videoRatio currentVideoRatio = videoRatio

View File

@ -87,7 +87,8 @@ interface VideoPlayerViewModel {
@HiltViewModel @HiltViewModel
class VideoPlayerViewModelImpl @Inject constructor( class VideoPlayerViewModelImpl @Inject constructor(
private val savedStateHandle: SavedStateHandle, private val savedStateHandle: SavedStateHandle,
application: Application application: Application,
override var newPlayer: NewPlayer?
) : AndroidViewModel(application), VideoPlayerViewModel { ) : AndroidViewModel(application), VideoPlayerViewModel {
// private // private
@ -99,20 +100,20 @@ class VideoPlayerViewModelImpl @Inject constructor(
override val uiState = mutableUiState.asStateFlow() override val uiState = mutableUiState.asStateFlow()
override val events: SharedFlow<VideoPlayerViewModel.Events> = mutableEvent override val events: SharedFlow<VideoPlayerViewModel.Events> = mutableEvent
override var listener: VideoPlayerViewModel.Listener? = null override var listener: VideoPlayerViewModel.Listener? = null
override var newPlayer: NewPlayer? = null
set(value) {
field = value
installExoPlayer()
}
override val player:Player? override val player:Player?
get() = newPlayer?.player get() = newPlayer?.player
init {
installExoPlayer()
}
private fun installExoPlayer() { private fun installExoPlayer() {
player?.let { player -> player?.let { player ->
player.addListener(object : Player.Listener { player.addListener(object : Player.Listener {
override fun onIsPlayingChanged(isPlaying: Boolean) { override fun onIsPlayingChanged(isPlaying: Boolean) {
super.onIsPlayingChanged(isPlaying) super.onIsPlayingChanged(isPlaying)
println("gurken playerstate: $isPlaying")
mutableUiState.update { mutableUiState.update {
it.copy(playing = isPlaying) it.copy(playing = isPlaying)
} }
@ -163,7 +164,6 @@ class VideoPlayerViewModelImpl @Inject constructor(
} }
override fun play() { override fun play() {
println("gurken player: $newPlayer")
newPlayer?.play() newPlayer?.play()
} }

View File

@ -112,13 +112,11 @@ fun VideoPlayerUI(
modifier = Modifier.fillMaxSize(), modifier = Modifier.fillMaxSize(),
factory = { context -> factory = { context ->
SurfaceView(context).also { view -> SurfaceView(context).also { view ->
println("gurken attach player: ${viewModel.player}")
viewModel.player?.setVideoSurfaceView(view) viewModel.player?.setVideoSurfaceView(view)
} }
}, update = { view -> }, update = { view ->
when (lifecycle) { when (lifecycle) {
Lifecycle.Event.ON_RESUME -> { Lifecycle.Event.ON_RESUME -> {
println("gurken reattach player: ${viewModel.player}")
viewModel.player?.setVideoSurfaceView(view) viewModel.player?.setVideoSurfaceView(view)
} }

View File

@ -43,7 +43,6 @@ class MainActivity : AppCompatActivity() {
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
val video_view = findViewById<VideoPlayerView>(R.id.new_player_video_view) val video_view = findViewById<VideoPlayerView>(R.id.new_player_video_view)
video_view.newPlayer = newPlayer
newPlayer.playWhenReady = true newPlayer.playWhenReady = true
newPlayer.setStream(getString(R.string.ccc_chromebooks_video)) newPlayer.setStream(getString(R.string.ccc_chromebooks_video))