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
import kotlinx.coroutines.flow.* | |
sealed class Result<out T> { | |
class Success<T>(data: T) : Result<T>() | |
object Loading : Result<Nothing>() | |
class Error(error: Throwable) : Result<Nothing>() | |
} |
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
Scaffold(topBar = { | |
Toolbar(getString(R.string.app_name)) { | |
dialogState.value = true | |
} | |
}) { | |
CountriesDialog() | |
} |
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
// For handling the state of dialog | |
private val dialogState by lazy { mutableStateOf(false) } | |
@Composable | |
fun CountriesDialog() { | |
if (dialogState.value) { | |
SingleSelectDialog(title = getString(R.string.title_select_country), | |
optionsList = countriesMap.values.toList(), | |
defaultSelected = countriesMap.keys.toList().indexOf(selectedOption), |
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
fun getCountries(): Map<String, String> { | |
val countriesMap = hashMapOf<String, String>() | |
val isoCountries = Locale.getISOCountries() | |
isoCountries.forEach { | |
val locale = Locale("", it) | |
countriesMap[locale.country.toLowerCase(Locale.getDefault())] = locale.displayCountry | |
} | |
return countriesMap.toList().sortedBy { (_, value) -> value }.toMap() | |
} |
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
@Composable | |
fun SingleSelectDialog(title: String, | |
optionsList: List<String>, | |
defaultSelected: Int, | |
submitButtonText: String, | |
onSubmitButtonClick: (Int) -> Unit, | |
onDismissRequest: () -> Unit) { | |
val selectedOption = mutableStateOf(defaultSelected) |