From 076c1523d42c5b9e4438194217447aca59378499 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Tue, 20 Sep 2022 16:51:38 +0200 Subject: [PATCH] remove findviewbyid --- .../libretube/activities/MainActivity.kt | 37 +++++++++++-------- .../libretube/fragments/PlayerFragment.kt | 2 +- .../github/libretube/util/NavigationHelper.kt | 13 +++---- .../views/SingleViewTouchableMotionLayout.kt | 6 --- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/com/github/libretube/activities/MainActivity.kt b/app/src/main/java/com/github/libretube/activities/MainActivity.kt index 0ad54fdc3..4ab7c76a4 100644 --- a/app/src/main/java/com/github/libretube/activities/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/activities/MainActivity.kt @@ -13,12 +13,9 @@ import android.view.View import android.view.WindowInsets import android.view.WindowInsetsController import android.view.WindowManager -import android.widget.LinearLayout import android.widget.Toast import androidx.activity.OnBackPressedCallback import androidx.appcompat.widget.SearchView -import androidx.constraintlayout.motion.widget.MotionLayout -import androidx.constraintlayout.widget.ConstraintLayout import androidx.core.os.bundleOf import androidx.lifecycle.ViewModelProvider import androidx.navigation.NavController @@ -394,24 +391,34 @@ class MainActivity : BaseActivity() { .replace(R.id.container, frag) .commitNow() Handler(Looper.getMainLooper()).postDelayed({ - val motionLayout = findViewById(R.id.playerMotionLayout) - motionLayout.transitionToEnd() - motionLayout.transitionToStart() + supportFragmentManager.fragments.forEach { fragment -> + (fragment as? PlayerFragment) + ?.binding?.playerMotionLayout?.apply { + transitionToEnd() + transitionToStart() + } + } }, 100) } private fun minimizePlayer() { binding.mainMotionLayout.transitionToEnd() - findViewById(R.id.main_container).isClickable = false - val motionLayout = findViewById(R.id.playerMotionLayout) - // set the animation duration - motionLayout.setTransitionDuration(250) - motionLayout.transitionToEnd() - with(motionLayout) { - getConstraintSet(R.id.start).constrainHeight(R.id.player, 0) - enableTransition(R.id.yt_transition, true) + supportFragmentManager.fragments.forEach { fragment -> + (fragment as? PlayerFragment)?.binding?.apply { + mainContainer.isClickable = false + linLayout.visibility = View.VISIBLE + } } - findViewById(R.id.linLayout).visibility = View.VISIBLE + supportFragmentManager.fragments.forEach { fragment -> + (fragment as? PlayerFragment)?.binding?.playerMotionLayout?.apply { + // set the animation duration + setTransitionDuration(250) + transitionToEnd() + getConstraintSet(R.id.start).constrainHeight(R.id.player, 0) + enableTransition(R.id.yt_transition, true) + } + } + val playerViewModel = ViewModelProvider(this)[PlayerViewModel::class.java] playerViewModel.isFullscreen.value = false requestedOrientation = if (autoRotationEnabled) { diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index 858bca57d..0d2d048cb 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -107,7 +107,7 @@ import kotlin.math.abs class PlayerFragment : BaseFragment() { - private lateinit var binding: FragmentPlayerBinding + lateinit var binding: FragmentPlayerBinding private lateinit var playerBinding: ExoStyledPlayerControlViewBinding private lateinit var doubleTapOverlayBinding: DoubleTapOverlayBinding private val viewModel: PlayerViewModel by activityViewModels() diff --git a/app/src/main/java/com/github/libretube/util/NavigationHelper.kt b/app/src/main/java/com/github/libretube/util/NavigationHelper.kt index 2f473dd26..0450a41e1 100644 --- a/app/src/main/java/com/github/libretube/util/NavigationHelper.kt +++ b/app/src/main/java/com/github/libretube/util/NavigationHelper.kt @@ -3,7 +3,6 @@ package com.github.libretube.util import android.content.Context import android.os.Bundle import androidx.appcompat.app.AppCompatActivity -import androidx.constraintlayout.motion.widget.MotionLayout import androidx.core.os.bundleOf import com.github.libretube.R import com.github.libretube.activities.MainActivity @@ -21,14 +20,14 @@ object NavigationHelper { val bundle = bundleOf(IntentData.channelId to channelId) activity.navController.navigate(R.id.channelFragment, bundle) try { - val mainMotionLayout = - activity.findViewById(R.id.mainMotionLayout) - if (mainMotionLayout.progress == 0.toFloat()) { - mainMotionLayout.transitionToEnd() - activity.findViewById(R.id.playerMotionLayout) - .transitionToEnd() + if (activity.binding.mainMotionLayout.progress == 0.toFloat()) { + activity.binding.mainMotionLayout.transitionToEnd() + activity.supportFragmentManager.fragments.forEach { + (it as PlayerFragment?)?.binding?.playerMotionLayout?.transitionToEnd() + } } } catch (e: Exception) { + e.printStackTrace() } } } diff --git a/app/src/main/java/com/github/libretube/views/SingleViewTouchableMotionLayout.kt b/app/src/main/java/com/github/libretube/views/SingleViewTouchableMotionLayout.kt index 36f889145..51d2871d0 100644 --- a/app/src/main/java/com/github/libretube/views/SingleViewTouchableMotionLayout.kt +++ b/app/src/main/java/com/github/libretube/views/SingleViewTouchableMotionLayout.kt @@ -10,12 +10,6 @@ import android.view.View import androidx.constraintlayout.motion.widget.MotionLayout import com.github.libretube.R -/** - * - * Created by Arman Chatikyan on 05 Sep 2018 - * - */ - class SingleViewTouchableMotionLayout(context: Context, attributeSet: AttributeSet? = null) : MotionLayout(context, attributeSet) {