Skip to content

Instantly share code, notes, and snippets.

@Debdutta-Panda
Created June 22, 2022 18:13
Show Gist options
  • Select an option

  • Save Debdutta-Panda/140321651f6c7f03862860dae7039b09 to your computer and use it in GitHub Desktop.

Select an option

Save Debdutta-Panda/140321651f6c7f03862860dae7039b09 to your computer and use it in GitHub Desktop.
UI scope mechanism
data class Toaster(
private val context: Context
){
fun toast(message: String, duration: Int = Toast.LENGTH_SHORT){
Toast.makeText(context, message,duration).show()
}
fun stringResource(@StringRes id: Int): String{
return context.getString(id)
}
}
typealias UIScope = suspend (ScopeManager)->Unit
data class ScopeManager(
val navHostController: NavHostController? = null,
val owner: LifecycleOwner? = null,
val toaster: Toaster? = null
)
fun MutableState<UIScope?>.scope(block: UIScope?){
this.value = {scopeManager ->
block?.invoke(scopeManager)
this.value = null
}
}
suspend fun MutableState<UIScope?>.forward(scopeManager: ScopeManager){
this.value?.invoke(scopeManager)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment