Skip to content

Instantly share code, notes, and snippets.

View rooparsh's full-sized avatar
🚀

Rooparsh Kalia rooparsh

🚀
View GitHub Profile
@rooparsh
rooparsh / NetworkBoundResource.kt
Last active April 3, 2021 09:13
Handling Offline First Gracefully
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>()
}
@rooparsh
rooparsh / Scafold.kt
Last active October 16, 2020 08:48
Showing dialog from menu item click
Scaffold(topBar = {
Toolbar(getString(R.string.app_name)) {
dialogState.value = true
}
}) {
CountriesDialog()
}
// 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),
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()
}
@rooparsh
rooparsh / RadioButton.kt
Last active October 16, 2020 08:49
Composable Function for Creating Text Radio Button
@Composable
fun RadioButton(text: String, selectedValue: String, onClickListener: (String) -> Unit) {
Row(Modifier
.fillMaxWidth()
.selectable(
selected = (text == selectedValue),
onClick = {
onClickListener(text)
}
)
@Composable
fun SingleSelectDialog(title: String,
optionsList: List<String>,
defaultSelected: Int,
submitButtonText: String,
onSubmitButtonClick: (Int) -> Unit,
onDismissRequest: () -> Unit) {
val selectedOption = mutableStateOf(defaultSelected)