introduce viewbinding

This commit is contained in:
Bnyro 2022-06-30 21:05:31 +02:00
parent 8f758566f1
commit 6806f4fe6e
3 changed files with 44 additions and 39 deletions

View File

@ -17,6 +17,10 @@ android {
resValue "string", "app_name", "LibreTube" resValue "string", "app_name", "LibreTube"
} }
buildFeatures {
viewBinding true
}
buildTypes { buildTypes {
release { release {
minifyEnabled true minifyEnabled true

View File

@ -32,6 +32,7 @@ import androidx.fragment.app.Fragment
import androidx.navigation.NavController import androidx.navigation.NavController
import androidx.navigation.findNavController import androidx.navigation.findNavController
import androidx.navigation.ui.setupWithNavController import androidx.navigation.ui.setupWithNavController
import com.github.libretube.databinding.ActivityMainBinding
import com.github.libretube.fragments.PlayerFragment import com.github.libretube.fragments.PlayerFragment
import com.github.libretube.fragments.isFullScreen import com.github.libretube.fragments.isFullScreen
import com.github.libretube.services.ClosingService import com.github.libretube.services.ClosingService
@ -46,7 +47,9 @@ import com.google.android.material.color.DynamicColors
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
val TAG = "MainActivity" val TAG = "MainActivity"
lateinit var bottomNavigationView: BottomNavigationView lateinit var binding: ActivityMainBinding
private lateinit var bottomNavigationView: BottomNavigationView
private lateinit var toolbar: Toolbar private lateinit var toolbar: Toolbar
lateinit var navController: NavController lateinit var navController: NavController
@ -76,12 +79,12 @@ class MainActivity : AppCompatActivity() {
startActivity(intent) startActivity(intent)
} }
} else { } else {
setContentView(R.layout.activity_main) binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
bottomNavigationView = findViewById(R.id.bottomNav)
navController = findNavController(R.id.fragment) navController = findNavController(R.id.fragment)
bottomNavigationView.setupWithNavController(navController) binding.bottomNav.setupWithNavController(navController)
// hide the trending page if enabled // hide the trending page if enabled
val hideTrendingPage = PreferenceHelper.getBoolean(this, "hide_trending_page", false) val hideTrendingPage = PreferenceHelper.getBoolean(this, "hide_trending_page", false)
@ -94,7 +97,7 @@ class MainActivity : AppCompatActivity() {
"library" -> navController.navigate(R.id.library) "library" -> navController.navigate(R.id.library)
} }
bottomNavigationView.setOnItemSelectedListener { binding.bottomNav.setOnItemSelectedListener {
when (it.itemId) { when (it.itemId) {
R.id.home2 -> { R.id.home2 -> {
navController.backQueue.clear() navController.backQueue.clear()
@ -112,7 +115,6 @@ class MainActivity : AppCompatActivity() {
false false
} }
toolbar = findViewById(R.id.toolbar)
val typedValue = TypedValue() val typedValue = TypedValue()
this.theme.resolveAttribute(R.attr.colorPrimary, typedValue, true) this.theme.resolveAttribute(R.attr.colorPrimary, typedValue, true)
val hexColor = String.format("#%06X", (0xFFFFFF and typedValue.data)) val hexColor = String.format("#%06X", (0xFFFFFF and typedValue.data))
@ -120,15 +122,15 @@ class MainActivity : AppCompatActivity() {
"Libre<span style='color:$hexColor';>Tube</span>", "Libre<span style='color:$hexColor';>Tube</span>",
HtmlCompat.FROM_HTML_MODE_COMPACT HtmlCompat.FROM_HTML_MODE_COMPACT
) )
toolbar.title = appName binding.toolbar.title = appName
toolbar.setNavigationOnClickListener { binding.toolbar.setNavigationOnClickListener {
// settings activity stuff // settings activity stuff
val intent = Intent(this, SettingsActivity::class.java) val intent = Intent(this, SettingsActivity::class.java)
startActivity(intent) startActivity(intent)
} }
toolbar.setOnMenuItemClickListener { binding.toolbar.setOnMenuItemClickListener {
when (it.itemId) { when (it.itemId) {
R.id.action_search -> { R.id.action_search -> {
navController.navigate(R.id.searchFragment) navController.navigate(R.id.searchFragment)
@ -267,7 +269,7 @@ class MainActivity : AppCompatActivity() {
override fun onBackPressed() { override fun onBackPressed() {
try { try {
val mainMotionLayout = findViewById<MotionLayout>(R.id.mainMotionLayout) val mainMotionLayout = binding.mainMotionLayout
if (mainMotionLayout.progress == 0.toFloat()) { if (mainMotionLayout.progress == 0.toFloat()) {
mainMotionLayout.transitionToEnd() mainMotionLayout.transitionToEnd()
findViewById<ConstraintLayout>(R.id.main_container).isClickable = false findViewById<ConstraintLayout>(R.id.main_container).isClickable = false

View File

@ -43,6 +43,7 @@ import com.github.libretube.R
import com.github.libretube.adapters.ChaptersAdapter import com.github.libretube.adapters.ChaptersAdapter
import com.github.libretube.adapters.CommentsAdapter import com.github.libretube.adapters.CommentsAdapter
import com.github.libretube.adapters.TrendingAdapter import com.github.libretube.adapters.TrendingAdapter
import com.github.libretube.databinding.FragmentPlayerBinding
import com.github.libretube.dialogs.AddtoPlaylistDialog import com.github.libretube.dialogs.AddtoPlaylistDialog
import com.github.libretube.dialogs.DownloadDialog import com.github.libretube.dialogs.DownloadDialog
import com.github.libretube.dialogs.ShareDialog import com.github.libretube.dialogs.ShareDialog
@ -102,6 +103,8 @@ var isMiniPlayerVisible = false
class PlayerFragment : Fragment() { class PlayerFragment : Fragment() {
private val TAG = "PlayerFragment" private val TAG = "PlayerFragment"
private lateinit var binding: FragmentPlayerBinding
private var videoId: String? = null private var videoId: String? = null
private var playlistId: String? = null private var playlistId: String? = null
private var sId: Int = 0 private var sId: Int = 0
@ -157,8 +160,9 @@ class PlayerFragment : Fragment() {
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View? {
binding = FragmentPlayerBinding.inflate(layoutInflater, container, false)
// Inflate the layout for this fragment // Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_player, container, false) return binding.root
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@ -172,17 +176,16 @@ class PlayerFragment : Fragment() {
} }
private fun initializeTransitionLayout(view: View) { private fun initializeTransitionLayout(view: View) {
val playerDescription = view.findViewById<TextView>(R.id.player_description)
videoId = videoId!!.replace("/watch?v=", "") videoId = videoId!!.replace("/watch?v=", "")
relDownloadVideo = view.findViewById(R.id.relPlayer_download)
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
mainActivity.findViewById<FrameLayout>(R.id.container).visibility = View.VISIBLE mainActivity.binding.container.visibility = View.VISIBLE
val playerMotionLayout = view.findViewById<MotionLayout>(R.id.playerMotionLayout)
motionLayout = playerMotionLayout motionLayout = binding.playerMotionLayout
exoPlayerView = view.findViewById(R.id.player) exoPlayerView = binding.player
view.findViewById<TextView>(R.id.player_description).text = videoId view.findViewById<TextView>(R.id.player_description).text = videoId
playerMotionLayout.addTransitionListener(object : MotionLayout.TransitionListener { motionLayout.addTransitionListener(object : MotionLayout.TransitionListener {
override fun onTransitionStarted( override fun onTransitionStarted(
motionLayout: MotionLayout?, motionLayout: MotionLayout?,
startId: Int, startId: Int,
@ -198,7 +201,7 @@ class PlayerFragment : Fragment() {
) { ) {
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
val mainMotionLayout = val mainMotionLayout =
mainActivity.findViewById<MotionLayout>(R.id.mainMotionLayout) mainActivity.binding.mainMotionLayout
mainMotionLayout.progress = abs(progress) mainMotionLayout.progress = abs(progress)
exoPlayerView.hideController() exoPlayerView.hideController()
eId = endId eId = endId
@ -209,7 +212,7 @@ class PlayerFragment : Fragment() {
println(currentId) println(currentId)
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
val mainMotionLayout = val mainMotionLayout =
mainActivity.findViewById<MotionLayout>(R.id.mainMotionLayout) mainActivity.binding.mainMotionLayout
if (currentId == eId) { if (currentId == eId) {
isMiniPlayerVisible = true isMiniPlayerVisible = true
exoPlayerView.useController = false exoPlayerView.useController = false
@ -230,10 +233,10 @@ class PlayerFragment : Fragment() {
} }
}) })
playerMotionLayout.progress = 1.toFloat() motionLayout.progress = 1.toFloat()
playerMotionLayout.transitionToStart() motionLayout.transitionToStart()
view.findViewById<ImageView>(R.id.close_imageView).setOnClickListener { binding.closeImageView.setOnClickListener {
isMiniPlayerVisible = false isMiniPlayerVisible = false
motionLayout.transitionToEnd() motionLayout.transitionToEnd()
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
@ -251,25 +254,23 @@ class PlayerFragment : Fragment() {
.remove(this) .remove(this)
.commit() .commit()
} }
val playImageView = view.findViewById<ImageView>(R.id.play_imageView) binding.playImageView.setOnClickListener {
playImageView.setOnClickListener {
paused = if (paused) { paused = if (paused) {
playImageView.setImageResource(R.drawable.ic_pause) binding.playImageView.setImageResource(R.drawable.ic_pause)
exoPlayer.play() exoPlayer.play()
false false
} else { } else {
playImageView.setImageResource(R.drawable.ic_play) binding.playImageView.setImageResource(R.drawable.ic_play)
exoPlayer.pause() exoPlayer.pause()
true true
} }
} }
// video description and chapters toggle // video description and chapters toggle
val descLinLayout = view.findViewById<LinearLayout>(R.id.desc_linLayout) binding.playerTitleLayout.setOnClickListener {
view.findViewById<RelativeLayout>(R.id.player_title_layout).setOnClickListener { binding.playerDescriptionArrow.animate().rotationBy(180F).setDuration(250).start()
val arrowImageView = view.findViewById<ImageView>(R.id.player_description_arrow) binding.descLinLayout.visibility =
arrowImageView.animate().rotationBy(180F).setDuration(250).start() if (binding.descLinLayout.isVisible) View.GONE else View.VISIBLE
descLinLayout.visibility = if (descLinLayout.isVisible) View.GONE else View.VISIBLE
} }
view.findViewById<MaterialCardView>(R.id.comments_toggle) view.findViewById<MaterialCardView>(R.id.comments_toggle)
@ -279,8 +280,6 @@ class PlayerFragment : Fragment() {
val fullScreenButton = view.findViewById<ImageButton>(R.id.fullscreen) val fullScreenButton = view.findViewById<ImageButton>(R.id.fullscreen)
val exoTitle = view.findViewById<TextView>(R.id.exo_title) val exoTitle = view.findViewById<TextView>(R.id.exo_title)
val mainContainer = view.findViewById<ConstraintLayout>(R.id.main_container)
val linLayout = view.findViewById<LinearLayout>(R.id.linLayout)
// FullScreen button trigger // FullScreen button trigger
fullScreenButton.setOnClickListener { fullScreenButton.setOnClickListener {
@ -291,8 +290,8 @@ class PlayerFragment : Fragment() {
enableTransition(R.id.yt_transition, false) enableTransition(R.id.yt_transition, false)
} }
mainContainer.isClickable = true binding.mainContainer.isClickable = true
linLayout.visibility = View.GONE binding.linLayout.visibility = View.GONE
fullScreenButton.setImageResource(R.drawable.ic_fullscreen_exit) fullScreenButton.setImageResource(R.drawable.ic_fullscreen_exit)
exoTitle.visibility = View.VISIBLE exoTitle.visibility = View.VISIBLE
@ -304,8 +303,8 @@ class PlayerFragment : Fragment() {
enableTransition(R.id.yt_transition, true) enableTransition(R.id.yt_transition, true)
} }
mainContainer.isClickable = false binding.mainContainer.isClickable = false
linLayout.visibility = View.VISIBLE binding.linLayout.visibility = View.VISIBLE
fullScreenButton.setImageResource(R.drawable.ic_fullscreen) fullScreenButton.setImageResource(R.drawable.ic_fullscreen)
exoTitle.visibility = View.INVISIBLE exoTitle.visibility = View.INVISIBLE
@ -714,7 +713,7 @@ class PlayerFragment : Fragment() {
// check if livestream // check if livestream
if (response.duration!! > 0) { if (response.duration!! > 0) {
// download clicked // download clicked
relDownloadVideo.setOnClickListener { binding.relPlayerDownload.setOnClickListener {
if (!IS_DOWNLOAD_RUNNING) { if (!IS_DOWNLOAD_RUNNING) {
val newFragment = DownloadDialog() val newFragment = DownloadDialog()
val bundle = Bundle() val bundle = Bundle()