Skip to content

Commit fa210d5

Browse files
committed
feat: Update cat seed input
Signed-off-by: Hu Shenghao <dede.hu@qq.com>
1 parent 276181e commit fa210d5

File tree

5 files changed

+155
-88
lines changed

5 files changed

+155
-88
lines changed

core/resources/src/main/res/values-zh/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@
4747
<string name="animator_disabled_alert_message">由于系统动画被禁用,可能导致一些功能无法正常使用。请进入“开发者选项”并启用“Animator 时长缩放”选项以开启动画。</string>
4848
<string name="action_goto_settings">去设置</string>
4949
<string name="cat_editor">Cat Editor</string>
50+
<string name="cat_editor_input_seed">输入猫种子</string>
5051
</resources>

core/resources/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,5 @@
5555
<string name="action_goto_settings">Go to Settings</string>
5656

5757
<string name="cat_editor">Cat Editor</string>
58-
<string name="cat_editor_input_seed">Input Cat Seed</string>
58+
<string name="cat_editor_input_seed">Input cat\'s seed</string>
5959
</resources>

feature/cat-editor/src/main/java/com/dede/android_eggs/cat_editor/CatEditorScreen.kt

Lines changed: 6 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,15 @@ import androidx.compose.animation.fadeOut
1010
import androidx.compose.animation.slideInVertically
1111
import androidx.compose.animation.slideOutVertically
1212
import androidx.compose.foundation.background
13-
import androidx.compose.foundation.focusable
1413
import androidx.compose.foundation.layout.Box
1514
import androidx.compose.foundation.layout.Column
1615
import androidx.compose.foundation.layout.Spacer
1716
import androidx.compose.foundation.layout.fillMaxSize
1817
import androidx.compose.foundation.layout.padding
1918
import androidx.compose.foundation.shape.CircleShape
20-
import androidx.compose.foundation.text.KeyboardActions
21-
import androidx.compose.foundation.text.KeyboardOptions
2219
import androidx.compose.material.icons.Icons
2320
import androidx.compose.material.icons.automirrored.rounded.ArrowBack
2421
import androidx.compose.material.icons.automirrored.rounded.ArrowForward
25-
import androidx.compose.material.icons.rounded.Clear
2622
import androidx.compose.material.icons.rounded.ContentCopy
2723
import androidx.compose.material.icons.rounded.Edit
2824
import androidx.compose.material.icons.rounded.GridOff
@@ -31,7 +27,6 @@ import androidx.compose.material.icons.rounded.MoreVert
3127
import androidx.compose.material.icons.rounded.Palette
3228
import androidx.compose.material.icons.rounded.Refresh
3329
import androidx.compose.material.icons.rounded.Save
34-
import androidx.compose.material3.AlertDialog
3530
import androidx.compose.material3.BottomAppBar
3631
import androidx.compose.material3.CenterAlignedTopAppBar
3732
import androidx.compose.material3.ExperimentalMaterial3Api
@@ -41,8 +36,6 @@ import androidx.compose.material3.MaterialTheme
4136
import androidx.compose.material3.MaterialTheme.colorScheme
4237
import androidx.compose.material3.Scaffold
4338
import androidx.compose.material3.Text
44-
import androidx.compose.material3.TextButton
45-
import androidx.compose.material3.TextField
4639
import androidx.compose.material3.surfaceColorAtElevation
4740
import androidx.compose.runtime.Composable
4841
import androidx.compose.runtime.LaunchedEffect
@@ -60,24 +53,18 @@ import androidx.compose.ui.graphics.asAndroidBitmap
6053
import androidx.compose.ui.platform.LocalContext
6154
import androidx.compose.ui.res.stringResource
6255
import androidx.compose.ui.text.font.FontWeight
63-
import androidx.compose.ui.text.input.ImeAction
64-
import androidx.compose.ui.text.input.KeyboardCapitalization
65-
import androidx.compose.ui.text.input.KeyboardType
66-
import androidx.compose.ui.text.intl.LocaleList
6756
import androidx.compose.ui.text.style.TextOverflow
6857
import androidx.compose.ui.unit.dp
6958
import com.dede.android_eggs.cat_editor.CaptureControllerDelegate.Companion.rememberCaptureControllerDelegate
7059
import com.dede.android_eggs.cat_editor.CatEditorRecords.Companion.rememberCatEditorRecords
7160
import com.dede.android_eggs.navigation.EasterEggsDestination
7261
import com.dede.android_eggs.navigation.LocalNavController
73-
import com.dede.android_eggs.ui.composes.icons.rounded.Cat
7462
import com.dede.basic.copy
7563
import com.dede.basic.toast
7664
import com.dede.basic.utils.ShareCatUtils
7765
import com.google.accompanist.permissions.ExperimentalPermissionsApi
7866
import kotlinx.coroutines.launch
7967
import androidx.appcompat.R as AppCompatR
80-
import com.dede.android_eggs.resources.R as StringR
8168

