put viewmodel into fragment

This commit is contained in:
Christian Schabesberger 2024-07-10 16:31:08 +02:00
parent e1d8447304
commit 70d7f55cf6
6 changed files with 33 additions and 22 deletions

View File

@ -68,7 +68,6 @@ android {
} }
dependencies { dependencies {
implementation(libs.androidx.core.ktx) implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat) implementation(libs.androidx.appcompat)
implementation(libs.material) implementation(libs.material)
@ -83,7 +82,7 @@ dependencies {
implementation(libs.hilt.android) implementation(libs.hilt.android)
implementation(libs.androidx.lifecycle.viewmodel.compose) implementation(libs.androidx.lifecycle.viewmodel.compose)
implementation(libs.androidx.foundation) implementation(libs.androidx.foundation)
implementation(libs.androidx.runtime.livedata) implementation(libs.androidx.fragment.ktx)
ksp(libs.hilt.android.compiler) ksp(libs.hilt.android.compiler)
ksp(libs.androidx.hilt.compiler) ksp(libs.androidx.hilt.compiler)
implementation(libs.androidx.hilt.navigation.compose) implementation(libs.androidx.hilt.navigation.compose)

View File

@ -27,13 +27,19 @@ import android.view.ViewGroup
import androidx.compose.ui.platform.ComposeView import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.hilt.navigation.compose.hiltViewModel
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import net.newpipe.newplayer.model.VideoPlayerViewModel
import net.newpipe.newplayer.model.VideoPlayerViewModelImpl
import net.newpipe.newplayer.ui.VideoPlayerControllerUI import net.newpipe.newplayer.ui.VideoPlayerControllerUI
import net.newpipe.newplayer.ui.VideoPlayerUI import net.newpipe.newplayer.ui.VideoPlayerUI
import net.newpipe.newplayer.ui.theme.VideoPlayerTheme import net.newpipe.newplayer.ui.theme.VideoPlayerTheme
@AndroidEntryPoint @AndroidEntryPoint
class PlayerFragment : Fragment() { class PlayerFragment() : Fragment() {
private val viewModel: VideoPlayerViewModel by viewModels<VideoPlayerViewModelImpl>()
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
@ -47,7 +53,7 @@ class PlayerFragment : Fragment() {
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed) setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
setContent { setContent {
VideoPlayerTheme { VideoPlayerTheme {
VideoPlayerUI() VideoPlayerUI(viewModel)
} }
} }
} }

View File

@ -34,14 +34,14 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
data class VideoPlayerUIState( data class VideoPlayerUIState(
val playing: Boolean,
var fullscreen: Boolean, var fullscreen: Boolean,
var playing: Boolean,
var uiVissible: Boolean var uiVissible: Boolean
){ ){
companion object { companion object {
val DEFAULT = VideoPlayerUIState( val DEFAULT = VideoPlayerUIState(
playing = true,
fullscreen = false, fullscreen = false,
playing = false,
uiVissible = false uiVissible = false
) )
} }
@ -52,6 +52,7 @@ interface VideoPlayerViewModel {
val uiState: StateFlow<VideoPlayerUIState> val uiState: StateFlow<VideoPlayerUIState>
fun play() fun play()
fun pause() fun pause()
fun uiResume()
fun prevStream() fun prevStream()
fun nextStream() fun nextStream()
fun switchToFullscreen() fun switchToFullscreen()
@ -81,17 +82,22 @@ class VideoPlayerViewModelImpl @Inject constructor(
override fun play() { override fun play() {
player.play() player.play()
mutableUiState.update { mutableUiState.update {
it.copy(playing = true) it.copy(playing = player.isPlaying)
} }
} }
override fun pause() { override fun pause() {
player.pause() player.pause()
mutableUiState.update { mutableUiState.update {
it.copy(playing = false) it.copy(playing = player.isPlaying)
} }
} }
override fun uiResume() {
play()
}
override fun prevStream() { override fun prevStream() {
println("imeplement prev stream") println("imeplement prev stream")
} }
@ -136,6 +142,10 @@ class VideoPlayerViewModelImpl @Inject constructor(
println("dummy pause") println("dummy pause")
} }
override fun uiResume() {
println("dummy ui resume")
}
override fun prevStream() { override fun prevStream() {
println("dummy impl") println("dummy impl")
} }

View File

@ -20,6 +20,7 @@
package net.newpipe.newplayer.ui package net.newpipe.newplayer.ui
import android.view.SurfaceView
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Surface import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
@ -37,15 +38,13 @@ import androidx.compose.ui.viewinterop.AndroidView
import androidx.hilt.navigation.compose.hiltViewModel import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.Lifecycle import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver import androidx.lifecycle.LifecycleEventObserver
import androidx.media3.ui.PlayerView
import net.newpipe.newplayer.model.VideoPlayerViewModel import net.newpipe.newplayer.model.VideoPlayerViewModel
import net.newpipe.newplayer.model.VideoPlayerViewModelImpl import net.newpipe.newplayer.model.VideoPlayerViewModelImpl
import net.newpipe.newplayer.ui.theme.VideoPlayerTheme import net.newpipe.newplayer.ui.theme.VideoPlayerTheme
@Composable @Composable
fun VideoPlayerUI( fun VideoPlayerUI(
viewModel: VideoPlayerViewModel = hiltViewModel<VideoPlayerViewModelImpl>() viewModel: VideoPlayerViewModel
) { ) {
val uiState by viewModel.uiState.collectAsState() val uiState by viewModel.uiState.collectAsState()
@ -73,29 +72,25 @@ fun VideoPlayerUI(
AndroidView( AndroidView(
modifier = Modifier.fillMaxSize(), modifier = Modifier.fillMaxSize(),
factory = { context -> factory = { context ->
PlayerView(context).also { SurfaceView(context).also {
it.player = viewModel.player viewModel.player?.setVideoSurfaceView(it)
it.useController = false
} }
}, update = { }, update = {
when (lifecycle) { when (lifecycle) {
Lifecycle.Event.ON_PAUSE -> { Lifecycle.Event.ON_PAUSE -> {
it.onPause()
viewModel.pause() viewModel.pause()
} }
Lifecycle.Event.ON_RESUME -> { Lifecycle.Event.ON_RESUME -> {
it.onResume() viewModel.uiResume()
}
Lifecycle.Event.ON_START -> {
viewModel.play()
} }
else -> Unit else -> Unit
} }
}) })
val isPlaying = viewModel.player!!.isPlaying
println("is Player playing: $isPlaying")
VideoPlayerControllerUI( VideoPlayerControllerUI(
isPlaying = viewModel.player?.isPlaying ?: false, isPlaying = viewModel.player?.isPlaying ?: false,
isFullscreen = uiState.fullscreen, isFullscreen = uiState.fullscreen,

View File

@ -30,7 +30,7 @@
<androidx.compose.ui.platform.ComposeView <androidx.compose.ui.platform.ComposeView
android:id="@+id/player_copose_view" android:id="@+id/player_copose_view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="wrap_content" />
</FrameLayout> </FrameLayout>

View File

@ -38,6 +38,7 @@ hiltNavigationCompose = "1.2.0"
lifecycleViewmodelCompose = "2.8.3" lifecycleViewmodelCompose = "2.8.3"
kspVersion = "1.9.0-1.0.13" kspVersion = "1.9.0-1.0.13"
runtimeLivedata = "1.7.0-beta04" runtimeLivedata = "1.7.0-beta04"
fragmentKtx = "1.8.1"
[libraries] [libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" } androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
@ -60,7 +61,7 @@ androidx-hilt-navigation-compose = { group = "androidx.hilt", name = "hilt-navig
hilt-android-compiler = { group = "com.google.dagger", name = "hilt-android-compiler", version.ref = "hiltAndroid" } hilt-android-compiler = { group = "com.google.dagger", name = "hilt-android-compiler", version.ref = "hiltAndroid" }
androidx-lifecycle-viewmodel-compose = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-compose", version.ref = "lifecycleViewmodelCompose" } androidx-lifecycle-viewmodel-compose = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-compose", version.ref = "lifecycleViewmodelCompose" }
androidx-foundation = { group = "androidx.compose.foundation", name = "foundation", version.ref = "uiTooling" } androidx-foundation = { group = "androidx.compose.foundation", name = "foundation", version.ref = "uiTooling" }
androidx-runtime-livedata = { group = "androidx.compose.runtime", name = "runtime-livedata", version.ref = "runtimeLivedata" } androidx-fragment-ktx = { group = "androidx.fragment", name = "fragment-ktx", version.ref = "fragmentKtx" }
[plugins] [plugins]
android-application = { id = "com.android.application", version.ref = "agp" } android-application = { id = "com.android.application", version.ref = "agp" }