|
5 | 5 | package com.osfans.trime.ime.candidates
|
6 | 6 |
|
7 | 7 | import android.content.Context
|
8 |
| -import android.text.TextUtils |
9 |
| -import androidx.constraintlayout.widget.ConstraintLayout |
| 8 | +import android.graphics.drawable.ColorDrawable |
| 9 | +import android.view.View |
| 10 | +import com.osfans.trime.core.CandidateItem |
10 | 11 | import com.osfans.trime.data.theme.ColorManager
|
11 | 12 | import com.osfans.trime.data.theme.FontManager
|
12 | 13 | import com.osfans.trime.data.theme.Theme
|
| 14 | +import com.osfans.trime.ime.core.AutoScaleTextView |
13 | 15 | import com.osfans.trime.util.pressHighlightDrawable
|
14 |
| -import splitties.views.dsl.constraintlayout.after |
15 |
| -import splitties.views.dsl.constraintlayout.before |
16 | 16 | import splitties.views.dsl.constraintlayout.centerHorizontally
|
17 |
| -import splitties.views.dsl.constraintlayout.centerInParent |
18 | 17 | import splitties.views.dsl.constraintlayout.centerVertically
|
19 | 18 | import splitties.views.dsl.constraintlayout.constraintLayout
|
20 |
| -import splitties.views.dsl.constraintlayout.endOfParent |
21 |
| -import splitties.views.dsl.constraintlayout.lParams |
22 |
| -import splitties.views.dsl.constraintlayout.startOfParent |
23 |
| -import splitties.views.dsl.constraintlayout.topOfParent |
| 19 | +import splitties.views.dsl.constraintlayout.horizontalChain |
| 20 | +import splitties.views.dsl.constraintlayout.packed |
| 21 | +import splitties.views.dsl.constraintlayout.verticalChain |
24 | 22 | import splitties.views.dsl.core.Ui
|
25 |
| -import splitties.views.dsl.core.add |
26 |
| -import splitties.views.dsl.core.textView |
| 23 | +import splitties.views.dsl.core.view |
27 | 24 | import splitties.views.dsl.core.wrapContent
|
28 | 25 | import splitties.views.gravityCenter
|
29 | 26 |
|
30 | 27 | class CandidateItemUi(
|
31 | 28 | override val ctx: Context,
|
32 | 29 | theme: Theme,
|
33 | 30 | ) : Ui {
|
34 |
| - private val maybeCandidateTextColor = ColorManager.getColor("candidate_text_color") |
35 |
| - private val maybeCommentTextColor = ColorManager.getColor("comment_text_color") |
36 |
| - private val maybeHighlightedCandidateTextColor = ColorManager.getColor("hilited_candidate_text_color") |
37 |
| - private val maybeHighlightedCommentTextColor = ColorManager.getColor("hilited_comment_text_color") |
38 |
| - private val maybeHighlightedCandidateBackColor = ColorManager.getColor("hilited_candidate_back_color") |
| 31 | + private val firstTextSize = theme.generalStyle.candidateTextSize |
| 32 | + private val lastTextSize = theme.generalStyle.commentTextSize |
| 33 | + private val firstTextFont = FontManager.getTypeface("candidate_font") |
| 34 | + private val lastTextFont = FontManager.getTypeface("comment_font") |
| 35 | + private val firstTextColor = ColorManager.getColor("candidate_text_color")!! |
| 36 | + private val lastTextColor = ColorManager.getColor("comment_text_color")!! |
| 37 | + private val lastTextColorH = ColorManager.getColor("hilited_comment_text_color")!! |
| 38 | + private val firstTextColorH = ColorManager.getColor("hilited_candidate_text_color")!! |
| 39 | + private val firstBackColorH = ColorManager.getColor("hilited_candidate_back_color")!! |
39 | 40 |
|
40 |
| - val label = |
41 |
| - textView { |
42 |
| - textSize = theme.generalStyle.candidateTextSize.toFloat() |
43 |
| - typeface = FontManager.getTypeface("candidate_font") |
| 41 | + private val firstText = |
| 42 | + view(::AutoScaleTextView) { |
| 43 | + textSize = firstTextSize |
| 44 | + typeface = firstTextFont |
44 | 45 | isSingleLine = true
|
45 | 46 | gravity = gravityCenter
|
46 |
| - ellipsize = TextUtils.TruncateAt.END |
47 |
| - maybeCandidateTextColor?.let { setTextColor(it) } |
| 47 | + scaleMode = AutoScaleTextView.Mode.Proportional |
48 | 48 | }
|
49 | 49 |
|
50 |
| - val altLabel = |
51 |
| - textView { |
52 |
| - textSize = theme.generalStyle.commentTextSize.toFloat() |
53 |
| - typeface = FontManager.getTypeface("comment_font") |
| 50 | + private val lastText = |
| 51 | + view(::AutoScaleTextView) { |
| 52 | + textSize = lastTextSize |
| 53 | + typeface = lastTextFont |
54 | 54 | isSingleLine = true
|
55 | 55 | gravity = gravityCenter
|
56 |
| - ellipsize = TextUtils.TruncateAt.END |
57 |
| - maybeCommentTextColor?.let { setTextColor(it) } |
| 56 | + scaleMode = AutoScaleTextView.Mode.Proportional |
58 | 57 | }
|
59 | 58 |
|
60 | 59 | override val root =
|
61 | 60 | constraintLayout {
|
62 | 61 | if (theme.generalStyle.commentOnTop) {
|
63 |
| - add( |
64 |
| - altLabel, |
65 |
| - lParams(wrapContent, wrapContent) { |
66 |
| - topOfParent() |
67 |
| - centerHorizontally() |
68 |
| - }, |
69 |
| - ) |
70 |
| - add( |
71 |
| - label, |
72 |
| - lParams(wrapContent, wrapContent) { |
73 |
| - centerInParent() |
74 |
| - }, |
| 62 | + verticalChain( |
| 63 | + listOf(lastText, firstText), |
| 64 | + style = packed, |
| 65 | + defaultWidth = wrapContent, |
| 66 | + defaultHeight = wrapContent, |
| 67 | + initParams = { centerHorizontally() }, |
75 | 68 | )
|
76 | 69 | } else {
|
77 |
| - add( |
78 |
| - label, |
79 |
| - lParams(wrapContent, wrapContent) { |
80 |
| - startOfParent() |
81 |
| - centerVertically() |
82 |
| - before(altLabel) |
83 |
| - |
84 |
| - horizontalChainStyle = ConstraintLayout.LayoutParams.CHAIN_PACKED |
85 |
| - horizontalBias = 0.5f |
86 |
| - }, |
87 |
| - ) |
88 |
| - add( |
89 |
| - altLabel, |
90 |
| - lParams(wrapContent, wrapContent) { |
91 |
| - after(label) |
92 |
| - centerVertically() |
93 |
| - endOfParent() |
94 |
| - |
95 |
| - horizontalBias = 0.5f |
96 |
| - }, |
| 70 | + horizontalChain( |
| 71 | + listOf(firstText, lastText), |
| 72 | + style = packed, |
| 73 | + defaultWidth = wrapContent, |
| 74 | + initParams = { centerVertically() }, |
97 | 75 | )
|
98 | 76 | }
|
99 | 77 | }
|
100 | 78 |
|
101 |
| - fun highlight(yes: Boolean) { |
102 |
| - if (yes) { |
103 |
| - maybeHighlightedCandidateTextColor?.let { label.setTextColor(it) } |
104 |
| - maybeHighlightedCommentTextColor?.let { altLabel.setTextColor(it) } |
105 |
| - maybeHighlightedCandidateBackColor?.let { root.setBackgroundColor(it) } |
106 |
| - } else { |
107 |
| - maybeCandidateTextColor?.let { label.setTextColor(it) } |
108 |
| - maybeCommentTextColor?.let { altLabel.setTextColor(it) } |
109 |
| - maybeHighlightedCandidateBackColor?.let { root.background = pressHighlightDrawable(it) } |
| 79 | + fun update( |
| 80 | + item: CandidateItem, |
| 81 | + isHighlighted: Boolean, |
| 82 | + obtainLast: Boolean, |
| 83 | + ) { |
| 84 | + val firstColor = if (isHighlighted) firstTextColorH else firstTextColor |
| 85 | + val lastColor = if (isHighlighted) lastTextColorH else lastTextColor |
| 86 | + firstText.text = item.text |
| 87 | + firstText.setTextColor(firstColor) |
| 88 | + lastText.run { |
| 89 | + if (obtainLast) { |
| 90 | + lastText.text = item.comment |
| 91 | + lastText.setTextColor(lastColor) |
| 92 | + if (visibility == View.GONE) visibility = View.VISIBLE |
| 93 | + } else if (visibility != View.GONE) { |
| 94 | + visibility = View.GONE |
| 95 | + } |
110 | 96 | }
|
| 97 | + root.background = |
| 98 | + if (isHighlighted) { |
| 99 | + ColorDrawable(firstBackColorH) |
| 100 | + } else { |
| 101 | + pressHighlightDrawable(firstBackColorH) |
| 102 | + } |
111 | 103 | }
|
112 | 104 | }
|
0 commit comments