Skip to content

Commit

Permalink
* fix adapter / item
Browse files Browse the repository at this point in the history
  • Loading branch information
angcyo committed Jul 6, 2022
1 parent 6385a82 commit 56979b8
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 35 deletions.
83 changes: 58 additions & 25 deletions Adapter/src/main/java/com/angcyo/dsladapter/DslAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,6 @@ open class DslAdapter(dataItems: List<DslAdapterItem>? = null) :

/**数据过滤规则*/
var dslDataFilter: DslDataFilter? = null
set(value) {
if (field == value) {
return
}
//remove
field?.apply {
removeDispatchUpdatesListener(this@DslAdapter)
beforeFilterInterceptorList.remove(adapterStatusIFilterInterceptor)
afterFilterInterceptorList.remove(loadMoreIFilterInterceptor)
}
field = value
//add
field?.apply {
addDispatchUpdatesListener(this@DslAdapter)
beforeFilterInterceptorList.add(0, adapterStatusIFilterInterceptor)
afterFilterInterceptorList.add(loadMoreIFilterInterceptor)
}
updateItemDepend()
}

/**单/多选助手*/
val itemSelectorHelper = ItemSelectorHelper(this)
Expand Down Expand Up @@ -107,8 +88,7 @@ open class DslAdapter(dataItems: List<DslAdapterItem>? = null) :
val _itemLayoutHold = hashMapOf<Int, Int>()

