parent
add179c578
commit
2fed64b3ef
@ -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 } |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
@ -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) |
|
||||||
|
|
||||||
} |
|
Loading…
Reference in new issue