Created
June 22, 2022 18:13
-
-
Save Debdutta-Panda/140321651f6c7f03862860dae7039b09 to your computer and use it in GitHub Desktop.
UI scope mechanism
This file contains hidden or 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
| 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