From e09842a9c9f173b200049403e7627203690b0f22 Mon Sep 17 00:00:00 2001 From: Ztiany Date: Tue, 15 Oct 2019 17:34:26 +0800 Subject: [PATCH] add LiveResouerHandler --- ...eExtensionEx.kt => LiveResourceHandler.kt} | 28 ++++--------------- 1 file changed, 6 insertions(+), 22 deletions(-) rename lib_base/src/main/java/com/android/base/app/aac/{InjectableExtensionEx.kt => LiveResourceHandler.kt} (70%) 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 {