Skip to content

Instantly share code, notes, and snippets.

@rommansabbir
Last active January 13, 2022 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rommansabbir/44acd7f35987846a0cb6ab983f4ff27b to your computer and use it in GitHub Desktop.
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…
/**
* 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