Skip to content

Commit 4e7247c

Browse files
committed
fix: could not unroll the candidates somehow
refactor: rename field `left` to `before` in CompatCandidateViewAdapter
1 parent 2b6063e commit 4e7247c

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

app/src/main/java/com/osfans/trime/ime/candidates/CompactCandidateModule.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class CompactCandidateModule(
5959

6060
fun refreshUnrolled() {
6161
runBlocking {
62-
_unrolledCandidateOffset.emit(adapter.left + view.childCount)
62+
_unrolledCandidateOffset.emit(adapter.before + view.childCount)
6363
}
6464
bar.unrollButtonStateMachine.push(
6565
UnrollButtonStateMachine.TransitionEvent.UnrolledCandidatesUpdated,
@@ -71,10 +71,10 @@ class CompactCandidateModule(
7171
val adapter by lazy {
7272
CompactCandidateViewAdapter(theme).apply {
7373
setOnDebouncedItemClick { _, _, position ->
74-
rime.launchOnReady { it.selectCandidate(left + position) }
74+
rime.launchOnReady { it.selectCandidate(before + position) }
7575
}
7676
setOnItemLongClickListener { _, view, position ->
77-
showCandidateAction(left + position, items[position].text, view)
77+
showCandidateAction(before + position, items[position].text, view)
7878
true
7979
}
8080
}

app/src/main/java/com/osfans/trime/ime/candidates/adapter/CompactCandidateViewAdapter.kt

+3-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import splitties.dimensions.dp
1717
import splitties.views.dsl.core.matchParent
1818
import splitties.views.dsl.core.wrapContent
1919
import splitties.views.setPaddingDp
20-
import kotlin.math.max
2120

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

35-
val left: Int
36-
get() =
37-
if (sticky > 0 && previous > 0) {
38-
sticky + previous
39-
} else {
40-
max(sticky, previous)
41-
}
34+
val before: Int
35+
get() = sticky + previous
4236

4337
fun updateCandidates(
4438
list: List<CandidateItem>,
@@ -83,7 +77,7 @@ open class CompactCandidateViewAdapter(val theme: Theme) : BaseQuickAdapter<Cand
8377
}
8478
holder.text = text
8579
holder.comment = comment
86-
holder.idx = left + position // unused
80+
holder.idx = before + position // unused
8781
holder.ui.root.updateLayoutParams<FlexboxLayoutManager.LayoutParams> {
8882
minWidth = 0
8983
flexGrow = 1f

app/src/main/java/com/osfans/trime/ime/candidates/unrolled/CandidatesPagingSource.kt

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import com.osfans.trime.core.CandidateItem
1010
import com.osfans.trime.daemon.RimeSession
1111
import timber.log.Timber
1212

13-
class CandidatesPagingSource(val rime: RimeSession, val offset: Int) :
13+
class CandidatesPagingSource(val rime: RimeSession, val total: Int, val offset: Int) :
1414
PagingSource<Int, CandidateItem>() {
1515
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, CandidateItem> {
1616
// use candidate index for key, null means load from beginning (including offset)
@@ -22,7 +22,12 @@ class CandidatesPagingSource(val rime: RimeSession, val offset: Int) :
2222
getCandidates(startIndex, pageSize)
2323
}
2424
val prevKey = if (startIndex >= pageSize) startIndex - pageSize else null
25-
val nextKey = if (candidates.size < pageSize) null else startIndex + pageSize
25+
val nextKey =
26+
if (total > 0) {
27+
if (startIndex + pageSize + 1 >= total) null else startIndex + pageSize
28+
} else {
29+
if (candidates.size < pageSize) null else startIndex + pageSize
30+
}
2631
return LoadResult.Page(candidates.toList(), prevKey, nextKey)
2732
}
2833

app/src/main/java/com/osfans/trime/ime/candidates/unrolled/window/BaseUnrolledCandidateWindow.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ abstract class BaseUnrolledCandidateWindow(
8282
Pager(PagingConfig(pageSize = 48)) {
8383
CandidatesPagingSource(
8484
rime,
85+
total = compactCandidate.adapter.run { before + itemCount },
8586
offset = adapter.offset,
8687
)
8788
}
@@ -123,7 +124,8 @@ abstract class BaseUnrolledCandidateWindow(
123124

124125
private fun updateCandidatesWithOffset(offset: Int) {
125126
val candidates = compactCandidate.adapter.items
126-
if (candidates.isEmpty()) {
127+
val sticky = compactCandidate.adapter.sticky
128+
if (candidates.isEmpty() && sticky == 0) {
127129
windowManager.attachWindow(KeyboardWindow)
128130
} else {
129131
adapter.refreshWithOffset(offset)
@@ -137,7 +139,7 @@ abstract class BaseUnrolledCandidateWindow(
137139
bar.unrollButtonStateMachine.push(
138140
UnrollButtonStateMachine.TransitionEvent.UnrolledCandidatesDetached,
139141
UnrollButtonStateMachine.BooleanKey.UnrolledCandidatesEmpty to
140-
(compactCandidate.adapter.run { isLastPage && itemCount == adapter.offset }),
142+
(compactCandidate.adapter.run { isLastPage && (before + itemCount) == adapter.offset }),
141143
)
142144
offsetJob?.cancel()
143145
candidatesSubmitJob?.cancel()

0 commit comments

Comments
 (0)