Last active
June 23, 2022 15:51
-
-
Save Debdutta-Panda/1fd22dd136426f9b906efd0ab39d5d48 to your computer and use it in GitHub Desktop.
Dialoger mechanism
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
if(vm.dialoger.enabled.value){ | |
Dialog( | |
onDismissRequest = { | |
vm.dialoger.onDone("dismiss") | |
}, | |
properties = DialogProperties( | |
dismissOnBackPress = true, | |
dismissOnClickOutside = true | |
) | |
) { | |
Card( | |
modifier = Modifier | |
.fillMaxWidth() | |
){ | |
Column( | |
modifier = Modifier | |
.fillMaxWidth() | |
.padding(12.dp) | |
){ | |
Text("Heading") | |
Divider() | |
Text("Message?") | |
Row( | |
modifier = Modifier | |
.fillMaxWidth(), | |
horizontalArrangement = Arrangement.End | |
){ | |
TextButton(onClick = { | |
vm.dialoger.onDone("positive") | |
}) { | |
Text("Positive") | |
} | |
Spacer(modifier = Modifier.width(12.dp)) | |
TextButton(onClick = { | |
vm.dialoger.onDone("negative") | |
}) { | |
Text(vm.negativeText.value) | |
} | |
} | |
} | |
} | |
} | |
} |
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
package com.debduttapanda.innovativedialoger | |
import androidx.compose.runtime.State | |
import androidx.compose.runtime.mutableStateOf | |
import kotlinx.coroutines.suspendCancellableCoroutine | |
import kotlin.coroutines.resume | |
class Dialoger{ | |
private val _enabled = mutableStateOf(false) | |
val enabled: State<Boolean> = _enabled | |
private var onDoneCallback: (Any?)->Unit = {} | |
private var onDoneMediator: ((Any?)->Boolean)? = { true } | |
suspend fun show( | |
onDoneMediator: ((Any?)->Boolean)? = { true } | |
) = suspendCancellableCoroutine<Any?> { coroutine-> | |
this.onDoneMediator = onDoneMediator | |
_enabled.value = true | |
onDoneCallback = {param-> | |
coroutine.resume(param) | |
} | |
} | |
fun onDone(param: Any?) { | |
if(onDoneMediator?.invoke(param)==true){ | |
_enabled.value = false | |
onDoneCallback.invoke(param) | |
} | |
} | |
} |
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
val negativeText = mutableStateOf("Negative") | |
val dialoger = Dialoger() | |
fun showDialog1() { | |
viewModelScope.launch { | |
//val result = dialoger.show() // it is enough for normal use cases, but for advance use case do the following | |
val result = dialoger.show{ | |
if(it=="negative"){ | |
negativeText.value = (System.currentTimeMillis()%4).toString() | |
false | |
} | |
else{ | |
true | |
} | |
} | |
Log.d("my_result","$result") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment