Skip to content

Commit

Permalink
custom flag works and ignore cases
Browse files Browse the repository at this point in the history
  • Loading branch information
hbb20 committed Dec 26, 2023
1 parent 4f435f1 commit 79bf923
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.hbb20.androidcountrypicker.R
import com.hbb20.androidcountrypicker.compose.theme.DemoTheme
import com.hbb20.contrypicker.flagpack1.FlagPack1
import com.hbb20.countrypicker.compose.CountryFlagLayout
import com.hbb20.countrypicker.compose.CountryPicker
Expand All @@ -51,7 +52,7 @@ class ComposeDemoActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MaterialTheme {
DemoTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
Expand Down Expand Up @@ -157,13 +158,22 @@ class ComposeDemoActivity : ComponentActivity() {
flagProvider = CPFlagImageProvider(
// map of alpha2 to drawable resource id
alpha2ToFlag = mapOf(
"IN" to R.drawable.ic_flag,
"US" to R.drawable.ic_flag,
"GB" to R.drawable.ic_flag,
"IN" to R.drawable.ic_flag_green_outlint,
"gb" to R.drawable.ic_flag_yellow,
"US" to R.drawable.ic_flag_blue,
),
// drawable resource id for missing flag
missingFlagPlaceHolder = R.drawable.ic_flag
)
),
pickerDialog = { cpDataStore, flagProvider, onDismissRequest, onCountrySelected ->
CountryPickerDialog(
cpDataStore = cpDataStore,
flagProvider = flagProvider,
onDismissRequest = onDismissRequest,
onCountrySelected = onCountrySelected,
quickAccessCountries = listOf("IN", "us", "GB"),
)
}
) {
setCountryCode(it?.alpha2)
}
Expand Down Expand Up @@ -215,7 +225,7 @@ class ComposeDemoActivity : ComponentActivity() {
)
}

