put viewmodel into fragment
This commit is contained in:
parent
e1d8447304
commit
70d7f55cf6
|
@ -68,7 +68,6 @@ android {
|
|||
}
|
||||
|
||||
dependencies {
|
||||
|
||||
implementation(libs.androidx.core.ktx)
|
||||
implementation(libs.androidx.appcompat)
|
||||
implementation(libs.material)
|
||||
|
@ -83,7 +82,7 @@ dependencies {
|
|||
implementation(libs.hilt.android)
|
||||
implementation(libs.androidx.lifecycle.viewmodel.compose)
|
||||
implementation(libs.androidx.foundation)
|
||||
implementation(libs.androidx.runtime.livedata)
|
||||
implementation(libs.androidx.fragment.ktx)
|
||||
ksp(libs.hilt.android.compiler)
|
||||
ksp(libs.androidx.hilt.compiler)
|
||||
implementation(libs.androidx.hilt.navigation.compose)
|
||||
|
|
|
@ -27,13 +27,19 @@ import android.view.ViewGroup
|
|||
import androidx.compose.ui.platform.ComposeView
|
||||
import androidx.compose.ui.platform.ViewCompositionStrategy
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.viewModels
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
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.VideoPlayerUI
|
||||
import net.newpipe.newplayer.ui.theme.VideoPlayerTheme
|
||||
|
||||
@AndroidEntryPoint
|
||||
class PlayerFragment : Fragment() {
|
||||
class PlayerFragment() : Fragment() {
|
||||
|
||||
private val viewModel: VideoPlayerViewModel by viewModels<VideoPlayerViewModelImpl>()
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
|
@ -47,7 +53,7 @@ class PlayerFragment : Fragment() {
|
|||
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
|
||||
setContent {
|
||||
VideoPlayerTheme {
|
||||
VideoPlayerUI()
|
||||
VideoPlayerUI(viewModel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,14 +34,14 @@ import kotlinx.coroutines.flow.asStateFlow
|
|||
import kotlinx.coroutines.flow.update
|
||||
|
||||
data class VideoPlayerUIState(
|
||||
val playing: Boolean,
|
||||
var fullscreen: Boolean,
|
||||
var playing: Boolean,
|
||||
var uiVissible: Boolean
|
||||
){
|
||||
companion object {
|
||||
val DEFAULT = VideoPlayerUIState(
|
||||
playing = true,
|
||||
fullscreen = false,
|
||||
playing = false,
|
||||
uiVissible = false
|
||||
)
|
||||
}
|
||||
|
@ -52,6 +52,7 @@ interface VideoPlayerViewModel {
|
|||
val uiState: StateFlow<VideoPlayerUIState>
|
||||
fun play()
|
||||
fun pause()
|
||||
fun uiResume()
|
||||
fun prevStream()
|
||||
fun nextStream()
|
||||
fun switchToFullscreen()
|
||||
|
@ -81,17 +82,22 @@ class VideoPlayerViewModelImpl @Inject constructor(
|
|||
override fun play() {
|
||||
player.play()
|
||||
mutableUiState.update {
|
||||
it.copy(playing = true)
|
||||
it.copy(playing = player.isPlaying)
|
||||
}
|
||||
}
|
||||
|
||||
override fun pause() {
|
||||
player.pause()
|
||||
|
||||
mutableUiState.update {
|
||||
it.copy(playing = false)
|
||||
it.copy(playing = player.isPlaying)
|
||||
}
|
||||
}
|
||||
|
||||
override fun uiResume() {
|
||||
play()
|
||||
}
|
||||
|
||||
override fun prevStream() {
|
||||
println("imeplement prev stream")
|
||||
}
|
||||
|
@ -136,6 +142,10 @@ class VideoPlayerViewModelImpl @Inject constructor(
|
|||
println("dummy pause")
|
||||
}
|
||||
|
||||
override fun uiResume() {
|
||||
println("dummy ui resume")
|
||||
}
|
||||
|
||||
override fun prevStream() {
|
||||
println("dummy impl")
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
|
||||
package net.newpipe.newplayer.ui
|
||||
|
||||
import android.view.SurfaceView
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.runtime.Composable
|
||||
|
@ -37,15 +38,13 @@ import androidx.compose.ui.viewinterop.AndroidView
|
|||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.LifecycleEventObserver
|
||||
import androidx.media3.ui.PlayerView
|
||||
import net.newpipe.newplayer.model.VideoPlayerViewModel
|
||||
import net.newpipe.newplayer.model.VideoPlayerViewModelImpl
|
||||
import net.newpipe.newplayer.ui.theme.VideoPlayerTheme
|
||||
|
||||
|
||||
@Composable
|
||||
fun VideoPlayerUI(
|
||||
viewModel: VideoPlayerViewModel = hiltViewModel<VideoPlayerViewModelImpl>()
|
||||
viewModel: VideoPlayerViewModel
|
||||
) {
|
||||
|
||||
val uiState by viewModel.uiState.collectAsState()
|
||||
|
@ -73,29 +72,25 @@ fun VideoPlayerUI(
|
|||
AndroidView(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
factory = { context ->
|
||||
PlayerView(context).also {
|
||||
it.player = viewModel.player
|
||||
it.useController = false
|
||||
SurfaceView(context).also {
|
||||
viewModel.player?.setVideoSurfaceView(it)
|
||||
}
|
||||
}, update = {
|
||||
when (lifecycle) {
|
||||
Lifecycle.Event.ON_PAUSE -> {
|
||||
it.onPause()
|
||||
viewModel.pause()
|
||||
}
|
||||
|
||||
Lifecycle.Event.ON_RESUME -> {
|
||||
it.onResume()
|
||||
}
|
||||
|
||||
Lifecycle.Event.ON_START -> {
|
||||
viewModel.play()
|
||||
viewModel.uiResume()
|
||||
}
|
||||
|
||||
else -> Unit
|
||||
}
|
||||
})
|
||||
|
||||
val isPlaying = viewModel.player!!.isPlaying
|
||||
println("is Player playing: $isPlaying")
|
||||
VideoPlayerControllerUI(
|
||||
isPlaying = viewModel.player?.isPlaying ?: false,
|
||||
isFullscreen = uiState.fullscreen,
|
||||
|
|
|
@ -30,7 +30,7 @@
|
|||
<androidx.compose.ui.platform.ComposeView
|
||||
android:id="@+id/player_copose_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
</FrameLayout>
|
||||
|
||||
|
|
|
@ -38,6 +38,7 @@ hiltNavigationCompose = "1.2.0"
|
|||
lifecycleViewmodelCompose = "2.8.3"
|
||||
kspVersion = "1.9.0-1.0.13"
|
||||
runtimeLivedata = "1.7.0-beta04"
|
||||
fragmentKtx = "1.8.1"
|
||||
|
||||
[libraries]
|
||||
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" }
|
||||
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-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]
|
||||
android-application = { id = "com.android.application", version.ref = "agp" }
|
||||
|
|
Loading…
Reference in New Issue