diff --git a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectableExtension.kt b/lib_base/src/main/java/com/android/base/app/aac/InjectableExtensionEx.kt similarity index 54% rename from lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectableExtension.kt rename to lib_base/src/main/java/com/android/base/app/aac/InjectableExtensionEx.kt index 7e68a9d..9a374ba 100644 --- a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectableExtension.kt +++ b/lib_base/src/main/java/com/android/base/app/aac/InjectableExtensionEx.kt @@ -1,39 +1,52 @@ -package com.android.base.app.fragment.injectable +package com.android.base.app.aac -import androidx.lifecycle.LifecycleOwner -import androidx.lifecycle.LiveData -import androidx.lifecycle.Observer -import androidx.lifecycle.ViewModelProvider +import androidx.fragment.app.Fragment +import androidx.fragment.app.viewModels +import androidx.lifecycle.* import com.android.base.app.ui.LoadingView -import com.android.base.data.ErrorHandler import com.android.base.data.Resource import timber.log.Timber -interface InjectableExtension { +interface FragmentExtension { + /**用于创建 ViewModel 的 viewModelFactory*/ val viewModelFactory: ViewModelProvider.Factory - val errorHandler: ErrorHandler + /**返回Fragment*/ + val host: Fragment + /**处理异常*/ + 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>, - force: Boolean = true, + forceLoading: Boolean = true, onSuccess: (T?) -> Unit -) where H : InjectableExtension, H : LoadingView, H : LifecycleOwner { +) where H : FragmentExtension, H : LoadingView, H : LifecycleOwner { liveData.observe(this, Observer { when { it.isError -> { Timber.d("handleLiveResource -> isError") dismissLoadingDialog() - errorHandler.handleError(it.error()) + handleError(it.error()) } it.isLoading -> { Timber.d("handleLiveResource -> isLoading") - showLoadingDialog(!force) + showLoadingDialog(!forceLoading) } it.isSuccess -> { Timber.d("handleLiveResource -> isSuccess") @@ -47,25 +60,29 @@ fun H.handleLiveResource( fun H.handleLiveResourceWithData( liveData: LiveData>, - force: Boolean = true, + forceLoading: Boolean = true, + onEmpty: (() -> Unit)? = null, onSuccess: (T) -> Unit -) where H : InjectableExtension, H : LoadingView, H : LifecycleOwner { +) where H : FragmentExtension, H : LoadingView, H : LifecycleOwner { + liveData.observe(this, Observer { when { it.isError -> { Timber.d("handleLiveResourceWithData -> isError") dismissLoadingDialog() - errorHandler.handleError(it.error()) + handleError(it.error()) } it.isLoading -> { Timber.d("handleLiveResourceWithData -> isLoading") - showLoadingDialog(!force) + showLoadingDialog(!forceLoading) } it.isSuccess -> { Timber.d("handleLiveResourceWithData -> isSuccess") dismissLoadingDialog() if (it.hasData()) { onSuccess(it.data()) + } else { + onEmpty?.invoke() } } } diff --git a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseDialogFragment.kt b/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseDialogFragment.kt deleted file mode 100644 index a3b02a2..0000000 --- a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseDialogFragment.kt +++ /dev/null @@ -1,20 +0,0 @@ -package com.android.base.app.fragment.injectable - -import androidx.lifecycle.ViewModelProvider -import com.android.base.app.dagger.Injectable -import com.android.base.app.fragment.BaseDialogFragment -import com.android.base.data.ErrorHandler -import javax.inject.Inject - -/** - *@author Ztiany - * Email: ztiany3@gmail.com - * Date : 2019-01-15 12:57 - */ -open class InjectorBaseDialogFragment : BaseDialogFragment(), Injectable, InjectableExtension { - - @Inject override lateinit var viewModelFactory: ViewModelProvider.Factory - - @Inject override lateinit var errorHandler: ErrorHandler - -} \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseFragment.kt b/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseFragment.kt deleted file mode 100644 index 05bf79a..0000000 --- a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseFragment.kt +++ /dev/null @@ -1,34 +0,0 @@ -package com.android.base.app.fragment.injectable - -import androidx.fragment.app.viewModels -import androidx.lifecycle.ViewModel -import androidx.lifecycle.ViewModelProvider -import com.android.base.app.dagger.Injectable -import com.android.base.app.fragment.BaseFragment -import com.android.base.data.ErrorHandler -import javax.inject.Inject - -/** - *@author Ztiany - * Email: ztiany3@gmail.com - * Date : 2019-01-15 12:57 - */ -open class InjectorBaseFragment : BaseFragment(), Injectable, InjectableExtension { - - @Inject override lateinit var viewModelFactory: ViewModelProvider.Factory - - @Inject override lateinit var errorHandler: ErrorHandler - - inline fun injectViewModel(): Lazy { - return viewModels { viewModelFactory } - } - - inline fun injectActivityViewModel(): Lazy { - return viewModels( - ownerProducer = { - requireActivity() - } - ) { viewModelFactory } - } - -} \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseListFragment.kt b/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseListFragment.kt deleted file mode 100644 index 8caa8be..0000000 --- a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseListFragment.kt +++ /dev/null @@ -1,34 +0,0 @@ -package com.android.base.app.fragment.injectable - -import androidx.fragment.app.viewModels -import androidx.lifecycle.ViewModel -import androidx.lifecycle.ViewModelProvider -import com.android.base.app.dagger.Injectable -import com.android.base.app.fragment.BaseListFragment -import com.android.base.data.ErrorHandler -import javax.inject.Inject - -/** - *@author Ztiany - * Email: ztiany3@gmail.com - * Date : 2019-01-15 12:57 - */ -open class InjectorBaseListFragment : BaseListFragment(), Injectable, InjectableExtension { - - @Inject override lateinit var viewModelFactory: ViewModelProvider.Factory - - @Inject override lateinit var errorHandler: ErrorHandler - - inline fun injectViewModel(): Lazy { - return viewModels { viewModelFactory } - } - - inline fun injectActivityViewModel(): Lazy { - return viewModels( - ownerProducer = { - requireActivity() - } - ) { viewModelFactory } - } - -} \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseListV2Fragment.kt b/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseListV2Fragment.kt deleted file mode 100644 index f0b9142..0000000 --- a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseListV2Fragment.kt +++ /dev/null @@ -1,34 +0,0 @@ -package com.android.base.app.fragment.injectable - -import androidx.fragment.app.viewModels -import androidx.lifecycle.ViewModel -import androidx.lifecycle.ViewModelProvider -import com.android.base.app.dagger.Injectable -import com.android.base.app.fragment.BaseListV2Fragment -import com.android.base.data.ErrorHandler -import javax.inject.Inject - -/** - *@author Ztiany - * Email: ztiany3@gmail.com - * Date : 2019-01-15 12:57 - */ -open class InjectorBaseListV2Fragment : BaseListV2Fragment(), Injectable, InjectableExtension { - - @Inject override lateinit var viewModelFactory: ViewModelProvider.Factory - - @Inject override lateinit var errorHandler: ErrorHandler - - inline fun injectViewModel(): Lazy { - return viewModels { viewModelFactory } - } - - inline fun injectActivityViewModel(): Lazy { - return viewModels( - ownerProducer = { - requireActivity() - } - ) { viewModelFactory } - } - -} \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseStateDialogFragment.kt b/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseStateDialogFragment.kt deleted file mode 100644 index 939bb9a..0000000 --- a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseStateDialogFragment.kt +++ /dev/null @@ -1,34 +0,0 @@ -package com.android.base.app.fragment.injectable - -import androidx.fragment.app.viewModels -import androidx.lifecycle.ViewModel -import androidx.lifecycle.ViewModelProvider -import com.android.base.app.dagger.Injectable -import com.android.base.app.fragment.BaseStateDialogFragment -import com.android.base.data.ErrorHandler -import javax.inject.Inject - -/** - *@author Ztiany - * Email: ztiany3@gmail.com - * Date : 2019-01-15 12:57 - */ -open class InjectorBaseStateDialogFragment : BaseStateDialogFragment(), Injectable, InjectableExtension { - - @Inject override lateinit var viewModelFactory: ViewModelProvider.Factory - - @Inject override lateinit var errorHandler: ErrorHandler - - inline fun injectViewModel(): Lazy { - return viewModels { viewModelFactory } - } - - inline fun injectActivityViewModel(): Lazy { - return viewModels( - ownerProducer = { - requireActivity() - } - ) { viewModelFactory } - } - -} \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseStateFragment.kt b/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseStateFragment.kt deleted file mode 100644 index 7544101..0000000 --- a/lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseStateFragment.kt +++ /dev/null @@ -1,34 +0,0 @@ -package com.android.base.app.fragment.injectable - -import androidx.fragment.app.viewModels -import androidx.lifecycle.ViewModel -import androidx.lifecycle.ViewModelProvider -import com.android.base.app.dagger.Injectable -import com.android.base.app.fragment.BaseStateFragment -import com.android.base.data.ErrorHandler -import javax.inject.Inject - -/** - *@author Ztiany - * Email: ztiany3@gmail.com - * Date : 2019-01-15 12:57 - */ -open class InjectorBaseStateFragment : BaseStateFragment(), Injectable, InjectableExtension { - - @Inject override lateinit var viewModelFactory: ViewModelProvider.Factory - - @Inject override lateinit var errorHandler: ErrorHandler - - inline fun injectViewModel(): Lazy { - return viewModels { viewModelFactory } - } - - inline fun injectActivityViewModel(): Lazy { - return viewModels( - ownerProducer = { - requireActivity() - } - ) { viewModelFactory } - } - -} \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/app/ui/UIEx.kt b/lib_base/src/main/java/com/android/base/app/ui/RefreshListLayoutEx.kt similarity index 71% rename from lib_base/src/main/java/com/android/base/app/ui/UIEx.kt rename to lib_base/src/main/java/com/android/base/app/ui/RefreshListLayoutEx.kt index 6b4e134..39356dc 100644 --- a/lib_base/src/main/java/com/android/base/app/ui/UIEx.kt +++ b/lib_base/src/main/java/com/android/base/app/ui/RefreshListLayoutEx.kt @@ -124,41 +124,3 @@ fun RefreshListLayout<*>.showLoadingIfEmpty() { } } } - -fun RefreshStateLayout.handleResultWithStatus(t: T?, onResult: ((T) -> Unit)) { - if (isRefreshing) { - refreshCompleted() - } - if (t == null || (t is Collection<*> && t.isEmpty()) || (t is Map<*, *> && t.isEmpty())) { - showEmptyLayout() - } else { - onResult.invoke(t) - showContentLayout() - } -} - -fun RefreshStateLayout.handleErrorWithStatus(throwable: Throwable?) { - if (throwable == null) { - Timber.d("processErrorWithStatus called, but throwable is null") - return - } - if (isRefreshing) { - refreshCompleted() - } - val errorTypeClassifier = Sword.get().errorClassifier() - if (errorTypeClassifier != null) { - when { - errorTypeClassifier.isNetworkError(throwable) -> { - Timber.d("isNetworkError showNetErrorLayout") - showNetErrorLayout() - } - errorTypeClassifier.isServerError(throwable) -> { - Timber.d("isServerError showServerErrorLayout") - showServerErrorLayout() - } - else -> showErrorLayout() - } - } else { - showErrorLayout() - } -} \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/app/ui/RefreshStateLayoutEx.kt b/lib_base/src/main/java/com/android/base/app/ui/RefreshStateLayoutEx.kt new file mode 100644 index 0000000..bbc3846 --- /dev/null +++ b/lib_base/src/main/java/com/android/base/app/ui/RefreshStateLayoutEx.kt @@ -0,0 +1,42 @@ +package com.android.base.app.ui + +import com.android.base.app.Sword +import timber.log.Timber + +fun RefreshStateLayout.handleResultWithStatus(t: T?, onResult: ((T) -> Unit)) { + if (isRefreshing) { + refreshCompleted() + } + if (t == null || (t is Collection<*> && t.isEmpty()) || (t is Map<*, *> && t.isEmpty())) { + showEmptyLayout() + } else { + onResult.invoke(t) + showContentLayout() + } +} + +fun RefreshStateLayout.handleErrorWithStatus(throwable: Throwable?) { + if (throwable == null) { + Timber.d("processErrorWithStatus called, but throwable is null") + return + } + if (isRefreshing) { + refreshCompleted() + } + val errorTypeClassifier = Sword.get().errorClassifier() + if (errorTypeClassifier != null) { + when { + errorTypeClassifier.isNetworkError(throwable) -> { + Timber.d("isNetworkError showNetErrorLayout") + showNetErrorLayout() + } + errorTypeClassifier.isServerError(throwable) -> { + Timber.d("isServerError showServerErrorLayout") + showServerErrorLayout() + } + else -> showErrorLayout() + } + } else { + showErrorLayout() + } +} \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/data/ErrorHandler.kt b/lib_base/src/main/java/com/android/base/data/ErrorHandler.kt deleted file mode 100644 index 76af4ee..0000000 --- a/lib_base/src/main/java/com/android/base/data/ErrorHandler.kt +++ /dev/null @@ -1,25 +0,0 @@ -package com.android.base.data - - -/** - * 异常处理器 - * - * @author Ztiany - * Email: ztiany3@gmail.com - * Date : 2018-11-08 16:02 - */ -interface ErrorHandler { - - /** 根据异常,生成一个合理的错误提示 */ - fun createMessage(throwable: Throwable): CharSequence - - /** 直接处理异常,比如根据 [createMessage] 方法生成的消息弹出一个 toast。 */ - fun handleError(throwable: Throwable) - - /** 直接处理异常,自定义消息处理*/ - fun handleError(throwable: Throwable, processor: ((CharSequence) -> Unit)) - - /**处理全局异常,此方法仅由数据层调用,用于统一处理全局异常*/ - fun handleGlobalError(throwable: Throwable) - -} diff --git a/lib_base/src/main/java/com/android/base/rx/FlowableRetryDelay.java b/lib_base/src/main/java/com/android/base/rx/FlowableRetryDelay.java index 296da3f..8595fdd 100644 --- a/lib_base/src/main/java/com/android/base/rx/FlowableRetryDelay.java +++ b/lib_base/src/main/java/com/android/base/rx/FlowableRetryDelay.java @@ -36,8 +36,8 @@ public class FlowableRetryDelay implements Function, Publish return Flowable.error(throwable); } mRetryCount++; - Timber.i(new Date() + " 自动重试" + (mRetryCount + 1) + "次,在" + Thread.currentThread() + "线程"); if (mRetryCount <= mMaxRetries) { + Timber.i(new Date().toLocaleString() + " 自动重试" + (mRetryCount) + "次,在" + Thread.currentThread() + "线程"); return Flowable.timer(mRetryDelayMillis, TimeUnit.MILLISECONDS); } return Flowable.error(throwable); diff --git a/lib_base/src/main/java/com/android/base/rx/ObservableRetryDelay.java b/lib_base/src/main/java/com/android/base/rx/ObservableRetryDelay.java index 30f1427..deff581 100644 --- a/lib_base/src/main/java/com/android/base/rx/ObservableRetryDelay.java +++ b/lib_base/src/main/java/com/android/base/rx/ObservableRetryDelay.java @@ -35,7 +35,7 @@ public class ObservableRetryDelay implements Function, Obs return Observable.error(throwable); } mRetryCount++; - Timber.i(new Date() + " 自动重试" + (mRetryCount + 1) + "次,在" + Thread.currentThread() + "线程"); + Timber.i(new Date().toLocaleString() + " 自动重试" + (mRetryCount) + "次,在" + Thread.currentThread() + "线程"); if (mRetryCount <= mMaxRetries) { return Observable.timer(mRetryDelayMillis, TimeUnit.MILLISECONDS); }