Fix null crashes in ControllerExtensions

This commit is contained in:
Jays2Kings 2023-10-07 12:38:28 -07:00
parent dbb50fee29
commit 858d7725c7

View file

@ -274,7 +274,7 @@ fun Controller.scrollViewWith(
swipeRefreshLayout?.setDistanceToTriggerSync(150.dpToPx) swipeRefreshLayout?.setDistanceToTriggerSync(150.dpToPx)
val swipeCircle = swipeRefreshLayout?.findChild<ImageView>() val swipeCircle = swipeRefreshLayout?.findChild<ImageView>()
activityBinding!!.appBar.doOnLayout { activityBinding!!.appBar.doOnLayout {
if (fullAppBarHeight!! > 0 && isControllerVisible) { if ((fullAppBarHeight ?: 0) > 0 && isControllerVisible) {
appBarHeight = fullAppBarHeight!! appBarHeight = fullAppBarHeight!!
recycler.requestApplyInsets() recycler.requestApplyInsets()
} }
@ -312,7 +312,7 @@ fun Controller.scrollViewWith(
activityBinding.toolbar.height - if (includeTabView) tabBarHeight else 0 activityBinding.toolbar.height - if (includeTabView) tabBarHeight else 0
} }
recycler.doOnApplyWindowInsetsCompat { view, insets, _ -> recycler.doOnApplyWindowInsetsCompat { view, insets, _ ->
appBarHeight = fullAppBarHeight!! appBarHeight = fullAppBarHeight ?: 0
val systemInsets = if (ignoreInsetVisibility) insets.ignoredSystemInsets else insets.getInsets(systemBars()) val systemInsets = if (ignoreInsetVisibility) insets.ignoredSystemInsets else insets.getInsets(systemBars())
val headerHeight = systemInsets.top + appBarHeight val headerHeight = systemInsets.top + appBarHeight
if (!customPadding) { if (!customPadding) {
@ -342,7 +342,7 @@ fun Controller.scrollViewWith(
val floatingBar = val floatingBar =
(this as? FloatingSearchInterface)?.showFloatingBar() == true && !includeTabView (this as? FloatingSearchInterface)?.showFloatingBar() == true && !includeTabView
if (floatingBar) { if (floatingBar) {
setAppBarBG(isColored.toInt().toFloat(), includeTabView) setAppBarBG(isColored.toInt().toFloat(), false)
return@f return@f
} }
val percent = ImageUtil.getPercentOfColor( val percent = ImageUtil.getPercentOfColor(
@ -358,7 +358,7 @@ fun Controller.scrollViewWith(
} }
} }
if ((this as? FloatingSearchInterface)?.showFloatingBar() == true && !includeTabView) { if ((this as? FloatingSearchInterface)?.showFloatingBar() == true && !includeTabView) {
setAppBarBG(0f, includeTabView) setAppBarBG(0f, false)
} }
addLifecycleListener( addLifecycleListener(
object : Controller.LifecycleListener() { object : Controller.LifecycleListener() {
@ -388,9 +388,9 @@ fun Controller.scrollViewWith(
fakeBottomNavView = null fakeBottomNavView = null
} }
lastY = 0f lastY = 0f
activityBinding!!.appBar.updateAppBarAfterY(recycler) activityBinding?.appBar?.updateAppBarAfterY(recycler)
activityBinding!!.toolbar.tag = randomTag activityBinding?.toolbar?.tag = randomTag
activityBinding!!.toolbar.setOnClickListener { activityBinding?.toolbar?.setOnClickListener {
if (recycler is RecyclerView) { if (recycler is RecyclerView) {
recycler.smoothScrollToTop() recycler.smoothScrollToTop()
} else if (recycler is NestedScrollView) { } else if (recycler is NestedScrollView) {
@ -433,7 +433,9 @@ fun Controller.scrollViewWith(
v.layoutParams = params v.layoutParams = params
} }
toolbarColorAnim?.cancel() toolbarColorAnim?.cancel()
if (activityBinding!!.toolbar.tag == randomTag) activityBinding!!.toolbar.setOnClickListener(null) if (activityBinding?.toolbar?.tag == randomTag) {
activityBinding?.toolbar?.setOnClickListener(null)
}
} }
} }
}, },
@ -442,7 +444,7 @@ fun Controller.scrollViewWith(
recycler.post { recycler.post {
if (isControllerVisible) { if (isControllerVisible) {
activityBinding!!.appBar.updateAppBarAfterY(recycler) activityBinding?.appBar?.updateAppBarAfterY(recycler)
colorToolbar(!atTopOfRecyclerView()) colorToolbar(!atTopOfRecyclerView())
} }
} }
@ -450,15 +452,15 @@ fun Controller.scrollViewWith(
fun onScrolled(dy: Int) { fun onScrolled(dy: Int) {
if (isControllerVisible && statusBarHeight > -1 && if (isControllerVisible && statusBarHeight > -1 &&
(this@scrollViewWith as? BaseController<*>)?.isDragging != true && (this@scrollViewWith as? BaseController<*>)?.isDragging != true &&
activity != null && activityBinding!!.appBar.height > 0 && activity != null && (activityBinding?.appBar?.height ?: 0) > 0 &&
recycler.translationY == 0f recycler.translationY == 0f
) { ) {
if (!recycler.canScrollVertically(-1)) { if (!recycler.canScrollVertically(-1)) {
val shortAnimationDuration = resources?.getInteger( val shortAnimationDuration = resources?.getInteger(
android.R.integer.config_shortAnimTime, android.R.integer.config_shortAnimTime,
) ?: 0 ) ?: 0
activityBinding!!.appBar.y = 0f activityBinding?.appBar?.y = 0f
activityBinding!!.appBar.updateAppBarAfterY(recycler) activityBinding?.appBar?.updateAppBarAfterY(recycler)
if (router.backstackSize == 1 && isInView) { if (router.backstackSize == 1 && isInView) {
activityBinding!!.bottomNav?.let { activityBinding!!.bottomNav?.let {
val animator = it.animate()?.translationY(0f) val animator = it.animate()?.translationY(0f)
@ -472,9 +474,9 @@ fun Controller.scrollViewWith(
lastY = 0f lastY = 0f
if (isToolbarColor) colorToolbar(false) if (isToolbarColor) colorToolbar(false)
} else { } else {
activityBinding!!.appBar.y -= dy activityBinding?.appBar?.let { it.y -= dy }
activityBinding!!.appBar.updateAppBarAfterY(recycler) activityBinding?.appBar?.updateAppBarAfterY(recycler)
activityBinding!!.bottomNav?.let { bottomNav -> activityBinding?.bottomNav?.let { bottomNav ->
if (bottomNav.isVisible && isInView) { if (bottomNav.isVisible && isInView) {
if (preferences.hideBottomNavOnScroll().get()) { if (preferences.hideBottomNavOnScroll().get()) {
bottomNav.translationY += dy bottomNav.translationY += dy
@ -493,10 +495,7 @@ fun Controller.scrollViewWith(
if (!isToolbarColor && ( if (!isToolbarColor && (
dy == 0 || dy == 0 ||
( activityBinding?.appBar?.let { it.y <= -it.height.toFloat() } == true
activityBinding!!.appBar.y <= -activityBinding!!.appBar.height.toFloat() ||
dy == 0 && activityBinding!!.appBar.y == 0f
)
) )
) { ) {
colorToolbar(true) colorToolbar(true)
@ -505,45 +504,45 @@ fun Controller.scrollViewWith(
if (notAtTop != isToolbarColor) colorToolbar(notAtTop) if (notAtTop != isToolbarColor) colorToolbar(notAtTop)
lastY = activityBinding!!.appBar.y lastY = activityBinding!!.appBar.y
} }
swipeCircle?.translationY = max( activityBinding?.appBar?.let {
activityBinding!!.appBar.y, swipeCircle?.translationY = max(it.y, -it.height + it.paddingTop.toFloat())
-activityBinding!!.appBar.height + activityBinding!!.appBar.paddingTop.toFloat(), }
)
} }
} }
fun onScrollIdle() { fun onScrollIdle() {
val activityBinding = activityBinding ?: return
if (isControllerVisible && statusBarHeight > -1 && if (isControllerVisible && statusBarHeight > -1 &&
activity != null && activityBinding!!.appBar.height > 0 && activity != null && activityBinding.appBar.height > 0 &&
recycler.translationY == 0f recycler.translationY == 0f
) { ) {
val halfWay = activityBinding!!.appBar.height.toFloat() / 2 val halfWay = activityBinding.appBar.height.toFloat() / 2
val shortAnimationDuration = resources?.getInteger( val shortAnimationDuration = resources?.getInteger(
android.R.integer.config_shortAnimTime, android.R.integer.config_shortAnimTime,
) ?: 0 ) ?: 0
val closerToTop = abs(activityBinding!!.appBar.y) > halfWay val closerToTop = abs(activityBinding.appBar.y) > halfWay
val halfWayBottom = (activityBinding!!.bottomNav?.height?.toFloat() ?: 0f) / 2 val halfWayBottom = (activityBinding.bottomNav?.height?.toFloat() ?: 0f) / 2
val closerToBottom = activityBinding!!.bottomNav?.translationY ?: 0f > halfWayBottom val closerToBottom = (activityBinding.bottomNav?.translationY ?: 0f) > halfWayBottom
val atTop = !recycler.canScrollVertically(-1) val atTop = !recycler.canScrollVertically(-1)
val closerToEdge = val closerToEdge =
if (activityBinding!!.bottomNav?.isVisible == true && if (activityBinding.bottomNav?.isVisible == true &&
preferences.hideBottomNavOnScroll().get() preferences.hideBottomNavOnScroll().get()
) { ) {
closerToBottom closerToBottom
} else { } else {
closerToTop closerToTop
} }
lastY = activityBinding!!.appBar.snapAppBarY(this@scrollViewWith, recycler) { lastY = activityBinding.appBar.snapAppBarY(this@scrollViewWith, recycler) {
val activityBinding = activityBinding ?: return@snapAppBarY val appBar = this.activityBinding?.appBar ?: return@snapAppBarY
swipeCircle?.translationY = max( swipeCircle?.translationY = max(
activityBinding.appBar.y, appBar.y,
-activityBinding.appBar.height + activityBinding.appBar.paddingTop.toFloat(), -appBar.height + appBar.paddingTop.toFloat(),
) )
} }
if (activityBinding!!.bottomNav?.isVisible == true && if (activityBinding.bottomNav?.isVisible == true &&
isInView && preferences.hideBottomNavOnScroll().get() isInView && preferences.hideBottomNavOnScroll().get()
) { ) {
activityBinding!!.bottomNav?.let { activityBinding.bottomNav.let {
val lastBottomY = val lastBottomY =
if (closerToEdge && !atTop) it.height.toFloat() else 0f if (closerToEdge && !atTop) it.height.toFloat() else 0f
val animator = it.animate()?.translationY(lastBottomY) val animator = it.animate()?.translationY(lastBottomY)