Skip to content

Commit

Permalink
1、添加 ReactiveFragment 示例
Browse files Browse the repository at this point in the history
2、移除冗余方法
  • Loading branch information
leavesCZY committed Dec 1, 2021
1 parent c9b0570 commit 5fa5f68
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import github.leavesc.reactivehttp.viewmodel.IUIAction
import github.leavesc.reactivehttp.viewmodel.IUIActionEventObserver
import kotlinx.coroutines.CoroutineScope

Expand All @@ -21,9 +22,10 @@ open class ReactiveActivity : AppCompatActivity(), IUIActionEventObserver {
protected inline fun <reified VM> getViewModel(
factory: ViewModelProvider.Factory? = null,
noinline initializer: (VM.(lifecycleOwner: LifecycleOwner) -> Unit)? = null
): Lazy<VM> where VM : ViewModel {
): Lazy<VM> where VM : ViewModel, VM : IUIAction {
return lazy {
getViewModelFast(
viewModelStoreOwner = this,
lifecycleOwner = this,
viewModelClass = VM::class.java,
factory = factory,
Expand All @@ -35,9 +37,10 @@ open class ReactiveActivity : AppCompatActivity(), IUIActionEventObserver {
protected inline fun <reified VM> getViewModelInstance(
crossinline create: () -> VM,
noinline initializer: (VM.(lifecycleOwner: LifecycleOwner) -> Unit)? = null
): Lazy<VM> where VM : ViewModel {
): Lazy<VM> where VM : ViewModel, VM : IUIAction {
return lazy {
getViewModelFast(
viewModelStoreOwner = this,
lifecycleOwner = this,
viewModelClass = VM::class.java,
factory = object : ViewModelProvider.NewInstanceFactory() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package github.leavesc.reactivehttp.base

import android.app.ProgressDialog
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import github.leavesc.reactivehttp.viewmodel.IUIAction
import github.leavesc.reactivehttp.viewmodel.IUIActionEventObserver
import kotlinx.coroutines.CoroutineScope

/**
* @Author: leavesC
* @Date: 2021/12/1 23:16
* @Desc:
* @Github:https://github.com/leavesC
*/
class ReactiveFragment : Fragment(), IUIActionEventObserver {

protected inline fun <reified VM> getViewModel(
lifecycleOwner: LifecycleOwner = viewLifecycleOwner,
factory: ViewModelProvider.Factory? = null,
noinline initializer: (VM.(lifecycleOwner: LifecycleOwner) -> Unit)? = null
): Lazy<VM> where VM : ViewModel, VM : IUIAction {
return lazy {
getViewModelFast(
viewModelStoreOwner = this,
lifecycleOwner = lifecycleOwner,
viewModelClass = VM::class.java,
factory = factory,
initializer = initializer
)
}
}

protected inline fun <reified VM> getViewModelInstance(
lifecycleOwner: LifecycleOwner = viewLifecycleOwner,
crossinline create: () -> VM,
noinline initializer: (VM.(lifecycleOwner: LifecycleOwner) -> Unit)? = null
): Lazy<VM> where VM : ViewModel, VM : IUIAction {
return lazy {
getViewModelFast(
viewModelStoreOwner = this,
lifecycleOwner = lifecycleOwner,
viewModelClass = VM::class.java,
factory = object : ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return create() as T
}
},
initializer = initializer
)
}
}

override val lifecycleSupportedScope: CoroutineScope
get() = viewLifecycleOwner.lifecycleScope

private var loadDialog: ProgressDialog? = null

override fun showLoading() {
dismissLoading()
activity?.let { act ->
loadDialog = ProgressDialog(act).apply {
setCancelable(true)
setCanceledOnTouchOutside(false)
show()
}
}
}

override fun dismissLoading() {
loadDialog?.takeIf { it.isShowing }?.dismiss()
loadDialog = null
}

override fun showToast(msg: String) {
activity?.let { act ->
if (msg.isNotBlank()) {
Toast.makeText(act, msg, Toast.LENGTH_SHORT).show()
}
}
}

override fun finishView() {
activity?.finish()
}

override fun onDestroyView() {
super.onDestroyView()
dismissLoading()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,42 +56,37 @@ interface IUIActionEventObserver {

fun finishView()

/**
* 用于方便获取 ViewModel 实例
* 重点在于调用了 collectUiActionIfNeed 方法
* 启动了协程对 uiActionEventFlow 进行监听
*/
fun <VM> getViewModelFast(
viewModelStoreOwner: ViewModelStoreOwner,
lifecycleOwner: LifecycleOwner,
viewModelClass: Class<VM>,
factory: ViewModelProvider.Factory? = null,
initializer: (VM.(lifecycleOwner: LifecycleOwner) -> Unit)? = null
): VM where VM : ViewModel {
return when (lifecycleOwner) {
is ViewModelStoreOwner -> {
if (factory == null) {
ViewModelProvider(lifecycleOwner).get(viewModelClass)
} else {
ViewModelProvider(lifecycleOwner, factory).get(viewModelClass)
}
}
else -> {
factory?.create(viewModelClass) ?: viewModelClass.newInstance()
}
): VM where VM : ViewModel, VM : IUIAction {
return if (factory == null) {
ViewModelProvider(viewModelStoreOwner).get(viewModelClass)
} else {
ViewModelProvider(viewModelStoreOwner, factory).get(viewModelClass)
}.apply {
applyExtraAction(lifecycleOwner = lifecycleOwner, viewModel = this)
collectUiActionIfNeed(lifecycleOwner = lifecycleOwner, viewModel = this)
initializer?.invoke(this, lifecycleOwner)
collectUiActionIfNeed(viewModel = this)
}
}

/**
* 外部可以通过此方法来为 ViewModel 增加一些额外操作
* 如果外部不通过 getViewModelFast 方法来获取 ViewModel 实例的话
* 则必须在自己构建 ViewModel 实例后调用 collectUiActionIfNeed 方法
* 以便能够启动协程对 uiActionEventFlow 进行监听
*/
fun applyExtraAction(lifecycleOwner: LifecycleOwner, viewModel: ViewModel) {

}

fun <VM> collectUiActionIfNeed(
lifecycleOwner: LifecycleOwner,
viewModel: VM
) where VM : ViewModel {
val uiActionEventFlow = (viewModel as? IUIAction)?.uiActionEventFlow
) where VM : ViewModel, VM : IUIAction {
val uiActionEventFlow = viewModel.uiActionEventFlow
if (uiActionEventFlow != null) {
lifecycleSupportedScope.launch(Dispatchers.Main.immediate) {
uiActionEventFlow.collect {
Expand Down

0 comments on commit 5fa5f68

Please sign in to comment.