Skip to content

Commit

Permalink
LifecycleExt.launchAndRepeatWithViewLifecycle 新增 newCoroutineContext …
Browse files Browse the repository at this point in the history
…参数,可以传入协程运行的上下文。
  • Loading branch information
wsdydeni committed Apr 6, 2022
1 parent 4da75a6 commit 0392c8a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ open class BaseActivity : AppCompatActivity {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.effect.collect {
when(it) {
is wsdydeni.library.android.base.ToastEffect ->
is ToastEffect ->
Toast.makeText(this@BaseActivity, it.text, Toast.LENGTH_SHORT).show()
is wsdydeni.library.android.base.ToastDebugEffect ->
is ToastDebugEffect ->
Toast.makeText(this@BaseActivity, it.text, Toast.LENGTH_SHORT).show()
is wsdydeni.library.android.base.DialogShowEffect -> {
is DialogShowEffect -> {
if(!isFinishing && !isDestroyed) {
baseHandler.post {
if(!isFinishing && !isDestroyed) {
Expand All @@ -94,7 +94,7 @@ open class BaseActivity : AppCompatActivity {
}
}
}
is wsdydeni.library.android.base.DialogDismissEffect -> {
is DialogDismissEffect -> {
loadingDialog?.let { dialog ->
if(dialog.isShowing) {
dialog.dismissNormally()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.receiveAsFlow
import wsdydeni.library.android.utils.lifecycle.repeatOnLifecycle
import wsdydeni.library.android.utils.lifecycle.launchAndRepeatWithViewLifecycle


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,31 @@ import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlin.coroutines.CoroutineContext

/**
* Launches a new coroutine and repeats `block` every time the Fragment's viewLifecycleOwner
* is in and out of `minActiveState` lifecycle state.
* 更加简洁的语法封装
*
* 在 [Fragment.getViewLifecycleOwner] 达到 [minActiveState] 时,启动一个新的协程并重复 [block] 并在 [Lifecycle.Event.ON_DESTROY] 时,取消协程。
*
* 警告:不要在其他 CoroutineScope 中调用本函数!!!
*
* 如果在 CoroutineScope 中调用本函数,会隐式持有外部作用域
*
* 但是并不会保留外部作用域的上下文,也就是无法实现结构化并发
*
* @param newCoroutineContext 协程上下文 默认 [Dispatchers.Main]
* @param minActiveState 需要达到的最小生命周期状态 默认 [Lifecycle.State.STARTED]
* @param block 协程代码块
*/
inline fun Fragment.launchAndRepeatWithViewLifecycle(
newCoroutineContext: CoroutineContext = Dispatchers.Main,
minActiveState: Lifecycle.State = Lifecycle.State.STARTED,
crossinline block: suspend CoroutineScope.() -> Unit
) {
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.lifecycleScope.launch(newCoroutineContext) {
viewLifecycleOwner.lifecycle.repeatOnLifecycle(minActiveState) {
block()
}
Expand All @@ -25,20 +39,24 @@ inline fun Fragment.launchAndRepeatWithViewLifecycle(
/**
* 更加简洁的语法封装
*
* 在 [FragmentActivity.getLifecycle] 达到 [minActiveState] 时,启动一个新的协程并重复 [block] 并在 [Lifecycle.Event.ON_DESTROY] 时,取消协程。
*
* 警告:不要在其他 CoroutineScope 中调用本函数!!!
*
* 如果在 CoroutineScope 中调用本函数,会隐式持有外部作用域
*
* 但是并不会保留外部作用域的上下文,也就是无法实现结构化并发
*
* @param minActiveState 需要达到的最小生命周期状态
* @param newCoroutineContext 协程上下文 默认 [Dispatchers.Main]
* @param minActiveState 需要达到的最小生命周期状态 默认 [Lifecycle.State.STARTED]
* @param block 协程代码块
*/
inline fun FragmentActivity.launchAndRepeatWithViewLifecycle(
newCoroutineContext: CoroutineContext = Dispatchers.Main,
minActiveState: Lifecycle.State = Lifecycle.State.STARTED,
crossinline block: suspend CoroutineScope.() -> Unit
) {
lifecycleScope.launch {
lifecycleScope.launch(newCoroutineContext) {
lifecycle.repeatOnLifecycle(minActiveState) {
block()
}
Expand Down

0 comments on commit 0392c8a

Please sign in to comment.