Skip to content

Commit c2514ea

Browse files
authored
Refactoring Country Data (#32)
* Refactoring Country Data * cleanups * upgrade AGP * improved color handling for clear icon, add label to demo * upgrade compose, add missing hints * detekt * upgrade sonar * optimizations * use remember / derivedStateOf to minimize recomposition when searching
1 parent 32c6e10 commit c2514ea

22 files changed

+958
-951
lines changed

.github/workflows/documentation.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ name: Publish Dokka HTML to GitHub Pages
22

33
on:
44
push:
5-
branches: [ master ]
5+
tags:
6+
- '*'
67

78
jobs:
89
build:

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[![Android CI](https://github.com/jump-sdk/jetpack_compose_country_code_picker_emoji/actions/workflows/android.yml/badge.svg)](https://github.com/jump-sdk/jetpack_compose_country_code_picker_emoji/actions/workflows/android.yml)
44
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=jump-sdk_jetpack_compose_country_code_picker_emoji&metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=jump-sdk_jetpack_compose_country_code_picker_emoji)
55
[![Jitpack](https://jitpack.io/v/jump-sdk/jetpack_compose_country_code_picker_emoji.svg)](https://jitpack.io/#jump-sdk/jetpack_compose_country_code_picker_emoji)
6+
[![Documentation](https://img.shields.io/badge/Documentation-3d3d41?logo=kotlin)](https://jump-sdk.github.io/jetpack_compose_country_code_picker_emoji/)
67

78
* 😍 Emoji flag icons - beautiful and minimizes library size
89
* 🤔 Country numbers hints (option to disable)
@@ -28,6 +29,7 @@
2829

2930

3031
See [MainActivity in the sample app](https://github.com/jump-sdk/jetpack_compose_country_code_picker_emoji/blob/master/app/src/main/java/com/togitech/togii/MainActivity.kt) for a full example.
32+
Also check out the [ComposeCountryCodePicker documentation](https://jump-sdk.github.io/jetpack_compose_country_code_picker_emoji/) for all available composables and utilities.
3133

3234
```kotlin
3335
TogiCountryCodePicker(
@@ -87,7 +89,7 @@ Step 2. Add the dependency
8789

8890
```kotlin
8991
dependencies {
90-
implementation("com.github.jump-sdk:jetpack_compose_country_code_picker_emoji:2.0.8")
92+
implementation("com.github.jump-sdk:jetpack_compose_country_code_picker_emoji:2.2.0")
9193
}
9294
```
9395

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
android:label="@string/app_name"
88
android:roundIcon="@mipmap/ic_launcher_round"
99
android:supportsRtl="true"
10-
android:theme="@style/Theme.Togii">
10+
android:theme="@style/Theme.Togii"
11+
android:usesCleartextTraffic="false">
1112
<activity
1213
android:name=".MainActivity"
1314
android:exported="true"

app/src/main/java/com/togitech/togii/MainActivity.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import androidx.compose.foundation.layout.Arrangement
88
import androidx.compose.foundation.layout.Column
99
import androidx.compose.foundation.layout.Spacer
1010
import androidx.compose.foundation.layout.fillMaxSize
11+
import androidx.compose.foundation.layout.fillMaxWidth
1112
import androidx.compose.foundation.layout.height
13+
import androidx.compose.foundation.layout.padding
1214
import androidx.compose.foundation.rememberScrollState
1315
import androidx.compose.foundation.verticalScroll
1416
import androidx.compose.material.MaterialTheme
@@ -30,7 +32,6 @@ import com.togitech.ccp.component.TogiCountryCodePicker
3032
import com.togitech.togii.ui.theme.TogiiTheme
3133

3234
class MainActivity : ComponentActivity() {
33-
3435
override fun onCreate(savedInstanceState: Bundle?) {
3536
super.onCreate(savedInstanceState)
3637
setContent {
@@ -74,12 +75,14 @@ fun CountryCodePick() {
7475
Spacer(modifier = Modifier.height(100.dp))
7576

7677
TogiCountryCodePicker(
78+
modifier = Modifier.fillMaxWidth().padding(horizontal = 10.dp),
7779
onValueChange = { (code, phone), isValid ->
7880
Log.d("CCP", "onValueChange: $code $phone -> $isValid")
7981
phoneNumber.value = phone
8082
fullPhoneNumber.value = code + phone
8183
isNumberValid = isValid
8284
},
85+
label = { Text("Test Label") },
8386
)
8487
Spacer(modifier = Modifier.height(10.dp))
8588

build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins {
33
id("com.android.library") version libs.versions.android.gradle.plugin apply false
44
id("org.jetbrains.kotlin.android") version libs.versions.kotlin apply false
55
id("org.jetbrains.dokka") version libs.versions.dokka
6-
id("org.sonarqube") version "4.3.1.3277"
6+
id("org.sonarqube") version "4.4.0.3356"
77
alias(libs.plugins.gradleVersions)
88
}
99

@@ -12,7 +12,7 @@ sonarqube {
1212
property("sonar.projectKey", "jump-sdk_jetpack_compose_country_code_picker_emoji")
1313
property("sonar.organization", "jump-sdk")
1414
property("sonar.host.url", "https://sonarcloud.io")
15-
property("sonar.kotlin.source.version", "1.9.10")
15+
property("sonar.kotlin.source.version", libs.versions.kotlin.get())
1616
property("sonar.pullrequest.github.summary_comment", "true")
1717
}
1818
}

ccp/build.gradle.kts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ plugins {
77
id("app.cash.paparazzi") version libs.versions.paparazzi.get()
88
}
99

10-
group "com.togitech"
11-
version "2.0.5"
12-
1310
kotlin {
1411
jvmToolchain(17)
1512
}
@@ -81,7 +78,7 @@ afterEvaluate {
8178
from(components.getByName("release"))
8279
groupId = "com.togisoft"
8380
artifactId = "jetpack_country_code_picker"
84-
version = "2.0.5"
81+
version = "2.2.0"
8582
}
8683
}
8784
}

ccp/src/main/java/com/togitech/ccp/component/CountryDialog.kt

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.togitech.ccp.component
22

3-
import android.content.Context
43
import androidx.compose.foundation.clickable
54
import androidx.compose.foundation.layout.Arrangement
65
import androidx.compose.foundation.layout.Column
@@ -26,8 +25,10 @@ import androidx.compose.material.icons.Icons
2625
import androidx.compose.material.icons.filled.Clear
2726
import androidx.compose.material.icons.filled.Search
2827
import androidx.compose.runtime.Composable
28+
import androidx.compose.runtime.derivedStateOf
2929
import androidx.compose.runtime.getValue
3030
import androidx.compose.runtime.mutableStateOf
31+
import androidx.compose.runtime.remember
3132
import androidx.compose.runtime.saveable.rememberSaveable
3233
import androidx.compose.runtime.setValue
3334
import androidx.compose.ui.Alignment
@@ -47,31 +48,49 @@ import androidx.compose.ui.unit.sp
4748
import androidx.compose.ui.window.Dialog
4849
import com.togitech.ccp.R
4950
import com.togitech.ccp.data.CountryData
50-
import com.togitech.ccp.data.utils.countryCodeToEmojiFlag
5151
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
5454
import kotlinx.collections.immutable.ImmutableList
5555
import kotlinx.collections.immutable.toImmutableList
5656

57-
private val DEFAULT_ROUNDING = 10.dp
57+
internal val DEFAULT_ROUNDING = 10.dp
5858
private val DEFAULT_ROW_PADDING = 16.dp
5959
private const val ROW_PADDING_VERTICAL_SCALING = 1.1f
6060
private val DEFAULT_ROW_FONT_SIZE = 16.sp
6161
private val SEARCH_ICON_PADDING = 5.dp
6262

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+
*/
6471
@Composable
65-
internal fun CountryDialog(
72+
fun CountryDialog(
6673
onDismissRequest: () -> Unit,
6774
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,
7177
rowPadding: Dp = DEFAULT_ROW_PADDING,
7278
rowFontSize: TextUnit = DEFAULT_ROW_FONT_SIZE,
7379
) {
80+
val context = LocalContext.current
7481
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+
}
7594

7695
Dialog(
7796
onDismissRequest = onDismissRequest,
@@ -101,16 +120,7 @@ internal fun CountryDialog(
101120
Spacer(modifier = Modifier.height(DEFAULT_ROW_PADDING))
102121
Divider()
103122
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 ->
114124
CountryRowItem(
115125
rowPadding = rowPadding,
116126
onSelect = { onSelect(countryItem) },
@@ -167,10 +177,10 @@ private fun CountryRowItem(
167177
verticalAlignment = Alignment.CenterVertically,
168178
) {
169179
Text(
170-
text = countryCodeToEmojiFlag(countryItem.countryCode) + " " +
180+
text = countryItem.emojiFlag + " " +
171181
stringResource(
172182
id = countryNames.getOrDefault(
173-
countryItem.countryCode.lowercase(),
183+
countryItem.countryIso,
174184
R.string.unknown,
175185
),
176186
),
@@ -231,8 +241,8 @@ private fun SearchTextField(
231241
private fun CountryDialogPreview() {
232242
CountryDialog(
233243
onSelect = {},
234-
context = LocalContext.current,
235-
filteredCountryList = getLibCountries.toImmutableList(),
244+
countryList = CountryData.entries.toImmutableList(),
236245
onDismissRequest = {},
246+
modifier = Modifier.fillMaxWidth().clip(RoundedCornerShape(DEFAULT_ROUNDING)),
237247
)
238248
}

0 commit comments

Comments
 (0)