From 8ed1b488be9af5b6dad4159c0f19908b689fe455 Mon Sep 17 00:00:00 2001 From: LagradOst Date: Fri, 22 Oct 2021 20:11:26 +0200 Subject: [PATCH] center --- .../cloudstream3/ui/home/HomeFragment.kt | 15 ++++++- .../widget/CenterZoomLayoutManager.kt | 41 +++++++++++++++---- app/src/main/res/layout/fragment_home.xml | 4 ++ 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeFragment.kt index 6435bbdf..8a956b66 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeFragment.kt @@ -123,12 +123,25 @@ class HomeFragment : Fragment() { if (randomItems.isNullOrEmpty()) { toggleMainVisibility(false) } else { + val randomSize = randomItems.size home_main_poster_recyclerview.adapter = HomeChildItemAdapter(randomItems, R.layout.home_result_big_grid) { callback -> handleSearchClickCallback(activity, callback) } home_main_poster_recyclerview.post { - (home_main_poster_recyclerview.layoutManager as CenterZoomLayoutManager?)?.updateSize(forceUpdate = true) + (home_main_poster_recyclerview.layoutManager as CenterZoomLayoutManager?)?.let { manager -> + + manager.updateSize(forceUpdate = true) + if (randomSize > 2) { + manager.scrollToPosition(randomSize / 2) + manager.snap { dx -> + home_main_poster_recyclerview?.post { + // this is the best I can do, fuck android for not including instant scroll + home_main_poster_recyclerview?.smoothScrollBy(dx,0) + } + } + } + } } toggleMainVisibility(true) diff --git a/app/src/main/java/com/lagradost/cloudstream3/widget/CenterZoomLayoutManager.kt b/app/src/main/java/com/lagradost/cloudstream3/widget/CenterZoomLayoutManager.kt index 23e98a9f..e6b833ea 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/widget/CenterZoomLayoutManager.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/widget/CenterZoomLayoutManager.kt @@ -3,7 +3,9 @@ package com.lagradost.cloudstream3.widget import android.content.Context import android.util.AttributeSet import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.LinearSnapHelper import androidx.recyclerview.widget.RecyclerView +import com.lagradost.cloudstream3.mvvm.logError import kotlin.math.abs import kotlin.math.min @@ -38,17 +40,21 @@ class CenterZoomLayoutManager : LinearLayoutManager { var largestSize = 0f for (i in 0 until childCount) { getChildAt(i)?.let { child -> - val childMidpoint = (getDecoratedRight(child) + getDecoratedLeft(child)) / 2f - val d = min(d1, abs(midpoint - childMidpoint)) - val scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0) - child.scaleX = scale - child.scaleY = scale + try { + val childMidpoint = (getDecoratedRight(child) + getDecoratedLeft(child)) / 2f + val d = min(d1, abs(midpoint - childMidpoint)) + val scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0) + child.scaleX = scale + child.scaleY = scale - if (scale > largestSize) { - (child.tag as Int?)?.let { tag -> - largestSize = scale - largestTag = tag + if (scale > largestSize) { + (child.tag as Int?)?.let { tag -> + largestSize = scale + largestTag = tag + } } + } catch (e : Exception) { + logError(e) } } } @@ -72,9 +78,26 @@ class CenterZoomLayoutManager : LinearLayoutManager { override fun onLayoutCompleted(state: RecyclerView.State?) { super.onLayoutCompleted(state) + if(waitForSnap != null) { + this.getChildAt(snapChild ?: 1)?.let { view -> + LinearSnapHelper().calculateDistanceToFinalSnap(this,view)?.get(0)?.let { dx -> + println("DX: $dx") + waitForSnap?.invoke(dx) + waitForSnap = null + } + } + } updateSize() } + private var waitForSnap : ((Int) -> Unit)? = null + private var snapChild : Int? = null + + fun snap(snap : Int? = null, callback : (Int) -> Unit) { + waitForSnap = callback + snapChild = snap + } + override fun scrollHorizontallyBy(dx: Int, recycler: RecyclerView.Recycler, state: RecyclerView.State): Int { val orientation = orientation return if (orientation == HORIZONTAL) { diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index 2356ba66..a03f221b 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -165,6 +165,10 @@ android:nextFocusUp="@id/home_change_api" android:nextFocusDown="@id/home_main_info" + android:clipToPadding="false" + android:paddingStart="148dp" + android:paddingEnd="148dp" + app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" android:id="@+id/home_main_poster_recyclerview" android:orientation="horizontal"