From 0e091fa8614d114a81668dbdb585d980c4cc87e3 Mon Sep 17 00:00:00 2001 From: Fitz Date: Wed, 23 Nov 2022 21:26:56 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=94=B6=E6=95=9B=20API=20=E6=8A=BD?= =?UTF-8?q?=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yifeiyuan/flap/paging/FlapPagingDataAdapter.kt | 2 +- flap/src/main/java/me/yifeiyuan/flap/Flap.kt | 14 +++++++------- .../src/main/java/me/yifeiyuan/flap/FlapAdapter.kt | 2 +- flap/src/main/java/me/yifeiyuan/flap/FlapApi.kt | 2 +- .../main/java/me/yifeiyuan/flap/FlapInitializer.kt | 8 +++++++- flap/src/main/java/me/yifeiyuan/flap/Registry.kt | 12 ++++++++++++ 6 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 flap/src/main/java/me/yifeiyuan/flap/Registry.kt diff --git a/flap-paging/src/main/java/me/yifeiyuan/flap/paging/FlapPagingDataAdapter.kt b/flap-paging/src/main/java/me/yifeiyuan/flap/paging/FlapPagingDataAdapter.kt index 7aaa312a..9dc9b7a6 100644 --- a/flap-paging/src/main/java/me/yifeiyuan/flap/paging/FlapPagingDataAdapter.kt +++ b/flap-paging/src/main/java/me/yifeiyuan/flap/paging/FlapPagingDataAdapter.kt @@ -20,7 +20,7 @@ import me.yifeiyuan.flap.widget.FlapStickyHeaders * * @since 3.3.0 */ -class FlapPagingDataAdapter(private val flap: Flap = Flap(), diffCallback: DiffUtil.ItemCallback, private val flapInitBlock: (Flap.() -> Unit)? = null) : PagingDataAdapter>(diffCallback), IAdapterHookManager by flap, IAdapterDelegateManager by flap, IAdapterServiceManager by flap, SwipeDragHelper.Callback, FlapStickyHeaders, FlapApi by flap { +class FlapPagingDataAdapter(private val flap: Flap = Flap(), diffCallback: DiffUtil.ItemCallback, private val flapInitBlock: (FlapApi.() -> Unit)? = null) : PagingDataAdapter>(diffCallback), SwipeDragHelper.Callback, FlapStickyHeaders, FlapApi by flap { init { apply { diff --git a/flap/src/main/java/me/yifeiyuan/flap/Flap.kt b/flap/src/main/java/me/yifeiyuan/flap/Flap.kt index 53b7a8de..52c0aabb 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/Flap.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/Flap.kt @@ -7,19 +7,15 @@ import android.view.ViewGroup import androidx.lifecycle.LifecycleOwner import androidx.recyclerview.widget.RecyclerView import me.yifeiyuan.flap.delegate.AdapterDelegate -import me.yifeiyuan.flap.delegate.AdapterDelegateManager import me.yifeiyuan.flap.delegate.FallbackAdapterDelegate -import me.yifeiyuan.flap.delegate.IAdapterDelegateManager import me.yifeiyuan.flap.event.Event import me.yifeiyuan.flap.event.EventObserver import me.yifeiyuan.flap.event.EventObserverWrapper import me.yifeiyuan.flap.ext.* -import me.yifeiyuan.flap.hook.AdapterHookManager -import me.yifeiyuan.flap.hook.IAdapterHookManager +import me.yifeiyuan.flap.hook.AdapterHook import me.yifeiyuan.flap.hook.PreloadHook import me.yifeiyuan.flap.pool.ComponentPool -import me.yifeiyuan.flap.service.AdapterServiceManager -import me.yifeiyuan.flap.service.IAdapterServiceManager +import me.yifeiyuan.flap.service.AdapterService /** * 负责代理部分 Adapter API 实现 @@ -30,7 +26,11 @@ import me.yifeiyuan.flap.service.IAdapterServiceManager * * @since 3.1.5 */ -class Flap : IAdapterHookManager by AdapterHookManager(), IAdapterDelegateManager by AdapterDelegateManager(), IAdapterServiceManager by AdapterServiceManager(), FlapApi { +class Flap : FlapApi { + + override val adapterDelegates: MutableList> = mutableListOf() + override val adapterHooks: MutableList = mutableListOf() + override val adapterServices: MutableMap, AdapterService> = mutableMapOf() companion object { private const val TAG = "Flap" diff --git a/flap/src/main/java/me/yifeiyuan/flap/FlapAdapter.kt b/flap/src/main/java/me/yifeiyuan/flap/FlapAdapter.kt index fafdf300..ddff8918 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/FlapAdapter.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/FlapAdapter.kt @@ -19,7 +19,7 @@ import java.util.* * @since 2020/9/22 * @since 3.0.0 */ -open class FlapAdapter(private val flap: Flap = Flap(), private val flapInitBlock: (Flap.() -> Unit)? = null) : RecyclerView.Adapter>(), IAdapterHookManager by flap, IAdapterDelegateManager by flap, IAdapterServiceManager by flap, SwipeDragHelper.Callback, FlapStickyHeaders, FlapApi by flap { +open class FlapAdapter(private val flap: Flap = Flap(), private val flapInitBlock: (FlapApi.() -> Unit)? = null) : RecyclerView.Adapter>(), SwipeDragHelper.Callback, FlapStickyHeaders, FlapApi by flap { companion object { private const val TAG = "FlapAdapter" diff --git a/flap/src/main/java/me/yifeiyuan/flap/FlapApi.kt b/flap/src/main/java/me/yifeiyuan/flap/FlapApi.kt index 07b1c9a4..9c90d281 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/FlapApi.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/FlapApi.kt @@ -15,7 +15,7 @@ import me.yifeiyuan.flap.hook.PreloadHook * Created by 程序亦非猿 on 2022/11/3. * @since 3.3.0 */ -interface FlapApi { +interface FlapApi : Registry { /** * 通过 Adapter 发送事件 diff --git a/flap/src/main/java/me/yifeiyuan/flap/FlapInitializer.kt b/flap/src/main/java/me/yifeiyuan/flap/FlapInitializer.kt index e14ebf34..ba79872e 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/FlapInitializer.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/FlapInitializer.kt @@ -5,8 +5,10 @@ import me.yifeiyuan.flap.delegate.* import me.yifeiyuan.flap.delegate.AdapterDelegateManager import me.yifeiyuan.flap.delegate.DefaultFallbackAdapterDelegate import me.yifeiyuan.flap.delegate.IAdapterDelegateManager +import me.yifeiyuan.flap.hook.AdapterHook import me.yifeiyuan.flap.hook.AdapterHookManager import me.yifeiyuan.flap.hook.IAdapterHookManager +import me.yifeiyuan.flap.service.AdapterService import me.yifeiyuan.flap.service.AdapterServiceManager import me.yifeiyuan.flap.service.IAdapterServiceManager @@ -24,7 +26,11 @@ import me.yifeiyuan.flap.service.IAdapterServiceManager * @since 2020/9/22 * @since 3.0.0 */ -object FlapInitializer : IAdapterHookManager by AdapterHookManager(), IAdapterDelegateManager by AdapterDelegateManager(), IAdapterServiceManager by AdapterServiceManager() { +object FlapInitializer : Registry { + + override val adapterDelegates: MutableList> = mutableListOf() + override val adapterHooks: MutableList = mutableListOf() + override val adapterServices: MutableMap, AdapterService> = mutableMapOf() /** * 是否使用 application context 来创建 Component diff --git a/flap/src/main/java/me/yifeiyuan/flap/Registry.kt b/flap/src/main/java/me/yifeiyuan/flap/Registry.kt new file mode 100644 index 00000000..b5a447cc --- /dev/null +++ b/flap/src/main/java/me/yifeiyuan/flap/Registry.kt @@ -0,0 +1,12 @@ +package me.yifeiyuan.flap + +import me.yifeiyuan.flap.delegate.IAdapterDelegateManager +import me.yifeiyuan.flap.hook.IAdapterHookManager +import me.yifeiyuan.flap.service.IAdapterServiceManager + +/** + * Created by 程序亦非猿 on 2022/11/23. + */ +interface Registry : IAdapterHookManager, IAdapterDelegateManager, IAdapterServiceManager { + +} \ No newline at end of file From b9438f3fa972d96f6b51c7f82b4c5de0fa8c43ff Mon Sep 17 00:00:00 2001 From: Fitz Date: Wed, 23 Nov 2022 21:45:12 +0800 Subject: [PATCH 2/4] Update --- .../flap/paging/FlapPagingDataAdapter.kt | 47 ++++++++++++++++--- flap/src/main/java/me/yifeiyuan/flap/Flap.kt | 11 +++-- .../java/me/yifeiyuan/flap/FlapAdapter.kt | 5 -- 3 files changed, 48 insertions(+), 15 deletions(-) diff --git a/flap-paging/src/main/java/me/yifeiyuan/flap/paging/FlapPagingDataAdapter.kt b/flap-paging/src/main/java/me/yifeiyuan/flap/paging/FlapPagingDataAdapter.kt index 9dc9b7a6..6bae0817 100644 --- a/flap-paging/src/main/java/me/yifeiyuan/flap/paging/FlapPagingDataAdapter.kt +++ b/flap-paging/src/main/java/me/yifeiyuan/flap/paging/FlapPagingDataAdapter.kt @@ -7,17 +7,18 @@ import androidx.recyclerview.widget.RecyclerView import me.yifeiyuan.flap.Component import me.yifeiyuan.flap.Flap import me.yifeiyuan.flap.FlapApi -import me.yifeiyuan.flap.delegate.IAdapterDelegateManager import me.yifeiyuan.flap.ext.SwipeDragHelper -import me.yifeiyuan.flap.hook.IAdapterHookManager -import me.yifeiyuan.flap.service.IAdapterServiceManager import me.yifeiyuan.flap.widget.FlapStickyHeaders +import java.security.MessageDigest +import java.util.* /** * * 支持 Paging * Created by 程序亦非猿 on 2022/11/7. * + * 不支持拖动排序、滑动删除 + * * @since 3.3.0 */ class FlapPagingDataAdapter(private val flap: Flap = Flap(), diffCallback: DiffUtil.ItemCallback, private val flapInitBlock: (FlapApi.() -> Unit)? = null) : PagingDataAdapter>(diffCallback), SwipeDragHelper.Callback, FlapStickyHeaders, FlapApi by flap { @@ -48,16 +49,50 @@ class FlapPagingDataAdapter(private val flap: Flap = Flap(), diffCallba flap.onBindViewHolder(this, getItemData(position) as Any, component, position, payloads) } + override fun onViewRecycled(component: Component) { + flap.onViewRecycled(this, component) + } + + override fun onFailedToRecycleView(component: Component): Boolean { + return flap.onFailedToRecycleView(this,component) + } + override fun onAttachedToRecyclerView(recyclerView: RecyclerView) { - super.onAttachedToRecyclerView(recyclerView) flap.onAttachedToRecyclerView(this, recyclerView) } override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) { - super.onDetachedFromRecyclerView(recyclerView) flap.onDetachedFromRecyclerView(this, recyclerView) } + override fun onViewAttachedToWindow(component: Component) { + flap.onViewAttachedToWindow(this, component) + } + + override fun onViewDetachedFromWindow(component: Component) { + flap.onViewDetachedFromWindow(this, component) + } + +// fun swapData(fromPosition: Int, toPosition: Int, notify: Boolean = true) { +// Collections.swap(data, fromPosition, toPosition) +// if (notify) { +// notifyItemMoved(fromPosition, toPosition) +// } +// } + +// override fun onSwiped(position: Int) { +// val snapshotItems = snapshot().items +// val newList = mutableListOf().apply { +// addAll(snapshotItems) +// } +// newList.removeAt(position) +// submitData() +// } + +// override fun onMoved(fromPosition: Int, toPosition: Int) { +// swapData(fromPosition, toPosition) +// } + var stickyHeaderHandler: ((position: Int, itemData: T?) -> Boolean)? = null fun setupStickyHeaderHandler(block: (position: Int, itemData: T?) -> Boolean) { @@ -69,6 +104,6 @@ class FlapPagingDataAdapter(private val flap: Flap = Flap(), diffCallba } fun getItemData(position: Int): T? { - return peek(position) + return getItem(position) } } \ No newline at end of file diff --git a/flap/src/main/java/me/yifeiyuan/flap/Flap.kt b/flap/src/main/java/me/yifeiyuan/flap/Flap.kt index 52c0aabb..2c306838 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/Flap.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/Flap.kt @@ -216,17 +216,20 @@ class Flap : FlapApi { return delegate.getItemId(itemData, position) } - internal fun onViewRecycled(adapter: RecyclerView.Adapter<*>, component: Component<*>) { + /** + * 会优先于 FlapComponentPool.putRecycledView 被调用 + */ + fun onViewRecycled(adapter: RecyclerView.Adapter<*>, component: Component<*>) { val delegate = getDelegateByViewType(component.itemViewType) delegate.onViewRecycled(adapter, component) } - internal fun onFailedToRecycleView(adapter: RecyclerView.Adapter<*>, component: Component<*>): Boolean { + fun onFailedToRecycleView(adapter: RecyclerView.Adapter<*>, component: Component<*>): Boolean { val delegate = getDelegateByViewType(component.itemViewType) return delegate.onFailedToRecycleView(adapter, component) } - internal fun onViewAttachedToWindow(adapter: RecyclerView.Adapter<*>, component: Component<*>) { + fun onViewAttachedToWindow(adapter: RecyclerView.Adapter<*>, component: Component<*>) { val delegate = getDelegateByViewType(component.itemViewType) delegate.onViewAttachedToWindow(adapter, component) dispatchOnViewAttachedToWindow(adapter, component) @@ -242,7 +245,7 @@ class Flap : FlapApi { } } - internal fun onViewDetachedFromWindow(adapter: RecyclerView.Adapter<*>, component: Component<*>) { + fun onViewDetachedFromWindow(adapter: RecyclerView.Adapter<*>, component: Component<*>) { val delegate = getDelegateByViewType(component.itemViewType) delegate.onViewDetachedFromWindow(adapter, component) dispatchOnViewDetachedFromWindow(adapter, component) diff --git a/flap/src/main/java/me/yifeiyuan/flap/FlapAdapter.kt b/flap/src/main/java/me/yifeiyuan/flap/FlapAdapter.kt index ddff8918..225ca9e4 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/FlapAdapter.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/FlapAdapter.kt @@ -115,18 +115,13 @@ open class FlapAdapter(private val flap: Flap = Flap(), private val flapInitBloc } override fun onAttachedToRecyclerView(recyclerView: RecyclerView) { - super.onAttachedToRecyclerView(recyclerView) flap.onAttachedToRecyclerView(this, recyclerView) } override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) { - super.onDetachedFromRecyclerView(recyclerView) flap.onDetachedFromRecyclerView(this, recyclerView) } - /** - * 会优先于 FlapComponentPool.putRecycledView 被调用 - */ override fun onViewRecycled(component: Component<*>) { flap.onViewRecycled(this, component) } From aa9aaf286536b459a729d8a3a360c0357268edb9 Mon Sep 17 00:00:00 2001 From: Fitz Date: Mon, 28 Nov 2022 16:59:35 +0800 Subject: [PATCH 3/4] rename delegate to isDelegateFor --- .../flapdev/components/SimpleTextComponent.kt | 2 +- .../AdapterDelegateDataBindingDsl.kt | 2 +- .../AdapterDelegateViewBindingDsl.kt | 2 +- .../flap/paging/FlapPagingDataAdapter.kt | 29 +++---------------- flap/src/main/java/me/yifeiyuan/flap/Flap.kt | 6 ++-- .../flap/delegate/AdapterDelegate.kt | 5 ++-- .../flap/delegate/FallbackAdapterDelegate.kt | 2 +- .../yifeiyuan/flap/dsl/AdapterDelegateDsl.kt | 2 +- 8 files changed, 14 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/me/yifeiyuan/flapdev/components/SimpleTextComponent.kt b/app/src/main/java/me/yifeiyuan/flapdev/components/SimpleTextComponent.kt index 944466ba..e94484d2 100644 --- a/app/src/main/java/me/yifeiyuan/flapdev/components/SimpleTextComponent.kt +++ b/app/src/main/java/me/yifeiyuan/flapdev/components/SimpleTextComponent.kt @@ -43,7 +43,7 @@ class SimpleTextComponent(itemView: View) : Component(itemView) //自定义 AdapterDelegate 实现 class SimpleTextComponentDelegate : AdapterDelegate { - override fun delegate(model: Any): Boolean { + override fun isDelegateFor(model: Any): Boolean { return SimpleTextModel::class.java == model::class.java } diff --git a/flap-dsl-databinding/src/main/java/me/yifeiyuan/flap/dsl/databinding/AdapterDelegateDataBindingDsl.kt b/flap-dsl-databinding/src/main/java/me/yifeiyuan/flap/dsl/databinding/AdapterDelegateDataBindingDsl.kt index aeaf72b3..4b70ef8d 100644 --- a/flap-dsl-databinding/src/main/java/me/yifeiyuan/flap/dsl/databinding/AdapterDelegateDataBindingDsl.kt +++ b/flap-dsl-databinding/src/main/java/me/yifeiyuan/flap/dsl/databinding/AdapterDelegateDataBindingDsl.kt @@ -37,7 +37,7 @@ class DataBindingDslAdapterDelegate( private var block: DataBindingDslComponent.() -> Unit, ) : AdapterDelegate> { - override fun delegate(model: Any): Boolean { + override fun isDelegateFor(model: Any): Boolean { return isDelegateFor.invoke(model) } diff --git a/flap-dsl-viewbinding/src/main/java/me/yifeiyuan/flap/dsl/viewbinding/AdapterDelegateViewBindingDsl.kt b/flap-dsl-viewbinding/src/main/java/me/yifeiyuan/flap/dsl/viewbinding/AdapterDelegateViewBindingDsl.kt index 40dffb7e..c42e2385 100644 --- a/flap-dsl-viewbinding/src/main/java/me/yifeiyuan/flap/dsl/viewbinding/AdapterDelegateViewBindingDsl.kt +++ b/flap-dsl-viewbinding/src/main/java/me/yifeiyuan/flap/dsl/viewbinding/AdapterDelegateViewBindingDsl.kt @@ -35,7 +35,7 @@ class ViewBindingDslAdapterDelegate( private var block: ViewBindingDslComponent.() -> Unit, ) : AdapterDelegate> { - override fun delegate(model: Any): Boolean { + override fun isDelegateFor(model: Any): Boolean { return isDelegateFor.invoke(model) } diff --git a/flap-paging/src/main/java/me/yifeiyuan/flap/paging/FlapPagingDataAdapter.kt b/flap-paging/src/main/java/me/yifeiyuan/flap/paging/FlapPagingDataAdapter.kt index 6bae0817..f3e304b5 100644 --- a/flap-paging/src/main/java/me/yifeiyuan/flap/paging/FlapPagingDataAdapter.kt +++ b/flap-paging/src/main/java/me/yifeiyuan/flap/paging/FlapPagingDataAdapter.kt @@ -7,21 +7,18 @@ import androidx.recyclerview.widget.RecyclerView import me.yifeiyuan.flap.Component import me.yifeiyuan.flap.Flap import me.yifeiyuan.flap.FlapApi -import me.yifeiyuan.flap.ext.SwipeDragHelper import me.yifeiyuan.flap.widget.FlapStickyHeaders -import java.security.MessageDigest -import java.util.* /** * * 支持 Paging * Created by 程序亦非猿 on 2022/11/7. * - * 不支持拖动排序、滑动删除 + * 不支持拖动排序、滑动删除 todo 能支持吗? * * @since 3.3.0 */ -class FlapPagingDataAdapter(private val flap: Flap = Flap(), diffCallback: DiffUtil.ItemCallback, private val flapInitBlock: (FlapApi.() -> Unit)? = null) : PagingDataAdapter>(diffCallback), SwipeDragHelper.Callback, FlapStickyHeaders, FlapApi by flap { +class FlapPagingDataAdapter(private val flap: Flap = Flap(), diffCallback: DiffUtil.ItemCallback, private val flapInitBlock: (FlapApi.() -> Unit)? = null) : PagingDataAdapter>(diffCallback), FlapStickyHeaders, FlapApi by flap { init { apply { @@ -46,6 +43,7 @@ class FlapPagingDataAdapter(private val flap: Flap = Flap(), diffCallba position: Int, payloads: MutableList ) { + getItem(position) flap.onBindViewHolder(this, getItemData(position) as Any, component, position, payloads) } @@ -71,27 +69,8 @@ class FlapPagingDataAdapter(private val flap: Flap = Flap(), diffCallba override fun onViewDetachedFromWindow(component: Component) { flap.onViewDetachedFromWindow(this, component) - } -// fun swapData(fromPosition: Int, toPosition: Int, notify: Boolean = true) { -// Collections.swap(data, fromPosition, toPosition) -// if (notify) { -// notifyItemMoved(fromPosition, toPosition) -// } -// } - -// override fun onSwiped(position: Int) { -// val snapshotItems = snapshot().items -// val newList = mutableListOf().apply { -// addAll(snapshotItems) -// } -// newList.removeAt(position) -// submitData() -// } - -// override fun onMoved(fromPosition: Int, toPosition: Int) { -// swapData(fromPosition, toPosition) -// } + } var stickyHeaderHandler: ((position: Int, itemData: T?) -> Boolean)? = null diff --git a/flap/src/main/java/me/yifeiyuan/flap/Flap.kt b/flap/src/main/java/me/yifeiyuan/flap/Flap.kt index 2c306838..dc317a29 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/Flap.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/Flap.kt @@ -36,7 +36,7 @@ class Flap : FlapApi { private const val TAG = "Flap" /** - * 当 Adapter.data 中存在一个 Model 没有对应的 AdapterDelegate.delegate()==true 时抛出 + * 当 Adapter.data 中存在一个 Model 没有对应的 AdapterDelegate 时抛出 */ internal class AdapterDelegateNotFoundException(errorMessage: String) : Exception(errorMessage) } @@ -52,7 +52,7 @@ class Flap : FlapApi { private val viewTypeDelegateCache: MutableMap?> = mutableMapOf() private val delegateViewTypeCache: MutableMap, Int> = mutableMapOf() - var fallbackDelegate: FallbackAdapterDelegate? = null + var fallbackDelegate: FallbackAdapterDelegate /** * 是否使用 ApplicationContext 来创建 LayoutInflater 来创建 View @@ -184,7 +184,7 @@ class Flap : FlapApi { var itemViewType: Int val delegate: AdapterDelegate<*, *>? = adapterDelegates.firstOrNull { - it.delegate(itemData) + it.isDelegateFor(itemData) } ?: fallbackDelegate if (delegateViewTypeCache.containsKey(delegate)) { diff --git a/flap/src/main/java/me/yifeiyuan/flap/delegate/AdapterDelegate.kt b/flap/src/main/java/me/yifeiyuan/flap/delegate/AdapterDelegate.kt index 9290717b..c8278570 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/delegate/AdapterDelegate.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/delegate/AdapterDelegate.kt @@ -5,13 +5,12 @@ import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import me.yifeiyuan.flap.Component import me.yifeiyuan.flap.Flap -import java.lang.reflect.ParameterizedType /** * * AdapterDelegate 是一个代理类,负责代理 Adapter 的部分方法。 * - * 每个 model 都应该有一个对应的 AdapterDelegate,当一个 AdapterDelegate.delegate(model) 返回 true 表示它负责代理这个 model。 + * 每个 model 都应该有一个对应的 AdapterDelegate,当一个 AdapterDelegate.isDelegateFor(model) 返回 true 表示它负责代理这个 model。 * 一般一个 AdapterDelegate 代理一个类型的 Model,是一对一的关系,这样更加解耦。 * 这样可以解耦 Adapter 对不同类型的 Model 的处理。 * @@ -39,7 +38,7 @@ interface AdapterDelegate> { * @param model 数据 * @return true 表示代理该 model */ - fun delegate(model: Any): Boolean + fun isDelegateFor(model: Any): Boolean /** * 根据代理的 Model 创建与之对应的 Component diff --git a/flap/src/main/java/me/yifeiyuan/flap/delegate/FallbackAdapterDelegate.kt b/flap/src/main/java/me/yifeiyuan/flap/delegate/FallbackAdapterDelegate.kt index 6c1ce502..a9278409 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/delegate/FallbackAdapterDelegate.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/delegate/FallbackAdapterDelegate.kt @@ -29,7 +29,7 @@ import me.yifeiyuan.flap.FlapDebug * @since 3.0.0 */ abstract class FallbackAdapterDelegate : AdapterDelegate> { - override fun delegate(model: Any): Boolean { + override fun isDelegateFor(model: Any): Boolean { return true } } diff --git a/flap/src/main/java/me/yifeiyuan/flap/dsl/AdapterDelegateDsl.kt b/flap/src/main/java/me/yifeiyuan/flap/dsl/AdapterDelegateDsl.kt index 29e50003..4abffcb1 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/dsl/AdapterDelegateDsl.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/dsl/AdapterDelegateDsl.kt @@ -48,7 +48,7 @@ class DslAdapterDelegate( private var block: DslComponent.() -> Unit, ) : AdapterDelegate> { - override fun delegate(model: Any): Boolean { + override fun isDelegateFor(model: Any): Boolean { return isDelegateFor.invoke(model) } From e14dea106a391d85047b70d5644326bcb696c60c Mon Sep 17 00:00:00 2001 From: Fitz Date: Mon, 28 Nov 2022 17:16:54 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BC=98=E5=8C=96=20API=20=E8=AE=BE?= =?UTF-8?q?=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flapdev/testcases/BaseTestcaseFragment.kt | 2 +- flap/src/main/java/me/yifeiyuan/flap/Flap.kt | 21 +++++-------------- .../main/java/me/yifeiyuan/flap/FlapApi.kt | 6 +++--- .../java/me/yifeiyuan/flap/FlapInitializer.kt | 10 ++------- .../flap/{Registry.kt => FlapRegistry.kt} | 5 ++++- .../me/yifeiyuan/flap/pool/ComponentPool.kt | 2 +- 6 files changed, 16 insertions(+), 30 deletions(-) rename flap/src/main/java/me/yifeiyuan/flap/{Registry.kt => FlapRegistry.kt} (62%) diff --git a/app/src/main/java/me/yifeiyuan/flapdev/testcases/BaseTestcaseFragment.kt b/app/src/main/java/me/yifeiyuan/flapdev/testcases/BaseTestcaseFragment.kt index 133ea5f3..fd839376 100644 --- a/app/src/main/java/me/yifeiyuan/flapdev/testcases/BaseTestcaseFragment.kt +++ b/app/src/main/java/me/yifeiyuan/flapdev/testcases/BaseTestcaseFragment.kt @@ -107,7 +107,7 @@ open class BaseTestcaseFragment : Fragment(), Scrollable, IMenuView { } } - adapter.setParamProvider { + adapter.withParamProvider { when (it) { "intValue" -> { 233 diff --git a/flap/src/main/java/me/yifeiyuan/flap/Flap.kt b/flap/src/main/java/me/yifeiyuan/flap/Flap.kt index dc317a29..b71109f4 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/Flap.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/Flap.kt @@ -34,18 +34,10 @@ class Flap : FlapApi { companion object { private const val TAG = "Flap" - - /** - * 当 Adapter.data 中存在一个 Model 没有对应的 AdapterDelegate 时抛出 - */ - internal class AdapterDelegateNotFoundException(errorMessage: String) : Exception(errorMessage) } /** - * Components 监听的生命周期对象,一般是 Activity - * 默认取的是 RecyclerView.Context - * - * 如果使用 Fragment 那么需要自行设置 + * 默认使用 RecyclerView.Context,如果在 Fragment 里使用,则需要设置为 Fragment.viewLifecycleOwner */ private var lifecycleOwner: LifecycleOwner? = null @@ -109,7 +101,6 @@ class Flap : FlapApi { private fun getDelegateByViewType(viewType: Int): AdapterDelegate<*, *> { return viewTypeDelegateCache[viewType] ?: fallbackDelegate - ?: throw AdapterDelegateNotFoundException("找不到 viewType = $viewType 对应的 Delegate,请先注册,或设置默认的 Delegate") } fun onBindViewHolder( @@ -132,7 +123,6 @@ class Flap : FlapApi { dispatchOnBindViewHolderEnd(adapter, component, itemData, position, payloads) tryAttachLifecycleOwner(component) } catch (e: Exception) { - e.printStackTrace() FlapDebug.e(TAG, "onBindViewHolder: Error = ", e) } } @@ -183,7 +173,7 @@ class Flap : FlapApi { var itemViewType: Int - val delegate: AdapterDelegate<*, *>? = adapterDelegates.firstOrNull { + val delegate: AdapterDelegate<*, *> = adapterDelegates.firstOrNull { it.isDelegateFor(itemData) } ?: fallbackDelegate @@ -191,8 +181,7 @@ class Flap : FlapApi { return delegateViewTypeCache[delegate]!! } - itemViewType = delegate?.getItemViewType(itemData) - ?: throw AdapterDelegateNotFoundException("找不到对应的 AdapterDelegate,请先注册或设置默认 AdapterDelegate ,position=$position , itemData=$itemData") + itemViewType = delegate.getItemViewType(itemData) if (itemViewType == 0) { itemViewType = generateItemViewType() @@ -422,7 +411,7 @@ class Flap : FlapApi { return paramProvider?.getParam(key) as? P? } - override fun setParamProvider(block: (key: String) -> Any?) = apply { + override fun withParamProvider(block: (key: String) -> Any?) = apply { paramProvider = ExtraParamsProviderWrapper(block) } @@ -437,7 +426,7 @@ class Flap : FlapApi { /** * 设置是否使用 ComponentPool 作为缓存池 */ - override fun withComponentPoolEnable(enable: Boolean) = apply { + override fun setComponentPoolEnable(enable: Boolean) = apply { useComponentPool = enable } diff --git a/flap/src/main/java/me/yifeiyuan/flap/FlapApi.kt b/flap/src/main/java/me/yifeiyuan/flap/FlapApi.kt index 9c90d281..0e08c54a 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/FlapApi.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/FlapApi.kt @@ -15,7 +15,7 @@ import me.yifeiyuan.flap.hook.PreloadHook * Created by 程序亦非猿 on 2022/11/3. * @since 3.3.0 */ -interface FlapApi : Registry { +interface FlapApi : FlapRegistry { /** * 通过 Adapter 发送事件 @@ -45,7 +45,7 @@ interface FlapApi : Registry { /** * 设置是否使用 ComponentPool 作为缓存池 */ - fun withComponentPoolEnable(enable: Boolean): FlapApi + fun setComponentPoolEnable(enable: Boolean): FlapApi /** * 预加载 @@ -78,7 +78,7 @@ interface FlapApi : Registry { fun getEmptyViewHelper(): EmptyViewHelper - fun setParamProvider(block: (key: String) -> Any?): FlapApi + fun withParamProvider(block: (key: String) -> Any?): FlapApi /** * 提供 Component 从 Adapter 获取参数的方法 diff --git a/flap/src/main/java/me/yifeiyuan/flap/FlapInitializer.kt b/flap/src/main/java/me/yifeiyuan/flap/FlapInitializer.kt index ba79872e..43f94866 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/FlapInitializer.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/FlapInitializer.kt @@ -2,18 +2,12 @@ package me.yifeiyuan.flap import android.content.Context import me.yifeiyuan.flap.delegate.* -import me.yifeiyuan.flap.delegate.AdapterDelegateManager import me.yifeiyuan.flap.delegate.DefaultFallbackAdapterDelegate -import me.yifeiyuan.flap.delegate.IAdapterDelegateManager import me.yifeiyuan.flap.hook.AdapterHook -import me.yifeiyuan.flap.hook.AdapterHookManager -import me.yifeiyuan.flap.hook.IAdapterHookManager import me.yifeiyuan.flap.service.AdapterService -import me.yifeiyuan.flap.service.AdapterServiceManager -import me.yifeiyuan.flap.service.IAdapterServiceManager /** - * FlapInitializer 是 Flap 的初始化器,存放全局的配置,会应用于所有的 FlapAdapter 实例。 + * FlapInitializer ,初始化器,存放全局的配置,会应用于所有的 Flap 实例。 * * - AdapterDelegate * - AdapterHook @@ -26,7 +20,7 @@ import me.yifeiyuan.flap.service.IAdapterServiceManager * @since 2020/9/22 * @since 3.0.0 */ -object FlapInitializer : Registry { +object FlapInitializer : FlapRegistry { override val adapterDelegates: MutableList> = mutableListOf() override val adapterHooks: MutableList = mutableListOf() diff --git a/flap/src/main/java/me/yifeiyuan/flap/Registry.kt b/flap/src/main/java/me/yifeiyuan/flap/FlapRegistry.kt similarity index 62% rename from flap/src/main/java/me/yifeiyuan/flap/Registry.kt rename to flap/src/main/java/me/yifeiyuan/flap/FlapRegistry.kt index b5a447cc..956d1834 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/Registry.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/FlapRegistry.kt @@ -5,8 +5,11 @@ import me.yifeiyuan.flap.hook.IAdapterHookManager import me.yifeiyuan.flap.service.IAdapterServiceManager /** + * 统一注册中心接口抽象 + * * Created by 程序亦非猿 on 2022/11/23. + * @since 3.3.0 */ -interface Registry : IAdapterHookManager, IAdapterDelegateManager, IAdapterServiceManager { +interface FlapRegistry : IAdapterHookManager, IAdapterDelegateManager, IAdapterServiceManager { } \ No newline at end of file diff --git a/flap/src/main/java/me/yifeiyuan/flap/pool/ComponentPool.kt b/flap/src/main/java/me/yifeiyuan/flap/pool/ComponentPool.kt index 28aa264e..280cf5ad 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/pool/ComponentPool.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/pool/ComponentPool.kt @@ -9,7 +9,7 @@ import me.yifeiyuan.flap.FlapDebug /** * 自定义的 RecycledViewPool,实现了 ComponentCallbacks2 ,可以在内存不足的时候清理缓存 * - * @see me.yifeiyuan.flap.Flap.withComponentPoolEnable 设置开关 + * @see me.yifeiyuan.flap.Flap.setComponentPoolEnable 设置开关 * * Created by 程序亦非猿 on 2021/9/22. *