You've already forked Magisk
mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-09-06 06:36:58 +00:00
Cleanup code
This commit is contained in:
@@ -1,22 +0,0 @@
|
||||
package com.topjohnwu.magisk.utils
|
||||
|
||||
class CachedValue<T>(private val factory: () -> T) : Lazy<T> {
|
||||
|
||||
private var _val : T? = null
|
||||
|
||||
override val value: T
|
||||
get() {
|
||||
val local = _val
|
||||
return local ?: synchronized(this) {
|
||||
_val ?: factory().also { _val = it }
|
||||
}
|
||||
}
|
||||
|
||||
override fun isInitialized() = _val != null
|
||||
|
||||
fun invalidate() {
|
||||
synchronized(this) {
|
||||
_val = null
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,116 +0,0 @@
|
||||
package com.topjohnwu.magisk.utils
|
||||
|
||||
import androidx.recyclerview.widget.GridLayoutManager
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import androidx.recyclerview.widget.StaggeredGridLayoutManager
|
||||
import com.topjohnwu.magisk.arch.ViewEvent
|
||||
|
||||
class EndlessRecyclerScrollListener(
|
||||
private val layoutManager: RecyclerView.LayoutManager,
|
||||
private val loadMore: (page: Int, totalItemsCount: Int, view: RecyclerView?) -> Unit,
|
||||
private val direction: Direction = Direction.BOTTOM,
|
||||
visibleRowsThreshold: Int = VISIBLE_THRESHOLD
|
||||
) : RecyclerView.OnScrollListener() {
|
||||
|
||||
constructor(
|
||||
layoutManager: RecyclerView.LayoutManager,
|
||||
loadMore: () -> Unit,
|
||||
direction: Direction = Direction.BOTTOM,
|
||||
visibleRowsThreshold: Int = VISIBLE_THRESHOLD
|
||||
) : this(layoutManager, { _, _, _ -> loadMore() }, direction, visibleRowsThreshold)
|
||||
|
||||
enum class Direction {
|
||||
TOP, BOTTOM
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val VISIBLE_THRESHOLD = 5
|
||||
private const val STARTING_PAGE_INDEX = 0
|
||||
}
|
||||
|
||||
// The minimum amount of items to have above/below your current scroll position
|
||||
// before loading more.
|
||||
private val visibleThreshold = when (layoutManager) {
|
||||
is LinearLayoutManager -> visibleRowsThreshold
|
||||
is GridLayoutManager -> visibleRowsThreshold * layoutManager.spanCount
|
||||
is StaggeredGridLayoutManager -> visibleRowsThreshold * layoutManager.spanCount
|
||||
else -> throw IllegalArgumentException("Only LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager are supported")
|
||||
}
|
||||
|
||||
// The current offset index of data you have loaded
|
||||
private var currentPage = 0
|
||||
// The total number of items in the dataset after the last load
|
||||
private var previousTotalItemCount = 0
|
||||
// True if we are still waiting for the last set of data to load.
|
||||
private var loading = true
|
||||
|
||||
// This happens many times a second during a scroll, so be wary of the code you place here.
|
||||
// We are given a few useful parameters to help us work out if we need to load some more data,
|
||||
// but first we check if we are waiting for the previous load to finish.
|
||||
override fun onScrolled(view: RecyclerView, dx: Int, dy: Int) {
|
||||
if (dx == 0 && dy == 0) return
|
||||
val totalItemCount = layoutManager.itemCount
|
||||
|
||||
val visibleItemPosition = if (direction == Direction.BOTTOM) {
|
||||
when (layoutManager) {
|
||||
is StaggeredGridLayoutManager ->
|
||||
layoutManager.findLastVisibleItemPositions(null).maxOrNull() ?: 0
|
||||
is GridLayoutManager -> layoutManager.findLastVisibleItemPosition()
|
||||
is LinearLayoutManager -> layoutManager.findLastVisibleItemPosition()
|
||||
else -> throw IllegalArgumentException("Only LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager are supported")
|
||||
}
|
||||
} else {
|
||||
when (layoutManager) {
|
||||
is StaggeredGridLayoutManager ->
|
||||
layoutManager.findFirstVisibleItemPositions(null).minOrNull() ?: 0
|
||||
is GridLayoutManager -> layoutManager.findFirstVisibleItemPosition()
|
||||
is LinearLayoutManager -> layoutManager.findFirstVisibleItemPosition()
|
||||
else -> throw IllegalArgumentException("Only LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager are supported")
|
||||
}
|
||||
}
|
||||
|
||||
// If the total item count is zero and the previous isn't, assume the
|
||||
// list is invalidated and should be reset back to initial state
|
||||
if (totalItemCount < previousTotalItemCount) {
|
||||
currentPage =
|
||||
STARTING_PAGE_INDEX
|
||||
previousTotalItemCount = totalItemCount
|
||||
if (totalItemCount == 0) {
|
||||
loading = true
|
||||
}
|
||||
}
|
||||
|
||||
// If it’s still loading, we check to see if the dataset count has
|
||||
// changed, if so we conclude it has finished loading and update the current page
|
||||
// number and total item count.
|
||||
if (loading && totalItemCount > previousTotalItemCount) {
|
||||
loading = false
|
||||
previousTotalItemCount = totalItemCount
|
||||
}
|
||||
|
||||
// If it isn’t currently loading, we check to see if we have breached
|
||||
// the visibleThreshold and need to reload more data.
|
||||
// If we do need to reload some more data, we execute onLoadMore to fetch the data.
|
||||
// threshold should reflect how many total columns there are too
|
||||
if (!loading && shouldLoadMoreItems(visibleItemPosition, totalItemCount)) {
|
||||
currentPage++
|
||||
loadMore(currentPage, totalItemCount, view)
|
||||
loading = true
|
||||
}
|
||||
}
|
||||
|
||||
private fun shouldLoadMoreItems(visibleItemPosition: Int, itemCount: Int) = when (direction) {
|
||||
Direction.TOP -> visibleItemPosition < visibleThreshold
|
||||
Direction.BOTTOM -> visibleItemPosition + visibleThreshold > itemCount
|
||||
}
|
||||
|
||||
// Call this method whenever performing new searches
|
||||
fun resetState() {
|
||||
currentPage = STARTING_PAGE_INDEX
|
||||
previousTotalItemCount = 0
|
||||
loading = true
|
||||
}
|
||||
|
||||
class ResetState : ViewEvent()
|
||||
}
|
||||
@@ -1,118 +0,0 @@
|
||||
package com.topjohnwu.magisk.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.Rect
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.view.View
|
||||
import androidx.annotation.DrawableRes
|
||||
import androidx.core.view.get
|
||||
import androidx.recyclerview.widget.DividerItemDecoration
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.topjohnwu.magisk.ktx.drawableCompat
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
class KItemDecoration(
|
||||
private val context: Context,
|
||||
@RecyclerView.Orientation private val orientation: Int
|
||||
) :
|
||||
RecyclerView.ItemDecoration() {
|
||||
|
||||
private val bounds = Rect()
|
||||
private var divider: Drawable? = null
|
||||
var showAfterLast = true
|
||||
|
||||
fun setDeco(@DrawableRes drawable: Int) = apply {
|
||||
setDeco(context.drawableCompat(drawable))
|
||||
}
|
||||
|
||||
fun setDeco(drawable: Drawable?) = apply {
|
||||
divider = drawable
|
||||
}
|
||||
|
||||
override fun onDraw(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
|
||||
parent.layoutManager ?: return
|
||||
|
||||
divider?.let {
|
||||
if (orientation == DividerItemDecoration.VERTICAL) {
|
||||
drawVertical(canvas, parent, it)
|
||||
} else {
|
||||
drawHorizontal(canvas, parent, it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun drawVertical(canvas: Canvas, parent: RecyclerView, drawable: Drawable) {
|
||||
canvas.save()
|
||||
val left: Int
|
||||
val right: Int
|
||||
if (parent.clipToPadding) {
|
||||
left = parent.paddingLeft
|
||||
right = parent.width - parent.paddingRight
|
||||
canvas.clipRect(
|
||||
left, parent.paddingTop, right,
|
||||
parent.height - parent.paddingBottom
|
||||
)
|
||||
} else {
|
||||
left = 0
|
||||
right = parent.width
|
||||
}
|
||||
|
||||
val to = if (showAfterLast) parent.childCount else parent.childCount - 1
|
||||
|
||||
(0 until to)
|
||||
.map { parent[it] }
|
||||
.forEach { child ->
|
||||
parent.getDecoratedBoundsWithMargins(child, bounds)
|
||||
val bottom = bounds.bottom + child.translationY.roundToInt()
|
||||
val top = bottom - drawable.intrinsicHeight
|
||||
drawable.setBounds(left, top, right, bottom)
|
||||
drawable.draw(canvas)
|
||||
}
|
||||
canvas.restore()
|
||||
}
|
||||
|
||||
private fun drawHorizontal(canvas: Canvas, parent: RecyclerView, drawable: Drawable) {
|
||||
canvas.save()
|
||||
val top: Int
|
||||
val bottom: Int
|
||||
if (parent.clipToPadding) {
|
||||
top = parent.paddingTop
|
||||
bottom = parent.height - parent.paddingBottom
|
||||
canvas.clipRect(
|
||||
parent.paddingLeft, top,
|
||||
parent.width - parent.paddingRight, bottom
|
||||
)
|
||||
} else {
|
||||
top = 0
|
||||
bottom = parent.height
|
||||
}
|
||||
|
||||
val to = if (showAfterLast) parent.childCount else parent.childCount - 1
|
||||
|
||||
(0 until to)
|
||||
.map { parent[it] }
|
||||
.forEach { child ->
|
||||
parent.layoutManager!!.getDecoratedBoundsWithMargins(child, bounds)
|
||||
val right = bounds.right + child.translationX.roundToInt()
|
||||
val left = right - drawable.intrinsicWidth
|
||||
drawable.setBounds(left, top, right, bottom)
|
||||
drawable.draw(canvas)
|
||||
}
|
||||
canvas.restore()
|
||||
}
|
||||
|
||||
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
|
||||
if (parent.getChildAdapterPosition(view) == state.itemCount - 1) {
|
||||
outRect.setEmpty()
|
||||
return
|
||||
}
|
||||
|
||||
if (orientation == RecyclerView.VERTICAL) {
|
||||
outRect.set(0, 0, 0, divider?.intrinsicHeight ?: 0)
|
||||
} else {
|
||||
outRect.set(0, 0, divider?.intrinsicWidth ?: 0, 0)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user