Last active
January 13, 2022 12:30
-
-
Save rommansabbir/44acd7f35987846a0cb6ab983f4ff27b to your computer and use it in GitHub Desktop.
Extension functions that fire [CoroutineScope] (support: [Dispatchers.IO], [Dispatchers.Main], [Dispatchers.Default]) respective to [AppCompatActivity]/[Fragment]/[ViewModel]. Functions are called under the parent [CoroutineScope] of respective [AppCompatActivity]/[Fragment]/[ViewModel], So if the parent is no longer exists, cancel any ongoing o…
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
/** | |
* Extension functions that fire [CoroutineScope] (support: [Dispatchers.IO], [Dispatchers.Main], [Dispatchers.Default]) respective to [AppCompatActivity]/[Fragment]/[ViewModel]. | |
* Functions are called under the parent [CoroutineScope] of respective [AppCompatActivity]/[Fragment]/[ViewModel], | |
* So if the parent is no longer exists, cancel any ongoing operation automatically. | |
*/ | |
private suspend fun CoroutineScope.executeBody(block: suspend CoroutineScope.() -> Unit) { | |
try { | |
block.invoke(this) | |
} catch (e: Exception) { | |
e.printStackTrace(); logThis(e.message ?: "") | |
} | |
} | |
fun AppCompatActivity.mainScope(block: suspend CoroutineScope.() -> Unit) = | |
lifecycleScope.launch { executeBody(block) } | |
suspend fun AppCompatActivity.mainScopeSuspended(block: suspend CoroutineScope.() -> Unit) = | |
withContext(Dispatchers.Main) { executeBody(block) } | |
fun AppCompatActivity.ioScope(block: suspend CoroutineScope.() -> Unit) = | |
lifecycleScope.launch { executeBody(block) } | |
suspend fun AppCompatActivity.ioScopeSuspended(block: suspend CoroutineScope.() -> Unit) = | |
withContext(Dispatchers.IO) { executeBody(block) } | |
fun AppCompatActivity.defaultScope(block: suspend CoroutineScope.() -> Unit) = | |
lifecycleScope.launch { withContext(Dispatchers.Default) { executeBody(block) } } | |
suspend fun AppCompatActivity.defaultScopeSuspended(block: suspend CoroutineScope.() -> Unit) = | |
withContext(Dispatchers.Default) { executeBody(block) } | |
fun Fragment.mainScope(block: suspend CoroutineScope.() -> Unit) = | |
lifecycleScope.launch { executeBody(block) } | |
suspend fun Fragment.mainScopeSuspended(block: suspend CoroutineScope.() -> Unit) = | |
withContext(Dispatchers.Main) { executeBody(block) } | |
fun Fragment.ioScope(block: suspend CoroutineScope.() -> Unit) = | |
lifecycleScope.launch { withContext(Dispatchers.IO) { executeBody(block) } } | |
suspend fun Fragment.ioScopeSuspended(block: suspend CoroutineScope.() -> Unit) = | |
withContext(Dispatchers.IO) { executeBody(block) } | |
fun Fragment.defaultScope(block: suspend CoroutineScope.() -> Unit) = | |
lifecycleScope.launch { withContext(Dispatchers.Default) { executeBody(block) } } | |
suspend fun Fragment.defaultScopeSuspended(block: suspend CoroutineScope.() -> Unit) = | |
withContext(Dispatchers.Default) { executeBody(block) } | |
fun ViewModel.mainScope(block: suspend CoroutineScope.() -> Unit) = | |
viewModelScope.launch { executeBody(block) } | |
suspend fun ViewModel.mainScopeSuspended(block: suspend CoroutineScope.() -> Unit) = | |
withContext(Dispatchers.Main) { executeBody(block) } | |
fun ViewModel.ioScope(block: suspend CoroutineScope.() -> Unit) = | |
viewModelScope.launch { withContext(Dispatchers.IO) { executeBody(block) } } | |
suspend fun ViewModel.ioScopeSuspended(block: suspend CoroutineScope.() -> Unit) = | |
withContext(Dispatchers.IO) { executeBody(block) } | |
fun ViewModel.defaultScope(block: suspend CoroutineScope.() -> Unit) = | |
viewModelScope.launch { withContext(Dispatchers.Default) { executeBody(block) } } | |
suspend fun ViewModel.defaultScopeSuspended(block: suspend CoroutineScope.() -> Unit) = | |
withContext(Dispatchers.Default) { | |
executeBody(block) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment