-
-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implements unrolled candidate view
- Loading branch information
1 parent
663e9c4
commit c23add2
Showing
13 changed files
with
555 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
app/src/main/java/com/osfans/trime/ime/bar/UnrollButtonStateMachine.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// SPDX-FileCopyrightText: 2015 - 2024 Rime community | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
package com.osfans.trime.ime.bar | ||
|
||
import com.osfans.trime.ime.bar.UnrollButtonStateMachine.BooleanKey.UnrolledCandidatesEmpty | ||
import com.osfans.trime.ime.bar.UnrollButtonStateMachine.State.ClickToAttachWindow | ||
import com.osfans.trime.ime.bar.UnrollButtonStateMachine.State.ClickToDetachWindow | ||
import com.osfans.trime.ime.bar.UnrollButtonStateMachine.State.Hidden | ||
import com.osfans.trime.util.BuildTransitionEvent | ||
import com.osfans.trime.util.EventStateMachine | ||
import com.osfans.trime.util.TransitionBuildBlock | ||
|
||
object UnrollButtonStateMachine { | ||
enum class State { | ||
ClickToAttachWindow, | ||
ClickToDetachWindow, | ||
Hidden, | ||
} | ||
|
||
enum class BooleanKey : EventStateMachine.BooleanStateKey { | ||
UnrolledCandidatesEmpty, | ||
} | ||
|
||
enum class TransitionEvent(val builder: TransitionBuildBlock<State, BooleanKey>) : | ||
EventStateMachine.TransitionEvent<State, BooleanKey> by BuildTransitionEvent(builder) { | ||
UnrolledCandidatesUpdated({ | ||
from(Hidden) transitTo ClickToAttachWindow on (UnrolledCandidatesEmpty to false) | ||
from(ClickToAttachWindow) transitTo Hidden on (UnrolledCandidatesEmpty to true) | ||
}), | ||
UnrolledCandidatesAttached({ | ||
from(ClickToAttachWindow) transitTo ClickToDetachWindow | ||
}), | ||
UnrolledCandidatesDetached({ | ||
from(ClickToDetachWindow) transitTo Hidden on (UnrolledCandidatesEmpty to true) | ||
from(ClickToDetachWindow) transitTo ClickToAttachWindow on (UnrolledCandidatesEmpty to false) | ||
}), | ||
} | ||
|
||
fun new(block: (State) -> Unit) = | ||
EventStateMachine<State, TransitionEvent, BooleanKey>( | ||
initialState = Hidden, | ||
externalBooleanStates = | ||
mutableMapOf( | ||
UnrolledCandidatesEmpty to true, | ||
), | ||
).apply { | ||
onNewStateListener = block | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
app/src/main/java/com/osfans/trime/ime/candidates/adapter/PagingCandidateViewAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// SPDX-FileCopyrightText: 2015 - 2024 Rime community | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
package com.osfans.trime.ime.candidates.adapter | ||
|
||
import android.view.ViewGroup | ||
import androidx.paging.PagingDataAdapter | ||
import androidx.recyclerview.widget.DiffUtil | ||
import com.osfans.trime.core.CandidateItem | ||
import com.osfans.trime.data.theme.Theme | ||
import com.osfans.trime.ime.candidates.CandidateItemUi | ||
import com.osfans.trime.ime.candidates.CandidateViewHolder | ||
|
||
open class PagingCandidateViewAdapter(val theme: Theme) : PagingDataAdapter<CandidateItem, CandidateViewHolder>(diffCallback) { | ||
companion object { | ||
private val diffCallback = | ||
object : DiffUtil.ItemCallback<CandidateItem>() { | ||
override fun areItemsTheSame( | ||
oldItem: CandidateItem, | ||
newItem: CandidateItem, | ||
): Boolean { | ||
return oldItem === newItem | ||
} | ||
|
||
override fun areContentsTheSame( | ||
oldItem: CandidateItem, | ||
newItem: CandidateItem, | ||
): Boolean { | ||
return oldItem == newItem | ||
} | ||
} | ||
} | ||
|
||
var offset: Int = 0 | ||
private set | ||
|
||
fun refreshWithOffset(offset: Int) { | ||
this.offset = offset | ||
refresh() | ||
} | ||
|
||
override fun onCreateViewHolder( | ||
parent: ViewGroup, | ||
viewType: Int, | ||
): CandidateViewHolder { | ||
return CandidateViewHolder(CandidateItemUi(parent.context, theme)) | ||
} | ||
|
||
override fun onBindViewHolder( | ||
holder: CandidateViewHolder, | ||
position: Int, | ||
) { | ||
val (comment, text) = getItem(position)!! | ||
holder.ui.setText(text) | ||
holder.ui.setComment(comment) | ||
holder.text = text | ||
holder.comment = comment | ||
holder.idx = position + offset | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
app/src/main/java/com/osfans/trime/ime/candidates/unrolled/CandidatesPagingSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// SPDX-FileCopyrightText: 2015 - 2024 Rime community | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
package com.osfans.trime.ime.candidates.unrolled | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import com.osfans.trime.core.CandidateItem | ||
import com.osfans.trime.daemon.RimeSession | ||
import timber.log.Timber | ||
|
||
class CandidatesPagingSource(val rime: RimeSession, val num: 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) | ||
val startIndex = params.key ?: offset | ||
val pageSize = params.loadSize | ||
Timber.d("getCandidates(offset=$startIndex, limit=$pageSize)") | ||
val candidates = | ||
rime.runOnReady { | ||
getCandidates(startIndex, pageSize) | ||
} | ||
val prevKey = if (startIndex >= pageSize) startIndex - pageSize else null | ||
val nextKey = | ||
if (num > 0) { | ||
if (startIndex + pageSize + 1 >= num) null else startIndex + pageSize | ||
} else { | ||
if (candidates.size < pageSize) null else startIndex + pageSize | ||
} | ||
return LoadResult.Page(candidates.toList(), prevKey, nextKey) | ||
} | ||
|
||
// always reload from beginning | ||
override fun getRefreshKey(state: PagingState<Int, CandidateItem>) = null | ||
} |
46 changes: 46 additions & 0 deletions
46
app/src/main/java/com/osfans/trime/ime/candidates/unrolled/UnrolledCandidateLayout.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// SPDX-FileCopyrightText: 2015 - 2024 Rime community | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
package com.osfans.trime.ime.candidates.unrolled | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Context | ||
import androidx.constraintlayout.widget.ConstraintLayout | ||
import com.osfans.trime.R | ||
import com.osfans.trime.data.theme.ColorManager | ||
import com.osfans.trime.data.theme.Theme | ||
import splitties.views.dsl.constraintlayout.centerInParent | ||
import splitties.views.dsl.constraintlayout.lParams | ||
import splitties.views.dsl.core.add | ||
import splitties.views.dsl.recyclerview.recyclerView | ||
|
||
@SuppressLint("ViewConstructor") | ||
class UnrolledCandidateLayout(context: Context, theme: Theme) : ConstraintLayout(context) { | ||
val recyclerView = | ||
recyclerView { | ||
isVerticalScrollBarEnabled = false | ||
} | ||
|
||
init { | ||
id = R.id.unrolled_candidate_view | ||
background = | ||
ColorManager.getDrawable( | ||
context, | ||
"candidate_background", | ||
theme.generalStyle.candidateBorder, | ||
"candidate_border_color", | ||
theme.generalStyle.candidateBorderRound, | ||
) | ||
|
||
add( | ||
recyclerView, | ||
lParams { | ||
centerInParent() | ||
}, | ||
) | ||
} | ||
|
||
fun resetPosition() { | ||
recyclerView.scrollToPosition(0) | ||
} | ||
} |
Oops, something went wrong.