-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support custom text styles and dialog picker
- Loading branch information
Showing
10 changed files
with
388 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<resources> | ||
<string name="app_name">JetCountyPicker</string> | ||
<string name="select_country_text">Select country</string> | ||
<string name="picker_type">Picker Type</string> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
countrypicker/src/main/java/com/canopas/campose/countrypicker/CountryPickerDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.canopas.campose.countrypicker | ||
|
||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxHeight | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.heightIn | ||
import androidx.compose.foundation.layout.wrapContentHeight | ||
import androidx.compose.foundation.shape.CornerSize | ||
import androidx.compose.material3.LocalTextStyle | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.rememberCoroutineScope | ||
import androidx.compose.runtime.saveable.rememberSaveable | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.composed | ||
import androidx.compose.ui.draw.clip | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.Shape | ||
import androidx.compose.ui.platform.LocalConfiguration | ||
import androidx.compose.ui.text.TextStyle | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
import androidx.compose.ui.window.Dialog | ||
import androidx.compose.ui.window.DialogProperties | ||
import com.canopas.campose.countrypicker.model.Country | ||
import kotlinx.coroutines.launch | ||
|
||
|
||
/** | ||
* Composable for displaying a country picker as a dialog. | ||
* | ||
* @param shape The shape of the dialog. | ||
* @param backgroundColor The color of the dialog container. | ||
* @param dialogTitle The title composable for the dialog. | ||
* @param onItemSelected Callback when a country is selected. | ||
* @param searchFieldTextStyle The text style for the search field. | ||
* @param placeholderTextStyle The text style for the search field placeholder. | ||
* @param countriesTextStyle The text style for the countries list. | ||
* @param showFullScreenDialog Whether to show the dialog as a full screen dialog. | ||
* @param onDismissRequest Callback when the dialog is dismissed. | ||
*/ | ||
@Composable | ||
fun CountryPickerDialog( | ||
shape: Shape = MaterialTheme.shapes.small, | ||
backgroundColor: Color = MaterialTheme.colorScheme.background, | ||
dialogTitle: @Composable () -> Unit, | ||
onItemSelected: (country: Country) -> Unit, | ||
searchFieldTextStyle: TextStyle = LocalTextStyle.current.copy(fontSize = 14.sp), | ||
placeholderTextStyle: TextStyle = MaterialTheme.typography.labelMedium.copy( | ||
color = Color.Gray, | ||
fontSize = 16.sp, | ||
), | ||
countriesTextStyle: TextStyle = TextStyle(), | ||
showFullScreenDialog: Boolean = false, | ||
onDismissRequest: () -> Unit | ||
) { | ||
var searchValue by rememberSaveable { mutableStateOf("") } | ||
val configuration = LocalConfiguration.current | ||
val screenHeight = configuration.screenHeightDp.dp | ||
val scope = rememberCoroutineScope() | ||
val modifier = if (showFullScreenDialog) { | ||
Modifier | ||
.fillMaxSize() | ||
.background(color = backgroundColor) | ||
} else { | ||
Modifier | ||
.wrapContentHeight() | ||
.heightIn(max = (screenHeight * 0.85f)) | ||
.clip(shape = shape) | ||
.background(color = backgroundColor, shape) | ||
} | ||
|
||
Dialog( | ||
onDismissRequest = onDismissRequest, | ||
properties = DialogProperties( | ||
usePlatformDefaultWidth = !showFullScreenDialog, | ||
dismissOnBackPress = true, | ||
dismissOnClickOutside = !showFullScreenDialog | ||
) | ||
) { | ||
Column( | ||
modifier = modifier | ||
) { | ||
dialogTitle() | ||
CountrySearchView(searchValue, searchFieldTextStyle, placeholderTextStyle) { | ||
searchValue = it | ||
} | ||
|
||
Countries(searchValue, countriesTextStyle) { | ||
scope.launch { | ||
onDismissRequest() | ||
onItemSelected(it) | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.