Fix widget drawing during swipes

This commit is contained in:
Ensar Sarajčić 2023-08-23 12:05:56 +02:00
parent d4f55b8e8b
commit 8f96073e45
1 changed files with 33 additions and 29 deletions

View File

@ -608,16 +608,8 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
} }
private fun updateWidgetPositionAndSize(widgetView: AppWidgetHostView, item: HomeScreenGridItem): Size { private fun updateWidgetPositionAndSize(widgetView: AppWidgetHostView, item: HomeScreenGridItem): Size {
var x = calculateWidgetX(item.left) + width * item.page - width * lastPage val currentViewPosition = getCurrentViewPositionInFullPageSpace() * width.toFloat()
if (isBetweenPages() && (item.page == currentPage || item.page == lastPage || (pageChangeSwipedPercentage > 0f && item.page == currentPage - 1) || (pageChangeSwipedPercentage < 0f && item.page == currentPage + 1))) { val x = calculateWidgetX(item.left) + width * item.page - currentViewPosition
val xFactor = getXFactorForCurrentPage()
val lastXFactor = getXFactorForLastPage()
if (item.page == currentPage) {
x += width * xFactor
} else if (item.page == lastPage || (pageChangeSwipedPercentage > 0f && item.page == currentPage - 1) || (pageChangeSwipedPercentage < 0f && item.page == currentPage + 1)) {
x += width * lastXFactor
}
}
widgetView.x = x widgetView.x = x
widgetView.y = calculateWidgetY(item.top) widgetView.y = calculateWidgetY(item.top)
val widgetWidth = item.getWidthInCells() * cellWidth val widgetWidth = item.getWidthInCells() * cellWidth
@ -783,23 +775,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
} }
// Draw current page indicator on exact position // Draw current page indicator on exact position
val currentIndicatorRangeStart = pageIndicatorsStart + lastPage * pageIndicatorStep val currentIndicatorPosition = pageIndicatorsStart + getCurrentViewPositionInFullPageSpace() * pageIndicatorStep
val indicatorEndPage = if (abs(pageChangeSwipedPercentage) > 0f) {
if (pageChangeSwipedPercentage > 0f) {
currentPage - 1
} else {
currentPage + 1
}
} else {
currentPage
}
val currentIndicatorRangeEnd = pageIndicatorsStart + indicatorEndPage * pageIndicatorStep
val lerpAmount = if (pageChangeAnimLeftPercentage > 0f) {
1 - pageChangeAnimLeftPercentage
} else {
abs(pageChangeSwipedPercentage)
}
val currentIndicatorPosition = MathUtils.lerp(currentIndicatorRangeStart, currentIndicatorRangeEnd, lerpAmount)
if (pageChangeIndicatorsAlpha != 0f) { if (pageChangeIndicatorsAlpha != 0f) {
currentPageIndicatorPaint.alpha = (pageChangeIndicatorsAlpha * 255.0f).toInt() currentPageIndicatorPaint.alpha = (pageChangeIndicatorsAlpha * 255.0f).toInt()
} else { } else {
@ -1114,7 +1090,11 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
pageChangeAnimLeftPercentage = 0f pageChangeAnimLeftPercentage = 0f
pageChangeEnabled = true pageChangeEnabled = true
lastPage = currentPage lastPage = currentPage
if (draggedItem != null) {
schedulePageChange() schedulePageChange()
} else {
clearPageChangeFlags()
}
scheduleIndicatorsFade() scheduleIndicatorsFade()
redrawGrid() redrawGrid()
} }
@ -1151,7 +1131,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
} }
fun setSwipeMovement(diffX: Float) { fun setSwipeMovement(diffX: Float) {
if (!pageChangeEnabled) { if (!pageChangeEnabled || draggedItem != null) {
return return
} }
@ -1162,6 +1142,10 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
} }
fun finalizeSwipe() { fun finalizeSwipe() {
if (abs(pageChangeSwipedPercentage) == 0f) {
return
}
if (abs(pageChangeSwipedPercentage) > 0.5f) { if (abs(pageChangeSwipedPercentage) > 0.5f) {
lastPage = currentPage lastPage = currentPage
currentPage = if (pageChangeSwipedPercentage > 0f) { currentPage = if (pageChangeSwipedPercentage > 0f) {
@ -1181,6 +1165,26 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
} }
} }
private fun getCurrentViewPositionInFullPageSpace(): Float {
val rangeStart = lastPage.toFloat()
val rangeEndPage = if (abs(pageChangeSwipedPercentage) > 0f) {
if (pageChangeSwipedPercentage > 0f) {
currentPage - 1
} else {
currentPage + 1
}
} else {
currentPage
}
val rangeEnd = rangeEndPage.toFloat()
val lerpAmount = if (pageChangeAnimLeftPercentage > 0f) {
1 - pageChangeAnimLeftPercentage
} else {
abs(pageChangeSwipedPercentage)
}
return MathUtils.lerp(rangeStart, rangeEnd, lerpAmount)
}
companion object { companion object {
private const val PAGE_CHANGE_HOLD_THRESHOLD = 500L private const val PAGE_CHANGE_HOLD_THRESHOLD = 500L
private const val PAGE_INDICATORS_FADE_DELAY = PAGE_CHANGE_HOLD_THRESHOLD + 300L private const val PAGE_INDICATORS_FADE_DELAY = PAGE_CHANGE_HOLD_THRESHOLD + 300L