Move behavior to XML

This commit is contained in:
topjohnwu 2021-02-13 15:26:32 -08:00
parent be6386c410
commit f88e812b63
4 changed files with 12 additions and 17 deletions

View File

@ -10,9 +10,7 @@ import android.view.WindowManager
import androidx.coordinatorlayout.widget.CoordinatorLayout import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.content.pm.ShortcutManagerCompat import androidx.core.content.pm.ShortcutManagerCompat
import androidx.core.view.forEach import androidx.core.view.forEach
import androidx.core.view.updateLayoutParams
import androidx.navigation.NavDirections import androidx.navigation.NavDirections
import com.google.android.material.card.MaterialCardView
import com.topjohnwu.magisk.MainDirections import com.topjohnwu.magisk.MainDirections
import com.topjohnwu.magisk.R import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.arch.BaseUIActivity import com.topjohnwu.magisk.arch.BaseUIActivity
@ -22,8 +20,6 @@ import com.topjohnwu.magisk.core.*
import com.topjohnwu.magisk.databinding.ActivityMainMd2Binding import com.topjohnwu.magisk.databinding.ActivityMainMd2Binding
import com.topjohnwu.magisk.ktx.startAnimations import com.topjohnwu.magisk.ktx.startAnimations
import com.topjohnwu.magisk.ui.home.HomeFragmentDirections import com.topjohnwu.magisk.ui.home.HomeFragmentDirections
import com.topjohnwu.magisk.utils.HideBottomViewOnScrollBehavior
import com.topjohnwu.magisk.utils.HideTopViewOnScrollBehavior
import com.topjohnwu.magisk.utils.HideableBehavior import com.topjohnwu.magisk.utils.HideableBehavior
import com.topjohnwu.magisk.utils.Utils import com.topjohnwu.magisk.utils.Utils
import com.topjohnwu.magisk.view.MagiskDialog import com.topjohnwu.magisk.view.MagiskDialog
@ -77,12 +73,6 @@ open class MainActivity : BaseUIActivity<MainViewModel, ActivityMainMd2Binding>(
setSupportActionBar(binding.mainToolbar) setSupportActionBar(binding.mainToolbar)
binding.mainToolbarWrapper.updateLayoutParams<CoordinatorLayout.LayoutParams> {
behavior = HideTopViewOnScrollBehavior<MaterialCardView>()
}
binding.mainBottomBar.updateLayoutParams<CoordinatorLayout.LayoutParams> {
behavior = HideBottomViewOnScrollBehavior<MaterialCardView>()
}
binding.mainNavigation.setOnNavigationItemSelectedListener { binding.mainNavigation.setOnNavigationItemSelectedListener {
getScreen(it.itemId)?.navigate() getScreen(it.itemId)?.navigate()
true true

View File

@ -1,5 +1,7 @@
package com.topjohnwu.magisk.utils package com.topjohnwu.magisk.utils
import android.content.Context
import android.util.AttributeSet
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.coordinatorlayout.widget.CoordinatorLayout import androidx.coordinatorlayout.widget.CoordinatorLayout
@ -13,8 +15,8 @@ import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.core.Info import com.topjohnwu.magisk.core.Info
import kotlin.math.roundToInt import kotlin.math.roundToInt
class HideBottomViewOnScrollBehavior<V : View> : HideBottomViewOnScrollBehavior<V>(), class HideBottomViewOnScrollBehavior<V : View>(context: Context, attrs: AttributeSet) :
HideableBehavior<V> { HideBottomViewOnScrollBehavior<V>(), HideableBehavior<V> {
private var lockState: Boolean = false private var lockState: Boolean = false
private var isLaidOut = false private var isLaidOut = false
@ -122,4 +124,4 @@ class HideBottomViewOnScrollBehavior<V : View> : HideBottomViewOnScrollBehavior<
.setInterpolator(FastOutSlowInInterpolator()) .setInterpolator(FastOutSlowInInterpolator())
.start() .start()
} }

View File

@ -1,6 +1,8 @@
package com.topjohnwu.magisk.utils package com.topjohnwu.magisk.utils
import android.animation.TimeInterpolator import android.animation.TimeInterpolator
import android.content.Context
import android.util.AttributeSet
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.ViewPropertyAnimator import android.view.ViewPropertyAnimator
@ -8,9 +10,8 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.ViewCompat import androidx.core.view.ViewCompat
import com.google.android.material.animation.AnimationUtils import com.google.android.material.animation.AnimationUtils
class HideTopViewOnScrollBehavior<V : View> : class HideTopViewOnScrollBehavior<V : View>(context: Context, attrs: AttributeSet) :
CoordinatorLayout.Behavior<V>(), CoordinatorLayout.Behavior<V>(), HideableBehavior<V> {
HideableBehavior<V> {
companion object { companion object {
private const val STATE_SCROLLED_DOWN = 1 private const val STATE_SCROLLED_DOWN = 1
@ -139,4 +140,4 @@ class HideTopViewOnScrollBehavior<V : View> :
.setDuration(duration) .setDuration(duration)
.withEndAction { currentAnimator = null } .withEndAction { currentAnimator = null }
.let { currentAnimator = it } .let { currentAnimator = it }
} }

View File

@ -35,6 +35,7 @@
style="@style/WidgetFoundation.Appbar" style="@style/WidgetFoundation.Appbar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:layout_behavior=".utils.HideTopViewOnScrollBehavior"
app:fitsSystemWindowsInsets="top"> app:fitsSystemWindowsInsets="top">
<com.google.android.material.appbar.MaterialToolbar <com.google.android.material.appbar.MaterialToolbar
@ -76,6 +77,7 @@
android:layout_marginBottom="@dimen/l1" android:layout_marginBottom="@dimen/l1"
android:fitsSystemWindows="false" android:fitsSystemWindows="false"
app:layout_fitsSystemWindowsInsets="bottom" app:layout_fitsSystemWindowsInsets="bottom"
app:layout_behavior=".utils.HideBottomViewOnScrollBehavior"
tools:layout_marginBottom="64dp"> tools:layout_marginBottom="64dp">
<com.google.android.material.bottomnavigation.BottomNavigationView <com.google.android.material.bottomnavigation.BottomNavigationView