Skip to content

Commit

Permalink
fix: could not unroll the candidates somehow
Browse files Browse the repository at this point in the history
refactor: rename field `left` to `before` in CompatCandidateViewAdapter
  • Loading branch information
WhiredPlanck committed Sep 2, 2024
1 parent 2b6063e commit 4e7247c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class CompactCandidateModule(

fun refreshUnrolled() {
runBlocking {
_unrolledCandidateOffset.emit(adapter.left + view.childCount)
_unrolledCandidateOffset.emit(adapter.before + view.childCount)
}
bar.unrollButtonStateMachine.push(
UnrollButtonStateMachine.TransitionEvent.UnrolledCandidatesUpdated,
Expand All @@ -71,10 +71,10 @@ class CompactCandidateModule(
val adapter by lazy {
CompactCandidateViewAdapter(theme).apply {
setOnDebouncedItemClick { _, _, position ->
rime.launchOnReady { it.selectCandidate(left + position) }
rime.launchOnReady { it.selectCandidate(before + position) }
}
setOnItemLongClickListener { _, view, position ->
showCandidateAction(left + position, items[position].text, view)
showCandidateAction(before + position, items[position].text, view)
true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import splitties.dimensions.dp
import splitties.views.dsl.core.matchParent
import splitties.views.dsl.core.wrapContent
import splitties.views.setPaddingDp
import kotlin.math.max

open class CompactCandidateViewAdapter(val theme: Theme) : BaseQuickAdapter<CandidateItem, CandidateViewHolder>() {
var sticky: Int = 0
Expand All @@ -32,13 +31,8 @@ open class CompactCandidateViewAdapter(val theme: Theme) : BaseQuickAdapter<Cand
var highlightedIdx: Int = -1
private set

val left: Int
get() =
if (sticky > 0 && previous > 0) {
sticky + previous
} else {
max(sticky, previous)
}
val before: Int
get() = sticky + previous

fun updateCandidates(
list: List<CandidateItem>,
Expand Down Expand Up @@ -83,7 +77,7 @@ open class CompactCandidateViewAdapter(val theme: Theme) : BaseQuickAdapter<Cand
}
holder.text = text
holder.comment = comment
holder.idx = left + position // unused
holder.idx = before + position // unused
holder.ui.root.updateLayoutParams<FlexboxLayoutManager.LayoutParams> {
minWidth = 0
flexGrow = 1f
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.osfans.trime.core.CandidateItem
import com.osfans.trime.daemon.RimeSession
import timber.log.Timber

class CandidatesPagingSource(val rime: RimeSession, val offset: Int) :
class CandidatesPagingSource(val rime: RimeSession, val total: Int, val offset: Int) :
PagingSource<Int, CandidateItem>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, CandidateItem> {
// use candidate index for key, null means load from beginning (including offset)
Expand All @@ -22,7 +22,12 @@ class CandidatesPagingSource(val rime: RimeSession, val offset: Int) :
getCandidates(startIndex, pageSize)
}
val prevKey = if (startIndex >= pageSize) startIndex - pageSize else null
val nextKey = if (candidates.size < pageSize) null else startIndex + pageSize
val nextKey =
if (total > 0) {
if (startIndex + pageSize + 1 >= total) null else startIndex + pageSize
} else {
if (candidates.size < pageSize) null else startIndex + pageSize
}
return LoadResult.Page(candidates.toList(), prevKey, nextKey)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ abstract class BaseUnrolledCandidateWindow(
Pager(PagingConfig(pageSize = 48)) {
CandidatesPagingSource(
rime,
total = compactCandidate.adapter.run { before + itemCount },
offset = adapter.offset,
)
}
Expand Down Expand Up @@ -123,7 +124,8 @@ abstract class BaseUnrolledCandidateWindow(

private fun updateCandidatesWithOffset(offset: Int) {
val candidates = compactCandidate.adapter.items
if (candidates.isEmpty()) {
val sticky = compactCandidate.adapter.sticky
if (candidates.isEmpty() && sticky == 0) {
windowManager.attachWindow(KeyboardWindow)
} else {
adapter.refreshWithOffset(offset)
Expand All @@ -137,7 +139,7 @@ abstract class BaseUnrolledCandidateWindow(
bar.unrollButtonStateMachine.push(
UnrollButtonStateMachine.TransitionEvent.UnrolledCandidatesDetached,
UnrollButtonStateMachine.BooleanKey.UnrolledCandidatesEmpty to
(compactCandidate.adapter.run { isLastPage && itemCount == adapter.offset }),
(compactCandidate.adapter.run { isLastPage && (before + itemCount) == adapter.offset }),
)
offsetJob?.cancel()
candidatesSubmitJob?.cancel()
Expand Down

0 comments on commit 4e7247c

Please sign in to comment.