if(showPickerDialog){
if (showPickerDialog) {
CountryPickerDialog(
cpDataStore = cpDataStore,
flagProvider = flagProvider,
Expand Down Expand Up @@ -359,21 +369,24 @@ class ComposeDemoActivity : ComponentActivity() {
modifier = Modifier
.fillMaxWidth()
.background(MaterialTheme.colors.surface)
.padding(16.dp)
.padding(16.dp),
backgroundColor = MaterialTheme.colors.surface
) {
Column(modifier = Modifier.padding(16.dp)) {
Text(
text = title,
style = MaterialTheme.typography.subtitle1,
color = Color.Black
)
Text(
text = body,
style = MaterialTheme.typography.body2,
color = Color.DarkGray
)
Spacer(modifier = Modifier.padding(8.dp))
countryPickerLayout()
Surface(color = MaterialTheme.colors.surface) {
Column(modifier = Modifier.padding(16.dp)) {
Text(
text = title,
style = MaterialTheme.typography.subtitle1,
color = MaterialTheme.colors.onSurface
)
Text(
text = body,
style = MaterialTheme.typography.body2,
color = MaterialTheme.colors.onSurface
)
Spacer(modifier = Modifier.padding(8.dp))
countryPickerLayout()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.hbb20.androidcountrypicker.compose.theme

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material.MaterialTheme
import androidx.compose.material.darkColors
import androidx.compose.material.lightColors
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color


@Composable
fun DemoTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
content: @Composable () -> Unit
) {
MaterialTheme(
colors = if (darkTheme) darkThemeColors else lightThemeColors,
content = content
)
}

val lightThemeColors = lightColors(
primary = Color(0xFF008577),
primaryVariant = Color(0xFF00574B),
secondary = Color(0xFF3F51B5),
secondaryVariant = Color(0xFF3F51B5),
onPrimary = Color(0xFFFFFFFF),
onSecondary = Color(0xFFFFFFFF),
error = Color(0xFFED5933),
onError = Color(0xFFFFFFFF),
background = Color(0xFFF4F4F7),
onBackground = Color(0xFF000000),
surface = Color(0xFFFFFFFF),
onSurface = Color(0xFF000000)
)

val darkThemeColors = darkColors(
primary = Color(0xFF00E0C9),
primaryVariant = Color(0xFF00C7AB),
secondary = Color(0xFF3F51B5),
secondaryVariant = Color(0xFF3F51B5),
onPrimary = Color(0xFFFFFFFF),
onSecondary = Color(0xFFFFFFFF),
error = Color(0xFFED5933),
onError = Color(0xFFFFFFFF),
background = Color(0xFF000000),
onBackground = Color(0xFFFAFAFC),
surface = Color(0xFF292B2E),
onSurface = Color(0xFFFAFAFC)
)
6 changes: 6 additions & 0 deletions app/src/main/res/drawable/ic_flag_blue.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<vector android:height="24dp"
android:tint="#0C5695"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M14.4,6L14,4H5v17h2v-7h5.6l0.4,2h7V6z"/>
</vector>
12 changes: 12 additions & 0 deletions app/src/main/res/drawable/ic_flag_green_outlint.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#A0EF00"
android:viewportWidth="24"
android:viewportHeight="24">

<path
android:fillColor="@android:color/white"
android:pathData="M14,6l-1,-2L5,4v17h2v-7h5l1,2h7L20,6h-6zM18,14h-4l-1,-2L7,12L7,6h5l1,2h5v6z" />

</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_flag_yellow.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#FFC107"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M14.4,6L14,4H5v17h2v-7h5.6l0.4,2h7V6z" />
</vector>
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ private fun DefaultCountryPickerDialogContent(
.fillMaxWidth(0.8f)
.padding(16.dp),
shape = RoundedCornerShape(16.dp),
backgroundColor = MaterialTheme.colors.surface,
) {
val countryList = remember(cpDataStore) {
cpDataStore.countryList
Expand Down Expand Up @@ -439,7 +440,7 @@ private fun DefaultSearchField(
BasicTextField(
value = searchQuery,
onValueChange = { setSearchQuery(it) },
textStyle = MaterialTheme.typography.body1,
textStyle = MaterialTheme.typography.body1.copy(color = MaterialTheme.colors.onSurface),
cursorBrush = SolidColor(
TextFieldDefaults.outlinedTextFieldColors()
.cursorColor(isError = false).value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.hbb20.countrypicker.flagprovider

import androidx.annotation.DrawableRes
import java.util.Locale

abstract class CPFlagProvider

Expand All @@ -18,14 +19,17 @@ class DefaultEmojiFlagProvider(val useEmojiCompat: Boolean = false) : CPFlagProv
* other flags in this pack to maintain visual symmetry.
*/
class CPFlagImageProvider(
val alpha2ToFlag: Map<String, Int>,
alpha2ToFlag: Map<String, Int>,
@DrawableRes val missingFlagPlaceHolder: Int
) : CPFlagProvider() {
private val flagMap = alpha2ToFlag.map { it.key.uppercase(Locale.ENGLISH) to it.value }.toMap()

@DrawableRes
fun getFlag(alpha2Code: String): Int {
return alpha2ToFlag.getOrElse(
getNormalizedAlpha2ForFlag(alpha2Code)
val upperCaseAlpha2Code = alpha2Code.uppercase(Locale.ENGLISH)
val flag = flagMap[upperCaseAlpha2Code]
return flag ?: flagMap.getOrElse(
getNormalizedAlpha2ForFlag(upperCaseAlpha2Code)
) { missingFlagPlaceHolder }
}

Expand All @@ -35,12 +39,12 @@ class CPFlagImageProvider(
* This function will convert "UM" to "US"
*/
private fun getNormalizedAlpha2ForFlag(alpha2Code: String): String {
return when (alpha2Code.toLowerCase()) {
"um" -> "us"
"sj" -> "no"
"bv" -> "no"
"hm" -> "au"
else -> alpha2Code.toLowerCase()
return when (alpha2Code.uppercase(Locale.ENGLISH)) {
"UM" -> "US"
"SJ" -> "NO"
"BV" -> "NO"
"HM" -> "AU"
else -> alpha2Code.uppercase(Locale.ENGLISH)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ enum class CPLanguage(
UZBEK("uz"),
VIETNAMESE("vi");

val translationFileName: String by lazy { "cp_${name.toLowerCase(Locale.ROOT)}.xml" }
val translationFileName: String by lazy { "cp_${name.lowercase(Locale.ROOT)}.xml" }
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class CPViewHelper(
val detectedAlpha2 =
if (isInEditMode) "US" else countryDetector.detectCountry(countryDetectSources)
val detectedCountry = cpDataStore.countryList.firstOrNull {
it.alpha2.toLowerCase(Locale.ROOT) == detectedAlpha2?.toLowerCase(
it.alpha2.lowercase(Locale.ROOT) == detectedAlpha2?.lowercase(
Locale.ROOT
)
}
Expand Down

0 comments on commit 79bf923

Please sign in to comment.