init {
dslDataFilter = DslDataFilter(this)

updateDataFilter(DslDataFilter(this))
dataItems?.let {
this.dataItems.clear()
this.dataItems.addAll(dataItems)
Expand Down Expand Up @@ -279,6 +259,26 @@ open class DslAdapter(dataItems: List<DslAdapterItem>? = null) :
itemUpdateDependObserver.remove(action)
}

/**更新数据过滤器[DslDataFilter]*/
fun updateDataFilter(dataFilter: DslDataFilter?) {
if (dslDataFilter == dataFilter) {
return
}
//remove
dslDataFilter?.apply {
removeDispatchUpdatesListener(this@DslAdapter)
beforeFilterInterceptorList.remove(adapterStatusIFilterInterceptor)
afterFilterInterceptorList.remove(loadMoreIFilterInterceptor)
}
dslDataFilter = dataFilter
//add
dslDataFilter?.apply {
addDispatchUpdatesListener(this@DslAdapter)
beforeFilterInterceptorList.add(0, adapterStatusIFilterInterceptor)
afterFilterInterceptorList.add(loadMoreIFilterInterceptor)
}
}

//</editor-fold>

//<editor-fold desc="辅助方法">
Expand Down Expand Up @@ -320,10 +320,13 @@ open class DslAdapter(dataItems: List<DslAdapterItem>? = null) :
* [DslAdapterStatusItem.ADAPTER_STATUS_ERROR]
* */
@UpdateFlag
fun setAdapterStatus(status: Int) {
fun setAdapterStatus(status: Int, error: Throwable? = null) {
if (dslAdapterStatusItem.itemState == status) {
return
}
if (status == DslAdapterStatusItem.ADAPTER_STATUS_ERROR) {
dslAdapterStatusItem.itemErrorThrowable = error
}
dslAdapterStatusItem.itemState = status
dslAdapterStatusItem.itemUpdateFlag = true
}
Expand Down Expand Up @@ -448,7 +451,7 @@ open class DslAdapter(dataItems: List<DslAdapterItem>? = null) :

/**插入数据列表*/
@UpdateFlag
fun insertItem(index: Int, item: DslAdapterItem, checkExist: Boolean = false) {
fun insertItem(index: Int, item: DslAdapterItem, checkExist: Boolean = true) {
if (checkExist && dataItems.contains(item)) {
return
}
Expand Down Expand Up @@ -576,13 +579,13 @@ open class DslAdapter(dataItems: List<DslAdapterItem>? = null) :

@UpdateFlag
fun removeItemFrom(
list: MutableList<DslAdapterItem>,
fromList: MutableList<DslAdapterItem>,
item: DslAdapterItem,
updateOther: Boolean
) {
val index = adapterItems.indexOf(item)
if (index != -1) {
if (list.remove(item)) {
if (fromList.remove(item)) {
item.itemRemoveFlag = true
if (updateOther) {
for (i in (index + 1) until adapterItems.size) {
Expand Down Expand Up @@ -722,6 +725,17 @@ open class DslAdapter(dataItems: List<DslAdapterItem>? = null) :

//</editor-fold desc="Item操作">

/** 获取[fromItem]更新时, 有多少子项需要更新 */
fun getUpdateDependItemListFrom(fromItem: DslAdapterItem): List<DslAdapterItem> {
val notifyChildFormItemList = mutableListOf<DslAdapterItem>()
getValidFilterDataList().forEachIndexed { index, dslAdapterItem ->
if (fromItem.isItemInUpdateList(dslAdapterItem, index)) {
notifyChildFormItemList.add(dslAdapterItem)
}
}
return notifyChildFormItemList
}

/**获取有效过滤后的数据集合*/
fun getValidFilterDataList(): List<DslAdapterItem> {
return dslDataFilter?.filterDataList ?: adapterItems
Expand Down Expand Up @@ -858,6 +872,16 @@ open class DslAdapter(dataItems: List<DslAdapterItem>? = null) :
addLastItem(this, config)
}

/**
* ```
* DslDemoItem()(0){}
* ```
* */
@UpdateFlag
operator fun <T : DslAdapterItem> T.invoke(index: Int, config: T.() -> Unit = {}) {
insertItem(index, this, config)
}

@UpdateFlag
operator fun <T : DslAdapterItem> T.invoke(
list: MutableList<DslAdapterItem>,
Expand All @@ -866,6 +890,15 @@ open class DslAdapter(dataItems: List<DslAdapterItem>? = null) :
addLastItem(list, this, config)
}

@UpdateFlag
operator fun <T : DslAdapterItem> T.invoke(
index: Int,
list: MutableList<DslAdapterItem>,
config: T.() -> Unit = {}
) {
insertItem(list, index, this, config)
}

/**
* <pre>
* this + DslAdapterItem()
Expand Down
36 changes: 26 additions & 10 deletions Adapter/src/main/java/com/angcyo/dsladapter/DslAdapterItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.annotation.AnimRes
import androidx.annotation.AnimatorRes
import androidx.annotation.AnyThread
import androidx.annotation.CallSuper
import androidx.core.math.MathUtils.clamp
import androidx.lifecycle.Lifecycle
Expand All @@ -19,6 +20,8 @@ import androidx.lifecycle.LifecycleRegistry
import androidx.recyclerview.widget.*
import com.angcyo.dsladapter.SwipeMenuHelper.Companion.SWIPE_MENU_TYPE_DEFAULT
import com.angcyo.dsladapter.SwipeMenuHelper.Companion.SWIPE_MENU_TYPE_FLOWING
import com.angcyo.dsladapter.annotation.UpdateByDiff
import com.angcyo.dsladapter.annotation.UpdateFlag
import com.angcyo.dsladapter.internal.ThrottleClickListener
import java.lang.ref.WeakReference
import kotlin.properties.ReadWriteProperty
Expand Down Expand Up @@ -81,8 +84,9 @@ open class DslAdapterItem : LifecycleOwner {
//<editor-fold desc="update操作">

/**[com.angcyo.dsladapter.DslAdapter.notifyItemChanged]*/
@AnyThread
open fun updateAdapterItem(payload: Any? = PAYLOAD_UPDATE_PART, useFilterList: Boolean = true) {
if (itemDslAdapter?._recyclerView?.isComputingLayout == true) {
if (itemDslAdapter?._recyclerView?.isComputingLayout == true || !isMain()) {
//L.w("跳过操作! [RecyclerView]正在计算布局, 请不要在RecyclerView正在布局时, 更新Item. ")
itemDslAdapter?._recyclerView?.post {
updateAdapterItem(payload, useFilterList)
Expand All @@ -95,6 +99,7 @@ open class DslAdapterItem : LifecycleOwner {
}

/**移除[item]*/
@UpdateFlag
open fun removeAdapterItem() {
itemDslAdapter?.apply {
removeItemFromAll(this@DslAdapterItem)
Expand All @@ -104,6 +109,7 @@ open class DslAdapterItem : LifecycleOwner {
}

/**负载更新, 通常用于更新媒体item*/
@UpdateByDiff
open fun updateItemDependPayload(payload: Any? = mediaPayload()) {
updateItemDepend(
FilterParams(
Expand All @@ -121,6 +127,7 @@ open class DslAdapterItem : LifecycleOwner {
* [isItemInUpdateList]
* [itemUpdateFrom]
* */
@UpdateByDiff
open fun updateItemDepend(
filterParams: FilterParams = FilterParams(
fromDslAdapterItem = this,
Expand Down Expand Up @@ -151,22 +158,21 @@ open class DslAdapterItem : LifecycleOwner {
/**有依赖时, 才更新
* [updateItemDepend]*/
open fun updateItemOnHaveDepend(
updateSelf: Boolean = true,
filterParams: FilterParams = FilterParams(
fromDslAdapterItem = this,
updateDependItemWithEmpty = false,
payload = PAYLOAD_UPDATE_PART
)
) {
val notifyChildFormItemList = mutableListOf<DslAdapterItem>()
itemDslAdapter?.getValidFilterDataList()?.forEachIndexed { index, dslAdapterItem ->
if (isItemInUpdateList(dslAdapterItem, index)) {
return@forEachIndexed
}
itemDslAdapter?.getUpdateDependItemListFrom(this)?.let {
notifyChildFormItemList.addAll(it)
}

if (notifyChildFormItemList.isNotEmpty()) {
updateItemDepend(filterParams)
} else {
} else if (updateSelf) {
//否则更新自己
updateAdapterItem(filterParams.payload)
}
Expand Down Expand Up @@ -244,6 +250,9 @@ open class DslAdapterItem : LifecycleOwner {
/**唯一标识此item的值*/
var itemTag: String? = null

/**异常标识, 自定义数据*/
var itemThrowable: Throwable? = null

/**item存储数据使用*/
var itemTags: SparseArray<Any?>? = null

Expand Down Expand Up @@ -330,7 +339,7 @@ open class DslAdapterItem : LifecycleOwner {
_itemAnimateDelay =
itemDslAdapter?.adapterItemAnimateDelayHandler?.computeAnimateDelay(this)
?: _itemAnimateDelay
L.w("_itemAnimateDelay:$_itemAnimateDelay")
//L.w("_itemAnimateDelay:$_itemAnimateDelay")
if (_itemAnimateDelay >= 0) {
val animation = animationOf(itemHolder.context, itemAnimateRes)
if (animation != null) {
Expand Down Expand Up @@ -1139,7 +1148,8 @@ open class DslAdapterItem : LifecycleOwner {

/**其次, 提供一个可以被子类覆盖的方法*/
open fun onItemChangeListener(item: DslAdapterItem) {
updateItemDepend()
//有依赖的item需要更新时, 再刷新界面
updateItemOnHaveDepend()
}

/**[itemChangeListener]*/
Expand Down Expand Up @@ -1207,8 +1217,11 @@ open class DslAdapterItem : LifecycleOwner {
/**是否选中, 需要 [ItemSelectorHelper.selectorModel] 的支持. */
var itemIsSelected = false
set(value) {
val old = field
field = value
onSetItemSelected(value)
if (old != value) {
onSetItemSelected(value)
}
}

/**简单的互斥操作支持
Expand All @@ -1235,7 +1248,7 @@ open class DslAdapterItem : LifecycleOwner {
/**监听item select改变事件*/
var onItemSelectorChange: ItemSelectAction = {
if (it.updateItemDepend) {
updateItemDepend()
updateItemOnHaveDepend()
}
}

Expand Down Expand Up @@ -1462,6 +1475,9 @@ open class DslAdapterItem : LifecycleOwner {
if (lifecycleRegistry.currentState.isAtLeast(Lifecycle.State.CREATED)) {
lifecycleRegistry.currentState = Lifecycle.State.DESTROYED
}
if (itemAnimateRes != 0) {
itemHolder.itemView.clearAnimation()
}
itemHolder.clear()
}

Expand Down

0 comments on commit 56979b8

Please sign in to comment.