@@ -2,7 +2,6 @@ package com.osfans.trime.ime.symbol
2
2
3
3
import android.annotation.SuppressLint
4
4
import android.content.Context
5
- import android.view.View
6
5
import androidx.core.view.setPadding
7
6
import androidx.lifecycle.lifecycleScope
8
7
import androidx.recyclerview.widget.RecyclerView
@@ -11,8 +10,8 @@ import com.google.android.flexbox.FlexDirection
11
10
import com.google.android.flexbox.FlexWrap
12
11
import com.google.android.flexbox.FlexboxLayoutManager
13
12
import com.google.android.flexbox.JustifyContent
14
- import com.osfans.trime.R
15
13
import com.osfans.trime.core.Rime
14
+ import com.osfans.trime.data.SymbolHistory
16
15
import com.osfans.trime.data.db.ClipboardHelper
17
16
import com.osfans.trime.data.db.CollectionHelper
18
17
import com.osfans.trime.data.db.DatabaseBean
@@ -22,33 +21,22 @@ import com.osfans.trime.ime.core.Trime
22
21
import com.osfans.trime.ime.enums.KeyCommandType
23
22
import com.osfans.trime.ime.enums.SymbolKeyboardType
24
23
import com.osfans.trime.ime.text.TextInputManager
25
- import com.osfans.trime.util.appContext
26
24
import com.osfans.trime.util.dp2px
27
25
import kotlinx.coroutines.launch
28
26
import timber.log.Timber
29
- import kotlin.math.ceil
30
27
31
28
class LiquidKeyboard (private val context : Context ) {
32
29
private val theme: Config = Config .get()
33
30
private val tabManager: TabManager = TabManager .get()
34
31
private val service: Trime = Trime .getService()
35
- private var rootView: View ? = null
36
32
private lateinit var keyboardView: RecyclerView
37
- private var historyBeans: MutableList <SimpleKeyBean >? = null
38
- private var marginLeft = 0
39
- private var marginTop = 0
40
- private var singleWidth = 0
41
- private var parentWidth = 0
42
- private var keyHeight = 0
43
- var isLand = false
44
- private val historySavePath = " ${appContext.getExternalFilesDir(null )!! .absolutePath} /key_history"
33
+ private val symbolHistory = SymbolHistory (180 )
45
34
46
35
private val flexbox: FlexboxLayoutManager by lazy {
47
36
return @lazy FlexboxLayoutManager (context).apply {
48
37
flexDirection = FlexDirection .ROW // 主轴为水平方向,起点在左端。
49
38
flexWrap = FlexWrap .WRAP // 按正常方向换行
50
39
justifyContent = JustifyContent .FLEX_START // 交叉轴的起点对齐
51
- // alignItems = AlignItems.BASELINE
52
40
}
53
41
}
54
42
@@ -67,7 +55,8 @@ class LiquidKeyboard(private val context: Context) {
67
55
68
56
fun select (i : Int ): SymbolKeyboardType {
69
57
val tag = TabManager .getTag(i)
70
- calcPadding(tag.type)
58
+ symbolHistory.load()
59
+ keyboardView.removeAllViews()
71
60
when (tag.type) {
72
61
SymbolKeyboardType .CLIPBOARD ,
73
62
SymbolKeyboardType .COLLECTION ,
@@ -91,72 +80,8 @@ class LiquidKeyboard(private val context: Context) {
91
80
return tag.type
92
81
}
93
82
94
- // 设置liquidKeyboard共用的布局参数
95
- fun calcPadding (width : Int ) {
96
- parentWidth = width
97
-
98
- // liquid_keyboard/margin_x定义了每个键左右两边的间隙,
99
- // 也就是说相邻两个键间隙是x2,而horizontal_gap定义的是spacer,使用时需要/2
100
- marginLeft = dp2px(theme.liquid.getFloat(" margin_x" )).toInt()
101
- if (marginLeft == 0 ) {
102
- var horizontal_gap = dp2px(theme.style.getFloat(" horizontal_gap" )).toInt()
103
- if (horizontal_gap > 1 ) {
104
- horizontal_gap / = 2
105
- }
106
- marginLeft = horizontal_gap
107
- }
108
-
109
- // 初次显示布局,需要刷新背景
110
- rootView = keyboardView.parent as View
111
- val keyboardBackground = theme.colors.getDrawable(" liquid_keyboard_background" )
112
- if (keyboardBackground != null ) rootView!! .background = keyboardBackground
113
- var keyboardHeight = dp2px(theme.style.getFloat(" keyboard_height" )).toInt()
114
- if (isLand) {
115
- val keyBoardHeightLand = dp2px(theme.style.getFloat(" keyboard_height_land" )).toInt()
116
- if (keyBoardHeightLand > 0 ) keyboardHeight = keyBoardHeightLand
117
- }
118
- var row = theme.liquid.getInt(" row" )
119
- if (row > 0 ) {
120
- if (isLand) {
121
- val r = theme.liquid.getInt(" row_land" )
122
- if (r > 0 ) row = r
123
- }
124
- val rawHeight = theme.liquid.getFloat(" key_height" )
125
- val rawVGap = theme.liquid.getFloat(" vertical_gap" )
126
- val scale = keyboardHeight.toFloat() / ((rawHeight + rawVGap) * row)
127
- marginTop = ceil((rawVGap * scale).toDouble()).toInt()
128
- keyHeight = keyboardHeight / row - marginTop
129
- } else {
130
- keyHeight = dp2px(theme.liquid.getFloat(" key_height_land" )).toInt()
131
- if (! isLand || keyHeight <= 0 ) keyHeight = dp2px(theme.liquid.getFloat(" key_height" )).toInt()
132
- marginTop = dp2px(theme.liquid.getFloat(" vertical_gap" )).toInt()
133
- }
134
- Timber .i(" config keyHeight=$keyHeight marginTop=$marginTop " )
135
- if (isLand) {
136
- singleWidth = dp2px(theme.liquid.getFloat(" single_width_land" )).toInt()
137
- if (singleWidth <= 0 ) singleWidth = dp2px(theme.liquid.getFloat(" single_width" )).toInt()
138
- } else singleWidth = dp2px(theme.liquid.getFloat(" single_width" )).toInt()
139
- if (singleWidth <= 0 ) singleWidth = context.resources.getDimensionPixelSize(R .dimen.simple_key_single_width)
140
- }
141
-
142
- // 每次点击tab都需要刷新的参数
143
- private fun calcPadding (type : SymbolKeyboardType ) {
144
- val padding = theme.keyboardPadding
145
- if (type == = SymbolKeyboardType .SINGLE ) {
146
- padding[0 ] = (
147
- (if (rootView!! .width > 0 ) rootView!! .width else parentWidth) %
148
- (singleWidth + marginLeft * 2 ) /
149
- 2
150
- )
151
- padding[1 ] = padding[0 ]
152
- }
153
- rootView!! .setPadding(padding[0 ], 0 , padding[1 ], 0 )
154
- historyBeans = SimpleKeyDao .getSymbolKeyHistory(historySavePath)
155
- }
156
-
157
83
private fun initFixData (i : Int ) {
158
84
val tabTag = TabManager .getTag(i)
159
- keyboardView.removeAllViews()
160
85
161
86
val simpleAdapter = SimpleAdapter (theme).apply {
162
87
// 列表适配器的点击监听事件
@@ -168,8 +93,8 @@ class LiquidKeyboard(private val context: Context) {
168
93
service.currentInputConnection?.run {
169
94
commitText(bean.text, 1 )
170
95
if (tabTag.type != = SymbolKeyboardType .HISTORY ) {
171
- historyBeans?.add( 0 , bean)
172
- SimpleKeyDao .saveSymbolKeyHistory(historySavePath, historyBeans !! )
96
+ symbolHistory.insert( bean.text )
97
+ symbolHistory.save( )
173
98
}
174
99
}
175
100
} else {
@@ -207,7 +132,7 @@ class LiquidKeyboard(private val context: Context) {
207
132
208
133
when (tabTag.type) {
209
134
SymbolKeyboardType .HISTORY ->
210
- simpleAdapter.updateBeans(historyBeans !! )
135
+ simpleAdapter.updateBeans(symbolHistory.toOrderedList().map(:: SimpleKeyBean ) )
211
136
SymbolKeyboardType .TABS ->
212
137
simpleAdapter.updateBeans(tabManager.tabSwitchData)
213
138
else ->
@@ -217,8 +142,6 @@ class LiquidKeyboard(private val context: Context) {
217
142
}
218
143
219
144
private fun initDbData (type : SymbolKeyboardType ) {
220
- keyboardView.removeAllViews()
221
-
222
145
val dbAdapter = FlexibleAdapter (theme).apply {
223
146
setListener(object : FlexibleAdapter .Listener {
224
147
override fun onPaste (bean : DatabaseBean ) {
@@ -316,8 +239,6 @@ class LiquidKeyboard(private val context: Context) {
316
239
}
317
240
318
241
private fun initCandidates () {
319
- keyboardView.removeAllViews()
320
-
321
242
val candidateAdapter = CandidateAdapter (theme).apply {
322
243
setListener { position ->
323
244
TextInputManager .getInstance().onCandidatePressed(position)
@@ -339,8 +260,6 @@ class LiquidKeyboard(private val context: Context) {
339
260
}
340
261
341
262
private fun initVarLengthKeys (data : List <SimpleKeyBean >) {
342
- keyboardView.removeAllViews()
343
-
344
263
val candidateAdapter = CandidateAdapter (theme).apply {
345
264
setListener { position ->
346
265
service.currentInputConnection?.commitText(data[position].text, 1 )
0 commit comments