Skip to content

Commit 481dc1f

Browse files
Danielekellertarikyasar
authored andcommitted
Add possibility to change imeAction for OTP view (#44)
Add ime action and callbacks
1 parent 52ab95d commit 481dc1f

7 files changed

Lines changed: 89 additions & 9 deletions

File tree

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.composeuisuite.ohteepee.helpers
22

3-
fun sleepThreadToSeeUiTestResults(){
3+
fun sleepThreadToSeeUiTestResults() {
44
Thread.sleep(1000)
5-
}
5+
}

ohteepee/src/main/java/com/composeuisuite/ohteepee/OhTeePeeCell.kt

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import androidx.compose.ui.graphics.SolidColor
2929
import androidx.compose.ui.input.key.Key
3030
import androidx.compose.ui.input.key.key
3131
import androidx.compose.ui.input.key.onPreviewKeyEvent
32+
import androidx.compose.ui.platform.SoftwareKeyboardController
3233
import androidx.compose.ui.platform.testTag
3334
import androidx.compose.ui.text.TextStyle
3435
import androidx.compose.ui.text.input.ImeAction
@@ -54,11 +55,19 @@ internal fun OhTeePeeCell(
5455
value: String,
5556
onValueChange: (String) -> Unit,
5657
keyboardType: KeyboardType,
58+
imeAction: ImeAction,
59+
keyboardController: SoftwareKeyboardController?,
5760
configurations: OhTeePeeConfigurations,
5861
placeHolder: String,
5962
visualTransformation: VisualTransformation,
6063
isErrorOccurred: Boolean,
6164
enabled: Boolean,
65+
callbackOnNext: () -> Unit,
66+
callbackOnDone: () -> Unit,
67+
callbackOnPrevious: () -> Unit,
68+
callbackOnSearch: () -> Unit,
69+
callbackOnSend: () -> Unit,
70+
callbackOnGo: () -> Unit,
6271
modifier: Modifier = Modifier,
6372
) {
6473
var isFocused by remember { mutableStateOf(false) }
@@ -138,13 +147,29 @@ internal fun OhTeePeeCell(
138147
.cellBackground(cellConfiguration.cellBackground)
139148
.testTag(OH_TEE_PEE_CELL_TEST_TAG),
140149
keyboardOptions = KeyboardOptions(
150+
autoCorrectEnabled = false,
141151
keyboardType = keyboardType,
142-
imeAction = ImeAction.Next,
143-
autoCorrect = false,
152+
imeAction = imeAction,
144153
),
145154
keyboardActions = KeyboardActions(
146-
onNext = {},
147-
onDone = {},
155+
onNext = {
156+
callbackOnNext()
157+
},
158+
onDone = {
159+
callbackOnDone()
160+
},
161+
onPrevious = {
162+
callbackOnPrevious()
163+
},
164+
onSearch = {
165+
callbackOnSearch()
166+
},
167+
onSend = {
168+
callbackOnSend()
169+
},
170+
onGo = {
171+
callbackOnGo()
172+
},
148173
),
149174
singleLine = true,
150175
enabled = enabled,

ohteepee/src/main/java/com/composeuisuite/ohteepee/OhTeePeeDefaults.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,4 @@ object OhTeePeeDefaults {
9494
textStyle = textStyle,
9595
placeHolderTextStyle = placeHolderTextStyle,
9696
)
97-
}
97+
}

ohteepee/src/main/java/com/composeuisuite/ohteepee/OhTeePeeInput.kt

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import androidx.compose.ui.graphics.Color.Companion.Transparent
2424
import androidx.compose.ui.graphics.graphicsLayer
2525
import androidx.compose.ui.platform.LocalFocusManager
2626
import androidx.compose.ui.platform.LocalLayoutDirection
27+
import androidx.compose.ui.platform.SoftwareKeyboardController
28+
import androidx.compose.ui.text.input.ImeAction
2729
import androidx.compose.ui.text.input.KeyboardType
2830
import androidx.compose.ui.text.input.PasswordVisualTransformation
2931
import androidx.compose.ui.text.input.VisualTransformation
@@ -87,7 +89,15 @@ fun OhTeePeeInput(
8789
modifier: Modifier = Modifier,
8890
isValueInvalid: Boolean = false,
8991
keyboardType: KeyboardType = KeyboardType.NumberPassword,
92+
imeAction: ImeAction = ImeAction.Next,
93+
callbackOnNext: () -> Unit = {},
94+
callbackOnDone: () -> Unit = {},
95+
callbackOnPrevious: () -> Unit = {},
96+
callbackOnSearch: () -> Unit = {},
97+
callbackOnSend: () -> Unit = {},
98+
callbackOnGo: () -> Unit = {},
9099
enabled: Boolean = true,
100+
keyboardController: SoftwareKeyboardController? = null,
91101
autoFocusByDefault: Boolean = true,
92102
horizontalArrangement: Arrangement.Horizontal = Arrangement.Start,
93103
layoutDirection: LayoutDirection = LocalLayoutDirection.current,
@@ -201,6 +211,14 @@ fun OhTeePeeInput(
201211
moveFocus = ::moveFocus,
202212
)
203213
},
214+
imeAction = imeAction,
215+
callbackOnNext = callbackOnNext,
216+
callbackOnDone = callbackOnDone,
217+
callbackOnPrevious = callbackOnPrevious,
218+
callbackOnSearch = callbackOnSearch,
219+
callbackOnSend = callbackOnSend,
220+
callbackOnGo = callbackOnGo,
221+
keyboardController = keyboardController,
204222
)
205223
}
206224