8269
object CatEditorScreen : EasterEggsDestination {
8370
override val route: String = "cat_editor"
@@ -109,8 +96,6 @@ fun CatEditorScreen() {
10996
}
11097

11198
var moreOptionsVisible by remember { mutableStateOf(false) }
112-
var inputSeedDialog by remember { mutableStateOf(false) }
113-
var inputSeedText by remember { mutableStateOf("") }
11499

115100
Scaffold(
116101
topBar = {
@@ -290,82 +275,17 @@ fun CatEditorScreen() {
290275
}
291276
)
292277

278+
val inputSeedDialogState = remember { mutableStateOf(false) }
279+
293280
fun updateCatSeed(seed: Long) {
294281
catSeed = seed
295282
catEditorController.updateColors(catSeed)
296283
catEditorRecords.addRecord(CatEditorRecords.seed(catSeed))
297284
}
298285

299-
if (inputSeedDialog) {
300-
301-
fun onInputDone() {
302-
if (inputSeedText.isBlank()) {
303-
return
304-
}
305-
val seed = Utilities.string2Seed(inputSeedText)
306-
updateCatSeed(seed)
307-
}
308-
309-
AlertDialog(
310-
onDismissRequest = {
311-
inputSeedDialog = false
312-
},
313-
title = {
314-
Text(text = stringResource(StringR.string.cat_editor))
315-
},
316-
text = {
317-
TextField(
318-
modifier = Modifier.focusable(true),
319-
value = inputSeedText,
320-
onValueChange = { inputSeedText = it },
321-
placeholder = {
322-
Text(text = "XXX")
323-
},
324-
leadingIcon = {
325-
Icon(imageVector = Icons.Rounded.Cat, contentDescription = null)
326-
},
327-
trailingIcon = {
328-
IconButton(onClick = {
329-
inputSeedText = ""
330-
}) {
331-
Icon(
332-
imageVector = Icons.Rounded.Clear,
333-
contentDescription = null
334-
)
335-
}
336-
},
337-
label = { Text(text = stringResource(StringR.string.cat_editor_input_seed)) },
338-
keyboardOptions = KeyboardOptions(
339-
autoCorrectEnabled = false,
340-
keyboardType = KeyboardType.Text,
341-
imeAction = ImeAction.Done,
342-
showKeyboardOnFocus = true,
343-
hintLocales = LocaleList("en"),
344-
capitalization = KeyboardCapitalization.None,
345-
),
346-
keyboardActions = KeyboardActions {
347-
onInputDone()
348-
inputSeedDialog = false
349-
}
350-
)
351-
},
352-
dismissButton = {
353-
TextButton(onClick = {
354-
inputSeedDialog = false
355-
}) {
356-
Text(text = stringResource(android.R.string.cancel))
357-
}
358-
},
359-
confirmButton = {
360-
TextButton(onClick = {
361-
onInputDone()
362-
inputSeedDialog = false
363-
}) {
364-
Text(text = stringResource(android.R.string.ok))
365-
}
366-
},
367-
)
368-
}
286+
CatSeedInputDialog(inputSeedDialogState, onConfirm = { seed ->
287+
updateCatSeed(seed)
288+
})
369289

370290
AnimatedVisibility(
371291
visible = moreOptionsVisible,
@@ -391,7 +311,7 @@ fun CatEditorScreen() {
391311
}
392312

393313
IconButton(onClick = {
394-
inputSeedDialog = true
314+
inputSeedDialogState.value = true
395315
}) {
396316
Icon(imageVector = Icons.Rounded.Edit, contentDescription = null)
397317
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package com.dede.android_eggs.cat_editor
2+
3+
import androidx.compose.animation.AnimatedVisibility
4+
import androidx.compose.animation.fadeIn
5+
import androidx.compose.animation.fadeOut
6+
import androidx.compose.animation.scaleIn
7+
import androidx.compose.animation.scaleOut
8+
import androidx.compose.foundation.text.KeyboardActions
9+
import androidx.compose.foundation.text.KeyboardOptions
10+
import androidx.compose.material.icons.Icons
11+
import androidx.compose.material.icons.rounded.Clear
12+
import androidx.compose.material3.AlertDialog
13+
import androidx.compose.material3.Icon
14+
import androidx.compose.material3.IconButton
15+
import androidx.compose.material3.Text
16+
import androidx.compose.material3.TextButton
17+
import androidx.compose.material3.TextField
18+
import androidx.compose.runtime.Composable
19+
import androidx.compose.runtime.LaunchedEffect
20+
import androidx.compose.runtime.MutableState
21+
import androidx.compose.runtime.getValue
22+
import androidx.compose.runtime.mutableStateOf
23+
import androidx.compose.runtime.remember
24+
import androidx.compose.runtime.setValue
25+
import androidx.compose.ui.Modifier
26+
import androidx.compose.ui.focus.FocusRequester
27+
import androidx.compose.ui.focus.focusRequester
28+
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
29+
import androidx.compose.ui.res.stringResource
30+
import androidx.compose.ui.text.input.ImeAction
31+
import androidx.compose.ui.text.input.KeyboardCapitalization
32+
import androidx.compose.ui.text.input.KeyboardType
33+
import androidx.compose.ui.text.intl.LocaleList
34+
import com.dede.android_eggs.resources.R
35+
import com.dede.android_eggs.ui.composes.icons.rounded.Cat
36+
37+
/**
38+
* A dialog for inputting a cat seed.
39+
*/
40+
@Composable
41+
fun CatSeedInputDialog(
42+
visibleState: MutableState<Boolean>,
43+
onConfirm: (seed: Long) -> Unit,
44+
onDismiss: () -> Unit = {},
45+
) {
46+
var visible by visibleState
47+
if (!visible) {
48+
return
49+
}
50+
51+
var inputSeedText by remember { mutableStateOf("") }
52+
var inputError by remember { mutableStateOf(false) }
53+
54+
fun dismiss() {
55+
visible = false
56+
inputSeedText = ""
57+
inputError = false
58+
onDismiss()
59+
}
60+
61+
fun done() {
62+
if (inputSeedText.isBlank()) {
63+
inputError = true
64+
return
65+
}
66+
67+
val seed = Utilities.string2Seed(inputSeedText)
68+
onConfirm(seed)
69+
dismiss()
70+
}
71+
72+
AlertDialog(
73+
onDismissRequest = {
74+
dismiss()
75+
},
76+
title = {
77+
Text(text = stringResource(R.string.cat_editor))
78+
},
79+
text = {
80+
val focusRequester = remember { FocusRequester() }
81+
val keyboardController = LocalSoftwareKeyboardController.current
82+
LaunchedEffect(Unit) {
83+
focusRequester.requestFocus()
84+
keyboardController?.show()
85+
}
86+
87+
TextField(
88+
modifier = Modifier.focusRequester(focusRequester),
89+
value = inputSeedText,
90+
onValueChange = {
91+
inputSeedText = it
92+
inputError = false
93+
},
94+
placeholder = {
95+
Text(text = "XXX")
96+
},
97+
leadingIcon = {
98+
Icon(imageVector = Icons.Rounded.Cat, contentDescription = null)
99+
},
100+
trailingIcon = {
101+
AnimatedVisibility(
102+
visible = inputSeedText.isNotEmpty(),
103+
enter = fadeIn() + scaleIn(),
104+
exit = fadeOut() + scaleOut(),
105+
) {
106+
IconButton(onClick = { inputSeedText = "" }) {
107+
Icon(
108+
imageVector = Icons.Rounded.Clear,
109+
contentDescription = null
110+
)
111+
}
112+
}
113+
},
114+
isError = inputError,
115+
label = {
116+
Text(text = stringResource(R.string.cat_editor_input_seed))
117+
},
118+
keyboardOptions = KeyboardOptions(
119+
autoCorrectEnabled = false,
120+
keyboardType = KeyboardType.Text,
121+
imeAction = ImeAction.Done,
122+
showKeyboardOnFocus = true,
123+
hintLocales = LocaleList("en"),
124+
capitalization = KeyboardCapitalization.None,
125+
),
126+
keyboardActions = KeyboardActions {
127+
done()
128+
}
129+
)
130+
},
131+
dismissButton = {
132+
TextButton(onClick = {
133+
dismiss()
134+
}) {
135+
Text(text = stringResource(android.R.string.cancel))
136+
}
137+
},
138+
confirmButton = {
139+
TextButton(onClick = {
140+
done()
141+
}) {
142+
Text(text = stringResource(android.R.string.ok))
143+
}
144+
},
145+
)
146+
}

feature/cat-editor/src/main/java/com/dede/android_eggs/cat_editor/OpenSimplex2S.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.dede.android_eggs.cat_editor;
22

3-
// https://github.com/KdotJPG/OpenSimplex2
3+
// OpenSimplex2 (https://github.com/KdotJPG/OpenSimplex2)
44
//
55
// Creative Commons Legal Code
66
//

0 commit comments

Comments
 (0)