1
1
package com.togitech.ccp.component
2
2
3
- import android.content.Context
4
3
import androidx.compose.foundation.clickable
5
4
import androidx.compose.foundation.layout.Arrangement
6
5
import androidx.compose.foundation.layout.Column
@@ -26,8 +25,10 @@ import androidx.compose.material.icons.Icons
26
25
import androidx.compose.material.icons.filled.Clear
27
26
import androidx.compose.material.icons.filled.Search
28
27
import androidx.compose.runtime.Composable
28
+ import androidx.compose.runtime.derivedStateOf
29
29
import androidx.compose.runtime.getValue
30
30
import androidx.compose.runtime.mutableStateOf
31
+ import androidx.compose.runtime.remember
31
32
import androidx.compose.runtime.saveable.rememberSaveable
32
33
import androidx.compose.runtime.setValue
33
34
import androidx.compose.ui.Alignment
@@ -47,31 +48,49 @@ import androidx.compose.ui.unit.sp
47
48
import androidx.compose.ui.window.Dialog
48
49
import com.togitech.ccp.R
49
50
import com.togitech.ccp.data.CountryData
50
- import com.togitech.ccp.data.utils.countryCodeToEmojiFlag
51
51
import com.togitech.ccp.data.utils.countryNames
52
- import com.togitech.ccp.data.utils.getLibCountries
53
- import com.togitech.ccp.utils.searchCountry
52
+ import com.togitech.ccp.data.utils.emojiFlag
53
+ import com.togitech.ccp.data. utils.searchCountry
54
54
import kotlinx.collections.immutable.ImmutableList
55
55
import kotlinx.collections.immutable.toImmutableList
56
56
57
- private val DEFAULT_ROUNDING = 10 .dp
57
+ internal val DEFAULT_ROUNDING = 10 .dp
58
58
private val DEFAULT_ROW_PADDING = 16 .dp
59
59
private const val ROW_PADDING_VERTICAL_SCALING = 1.1f
60
60
private val DEFAULT_ROW_FONT_SIZE = 16 .sp
61
61
private val SEARCH_ICON_PADDING = 5 .dp
62
62
63
- @Suppress(" ModifierDefaultValue" )
63
+ /* *
64
+ * @param onDismissRequest Executes when the user tries to dismiss the dialog.
65
+ * @param onSelect Executes when the user selects a country from the list.
66
+ * @param countryList The list of countries to display in the dialog.
67
+ * @param modifier The modifier to be applied to the dialog surface.
68
+ * @param rowPadding The padding to be applied to each row.
69
+ * @param rowFontSize The font size to be applied to each row.
70
+ */
64
71
@Composable
65
- internal fun CountryDialog (
72
+ fun CountryDialog (
66
73
onDismissRequest : () -> Unit ,
67
74
onSelect : (item: CountryData ) -> Unit ,
68
- filteredCountryList : ImmutableList <CountryData >,
69
- context : Context ,
70
- modifier : Modifier = Modifier .fillMaxWidth().clip(RoundedCornerShape (DEFAULT_ROUNDING )),
75
+ countryList : ImmutableList <CountryData >,
76
+ modifier : Modifier = Modifier ,
71
77
rowPadding : Dp = DEFAULT_ROW_PADDING ,
72
78
rowFontSize : TextUnit = DEFAULT_ROW_FONT_SIZE ,
73
79
) {
80
+ val context = LocalContext .current
74
81
var searchValue by rememberSaveable { mutableStateOf(" " ) }
82
+ val filteredCountries by remember(context, searchValue) {
83
+ derivedStateOf {
84
+ if (searchValue.isEmpty()) {
85
+ countryList
86
+ } else {
87
+ countryList.searchCountry(
88
+ searchValue,
89
+ context,
90
+ )
91
+ }
92
+ }
93
+ }
75
94
76
95
Dialog (
77
96
onDismissRequest = onDismissRequest,
@@ -101,16 +120,7 @@ internal fun CountryDialog(
101
120
Spacer (modifier = Modifier .height(DEFAULT_ROW_PADDING ))
102
121
Divider ()
103
122
LazyColumn {
104
- items(
105
- if (searchValue.isEmpty()) {
106
- filteredCountryList
107
- } else {
108
- filteredCountryList.searchCountry(
109
- searchValue,
110
- context,
111
- )
112
- },
113
- ) { countryItem ->
123
+ items(filteredCountries, key = { it.countryIso }) { countryItem ->
114
124
CountryRowItem (
115
125
rowPadding = rowPadding,
116
126
onSelect = { onSelect(countryItem) },
@@ -167,10 +177,10 @@ private fun CountryRowItem(
167
177
verticalAlignment = Alignment .CenterVertically ,
168
178
) {
169
179
Text (
170
- text = countryCodeToEmojiFlag( countryItem.countryCode) + " " +
180
+ text = countryItem.emojiFlag + " " +
171
181
stringResource(
172
182
id = countryNames.getOrDefault(
173
- countryItem.countryCode.lowercase() ,
183
+ countryItem.countryIso ,
174
184
R .string.unknown,
175
185
),
176
186
),
@@ -231,8 +241,8 @@ private fun SearchTextField(
231
241
private fun CountryDialogPreview () {
232
242
CountryDialog (
233
243
onSelect = {},
234
- context = LocalContext .current,
235
- filteredCountryList = getLibCountries.toImmutableList(),
244
+ countryList = CountryData .entries.toImmutableList(),
236
245
onDismissRequest = {},
246
+ modifier = Modifier .fillMaxWidth().clip(RoundedCornerShape (DEFAULT_ROUNDING )),
237
247
)
238
248
}
0 commit comments