@@ -233,13 +251,21 @@ private fun OhTeePeeInput(
233251
placeHolder: String,
234252
isErrorOccurred: Boolean,
235253
keyboardType: KeyboardType,
254+
imeAction: ImeAction,
236255
ohTeePeeConfigurations: OhTeePeeConfigurations,
237256
focusRequesters: List<FocusRequester>,
238257
enabled: Boolean,
239258
visualTransformation: VisualTransformation,
240259
onCellInputChange: (index: Int, value: String) -> Unit,
241260
horizontalArrangement: Arrangement.Horizontal,
242261
layoutDirection: LayoutDirection,
262+
callbackOnNext: () -> Unit,
263+
callbackOnDone: () -> Unit,
264+
callbackOnPrevious: () -> Unit,
265+
callbackOnSearch: () -> Unit,
266+
callbackOnSend: () -> Unit,
267+
callbackOnGo: () -> Unit,
268+
keyboardController: SoftwareKeyboardController? = null,
243269
divider: @Composable (RowScope.(cellIndex: Int) -> Unit)?,
244270
) {
245271
CompositionLocalProvider(
@@ -267,6 +293,14 @@ private fun OhTeePeeInput(
267293
onValueChange = { onCellInputChange(index, it) },
268294
placeHolder = placeHolder,
269295
visualTransformation = visualTransformation,
296+
imeAction = imeAction,
297+
callbackOnNext = callbackOnNext,
298+
callbackOnDone = callbackOnDone,
299+
callbackOnPrevious = callbackOnPrevious,
300+
callbackOnSearch = callbackOnSearch,
301+
callbackOnSend = callbackOnSend,
302+
callbackOnGo = callbackOnGo,
303+
keyboardController = keyboardController,
270304
)
271305
if (divider != null && index != cellsCount - 1) {
272306
divider(index)
@@ -336,4 +370,4 @@ private fun handleCellInputChange(
336370

337371
private fun getCellDisplayCharacter(
338372
currentChar: Char,
339-
): String = if (currentChar == NOT_ENTERED_VALUE) String.EMPTY else currentChar.toString()
373+
): String = if (currentChar == NOT_ENTERED_VALUE) String.EMPTY else currentChar.toString()

ohteepee/src/main/java/com/composeuisuite/ohteepee/configuration/OhTeePeeErrorAnimationConfig.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,3 @@ sealed class OhTeePeeErrorAnimationConfig {
1717
val animationSpec: AnimationSpec<Float> = OhTeePeeDefaults.defaultShakeAnimationSpec,
1818
) : OhTeePeeErrorAnimationConfig()
1919
}
20-

ohteepee/src/main/java/com/composeuisuite/ohteepee/example/BasicOhTeePeeExample.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import androidx.compose.runtime.remember
1010
import androidx.compose.runtime.setValue
1111
import androidx.compose.ui.Modifier
1212
import androidx.compose.ui.graphics.Color
13+
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
1314
import androidx.compose.ui.text.TextStyle
15+
import androidx.compose.ui.text.input.ImeAction
1416
import androidx.compose.ui.tooling.preview.Preview
1517
import androidx.compose.ui.unit.dp
1618
import com.composeuisuite.ohteepee.OhTeePeeDefaults
@@ -30,6 +32,8 @@ internal fun BasicOhTeePeeExample() {
3032
),
3133
)
3234

35+
val keyboardController = LocalSoftwareKeyboardController.current
36+
3337
OhTeePeeInput(
3438
value = otpValue,
3539
onValueChange = { newValue, isValid ->
@@ -55,6 +59,16 @@ internal fun BasicOhTeePeeExample() {
5559
placeHolder = "-",
5660
cellModifier = Modifier.size(48.dp),
5761
),
62+
imeAction = ImeAction.Done,
63+
callbackOnNext = {},
64+
callbackOnDone = {
65+
keyboardController?.hide()
66+
},
67+
callbackOnPrevious = {},
68+
callbackOnSearch = {},
69+
callbackOnSend = {},
70+
callbackOnGo = {},
71+
keyboardController = keyboardController,
5872
)
5973
}
6074

sample/src/main/java/com/composeuisuite/ohteepee/MainActivity.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ import androidx.compose.ui.graphics.Color
4343
import androidx.compose.ui.graphics.RadialGradientShader
4444
import androidx.compose.ui.graphics.Shader
4545
import androidx.compose.ui.graphics.ShaderBrush
46+
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
4647
import androidx.compose.ui.res.painterResource
4748
import androidx.compose.ui.text.TextStyle
4849
import androidx.compose.ui.text.font.FontWeight
50+
import androidx.compose.ui.text.input.ImeAction
4951
import androidx.compose.ui.text.style.TextAlign
5052
import androidx.compose.ui.text.style.TextDecoration
5153
import androidx.compose.ui.unit.dp
@@ -110,6 +112,7 @@ class MainActivity : ComponentActivity() {
110112

111113
@Composable
112114
private fun Sample0(modifier: Modifier = Modifier) {
115+
val keyboardController = LocalSoftwareKeyboardController.current
113116
val backgroundColor = Color(0xFF4F4F83)
114117
val cellBackgroundColor = Color(0xFFFFE09A).copy(alpha = 0.2f)
115118
var otpValue: String by remember { mutableStateOf("") }
@@ -177,7 +180,12 @@ private fun Sample0(modifier: Modifier = Modifier) {
177180
translationXRange = 5f,
178181
),
179182
),
183+
imeAction = ImeAction.Done,
180184
autoFocusByDefault = false,
185+
keyboardController = keyboardController,
186+
callbackOnDone = {
187+
keyboardController?.hide()
188+
},
181189
)
182190

183191
Spacer(modifier = Modifier.height(64.dp))

0 commit comments

Comments
 (0)