Skip to content

Commit 59aeb91

Browse files
ShaanNarendrandavid-allison
authored andcommitted
refactor: replace if with when in RecyclerFastScroller.kt
1 parent b825f75 commit 59aeb91

File tree

1 file changed

+40
-36
lines changed

1 file changed

+40
-36
lines changed

AnkiDroid/src/main/java/com/ichi2/anki/ui/RecyclerFastScroller.kt

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -196,45 +196,49 @@ class RecyclerFastScroller
196196
if (recyclerViewAdapter == null || recyclerViewAdapter.itemCount == 0) return false
197197

198198
onHandleTouchListener?.onTouch(v, event)
199-
if (event.actionMasked == MotionEvent.ACTION_DOWN) {
200-
handle.isPressed = true
201-
recyclerView.stopScroll()
202-
203-
var nestedScrollAxis = ViewCompat.SCROLL_AXIS_NONE
204-
nestedScrollAxis = nestedScrollAxis or ViewCompat.SCROLL_AXIS_VERTICAL
205-
206-
recyclerView.startNestedScroll(nestedScrollAxis)
207-
208-
initialBarHeight = bar.height.toFloat()
209-
lastPressedYAdjustedToInitial = event.y + handle.y + bar.y
210-
lastAppBarLayoutOffset = appBarLayoutOffset
211-
} else if (event.actionMasked == MotionEvent.ACTION_MOVE) {
212-
val newHandlePressedY = event.y + handle.y + bar.y
213-
val barHeight = bar.height
214-
val newHandlePressedYAdjustedToInitial =
215-
newHandlePressedY + (initialBarHeight - barHeight)
216-
217-
val scrollProportion = newHandlePressedYAdjustedToInitial / initialBarHeight
218-
val targetPosition =
219-
(scrollProportion * recyclerViewAdapter.itemCount)
220-
.toInt()
221-
.coerceIn(0, recyclerViewAdapter.itemCount - 1)
222-
223-
try {
224-
recyclerView.scrollToPosition(targetPosition)
225-
} catch (e: Exception) {
226-
Timber.w(e, "scrollToPosition")
227-
}
199+
when (event.actionMasked) {
200+
event.actionMasked -> {
201+
handle.isPressed = true
202+
recyclerView.stopScroll()
203+
204+
var nestedScrollAxis = ViewCompat.SCROLL_AXIS_NONE
205+
nestedScrollAxis = nestedScrollAxis or ViewCompat.SCROLL_AXIS_VERTICAL
206+
207+
recyclerView.startNestedScroll(nestedScrollAxis)
228208

229-
lastPressedYAdjustedToInitial = newHandlePressedYAdjustedToInitial
230-
lastAppBarLayoutOffset = appBarLayoutOffset
231-
} else if (event.actionMasked == MotionEvent.ACTION_UP) {
232-
lastPressedYAdjustedToInitial = -1f
209+
initialBarHeight = bar.height.toFloat()
210+
lastPressedYAdjustedToInitial = event.y + handle.y + bar.y
211+
lastAppBarLayoutOffset = appBarLayoutOffset
212+
}
213+
event.actionMasked -> {
214+
val newHandlePressedY = event.y + handle.y + bar.y
215+
val barHeight = bar.height
216+
val newHandlePressedYAdjustedToInitial =
217+
newHandlePressedY + (initialBarHeight - barHeight)
218+
219+
val scrollProportion = newHandlePressedYAdjustedToInitial / initialBarHeight
220+
val targetPosition =
221+
(scrollProportion * recyclerViewAdapter.itemCount)
222+
.toInt()
223+
.coerceIn(0, recyclerViewAdapter.itemCount - 1)
224+
225+
try {
226+
recyclerView.scrollToPosition(targetPosition)
227+
} catch (e: Exception) {
228+
Timber.w(e, "scrollToPosition")
229+
}
230+
231+
lastPressedYAdjustedToInitial = newHandlePressedYAdjustedToInitial
232+
lastAppBarLayoutOffset = appBarLayoutOffset
233+
}
234+
event.actionMasked -> {
235+
lastPressedYAdjustedToInitial = -1f
233236

234-
recyclerView.stopNestedScroll()
237+
recyclerView.stopNestedScroll()
235238

236-
handle.isPressed = false
237-
postAutoHide()
239+
handle.isPressed = false
240+
postAutoHide()
241+
}
238242
}
239243

240244
return true

0 commit comments

Comments
 (0)