Skip to content

Commit

Permalink
feat: coroutines for fragments and appcompat activities
Browse files Browse the repository at this point in the history
  • Loading branch information
CraZyLegenD committed Sep 9, 2020
1 parent 8e2201d commit 3f58363
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
4 changes: 4 additions & 0 deletions coroutines/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ dependencies {

//live data
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle"

implementation "androidx.fragment:fragment-ktx:$fragment"
implementation "androidx.appcompat:appcompat:$appCompat"

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package com.crazylegend.coroutines

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.*

Expand Down Expand Up @@ -129,11 +132,11 @@ fun ViewModel.viewModelDefaultCoroutine(coroutineStart: CoroutineStart = Corouti
* @param action SuspendFunction0<Unit>
* @return Job
*/
fun ViewModel.viewModelUnconfinedCoroutine(coroutineStart: CoroutineStart = CoroutineStart.DEFAULT, action: suspend (scope: CoroutineScope) -> Unit = {}): Job {
return viewModelScope.launch(unconfinedDispatcher, coroutineStart) {
action(this)
}
}
fun ViewModel.viewModelUnconfinedCoroutine(coroutineStart: CoroutineStart = CoroutineStart.DEFAULT, action: suspend (scope: CoroutineScope) -> Unit = {}): Job =
viewModelScope.launch(unconfinedDispatcher, coroutineStart) {
action(this)
}


/**
*
Expand Down Expand Up @@ -188,3 +191,62 @@ internal inline fun <T> tryOrNull(block: () -> T): T? = try {
} catch (e: Exception) {
null
}

/**
* Cancel the Job if it's active.
*/
fun Job?.cancelIfActive() {
if (this?.isActive == true) {
cancel()
}
}


fun AppCompatActivity.ioCoroutine(action: suspend (scope: CoroutineScope) -> Unit = {}): Job = lifecycleScope.launch(ioDispatcher) {
action(this)
}


fun AppCompatActivity.mainCoroutine(action: suspend (scope: CoroutineScope) -> Unit = {}): Job = lifecycleScope.launch(mainDispatcher) {
action(this)
}


fun AppCompatActivity.unconfinedCoroutine(action: suspend (scope: CoroutineScope) -> Unit = {}): Job = lifecycleScope.launch(unconfinedDispatcher) {
action(this)
}


fun AppCompatActivity.defaultCoroutine(action: suspend (scope: CoroutineScope) -> Unit = {}): Job = lifecycleScope.launch(defaultDispatcher) {
action(this)
}


fun AppCompatActivity.nonCancellableCoroutine(action: suspend (scope: CoroutineScope) -> Unit = {}): Job = lifecycleScope.launch(NonCancellable) {
action(this)
}


fun Fragment.ioCoroutine(action: suspend (scope: CoroutineScope) -> Unit = {}): Job = lifecycleScope.launch(ioDispatcher) {
action(this)
}


fun Fragment.mainCoroutine(action: suspend (scope: CoroutineScope) -> Unit = {}): Job = lifecycleScope.launch(mainDispatcher) {
action(this)
}


fun Fragment.unconfinedCoroutine(action: suspend (scope: CoroutineScope) -> Unit = {}): Job = lifecycleScope.launch(unconfinedDispatcher) {
action(this)
}


fun Fragment.defaultCoroutine(action: suspend (scope: CoroutineScope) -> Unit = {}): Job = lifecycleScope.launch(defaultDispatcher) {
action(this)
}


fun Fragment.nonCancellableCoroutine(action: suspend (scope: CoroutineScope) -> Unit = {}): Job = lifecycleScope.launch(NonCancellable) {
action(this)
}

0 comments on commit 3f58363

Please sign in to comment.