diff --git a/lib_base/src/main/java/com/android/base/app/aac/InjectableExtensionEx.kt b/lib_base/src/main/java/com/android/base/app/aac/LiveResourceHandler.kt similarity index 70% rename from lib_base/src/main/java/com/android/base/app/aac/InjectableExtensionEx.kt rename to lib_base/src/main/java/com/android/base/app/aac/LiveResourceHandler.kt index 9a374ba..db49005 100644 --- a/lib_base/src/main/java/com/android/base/app/aac/InjectableExtensionEx.kt +++ b/lib_base/src/main/java/com/android/base/app/aac/LiveResourceHandler.kt @@ -1,41 +1,25 @@ package com.android.base.app.aac -import androidx.fragment.app.Fragment -import androidx.fragment.app.viewModels -import androidx.lifecycle.* +import androidx.lifecycle.LifecycleOwner +import androidx.lifecycle.LiveData +import androidx.lifecycle.Observer import com.android.base.app.ui.LoadingView import com.android.base.data.Resource import timber.log.Timber -interface FragmentExtension { - - /**用于创建 ViewModel 的 viewModelFactory*/ - val viewModelFactory: ViewModelProvider.Factory - - /**返回Fragment*/ - val host: Fragment +interface LiveResourceHandler { /**处理异常*/ fun handleError(throwable: Throwable) } -inline fun FragmentExtension.injectViewModel(): Lazy { - return host.viewModels { viewModelFactory } -} - -inline fun FragmentExtension.injectActivityViewModel(): Lazy { - return host.viewModels( - ownerProducer = { host.requireActivity() }, - factoryProducer = { viewModelFactory }) -} - fun H.handleLiveResource( liveData: LiveData>, forceLoading: Boolean = true, onSuccess: (T?) -> Unit -) where H : FragmentExtension, H : LoadingView, H : LifecycleOwner { +) where H : LiveResourceHandler, H : LoadingView, H : LifecycleOwner { liveData.observe(this, Observer { when { @@ -63,7 +47,7 @@ fun H.handleLiveResourceWithData( forceLoading: Boolean = true, onEmpty: (() -> Unit)? = null, onSuccess: (T) -> Unit -) where H : FragmentExtension, H : LoadingView, H : LifecycleOwner { +) where H : LiveResourceHandler, H : LoadingView, H : LifecycleOwner { liveData.observe(this, Observer { when {