Skip to content

Commit

Permalink
feat: page navigation using prevIcon and nextIcon components
Browse files Browse the repository at this point in the history
  • Loading branch information
if-can authored and WhiredPlanck committed Jan 17, 2025
1 parent 7a3232d commit 069a12f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ 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 =
object : BaseQuickAdapter<RimeProto.Candidate, UiHolder>() {
override fun getItemCount(items: List<RimeProto.Candidate>) =
Expand Down Expand Up @@ -75,13 +87,22 @@ class PagedCandidatesUi(
is UiHolder.Candidate -> {
val candidate = item ?: return
holder.ui.update(candidate, position == menu.highlightedCandidateIndex)
holder.ui.root.setOnClickListener {
clickListener?.invoke(ClickType.CANDIDATE, position)
}
}
is UiHolder.Pagination -> {
holder.ui.update(menu)
holder.ui.root.updateLayoutParams<FlexboxLayoutManager.LayoutParams> {
width = if (isHorizontal) ViewGroup.LayoutParams.WRAP_CONTENT else ViewGroup.LayoutParams.MATCH_PARENT
alignSelf = if (isHorizontal) AlignItems.CENTER else AlignItems.STRETCH
}
holder.ui.prevIcon.setOnClickListener {
clickListener?.invoke(ClickType.PREV_PAGE, menu.pageNumber)
}
holder.ui.nextIcon.setOnClickListener {
clickListener?.invoke(ClickType.NEXT_PAGE, menu.pageNumber)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class PaginationUi(
scaleType = ImageView.ScaleType.CENTER_CROP
}

private val prevIcon = createIcon(R.drawable.ic_baseline_arrow_left_24)
private val nextIcon = createIcon(R.drawable.ic_baseline_arrow_right_24)
val prevIcon = createIcon(R.drawable.ic_baseline_arrow_left_24)
val nextIcon = createIcon(R.drawable.ic_baseline_arrow_right_24)

private val disabledAlpha = ctx.styledFloat(android.R.attr.disabledAlpha)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
package com.osfans.trime.ime.composition

import android.annotation.SuppressLint
import android.view.KeyEvent
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import com.osfans.trime.core.RimeKeyMapping
import com.osfans.trime.core.RimeProto
import com.osfans.trime.daemon.RimeSession
import com.osfans.trime.daemon.launchOnReady
Expand Down Expand Up @@ -49,8 +51,20 @@ class CandidatesView(

private val candidatesUi =
PagedCandidatesUi(ctx, theme).apply {
candidatesAdapter.setOnItemClickListener { _, _, position ->
rime.launchOnReady { it.selectPagedCandidate(position) }
setOnClickListener { type, position ->
when (type) {
PagedCandidatesUi.ClickType.CANDIDATE -> {
rime.launchOnReady { it.selectPagedCandidate(position) }
}
PagedCandidatesUi.ClickType.PREV_PAGE -> {
val value = RimeKeyMapping.keyCodeToVal(KeyEvent.KEYCODE_PAGE_UP)
rime.launchOnReady { it.processKey(value, 0u) }
}
PagedCandidatesUi.ClickType.NEXT_PAGE -> {
val value = RimeKeyMapping.keyCodeToVal(KeyEvent.KEYCODE_PAGE_DOWN)
rime.launchOnReady { it.processKey(value, 0u) }
}
}
}
}

Expand Down

0 comments on commit 069a12f

Please sign in to comment.