Skip to content

Commit 45baf5b

Browse files
committed
toast add and import improvement
1 parent 93274b1 commit 45baf5b

File tree

3 files changed

+51
-61
lines changed

3 files changed

+51
-61
lines changed

src/main/kotlin/App.kt

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,64 @@
11
import androidx.compose.desktop.ui.tooling.preview.Preview
2-
import androidx.compose.foundation.layout.Column
3-
import androidx.compose.foundation.layout.Row
4-
import androidx.compose.foundation.layout.fillMaxHeight
5-
import androidx.compose.foundation.layout.fillMaxSize
6-
import androidx.compose.foundation.layout.padding
2+
import androidx.compose.foundation.layout.*
73
import androidx.compose.material.MaterialTheme
8-
import androidx.compose.runtime.Composable
9-
import androidx.compose.runtime.getValue
10-
import androidx.compose.runtime.mutableStateOf
11-
import androidx.compose.runtime.remember
12-
import androidx.compose.runtime.setValue
4+
import androidx.compose.runtime.*
135
import androidx.compose.ui.Modifier
14-
import androidx.compose.ui.unit.dp
15-
import components.CustomButton
16-
import components.CustomTextField
17-
import components.SelectCountries
6+
import androidx.compose.ui.unit.*
7+
import components.*
188
import utils.languageList
199

2010
@Composable
2111
@Preview
2212
fun App() {
2313

24-
var showToast by remember { mutableStateOf(false) }
25-
var inputDependencyText by remember { mutableStateOf("") }
14+
var isShowWindowState by remember { mutableStateOf(false) }
15+
var stringState by remember { mutableStateOf("") }
2616
var countryListState by remember { mutableStateOf(languageList) }
27-
17+
var isShowToast by remember { mutableStateOf(false) }
18+
var toastMessage by remember { mutableStateOf("") }
19+
2820
MaterialTheme {
2921
Row(
30-
Modifier.fillMaxSize()
31-
.padding(10.dp)
22+
Modifier.fillMaxSize().padding(10.dp)
3223
) {
3324
CustomTextField(
34-
inputDependencyText,
25+
stringState,
3526
"Enter the Strings",
3627
Modifier.fillMaxHeight().weight(0.8f, true),
37-
) { inputDependencyText = it }
28+
) { stringState = it }
3829

3930
Column(
4031
Modifier.weight(0.2f, true).padding(start = 10.dp)
4132
) {
4233
CustomButton("Select Languages", onClick = {
43-
showToast = true
44-
})
45-
CustomButton("Convert", isEnable = inputDependencyText.isNotEmpty() && countryListState.any { it.isChecked }, onClick = {
46-
println(countryListState.filter { it.isChecked })
34+
isShowWindowState = true
4735
})
36+
CustomButton("Convert",
37+
isEnable = stringState.isNotEmpty() && countryListState.any { it.isChecked },
38+
onClick = {
39+
println(countryListState.filter { it.isChecked })
40+
})
4841
}
4942
}
50-
if (showToast) {
51-
SelectCountries(countryListState){
52-
// countryListState = it
53-
showToast = false
43+
if (isShowWindowState) {
44+
SelectCountries(countryListState) {
45+
countryListState = it
46+
isShowWindowState = false
5447
}
55-
48+
}
49+
if (isShowToast) {
50+
Toast(toastMessage) {}
5651
}
5752

5853
}
5954
}
6055

56+
fun showToast() {
57+
// isShowToast = true
58+
// toastMessage = "This is a toast message!"
59+
// // Delay to simulate a real-world scenario
60+
// CoroutineScope(Dispatchers.IO).launch {
61+
// delay(3000)
62+
// isShowToast = false
63+
// }
64+
}

src/main/kotlin/Main.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
import androidx.compose.ui.Alignment
23
import androidx.compose.ui.unit.dp
34
import androidx.compose.ui.window.Window

src/main/kotlin/components/Components.kt

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ fun CustomTextField(
6565
backgroundColor = TextFieldBackground
6666
),
6767
readOnly = readOnly,
68-
modifier = modifier.fillMaxWidth()
69-
.clip(RoundedCornerShape(12.dp))
70-
.height(280.dp)
68+
modifier = modifier.fillMaxWidth().clip(RoundedCornerShape(12.dp)).height(280.dp)
7169
)
7270
}
7371

@@ -86,47 +84,38 @@ fun CustomButton(text: String, onClick: () -> Unit, isEnable: Boolean = true) {
8684

8785
@Composable
8886
fun Toast(message: String, onDismiss: () -> Unit) {
89-
val modifier = Modifier
90-
.background(MaterialTheme.colors.primary)
91-
.padding(8.dp)
92-
.fillMaxWidth()
93-
.wrapContentHeight()
94-
.height(60.dp)
87+
val modifier = Modifier.background(MaterialTheme.colors.primary).padding(8.dp).fillMaxWidth()
88+
.wrapContentHeight().height(60.dp)
9589

9690
Box(
97-
modifier = modifier,
98-
contentAlignment = Alignment.Center
91+
modifier = modifier, contentAlignment = Alignment.Center
9992
) {
10093
Text(
101-
text = message,
102-
color = Color.White,
103-
fontSize = 16.sp
94+
text = message, color = Color.White, fontSize = 16.sp
10495
)
10596

10697
IconButton(
107-
onClick = onDismiss,
108-
modifier = Modifier.align(Alignment.CenterEnd)
98+
onClick = onDismiss, modifier = Modifier.align(Alignment.CenterEnd)
10999
) {
110100
Icon(
111-
imageVector = Icons.Default.Close,
112-
contentDescription = "Close",
113-
tint = Color.White
101+
imageVector = Icons.Default.Close, contentDescription = "Close", tint = Color.White
114102
)
115103
}
116104
}
117105
}
118106

119107
@Composable
120-
fun SelectCountries(languageList: MutableList<Language> ,onDismiss: () -> Unit) {
108+
fun SelectCountries(
109+
languageList: MutableList<Language>,
110+
onDismiss: (MutableList<Language>) -> Unit,
111+
) {
121112
var countryListState by remember { mutableStateOf(languageList) }
122113
val dialogState = DialogState(
123-
width = 1000.dp,
124-
height = 720.dp,
125-
position = WindowPosition(Alignment.Center)
114+
width = 1000.dp, height = 720.dp, position = WindowPosition(Alignment.Center)
126115
)
127116
DialogWindow(
128117
state = dialogState,
129-
onCloseRequest = { onDismiss() },
118+
onCloseRequest = { onDismiss(countryListState) },
130119
title = "Select language",
131120
content = {
132121
LazyVerticalGrid(
@@ -136,17 +125,13 @@ fun SelectCountries(languageList: MutableList<Language> ,onDismiss: () -> Unit)
136125
modifier = Modifier.fillMaxSize()
137126
) {
138127
itemsIndexed(countryListState) { index, language ->
139-
Row(
140-
horizontalArrangement = Arrangement.Center,
128+
Row(horizontalArrangement = Arrangement.Center,
141129
verticalAlignment = Alignment.CenterVertically,
142-
modifier = Modifier
143-
.fillMaxWidth()
144-
.clickable {
130+
modifier = Modifier.fillMaxWidth().clickable {
145131
countryListState = countryListState.toMutableList().apply {
146132
this[index] = language.copy(isChecked = !language.isChecked)
147133
}
148-
}
149-
) {
134+
}) {
150135
Checkbox(
151136
checked = language.isChecked,
152137
onCheckedChange = {

0 commit comments

Comments
 (0)