diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/list/fastscroll/FastScroller.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/list/fastscroll/FastScroller.kt index 973664042..454b5c453 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/list/fastscroll/FastScroller.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/list/fastscroll/FastScroller.kt @@ -19,13 +19,13 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout import androidx.core.content.ContextCompat import androidx.core.content.withStyledAttributes import androidx.core.view.GravityCompat +import androidx.core.view.ancestors import androidx.core.view.isGone import androidx.core.view.isVisible import androidx.recyclerview.widget.RecyclerView import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.util.ext.getThemeColor import org.koitharu.kotatsu.core.util.ext.isLayoutReversed -import org.koitharu.kotatsu.core.util.ext.parents import org.koitharu.kotatsu.databinding.FastScrollerBinding import kotlin.math.roundToInt import com.google.android.material.R as materialR @@ -522,7 +522,7 @@ class FastScroller @JvmOverloads constructor( return BubbleSize.entries.getOrNull(ordinal) ?: defaultValue } - private fun findValidParent(view: View): ViewGroup? = view.parents.firstNotNullOfOrNull { p -> + private fun findValidParent(view: View): ViewGroup? = view.ancestors.firstNotNullOfOrNull { p -> if (p is FrameLayout || p is ConstraintLayout || p is CoordinatorLayout || p is RelativeLayout) { p as ViewGroup } else { diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/sheet/AdaptiveSheetHeaderBar.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/sheet/AdaptiveSheetHeaderBar.kt index 0e062a844..055454628 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/sheet/AdaptiveSheetHeaderBar.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/sheet/AdaptiveSheetHeaderBar.kt @@ -9,10 +9,10 @@ import androidx.annotation.AttrRes import androidx.annotation.StringRes import androidx.coordinatorlayout.widget.CoordinatorLayout import androidx.core.content.withStyledAttributes +import androidx.core.view.ancestors import androidx.core.view.isGone import androidx.core.view.isVisible import org.koitharu.kotatsu.R -import org.koitharu.kotatsu.core.util.ext.parents import org.koitharu.kotatsu.databinding.LayoutSheetHeaderAdaptiveBinding class AdaptiveSheetHeaderBar @JvmOverloads constructor( @@ -81,14 +81,9 @@ class AdaptiveSheetHeaderBar @JvmOverloads constructor( } private fun findParentSheetBehavior(): AdaptiveSheetBehavior? { - for (p in parents) { - val layoutParams = (p as? View)?.layoutParams - if (layoutParams is CoordinatorLayout.LayoutParams) { - AdaptiveSheetBehavior.from(layoutParams)?.let { - return it - } - } + return ancestors.firstNotNullOfOrNull { + ((it as? View)?.layoutParams as? CoordinatorLayout.LayoutParams) + ?.let { params -> AdaptiveSheetBehavior.from(params) } } - return null } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/View.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/View.kt index 5f3bc159e..41048c7e1 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/View.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/View.kt @@ -5,10 +5,10 @@ import android.graphics.Rect import android.view.View import android.view.View.MeasureSpec import android.view.ViewGroup -import android.view.ViewParent import android.view.inputmethod.InputMethodManager import android.widget.Checkable import androidx.core.view.children +import androidx.core.view.descendants import androidx.core.view.isVisible import androidx.recyclerview.widget.RecyclerView import androidx.viewpager2.widget.ViewPager2 @@ -89,23 +89,8 @@ fun Slider.setValueRounded(newValue: Float) { value = roundedValue.coerceIn(valueFrom, valueTo) } -fun ViewGroup.findViewsByType(clazz: Class): Sequence { - if (childCount == 0) { - return emptySequence() - } - return sequence { - for (view in children) { - if (clazz.isInstance(view)) { - yield(clazz.cast(view)!!) - } else if (view is ViewGroup && view.childCount != 0) { - yieldAll(view.findViewsByType(clazz)) - } - } - } -} - fun RecyclerView.invalidateNestedItemDecorations() { - findViewsByType(RecyclerView::class.java).forEach { + descendants.filterIsInstance().forEach { it.invalidateItemDecorations() } } @@ -113,15 +98,6 @@ fun RecyclerView.invalidateNestedItemDecorations() { val View.parentView: ViewGroup? get() = parent as? ViewGroup -val View.parents: Sequence - get() = sequence { - var p: ViewParent? = parent - while (p != null) { - yield(p) - p = p.parent - } - } - fun View.measureDimension(desiredSize: Int, measureSpec: Int): Int { var result: Int val specMode = MeasureSpec.getMode(measureSpec) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/pager/webtoon/WebtoonImageView.kt b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/pager/webtoon/WebtoonImageView.kt index 22c87c944..d663fa2da 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/pager/webtoon/WebtoonImageView.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/pager/webtoon/WebtoonImageView.kt @@ -3,9 +3,9 @@ package org.koitharu.kotatsu.reader.ui.pager.webtoon import android.content.Context import android.graphics.PointF import android.util.AttributeSet +import androidx.core.view.ancestors import androidx.recyclerview.widget.RecyclerView import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView -import org.koitharu.kotatsu.core.util.ext.parents import org.koitharu.kotatsu.parsers.util.toIntUp private const val SCROLL_UNKNOWN = -1 @@ -93,7 +93,7 @@ class WebtoonImageView @JvmOverloads constructor( if (oldh == h || oldw == 0 || oldh == 0 || scrollRange == SCROLL_UNKNOWN) return computeScrollRange() - val container = parents.firstNotNullOfOrNull { it as? WebtoonFrameLayout } ?: return + val container = ancestors.firstNotNullOfOrNull { it as? WebtoonFrameLayout } ?: return val parentHeight = parentHeight() if (scrollPos != 0 && container.bottom < parentHeight) { scrollTo(scrollRange) @@ -115,6 +115,6 @@ class WebtoonImageView @JvmOverloads constructor( } private fun parentHeight(): Int { - return parents.firstNotNullOfOrNull { it as? RecyclerView }?.height ?: 0 + return ancestors.firstNotNullOfOrNull { it as? RecyclerView }?.height ?: 0 } }