Skip to content

Commit

Permalink
refactor: simplify the setup of PageCandidatesUi's listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Jan 29, 2025
1 parent 34d31d4 commit d66eaa6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import splitties.views.dsl.recyclerview.recyclerView
class PagedCandidatesUi(
override val ctx: Context,
val theme: Theme,
private val onCandidateClick: (Int) -> Unit,
private val onPrevPage: () -> Unit,
private val onNextPage: () -> Unit,
) : Ui {
private var menu = RimeProto.Context.Menu()

Expand All @@ -40,19 +43,7 @@ class PagedCandidatesUi(
) : UiHolder(ui)
}

enum class ClickType {
CANDIDATE,
PREV_PAGE,
NEXT_PAGE,
}

private var clickListener: ((type: ClickType, position: Int) -> Unit)? = null

fun setOnClickListener(listener: (type: ClickType, position: Int) -> Unit) {
clickListener = listener
}

val candidatesAdapter =
private val candidatesAdapter =
object : BaseQuickAdapter<RimeProto.Candidate, UiHolder>() {
override fun getItemCount(items: List<RimeProto.Candidate>) =
items.size + (if (menu.pageNumber != 0 || !menu.isLastPage) 1 else 0)
Expand Down Expand Up @@ -89,7 +80,7 @@ class PagedCandidatesUi(
val candidate = item ?: return
holder.ui.update(candidate, position == menu.highlightedCandidateIndex)
holder.ui.root.setOnClickListener {
clickListener?.invoke(ClickType.CANDIDATE, position)
onCandidateClick.invoke(position)
}
}
is UiHolder.Pagination -> {
Expand All @@ -99,10 +90,10 @@ class PagedCandidatesUi(
alignSelf = if (isHorizontal) AlignItems.CENTER else AlignItems.STRETCH
}
holder.ui.prevIcon.setOnClickListener {
clickListener?.invoke(ClickType.PREV_PAGE, menu.pageNumber)
onPrevPage.invoke()
}
holder.ui.nextIcon.setOnClickListener {
clickListener?.invoke(ClickType.NEXT_PAGE, menu.pageNumber)
onNextPage.invoke()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,13 @@ class CandidatesView(
}

private val candidatesUi =
PagedCandidatesUi(ctx, theme).apply {
setOnClickListener { type, position ->
when (type) {
PagedCandidatesUi.ClickType.CANDIDATE -> {
rime.launchOnReady { it.selectPagedCandidate(position) }
}
PagedCandidatesUi.ClickType.PREV_PAGE -> {
rime.launchOnReady { it.changeCandidatePage(true) }
}
PagedCandidatesUi.ClickType.NEXT_PAGE -> {
rime.launchOnReady { it.changeCandidatePage(false) }
}
}
}
}
PagedCandidatesUi(
ctx,
theme,
onCandidateClick = { index -> rime.launchOnReady { it.selectPagedCandidate(index) } },
onPrevPage = { rime.launchOnReady { it.changeCandidatePage(true) } },
onNextPage = { rime.launchOnReady { it.changeCandidatePage(false) } },
)

private val touchEventReceiverWindow = TouchEventReceiverWindow(this)

Expand Down

0 comments on commit d66eaa6

Please sign in to comment.