-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHushKeyboardView.kt
More file actions
171 lines (158 loc) · 6.58 KB
/
HushKeyboardView.kt
File metadata and controls
171 lines (158 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package com.rickyhu.hushkeyboard.keyboard
import android.content.Context
import android.os.Build
import android.os.VibratorManager
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.annotation.VisibleForTesting
import androidx.compose.foundation.background
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.AbstractComposeView
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.rickyhu.hushkeyboard.data.ThemeOption
import com.rickyhu.hushkeyboard.data.WideNotationOption
import com.rickyhu.hushkeyboard.keyboard.ui.rows.ControlKeyButtonRow
import com.rickyhu.hushkeyboard.keyboard.ui.rows.NotationKeyButtonsRow
import com.rickyhu.hushkeyboard.model.CubeKey
import com.rickyhu.hushkeyboard.model.NotationKeyProvider
import com.rickyhu.hushkeyboard.model.Turns
import com.rickyhu.hushkeyboard.service.HushIMEService
import com.rickyhu.hushkeyboard.theme.DarkBackground
import com.rickyhu.hushkeyboard.theme.LightBackground
import com.rickyhu.hushkeyboard.utils.deleteText
import com.rickyhu.hushkeyboard.utils.inputText
import com.rickyhu.hushkeyboard.utils.maybeVibrate
import com.rickyhu.hushkeyboard.utils.smartDelete
import com.rickyhu.hushkeyboard.utils.toInputConnection
import splitties.systemservices.inputMethodManager
private const val TAG = "HushKeyboardView"
class HushKeyboardView(context: Context) : AbstractComposeView(context) {
@RequiresApi(Build.VERSION_CODES.S)
@Composable
override fun Content() {
val viewModel = (context as HushIMEService).viewModel
val state by viewModel.keyboardState.collectAsState(KeyboardState())
HushKeyboardContent(state)
}
}
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
@Composable
fun HushKeyboardContent(state: KeyboardState) {
val context = LocalContext.current
val vibratorManager = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
context.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
} else {
null
}
var keyConfigState by remember { mutableStateOf(CubeKey.Config()) }
val isDarkTheme = when (state.themeOption) {
ThemeOption.System -> isSystemInDarkTheme()
ThemeOption.Light -> false
ThemeOption.Dark -> true
}
Column(
modifier = Modifier
.fillMaxWidth()
.background(color = if (isDarkTheme) DarkBackground else LightBackground)
.padding(vertical = 32.dp)
) {
NotationKeyButtonsRow(
keys = NotationKeyProvider.getFirstRowKeys(keyConfigState),
isDarkTheme = isDarkTheme,
addSpaceAfterNotation = state.addSpaceAfterNotation,
wideNotationOption = state.wideNotationOption,
onTextInput = {
Log.d(TAG, "Notation key tapped")
context.toInputConnection().inputText(it)
if (state.vibrateOnTap) vibratorManager?.maybeVibrate()
}
)
NotationKeyButtonsRow(
keys = NotationKeyProvider.getSecondRowKeys(keyConfigState),
isDarkTheme = isDarkTheme,
addSpaceAfterNotation = state.addSpaceAfterNotation,
wideNotationOption = state.wideNotationOption,
onTextInput = {
context.toInputConnection().inputText(it)
if (state.vibrateOnTap) vibratorManager?.maybeVibrate()
}
)
ControlKeyButtonRow(
turns = keyConfigState.turns,
isDarkTheme = isDarkTheme,
smartDelete = state.smartDelete,
inputMethodButtonAction = {
Log.d(TAG, "Input method picker tapped")
inputMethodManager.showInputMethodPicker()
if (state.vibrateOnTap) vibratorManager?.maybeVibrate()
},
rotateDirectionButtonAction = {
Log.d(TAG, "Rotate direction button tapped")
keyConfigState = keyConfigState.copy(
isCounterClockwise = !keyConfigState.isCounterClockwise
)
if (state.vibrateOnTap) vibratorManager?.maybeVibrate()
},
turnDegreeButtonAction = {
Log.d(TAG, "Turn degree button tapped")
keyConfigState = when (keyConfigState.turns) {
Turns.Single -> keyConfigState.copy(turns = Turns.Double)
Turns.Double -> keyConfigState.copy(turns = Turns.Triple)
Turns.Triple -> keyConfigState.copy(turns = Turns.Single)
}
if (state.vibrateOnTap) vibratorManager?.maybeVibrate()
},
wideTurnButtonAction = {
Log.d(TAG, "Wide turn button tapped")
keyConfigState = keyConfigState.copy(
isWideTurn = !keyConfigState.isWideTurn
)
if (state.vibrateOnTap) vibratorManager?.maybeVibrate()
},
deleteButtonAction = if (state.smartDelete) {
{
Log.d(TAG, "Delete button tapped")
context.toInputConnection().smartDelete()
if (state.vibrateOnTap) vibratorManager?.maybeVibrate()
}
} else {
{
Log.d(TAG, "Smart delete button tapped")
context.toInputConnection().deleteText()
if (state.vibrateOnTap) vibratorManager?.maybeVibrate()
}
},
newLineButtonAction = {
Log.d(TAG, "New line button tapped")
context.toInputConnection().inputText("\n")
if (state.vibrateOnTap) vibratorManager?.maybeVibrate()
}
)
}
}
@RequiresApi(Build.VERSION_CODES.S)
@Preview(showBackground = true)
@Composable
fun HushKeyboardPreview() {
HushKeyboardContent(
state = KeyboardState(
themeOption = ThemeOption.System,
wideNotationOption = WideNotationOption.WideWithW,
smartDelete = true,
addSpaceAfterNotation = true,
vibrateOnTap = true
)
)
}