Skip to content

Commit

Permalink
[修复] 切换页面时动画引起的崩溃问题
Browse files Browse the repository at this point in the history
[修复] 搜索栏无法正常收回历史记录的问题
  • Loading branch information
YenalyLiew committed May 16, 2024
1 parent 82c7987 commit 9f4a93d
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import com.yenaly.han1meviewer.ui.adapter.HanimePreviewNewsRvAdapter
import com.yenaly.han1meviewer.ui.adapter.HanimePreviewTourRvAdapter
import com.yenaly.han1meviewer.ui.view.CenterLinearLayoutManager
import com.yenaly.han1meviewer.ui.viewmodel.PreviewViewModel
import com.yenaly.han1meviewer.util.addUpdateListener
import com.yenaly.han1meviewer.util.colorTransition
import com.yenaly.han1meviewer.util.toColorStateList
import com.yenaly.yenaly_libs.base.YenalyActivity
Expand Down Expand Up @@ -309,7 +310,7 @@ class PreviewActivity : YenalyActivity<ActivityPreviewBinding, PreviewViewModel>
) {
duration = animDuration
interpolator = animInterpolator
addUpdateListener {
addUpdateListener(lifecycle) {
val value = it.animatedValue as Int
binding.collapsingToolbar.setContentScrimColor(value)
}
Expand All @@ -320,7 +321,7 @@ class PreviewActivity : YenalyActivity<ActivityPreviewBinding, PreviewViewModel>
) {
duration = animDuration
interpolator = animInterpolator
addUpdateListener {
addUpdateListener(lifecycle) {
val value = it.animatedValue as Int
binding.llTour.setBackgroundColor(value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.annotation.SuppressLint
import android.content.res.Configuration
import android.os.Bundle
import android.view.View
import androidx.activity.addCallback
import androidx.core.view.isGone
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.flowWithLifecycle
Expand All @@ -12,7 +13,10 @@ import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.OnScrollListener
import com.yenaly.han1meviewer.*
import com.yenaly.han1meviewer.ADVANCED_SEARCH_MAP
import com.yenaly.han1meviewer.AdvancedSearchMap
import com.yenaly.han1meviewer.HAdvancedSearch
import com.yenaly.han1meviewer.VideoCoverSize
import com.yenaly.han1meviewer.databinding.ActivitySearchBinding
import com.yenaly.han1meviewer.logic.entity.SearchHistoryEntity
import com.yenaly.han1meviewer.logic.model.HanimeInfo
Expand Down Expand Up @@ -175,6 +179,12 @@ class SearchActivity : YenalyActivity<ActivitySearchBinding, SearchViewModel>(),

@OptIn(FlowPreview::class, ExperimentalCoroutinesApi::class)
private fun initSearchBar() {
onBackPressedDispatcher.addCallback(this) {
if (binding.searchBar.hideHistory()) {
return@addCallback
}
finish()
}
val searchAdapter = HanimeSearchHistoryRvAdapter()
searchAdapter.listener = object : HanimeSearchHistoryRvAdapter.OnItemViewClickListener {
override fun onItemClickListener(v: View, history: SearchHistoryEntity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import com.yenaly.han1meviewer.ui.adapter.RvWrapper.Companion.wrappedWith
import com.yenaly.han1meviewer.ui.adapter.VideoColumnTitleAdapter
import com.yenaly.han1meviewer.ui.fragment.IToolbarFragment
import com.yenaly.han1meviewer.ui.viewmodel.MainViewModel
import com.yenaly.han1meviewer.util.addUpdateListener
import com.yenaly.han1meviewer.util.colorTransition
import com.yenaly.yenaly_libs.base.YenalyFragment
import com.yenaly.yenaly_libs.utils.startActivity
Expand Down Expand Up @@ -254,7 +255,7 @@ class HomePageFragment : YenalyFragment<FragmentHomePageBinding, MainViewModel>(
) {
interpolator = animInterpolator
duration = animDuration
addUpdateListener {
addUpdateListener(viewLifecycleOwner.lifecycle) {
val color = it.animatedValue as Int
binding.btnBanner.iconTint = ColorStateList.valueOf(color)
}
Expand All @@ -265,7 +266,7 @@ class HomePageFragment : YenalyFragment<FragmentHomePageBinding, MainViewModel>(
) {
interpolator = animInterpolator
duration = animDuration
addUpdateListener {
addUpdateListener(viewLifecycleOwner.lifecycle) {
val color = it.animatedValue as Int
binding.aColor.setBackgroundColor(color)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.google.android.material.chip.ChipGroup
import com.yenaly.han1meviewer.ADVANCED_SEARCH_MAP
import com.yenaly.han1meviewer.R
import com.yenaly.han1meviewer.ui.activity.SearchActivity
import com.yenaly.han1meviewer.util.addUpdateListener
import com.yenaly.han1meviewer.util.readBooleanCompat
import com.yenaly.han1meviewer.util.writeBooleanCompat
import com.yenaly.yenaly_libs.utils.activity
Expand Down Expand Up @@ -172,7 +173,7 @@ class CollapsibleTags @JvmOverloads constructor(
return ValueAnimator.ofInt(start, end).apply {
duration = animDuration
interpolator = animInterpolator
addUpdateListener {
addUpdateListener(lifecycle) {
val value = it.animatedValue as Int
chipGroup.updateLayoutParams {
height = value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.animation.ValueAnimator
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
Expand Down Expand Up @@ -134,7 +133,7 @@ class HanimeSearchBar @JvmOverloads constructor(
trySend(searchText)
Log.d("HanimeSearchBar", "focus: $searchText")
} else {
if (!isCollapsed) hideHistory()
hideHistory()
}
}

Expand Down Expand Up @@ -168,7 +167,7 @@ class HanimeSearchBar @JvmOverloads constructor(
isCollapsed = false
}

fun hideHistory() {
fun hideHistory(): Boolean {
if (!isCollapsed) {
etSearch.hideIme(window)
Log.d("HanimeSearchBar", "History Height: ${rvHistory.height}")
Expand All @@ -179,7 +178,9 @@ class HanimeSearchBar @JvmOverloads constructor(
.rotation(0F)
.start()
isCollapsed = true
return true
}
return false
}

private fun View.buildHeightAnimation(
Expand Down Expand Up @@ -207,11 +208,14 @@ class HanimeSearchBar @JvmOverloads constructor(
return measuredHeight
}

override fun dispatchKeyEvent(event: KeyEvent): Boolean {
if (event.keyCode == KeyEvent.KEYCODE_BACK && !isCollapsed) {
hideHistory()
return true
}
return super.dispatchKeyEvent(event)
}
// 使用 onBackPressedDispatcher.addCallback 替换
// 这个方法在 API 34 以上(貌似)无法返回 key event
//
// override fun dispatchKeyEvent(event: KeyEvent): Boolean {
// if (event.keyCode == KeyEvent.KEYCODE_BACK && !isCollapsed) {
// hideHistory()
// return true
// }
// return super.dispatchKeyEvent(event)
// }
}
34 changes: 34 additions & 0 deletions app/src/main/java/com/yenaly/han1meviewer/util/Animations.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.yenaly.han1meviewer.util

import android.animation.ArgbEvaluator
import android.animation.ValueAnimator
import androidx.annotation.ColorInt
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner

/**
* Add an update listener to the [ValueAnimator] and remove it when the [LifecycleOwner] is destroyed.
*/
fun ValueAnimator.addUpdateListener(
lifecycle: Lifecycle?,
listener: ValueAnimator.AnimatorUpdateListener?,
) {
addUpdateListener(listener)
lifecycle?.addObserver(object : DefaultLifecycleObserver {
override fun onDestroy(owner: LifecycleOwner) {
this@addUpdateListener.removeAllUpdateListeners()
}
})
}

fun colorTransition(
@ColorInt fromColor: Int,
@ColorInt toColor: Int,
action: (ValueAnimator.() -> Unit)? = null,
) {
val colorAnimation: ValueAnimator = ValueAnimator.ofObject(ArgbEvaluator(), fromColor, toColor)
action?.invoke(colorAnimation)

colorAnimation.start()
}
13 changes: 0 additions & 13 deletions app/src/main/java/com/yenaly/han1meviewer/util/Colors.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
package com.yenaly.han1meviewer.util

import android.animation.ArgbEvaluator
import android.animation.ValueAnimator
import android.content.res.ColorStateList
import androidx.annotation.ColorInt

fun colorTransition(
@ColorInt fromColor: Int,
@ColorInt toColor: Int,
action: (ValueAnimator.() -> Unit)? = null,
) {
val colorAnimation: ValueAnimator = ValueAnimator.ofObject(ArgbEvaluator(), fromColor, toColor)
action?.invoke(colorAnimation)

colorAnimation.start()
}

fun @receiver:ColorInt Int.toColorStateList() = ColorStateList.valueOf(this)
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ permissionX = "1.7.1"
junit = "4.13.2"
testJunit = "1.1.5"
testEspressoCore = "3.5.1"
guava = "33.1.0-android"
guava = "33.2.0-android"
exoplayer = "1.3.1"
leakCanary = "2.14"
circularRevealSwitch = "0.4.5"
Expand Down

0 comments on commit 9f4a93d

Please sign in to comment.