Skip to content

Commit

Permalink
日常修改:2024-12-09.
Browse files Browse the repository at this point in the history
  • Loading branch information
walgr committed Dec 9, 2024
1 parent 1d51ce5 commit d428716
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
2 changes: 2 additions & 0 deletions QuickWidget/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ dependencies {
implementation deps.androidx.recyclerview
implementation deps.flexbox

// implementation deps.kotlin_reflect

if (IS_REMOTE.toBoolean()) {
api deps.quickbind.util
api deps.quickbind.recyclerview
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class BaseEmptyView @JvmOverloads constructor(
override var curState: EmptyViewState = StateLoading,
) : FrameLayout(mContext, attrs, defStyleAttr), EmptyViewStateManager {

override val registerStateMap: MutableMap<Int, DealStateFun<EmptyViewState>> = mutableMapOf()
override val registerStateMap: MutableMap<Int, StateClassAndFun<out EmptyViewState>> = mutableMapOf()

init {
init()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,33 @@ package com.wpf.app.quickwidget.emptyview
import com.wpf.app.quickutil.helper.generic.forceTo

interface EmptyViewStateManager {
val registerStateMap: MutableMap<Int, DealStateFun<EmptyViewState>>
val registerStateMap: MutableMap<Int, StateClassAndFun<out EmptyViewState>>
var curState: EmptyViewState
fun changeState(state: EmptyViewState) {
this.curState = state
val dealStateFun =
val stateClassAndFun =
registerStateMap[getClassFirstInterface<EmptyViewState>(state)] ?: return
val stateClass: Class<EmptyViewState> =
dealStateFun.javaClass.methods.find { it.name == "invoke" }!!.parameterTypes.first()
.forceTo()
val dealStateFun: DealStateFun<EmptyViewState> = stateClassAndFun.dealStateFun as DealStateFun<EmptyViewState>
val stateClass: Class<EmptyViewState> = stateClassAndFun.statusClass as Class<EmptyViewState>
if (stateClass == state.javaClass) {
dealStateFun.invoke(state)
} else {
when (state) {
is NoErrorI -> {
dealStateFun.invoke(
stateClass.getDeclaredConstructor().newInstance()
)
}
is LoadingI -> {
dealStateFun.invoke(
stateClass.getDeclaredConstructor(Boolean::class.java).newInstance(state.listIsEmpty)
)
}
is SuccessI -> {
dealStateFun.invoke(
stateClass.getDeclaredConstructor().newInstance()
)
}
is StateNetError -> {
dealStateFun.invoke(
stateClass.getDeclaredConstructor(Int::class.java).newInstance(state.errorCode)
Expand All @@ -46,14 +55,17 @@ interface EmptyViewStateManager {

inline fun <reified T : EmptyViewState> EmptyViewStateManager.register(noinline dealState: T.() -> Unit) {
val key = getClassFirstInterface<T>()
registerStateMap[key] = dealState.forceTo<DealStateFun<EmptyViewState>>()
registerStateMap[key] = StateClassAndFun(T::class.java, dealState.forceTo<DealStateFun<EmptyViewState>>())
}

inline fun <reified T : EmptyViewState> EmptyViewStateManager.unRegister() {
val key = getClassFirstInterface<T>()
registerStateMap.remove(key)
}

/**
* 找到最终的接口类[NoErrorI,LoadingI,SuccessI,EmptyDataI,CustomErrorI]
*/
inline fun <reified T : EmptyViewState> getClassFirstInterface(state: EmptyViewState? = null): Int {
var hashCode = T::class.java.hashCode()
var curObj: Class<*> = state?.javaClass ?: T::class.java
Expand All @@ -69,6 +81,8 @@ inline fun <reified T : EmptyViewState> getClassFirstInterface(state: EmptyViewS

typealias DealStateFun<T> = T.() -> Unit

class StateClassAndFun<T>(val statusClass: Class<T>, val dealStateFun: DealStateFun<T>)

interface EmptyViewState

//正常状态
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ org.gradle.jvmargs=-Xmx4096m -Dfile.encoding=UTF-8

IS_REMOTE=true
GROUP=com.wpf.app.quick
VERSION_NAME=0.11.0
VERSION_NAME=0.11.1

POM_DESCRIPTION=Quick Android.

Expand Down

0 comments on commit d428716

Please sign in to comment.