Skip to content

Commit

Permalink
refactor: rework view bindings (#6975)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pittvandewitt authored Jan 17, 2025
1 parent 2b22bdc commit 6d8598e
Show file tree
Hide file tree
Showing 32 changed files with 81 additions and 388 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package com.github.libretube.ui.base
import android.content.res.Configuration
import android.os.Bundle
import android.view.View
import androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment
import com.github.libretube.R
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.helpers.PreferenceHelper

abstract class DynamicLayoutManagerFragment : Fragment() {
abstract class DynamicLayoutManagerFragment(@LayoutRes layoutResId: Int) : Fragment(layoutResId) {
abstract fun setLayoutManagers(gridItems: Int)

private fun getGridItemsCount(orientation: Int): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.text.format.DateUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.BackEventCompat
import androidx.activity.OnBackPressedCallback
import androidx.constraintlayout.motion.widget.MotionLayout
Expand Down Expand Up @@ -65,7 +63,7 @@ import kotlinx.coroutines.launch
import kotlin.math.abs

@UnstableApi
class AudioPlayerFragment : Fragment(), AudioPlayerOptions {
class AudioPlayerFragment : Fragment(R.layout.fragment_audio_player), AudioPlayerOptions {
private var _binding: FragmentAudioPlayerBinding? = null
val binding get() = _binding!!

Expand Down Expand Up @@ -109,17 +107,9 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions {
}
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentAudioPlayerBinding.inflate(inflater)
return binding.root
}

@SuppressLint("ClickableViewAccessibility")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
_binding = FragmentAudioPlayerBinding.bind(view)
super.onViewCreated(view, savedInstanceState)

mainActivity?.getBottomNavColor()?.let { color ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import android.content.res.Configuration
import android.os.Bundle
import android.os.Parcelable
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isGone
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.ChannelTab
import com.github.libretube.api.obj.StreamItem
Expand All @@ -29,7 +28,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class ChannelContentFragment : DynamicLayoutManagerFragment() {
class ChannelContentFragment : DynamicLayoutManagerFragment(R.layout.fragment_channel_content) {
private var _binding: FragmentChannelContentBinding? = null
private val binding get() = _binding!!
private var channelId: String? = null
Expand All @@ -39,15 +38,6 @@ class ChannelContentFragment : DynamicLayoutManagerFragment() {
private var nextPage: String? = null
private var isLoading: Boolean = false

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentChannelContentBinding.inflate(inflater, container, false)
return _binding!!.root
}

override fun setLayoutManagers(gridItems: Int) {
binding.channelRecView.layoutManager = GridLayoutManager(
requireContext(),
Expand Down Expand Up @@ -125,6 +115,7 @@ class ChannelContentFragment : DynamicLayoutManagerFragment() {
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
_binding = FragmentChannelContentBinding.bind(view)
super.onViewCreated(view, savedInstanceState)

val arguments = requireArguments()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package com.github.libretube.ui.fragments

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.core.view.isGone
import androidx.core.view.isVisible
Expand Down Expand Up @@ -42,7 +40,7 @@ import kotlinx.coroutines.withContext
import retrofit2.HttpException
import java.io.IOException

class ChannelFragment : DynamicLayoutManagerFragment() {
class ChannelFragment : DynamicLayoutManagerFragment(R.layout.fragment_channel) {
private var _binding: FragmentChannelBinding? = null
private val binding get() = _binding!!
private val args by navArgs<ChannelFragmentArgs>()
Expand Down Expand Up @@ -74,18 +72,10 @@ class ChannelFragment : DynamicLayoutManagerFragment() {
channelId = args.channelId
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentChannelBinding.inflate(inflater, container, false)
return binding.root
}

override fun setLayoutManagers(gridItems: Int) {}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
_binding = FragmentChannelBinding.bind(view)
super.onViewCreated(view, savedInstanceState)
// Check if the AppBarLayout is fully expanded
binding.channelAppBar.addOnOffsetChangedListener { _, verticalOffset ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.github.libretube.ui.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
Expand All @@ -26,24 +24,15 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class CommentsMainFragment : Fragment() {
class CommentsMainFragment : Fragment(R.layout.fragment_comments) {
private var _binding: FragmentCommentsBinding? = null
private val binding get() = _binding!!
private val viewModel: CommentsViewModel by activityViewModels()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentCommentsBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
_binding = FragmentCommentsBinding.bind(view)
super.onViewCreated(view, savedInstanceState)

val binding = binding
val layoutManager = LinearLayoutManager(requireContext())
binding.commentsRV.layoutManager = layoutManager
binding.commentsRV.setItemViewCacheSize(20)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.github.libretube.ui.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.core.view.isGone
import androidx.core.view.isVisible
Expand Down Expand Up @@ -31,29 +29,19 @@ import com.github.libretube.ui.models.sources.CommentRepliesPagingSource
import com.github.libretube.ui.sheets.CommentsSheet
import kotlinx.coroutines.launch

class CommentsRepliesFragment : Fragment() {
class CommentsRepliesFragment : Fragment(R.layout.fragment_comments) {
private var _binding: FragmentCommentsBinding? = null
private val binding get() = _binding!!

private val viewModel: CommentsViewModel by activityViewModels()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentCommentsBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
_binding = FragmentCommentsBinding.bind(view)
super.onViewCreated(view, savedInstanceState)
val arguments = requireArguments()
val videoId = arguments.getString(IntentData.videoId, "")
val comment = arguments.parcelable<Comment>(IntentData.comment)!!

val binding = binding

val commentsSheet = parentFragment as? CommentsSheet
commentsSheet?.binding?.btnScrollToTop?.isGone = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import android.content.IntentFilter
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
import androidx.core.view.isGone
Expand Down Expand Up @@ -59,20 +57,12 @@ enum class DownloadTab {
AUDIO
}

class DownloadsFragment : Fragment() {
class DownloadsFragment : Fragment(R.layout.fragment_downloads) {
private var _binding: FragmentDownloadsBinding? = null
private val binding get() = _binding!!

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentDownloadsBinding.inflate(inflater)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
_binding = FragmentDownloadsBinding.bind(view)
super.onViewCreated(view, savedInstanceState)

binding.downloadsPager.adapter = DownloadsFragmentAdapter(this)
Expand Down Expand Up @@ -112,7 +102,7 @@ class DownloadsFragmentAdapter(fragment: Fragment) : FragmentStateAdapter(fragme
}
}

class DownloadsFragmentPage : DynamicLayoutManagerFragment() {
class DownloadsFragmentPage : DynamicLayoutManagerFragment(R.layout.fragment_download_content) {
private lateinit var adapter: DownloadsAdapter
private var _binding: FragmentDownloadContentBinding? = null
private val binding get() = _binding!!
Expand Down Expand Up @@ -149,20 +139,12 @@ class DownloadsFragmentPage : DynamicLayoutManagerFragment() {
this.downloadTab = requireArguments().serializable(IntentData.currentPosition)!!
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentDownloadContentBinding.inflate(layoutInflater)
return binding.root
}

override fun setLayoutManagers(gridItems: Int) {
_binding?.downloadsRecView?.layoutManager = GridLayoutManager(context, gridItems.ceilHalf())
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
_binding = FragmentDownloadContentBinding.bind(view)
super.onViewCreated(view, savedInstanceState)

var selectedSortType =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package com.github.libretube.ui.fragments

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
Expand Down Expand Up @@ -35,23 +33,15 @@ import com.github.libretube.ui.models.HomeViewModel
import com.github.libretube.ui.models.SubscriptionsViewModel
import com.google.android.material.snackbar.Snackbar

class HomeFragment : Fragment() {
class HomeFragment : Fragment(R.layout.fragment_home) {
private var _binding: FragmentHomeBinding? = null
private val binding get() = _binding!!

private val subscriptionsViewModel: SubscriptionsViewModel by activityViewModels()
private val homeViewModel: HomeViewModel by activityViewModels()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentHomeBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
_binding = FragmentHomeBinding.bind(view)
super.onViewCreated(view, savedInstanceState)

with(homeViewModel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package com.github.libretube.ui.fragments

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams
import android.widget.Toast
import androidx.core.view.isGone
Expand Down Expand Up @@ -41,27 +39,19 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class LibraryFragment : DynamicLayoutManagerFragment() {
class LibraryFragment : DynamicLayoutManagerFragment(R.layout.fragment_library) {
private var _binding: FragmentLibraryBinding? = null
private val binding get() = _binding!!

private val commonPlayerViewModel: CommonPlayerViewModel by activityViewModels()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentLibraryBinding.inflate(inflater, container, false)
return binding.root
}

override fun setLayoutManagers(gridItems: Int) {
_binding?.bookmarksRecView?.layoutManager = GridLayoutManager(context, gridItems.ceilHalf())
_binding?.playlistRecView?.layoutManager = GridLayoutManager(context, gridItems.ceilHalf())
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
_binding = FragmentLibraryBinding.bind(view)
super.onViewCreated(view, savedInstanceState)

// listen for the mini player state changing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import android.os.Handler
import android.os.Looper
import android.os.PowerManager
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.PixelCopy
import android.view.SurfaceView
import android.view.View
Expand Down Expand Up @@ -121,7 +120,7 @@ import kotlin.math.ceil


@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
class PlayerFragment : Fragment(), OnlinePlayerOptions {
class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions {
private var _binding: FragmentPlayerBinding? = null
val binding get() = _binding!!

Expand Down Expand Up @@ -385,16 +384,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
noFullscreenResolution = PlayerHelper.getDefaultResolution(requireContext(), false)
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentPlayerBinding.inflate(inflater)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
_binding = FragmentPlayerBinding.bind(view)
super.onViewCreated(view, savedInstanceState)
SoftwareKeyboardControllerCompat(view).hide()

Expand Down
Loading

0 comments on commit 6d8598e

Please sign in to comment.