Replace CoordinatorLayout in some places :trollface:

This commit is contained in:
Zakhar Timoshenko
2022-07-04 22:51:37 +03:00
parent a2b8cfe512
commit 09105152e4
15 changed files with 198 additions and 45 deletions

View File

@@ -0,0 +1,120 @@
package org.koitharu.kotatsu.base.ui.widgets
import android.content.Context
import android.os.Parcel
import android.os.Parcelable
import android.util.AttributeSet
import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.doOnLayout
import androidx.core.view.isVisible
import androidx.customview.view.AbsSavedState
import androidx.fragment.app.FragmentContainerView
import androidx.viewpager.widget.ViewPager
import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.tabs.TabLayout
import org.koitharu.kotatsu.utils.ext.findChild
import org.koitharu.kotatsu.utils.ext.findDescendant
class KotatsuCoordinatorLayout @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = androidx.coordinatorlayout.R.attr.coordinatorLayoutStyle
) : CoordinatorLayout(context, attrs, defStyleAttr) {
private var appBarLayout: AppBarLayout? = null
private var tabLayout: TabLayout? = null
/**
* If true, [AppBarLayout] child will be lifted on nested scroll.
*/
var isLiftAppBarOnScroll = true
/**
* Internal check
*/
private val canLiftAppBarOnScroll
get() = isLiftAppBarOnScroll
override fun onNestedScroll(
target: View,
dxConsumed: Int,
dyConsumed: Int,
dxUnconsumed: Int,
dyUnconsumed: Int,
type: Int,
consumed: IntArray
) {
super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type, consumed)
// Disable elevation overlay when tabs are visible
if (canLiftAppBarOnScroll) {
appBarLayout?.isLifted = dyConsumed != 0 || dyUnconsumed > 0 && tabLayout?.isVisible == false
}
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
appBarLayout = findChild()
tabLayout = appBarLayout?.findChild()
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
appBarLayout = null
tabLayout = null
}
override fun onSaveInstanceState(): Parcelable? {
val superState = super.onSaveInstanceState()
return if (superState != null) {
SavedState(superState).also {
it.appBarLifted = appBarLayout?.isLifted ?: false
}
} else {
superState
}
}
override fun onRestoreInstanceState(state: Parcelable?) {
if (state is SavedState) {
super.onRestoreInstanceState(state.superState)
doOnLayout {
appBarLayout?.isLifted = state.appBarLifted
}
} else {
super.onRestoreInstanceState(state)
}
}
internal class SavedState : AbsSavedState {
var appBarLifted = false
constructor(superState: Parcelable) : super(superState)
constructor(source: Parcel, loader: ClassLoader?) : super(source, loader) {
appBarLifted = source.readByte().toInt() == 1
}
override fun writeToParcel(out: Parcel, flags: Int) {
super.writeToParcel(out, flags)
out.writeByte((if (appBarLifted) 1 else 0).toByte())
}
companion object {
@JvmField
val CREATOR: Parcelable.ClassLoaderCreator<SavedState> = object : Parcelable.ClassLoaderCreator<SavedState> {
override fun createFromParcel(source: Parcel, loader: ClassLoader): SavedState {
return SavedState(source, loader)
}
override fun createFromParcel(source: Parcel): SavedState {
return SavedState(source, null)
}
override fun newArray(size: Int): Array<SavedState> {
return newArray(size)
}
}
}
}
}

View File

@@ -143,9 +143,6 @@ class DetailsActivity :
binding.snackbar.updatePadding(
bottom = insets.bottom
)
binding.toolbar.updateLayoutParams<ViewGroup.MarginLayoutParams> {
topMargin = insets.top
}
binding.root.updatePadding(
left = insets.left,
right = insets.right

View File

@@ -28,15 +28,10 @@ class HistoryActivity : BaseActivity<ActivityContainerBinding>() {
}
override fun onWindowInsetsChanged(insets: Insets) {
with(binding.toolbar) {
updatePadding(
left = insets.left,
right = insets.right
)
updateLayoutParams<ViewGroup.MarginLayoutParams> {
topMargin = insets.top
}
}
binding.toolbar.updatePadding(
left = insets.left,
right = insets.right,
)
binding.container.updatePadding(
bottom = insets.bottom
)

View File

@@ -96,7 +96,6 @@ class LibraryFragment : BaseFragment<FragmentLibraryBinding>(), LibraryListEvent
binding.recyclerView.updatePadding(
left = insets.left,
right = insets.right,
top = insets.top,
bottom = insets.bottom,
)
}

View File

@@ -256,6 +256,7 @@ class MainActivity :
else -> return false
}
appBar.setExpanded(true)
appBar.isLifted = false
return true
}

View File

@@ -80,15 +80,10 @@ class MultiSearchActivity : BaseActivity<ActivitySearchMultiBinding>(), MangaLis
}
override fun onWindowInsetsChanged(insets: Insets) {
with(binding.toolbar) {
updatePadding(
left = insets.left,
right = insets.right,
)
updateLayoutParams<ViewGroup.MarginLayoutParams> {
topMargin = insets.top
}
}
binding.toolbar.updatePadding(
left = insets.left,
right = insets.right,
)
binding.recyclerView.updatePadding(
bottom = insets.bottom,
left = insets.left,

View File

@@ -18,6 +18,7 @@ import androidx.activity.result.ActivityResultLauncher
import androidx.constraintlayout.motion.widget.MotionScene
import androidx.core.app.ActivityOptionsCompat
import androidx.core.view.children
import androidx.core.view.descendants
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.coroutineScope
import androidx.work.CoroutineWorker
@@ -127,4 +128,8 @@ fun ViewPropertyAnimator.applySystemAnimatorScale(context: Context): ViewPropert
inline fun <reified T> ViewGroup.findChild(): T? {
return children.find { it is T } as? T
}
inline fun <reified T> ViewGroup.findDescendant(): T? {
return descendants.find { it is T } as? T
}