Skip to content

Commit 5ebd6c9

Browse files
authored
static cell & space modifiers removed (#10)
* isFocused added to check if view is focused OtpCell cursor condition added to only be visible, when PinInput is focused. * cellModifier & spaceModifier static changes removed. cellModifier default moved to params, to let developer define complete modifiers. * Update OtpView.kt cursor visibility removed for disabled PinInput
1 parent 2872c89 commit 5ebd6c9

File tree

2 files changed

+39
-27
lines changed

2 files changed

+39
-27
lines changed

app/src/main/java/com/yogeshpaliyal/speld/MainActivity.kt

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.os.Bundle
44
import androidx.activity.ComponentActivity
55
import androidx.activity.compose.setContent
66
import androidx.compose.foundation.BorderStroke
7+
import androidx.compose.foundation.background
78
import androidx.compose.foundation.border
89
import androidx.compose.foundation.layout.*
910
import androidx.compose.foundation.shape.RoundedCornerShape
@@ -16,6 +17,7 @@ import androidx.compose.runtime.mutableStateOf
1617
import androidx.compose.runtime.remember
1718
import androidx.compose.ui.Alignment
1819
import androidx.compose.ui.Modifier
20+
import androidx.compose.ui.draw.clip
1921
import androidx.compose.ui.graphics.Color
2022
import androidx.compose.ui.unit.dp
2123
import com.yogeshpaliyal.speld.ui.theme.AndroidComposeOTPViewTheme
@@ -30,7 +32,8 @@ class MainActivity : ComponentActivity() {
3032
val text = remember { mutableStateOf("") }
3133
val length = 5 // Define the length of the PIN here
3234

33-
Column(modifier = Modifier.fillMaxSize(),
35+
Column(
36+
modifier = Modifier.fillMaxSize(),
3437
Arrangement.Center, Alignment.CenterHorizontally
3538
) {
3639

@@ -45,10 +48,18 @@ class MainActivity : ComponentActivity() {
4548

4649
// Bordered PIN View
4750
PinInput(
48-
cellModifier = Modifier.border(
49-
BorderStroke(2.dp, Color.Red),
50-
shape = RoundedCornerShape(3.dp)
51-
), value = text.value,
51+
cellModifier = Modifier
52+
.size(width = 45.dp, height = 45.dp)
53+
.clip(MaterialTheme.shapes.large)
54+
.background(
55+
MaterialTheme.colors.primary.copy(alpha = 0.1f),
56+
shape = RoundedCornerShape(3.dp)
57+
)
58+
.border(
59+
BorderStroke(2.dp, Color.Red),
60+
shape = RoundedCornerShape(3.dp)
61+
),
62+
value = text.value,
5263
obscureText = "*",
5364
length = length, //Use the number defined here
5465
disableKeypad = true, //Do not open the android keypad
@@ -57,7 +68,7 @@ class MainActivity : ComponentActivity() {
5768
}
5869

5970
Spacer(modifier = Modifier.height(20.dp))
60-
71+
6172
KeyPad(input = text, length)
6273

6374
}
@@ -69,9 +80,9 @@ class MainActivity : ComponentActivity() {
6980
}
7081

7182
@Composable
72-
fun KeyPad(input: MutableState<String>, length: Int){
73-
val callback = {
74-
text: String -> handleButtonClick(text, input, length)
83+
fun KeyPad(input: MutableState<String>, length: Int) {
84+
val callback = { text: String ->
85+
handleButtonClick(text, input, length)
7586
}
7687

7788
Column(
@@ -115,7 +126,7 @@ class MainActivity : ComponentActivity() {
115126
.padding(2.dp, 2.dp)
116127
) {
117128
for (i in texts.indices) {
118-
val useIcon = texts[i]==""
129+
val useIcon = texts[i] == ""
119130
MyButton(
120131
text = texts[i],
121132
callback = callback,
@@ -155,7 +166,7 @@ class MainActivity : ComponentActivity() {
155166
contentDescription = "Favorite",
156167
modifier = Modifier.size(ButtonDefaults.IconSize)
157168
)
158-
}else Text(text)
169+
} else Text(text)
159170
}
160171
}
161172

@@ -168,8 +179,9 @@ class MainActivity : ComponentActivity() {
168179
"" -> if (inputTextView.value.isNotEmpty()) {
169180
inputTextView.value = inputTextView.value.dropLast(1) // remove last char
170181
}
182+
171183
"C" -> inputTextView.value = "" // clear all text
172-
else -> if(inputTextView.value.length<length) inputTextView.value += txt // handles key input here
184+
else -> if (inputTextView.value.length < length) inputTextView.value += txt // handles key input here
173185
}
174186
}
175187
}

speld/src/main/java/com/yogeshpaliyal/speld/OtpView.kt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,14 @@ fun OtpCell(
6262
@Composable
6363
fun PinInput(
6464
modifier: Modifier = Modifier,
65-
cellModifier: Modifier = Modifier,
65+
cellModifier: Modifier = Modifier
66+
.size(width = 45.dp, height = 45.dp)
67+
.clip(MaterialTheme.shapes.large)
68+
.background(
69+
MaterialTheme.colors.primary.copy(alpha = 0.1f),
70+
shape = RoundedCornerShape(3.dp)
71+
),
72+
spacerModifier: Modifier = Modifier.size(8.dp),
6673
length: Int = 5,
6774
value: String = "",
6875
disableKeypad: Boolean = false,
@@ -105,23 +112,16 @@ fun PinInput(
105112
) {
106113
repeat(length) {
107114
OtpCell(
108-
modifier = cellModifier
109-
.size(width = 45.dp, height = 45.dp)
110-
.clip(MaterialTheme.shapes.large)
111-
.background(
112-
MaterialTheme.colors.primary.copy(alpha = 0.1f),
113-
shape = RoundedCornerShape(3.dp)
114-
)
115-
.clickable {
116-
focusRequester.requestFocus()
117-
keyboard?.show()
118-
},
115+
modifier = cellModifier.clickable {
116+
focusRequester.requestFocus()
117+
keyboard?.show()
118+
},
119119
value = value.getOrNull(it),
120-
isCursorVisible = (isFocused.value || !cursorVisibleOnlyOnFocus) && value.length == it,
121-
obscureText
120+
isCursorVisible = !disableKeypad && (isFocused.value || !cursorVisibleOnlyOnFocus) && value.length == it,
121+
obscureText = obscureText
122122
)
123123
if (it != length - 1)
124-
Spacer(modifier = Modifier.size(8.dp))
124+
Spacer(modifier = spacerModifier)
125125
}
126126
}
127127
}

0 commit comments

Comments
 (0)