package com.lagradost.cloudstream3.ui.result import android.content.Context import android.view.View import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.lagradost.cloudstream3.mvvm.logError fun RecyclerView?.setLinearListLayout(isHorizontal: Boolean = true) { if (this == null) return this.layoutManager = this.context?.let { LinearListLayout(it).apply { if (isHorizontal) setHorizontal() else setVertical() } } ?: this.layoutManager } open class LinearListLayout(context: Context?) : LinearLayoutManager(context) { fun setHorizontal() { orientation = HORIZONTAL } fun setVertical() { orientation = VERTICAL } private fun getCorrectParent(focused: View?): View? { if (focused == null) return null var current: View? = focused val last: ArrayList = arrayListOf(focused) while (current != null && current !is RecyclerView) { current = (current.parent as? View?)?.also { last.add(it) } } return last.getOrNull(last.count() - 2) } private fun getPosition(view: View?): Int? { return (view?.layoutParams as? RecyclerView.LayoutParams?)?.absoluteAdapterPosition } private fun getViewFromPos(pos: Int): View? { for (i in 0 until childCount) { val child = getChildAt(i) if ((child?.layoutParams as? RecyclerView.LayoutParams?)?.absoluteAdapterPosition == pos) { return child } } return null //return recyclerView.children.firstOrNull { child -> (child.layoutParams as? RecyclerView.LayoutParams?)?.absoluteAdapterPosition == pos) } } /* private fun scrollTo(position: Int) { val linearSmoothScroller = LinearSmoothScroller(recyclerView.context) linearSmoothScroller.targetPosition = position startSmoothScroll(linearSmoothScroller) }*/ override fun onInterceptFocusSearch(focused: View, direction: Int): View? { val dir = if (orientation == HORIZONTAL) { if (direction == View.FOCUS_DOWN || direction == View.FOCUS_UP) { // This scrolls the recyclerview before doing focus search, which // allows the focus search to work better. // Without this the recyclerview focus location on the screen // would change when scrolling between recyclerviews. (focused.parent as? RecyclerView)?.focusSearch(direction) return null } if (direction == View.FOCUS_RIGHT) 1 else -1 } else { if (direction == View.FOCUS_RIGHT || direction == View.FOCUS_LEFT) return null if (direction == View.FOCUS_DOWN) 1 else -1 } return try { getPosition(getCorrectParent(focused))?.let { position -> val lookfor = dir + position //clamp(dir + position, 0, recyclerView.adapter?.itemCount ?: return null) getViewFromPos(lookfor) ?: run { scrollToPosition(lookfor) null } } } catch (e: Exception) { logError(e) null } } /*override fun onRequestChildFocus( parent: RecyclerView, state: RecyclerView.State, child: View, focused: View? ): Boolean { return super.onRequestChildFocus(parent, state, child, focused) getPosition(getCorrectParent(focused ?: return true))?.let { val startView = findFirstVisibleChildClosestToStart(true,true) val endView = findFirstVisibleChildClosestToEnd(true,true) val start = getPosition(startView) val end = getPosition(endView) fill(parent,LayoutState()) val helper = mOrientationHelper ?: return false val laidOutArea: Int = abs( helper.getDecoratedEnd(startView) - helper.getDecoratedStart(endView) ) val itemRange: Int = abs( (start - end) ) + 1 val avgSizePerRow = laidOutArea.toFloat() / itemRange return Math.round( itemsBefore * avgSizePerRow + ((orientation.getStartAfterPadding() - orientation.getDecoratedStart(startChild))) ) recyclerView.scrollToPosition(it) } return true*/ //return super.onRequestChildFocus(parent, state, child, focused) /* if (focused == null || focused == child) { return super.onRequestChildFocus(parent, state, child, focused) } try { val pos = getPosition(getCorrectParent(focused) ?: return true) scrollToPosition(pos) } catch (e: Exception) { logError(e) } return true }*/ }