remove injectable extension

androidx
Ztiany 5 years ago
parent add179c578
commit 2fed64b3ef
  1. 49
      lib_base/src/main/java/com/android/base/app/aac/InjectableExtensionEx.kt
  2. 20
      lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseDialogFragment.kt
  3. 34
      lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseFragment.kt
  4. 34
      lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseListFragment.kt
  5. 34
      lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseListV2Fragment.kt
  6. 34
      lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseStateDialogFragment.kt
  7. 34
      lib_base/src/main/java/com/android/base/app/fragment/injectable/InjectorBaseStateFragment.kt
  8. 38
      lib_base/src/main/java/com/android/base/app/ui/RefreshListLayoutEx.kt
  9. 42
      lib_base/src/main/java/com/android/base/app/ui/RefreshStateLayoutEx.kt
  10. 25
      lib_base/src/main/java/com/android/base/data/ErrorHandler.kt
  11. 2
      lib_base/src/main/java/com/android/base/rx/FlowableRetryDelay.java
  12. 2
      lib_base/src/main/java/com/android/base/rx/ObservableRetryDelay.java

@ -1,39 +1,52 @@
package com.android.base.app.fragment.injectable package com.android.base.app.aac
import androidx.lifecycle.LifecycleOwner import androidx.fragment.app.Fragment
import androidx.lifecycle.LiveData import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer import androidx.lifecycle.*
import androidx.lifecycle.ViewModelProvider
import com.android.base.app.ui.LoadingView import com.android.base.app.ui.LoadingView
import com.android.base.data.ErrorHandler
import com.android.base.data.Resource import com.android.base.data.Resource
import timber.log.Timber import timber.log.Timber
interface InjectableExtension { interface FragmentExtension {
/**用于创建 ViewModel 的 viewModelFactory*/
val viewModelFactory: ViewModelProvider.Factory val viewModelFactory: ViewModelProvider.Factory
val errorHandler: ErrorHandler /**返回Fragment*/
val host: Fragment
/**处理异常*/
fun handleError(throwable: Throwable)
}
inline fun <reified VM : ViewModel> FragmentExtension.injectViewModel(): Lazy<VM> {
return host.viewModels { viewModelFactory }
}
inline fun <reified VM : ViewModel> FragmentExtension.injectActivityViewModel(): Lazy<VM> {
return host.viewModels(
ownerProducer = { host.requireActivity() },
factoryProducer = { viewModelFactory })
} }
fun <H, T> H.handleLiveResource( fun <H, T> H.handleLiveResource(
liveData: LiveData<Resource<T>>, liveData: LiveData<Resource<T>>,
force: Boolean = true, forceLoading: Boolean = true,
onSuccess: (T?) -> Unit onSuccess: (T?) -> Unit
) where H : InjectableExtension, H : LoadingView, H : LifecycleOwner { ) where H : FragmentExtension, H : LoadingView, H : LifecycleOwner {
liveData.observe(this, Observer { liveData.observe(this, Observer {
when { when {
it.isError -> { it.isError -> {
Timber.d("handleLiveResource -> isError") Timber.d("handleLiveResource -> isError")
dismissLoadingDialog() dismissLoadingDialog()
errorHandler.handleError(it.error()) handleError(it.error())
} }
it.isLoading -> { it.isLoading -> {
Timber.d("handleLiveResource -> isLoading") Timber.d("handleLiveResource -> isLoading")
showLoadingDialog(!force) showLoadingDialog(!forceLoading)
} }
it.isSuccess -> { it.isSuccess -> {
Timber.d("handleLiveResource -> isSuccess") Timber.d("handleLiveResource -> isSuccess")
@ -47,25 +60,29 @@ fun <H, T> H.handleLiveResource(
fun <H, T> H.handleLiveResourceWithData( fun <H, T> H.handleLiveResourceWithData(
liveData: LiveData<Resource<T>>, liveData: LiveData<Resource<T>>,
force: Boolean = true, forceLoading: Boolean = true,
onEmpty: (() -> Unit)? = null,
onSuccess: (T) -> Unit onSuccess: (T) -> Unit
) where H : InjectableExtension, H : LoadingView, H : LifecycleOwner { ) where H : FragmentExtension, H : LoadingView, H : LifecycleOwner {
liveData.observe(this, Observer { liveData.observe(this, Observer {
when { when {
it.isError -> { it.isError -> {
Timber.d("handleLiveResourceWithData -> isError") Timber.d("handleLiveResourceWithData -> isError")
dismissLoadingDialog() dismissLoadingDialog()
errorHandler.handleError(it.error()) handleError(it.error())
} }
it.isLoading -> { it.isLoading -> {
Timber.d("handleLiveResourceWithData -> isLoading") Timber.d("handleLiveResourceWithData -> isLoading")
showLoadingDialog(!force) showLoadingDialog(!forceLoading)
} }
it.isSuccess -> { it.isSuccess -> {
Timber.d("handleLiveResourceWithData -> isSuccess") Timber.d("handleLiveResourceWithData -> isSuccess")
dismissLoadingDialog() dismissLoadingDialog()
if (it.hasData()) { if (it.hasData()) {
onSuccess(it.data()) onSuccess(it.data())
} else {
onEmpty?.invoke()
} }
} }
} }

@ -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
}

@ -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 <reified VM : ViewModel> injectViewModel(): Lazy<VM> {
return viewModels { viewModelFactory }
}
inline fun <reified VM : ViewModel> injectActivityViewModel(): Lazy<VM> {
return viewModels(
ownerProducer = {
requireActivity()
}
) { viewModelFactory }
}
}

@ -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<T> : BaseListFragment<T>(), Injectable, InjectableExtension {
@Inject override lateinit var viewModelFactory: ViewModelProvider.Factory
@Inject override lateinit var errorHandler: ErrorHandler
inline fun <reified VM : ViewModel> injectViewModel(): Lazy<VM> {
return viewModels { viewModelFactory }
}
inline fun <reified VM : ViewModel> injectActivityViewModel(): Lazy<VM> {
return viewModels(
ownerProducer = {
requireActivity()
}
) { viewModelFactory }
}
}

@ -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<T> : BaseListV2Fragment<T>(), Injectable, InjectableExtension {
@Inject override lateinit var viewModelFactory: ViewModelProvider.Factory
@Inject override lateinit var errorHandler: ErrorHandler
inline fun <reified VM : ViewModel> injectViewModel(): Lazy<VM> {
return viewModels { viewModelFactory }
}
inline fun <reified VM : ViewModel> injectActivityViewModel(): Lazy<VM> {
return viewModels(
ownerProducer = {
requireActivity()
}
) { viewModelFactory }
}
}

@ -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 <reified VM : ViewModel> injectViewModel(): Lazy<VM> {
return viewModels { viewModelFactory }
}
inline fun <reified VM : ViewModel> injectActivityViewModel(): Lazy<VM> {
return viewModels(
ownerProducer = {
requireActivity()
}
) { viewModelFactory }
}
}

@ -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 <reified VM : ViewModel> injectViewModel(): Lazy<VM> {
return viewModels { viewModelFactory }
}
inline fun <reified VM : ViewModel> injectActivityViewModel(): Lazy<VM> {
return viewModels(
ownerProducer = {
requireActivity()
}
) { viewModelFactory }
}
}

@ -124,41 +124,3 @@ fun RefreshListLayout<*>.showLoadingIfEmpty() {
} }
} }
} }
fun <T> 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()
}
}

@ -0,0 +1,42 @@
package com.android.base.app.ui
import com.android.base.app.Sword
import timber.log.Timber
fun <T> 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()
}
}

@ -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)
}

@ -36,8 +36,8 @@ public class FlowableRetryDelay implements Function<Flowable<Throwable>, Publish
return Flowable.error(throwable); return Flowable.error(throwable);
} }
mRetryCount++; mRetryCount++;
Timber.i(new Date() + " 自动重试" + (mRetryCount + 1) + "次,在" + Thread.currentThread() + "线程");
if (mRetryCount <= mMaxRetries) { if (mRetryCount <= mMaxRetries) {
Timber.i(new Date().toLocaleString() + " 自动重试" + (mRetryCount) + "次,在" + Thread.currentThread() + "线程");
return Flowable.timer(mRetryDelayMillis, TimeUnit.MILLISECONDS); return Flowable.timer(mRetryDelayMillis, TimeUnit.MILLISECONDS);
} }
return Flowable.error(throwable); return Flowable.error(throwable);

@ -35,7 +35,7 @@ public class ObservableRetryDelay implements Function<Observable<Throwable>, Obs
return Observable.error(throwable); return Observable.error(throwable);
} }
mRetryCount++; mRetryCount++;
Timber.i(new Date() + " 自动重试" + (mRetryCount + 1) + "次,在" + Thread.currentThread() + "线程"); Timber.i(new Date().toLocaleString() + " 自动重试" + (mRetryCount) + "次,在" + Thread.currentThread() + "线程");
if (mRetryCount <= mMaxRetries) { if (mRetryCount <= mMaxRetries) {
return Observable.timer(mRetryDelayMillis, TimeUnit.MILLISECONDS); return Observable.timer(mRetryDelayMillis, TimeUnit.MILLISECONDS);
} }

Loading…
Cancel
Save