@@ -12,6 +12,7 @@ import android.view.LayoutInflater
1212import android.view.View
1313import android.view.ViewGroup
1414import androidx.core.os.bundleOf
15+ import androidx.core.view.isVisible
1516import androidx.fragment.app.Fragment
1617import androidx.lifecycle.lifecycleScope
1718import com.nextcloud.client.player.media3.PlaybackModel
@@ -33,6 +34,8 @@ class VideoFileFragment :
3334
3435 companion object {
3536 private const val ARGUMENT_FILE = " ARGUMENT_FILE"
37+ private const val SURFACE_ALPHA_VISIBLE = 1f
38+ private const val SURFACE_ALPHA_HIDDEN = 0f
3639
3740 fun createInstance (file : PlaybackFile ) = VideoFileFragment ().apply {
3841 arguments = bundleOf(ARGUMENT_FILE to file)
@@ -45,23 +48,28 @@ class VideoFileFragment :
4548 @Inject
4649 lateinit var thumbnailLoader: ThumbnailLoader
4750
48- private lateinit var file: PlaybackFile
49-
50- private lateinit var binding: PlayerVideoFileFragmentBinding
51+ private var _binding : PlayerVideoFileFragmentBinding ? = null
52+ private val binding get() = checkNotNull(_binding ) { " Binding accessed outside of the view lifecycle" }
5153
5254 private var previousVideoSize: VideoSize ? = null
5355
56+ private val file by lazy {
57+ requireNotNull(arguments.getSerializableArgument(ARGUMENT_FILE , PlaybackFile ::class .java)) {
58+ " VideoFileFragment requires a $ARGUMENT_FILE argument"
59+ }
60+ }
61+
5462 override fun onCreate (savedInstanceState : Bundle ? ) {
5563 super .onCreate(savedInstanceState)
5664 AndroidSupportInjection .inject(this )
57- val playbackFile = arguments.getSerializableArgument(ARGUMENT_FILE , PlaybackFile ::class .java)
58- this .file = playbackFile ? : throw IllegalArgumentException (" bundle is not containing playback file" )
5965 }
6066
61- override fun onCreateView (inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ): View {
62- binding = PlayerVideoFileFragmentBinding .inflate(inflater, container, false )
67+ override fun onCreateView (inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ): View =
68+ PlayerVideoFileFragmentBinding .inflate(inflater, container, false ).also { _binding = it }.root
69+
70+ override fun onViewCreated (view : View , savedInstanceState : Bundle ? ) {
71+ super .onViewCreated(view, savedInstanceState)
6372 loadFileThumbnail()
64- return binding.root
6573 }
6674
6775 override fun onStart () {
@@ -75,39 +83,43 @@ class VideoFileFragment :
7583 super .onStop()
7684 }
7785
86+ override fun onDestroyView () {
87+ _binding = null
88+ super .onDestroyView()
89+ }
90+
7891 override fun onPlaybackUpdate (state : PlaybackState ) {
7992 render(state)
8093 }
8194
82- private fun loadFileThumbnail () {
83- viewLifecycleOwner.lifecycleScope.launch {
84- val context = context ? : return @launch
85- val thumbnailSize = context.resources.getDimension(R .dimen.player_album_cover_size)
86- val thumbnail = thumbnailLoader.await(context, file, thumbnailSize.toInt(), thumbnailSize.toInt())
87- thumbnail?.let (binding.thumbnail::setImageBitmap)
88- }
95+ private fun loadFileThumbnail () = viewLifecycleOwner.lifecycleScope.launch {
96+ val thumbnailSize = resources.getDimensionPixelSize(R .dimen.player_album_cover_size)
97+ val thumbnail = thumbnailLoader.await(requireContext(), file, thumbnailSize, thumbnailSize) ? : return @launch
98+ binding.thumbnail.setImageBitmap(thumbnail)
8999 }
90100
91101 private fun render (state : PlaybackState ? ) {
92102 val currentItemState = state?.currentItemState
103+
93104 if (currentItemState?.file == file) {
94105 showVideo(currentItemState.videoSize)
95- } else {
96- binding.surfaceView.visibility = View .GONE
97- if (currentItemState == null ) {
98- playerModel.setVideoSurfaceView(null )
99- }
106+ return
107+ }
108+
109+ binding.surfaceView.isVisible = false
110+ if (currentItemState == null ) {
111+ playerModel.setVideoSurfaceView(null )
100112 }
101113 }
102114
103115 private fun showVideo (videoSize : VideoSize ? ) {
104116 playerModel.setVideoSurfaceView(binding.surfaceView)
105- binding.surfaceView.visibility = View . VISIBLE
106- binding.surfaceView.alpha = if (videoSize != null ) 1f else 0f
117+ binding.surfaceView.isVisible = true
118+ binding.surfaceView.alpha = if (videoSize == null ) SURFACE_ALPHA_HIDDEN else SURFACE_ALPHA_VISIBLE
107119
108- if (videoSize != null && previousVideoSize != videoSize) {
109- previousVideoSize = videoSize
110- binding.surfaceView.applyVideoSize( videoSize)
111- }
120+ if (videoSize == null || previousVideoSize == videoSize) return
121+
122+ previousVideoSize = videoSize
123+ binding.surfaceView.applyVideoSize(videoSize)
112124 }
113125}
0 commit comments