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<SimpleTextModel>(itemView) //自定义 AdapterDelegate 实现 class SimpleTextComponentDelegate : AdapterDelegate<SimpleTextModel, SimpleTextComponent> { - override fun delegate(model: Any): Boolean { + override fun isDelegateFor(model: Any): Boolean { return SimpleTextModel::class.java == model::class.java } 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-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<T, D : ViewDataBinding>( private var block: DataBindingDslComponent<T, D>.() -> Unit, ) : AdapterDelegate<T, DataBindingDslComponent<T, D>> { - 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<T, V : ViewBinding>( private var block: ViewBindingDslComponent<T, V>.() -> Unit, ) : AdapterDelegate<T, ViewBindingDslComponent<T, V>> { - 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 7aaa312a..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,10 +7,6 @@ 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 /** @@ -18,9 +14,11 @@ import me.yifeiyuan.flap.widget.FlapStickyHeaders * 支持 Paging * Created by 程序亦非猿 on 2022/11/7. * + * 不支持拖动排序、滑动删除 todo 能支持吗? + * * @since 3.3.0 */ -class FlapPagingDataAdapter<T : Any>(private val flap: Flap = Flap(), diffCallback: DiffUtil.ItemCallback<T>, private val flapInitBlock: (Flap.() -> Unit)? = null) : PagingDataAdapter<T, Component<T>>(diffCallback), IAdapterHookManager by flap, IAdapterDelegateManager by flap, IAdapterServiceManager by flap, SwipeDragHelper.Callback, FlapStickyHeaders, FlapApi by flap { +class FlapPagingDataAdapter<T : Any>(private val flap: Flap = Flap(), diffCallback: DiffUtil.ItemCallback<T>, private val flapInitBlock: (FlapApi.() -> Unit)? = null) : PagingDataAdapter<T, Component<T>>(diffCallback), FlapStickyHeaders, FlapApi by flap { init { apply { @@ -45,19 +43,35 @@ class FlapPagingDataAdapter<T : Any>(private val flap: Flap = Flap(), diffCallba position: Int, payloads: MutableList<Any> ) { + getItem(position) flap.onBindViewHolder(this, getItemData(position) as Any, component, position, payloads) } + override fun onViewRecycled(component: Component<T>) { + flap.onViewRecycled(this, component) + } + + override fun onFailedToRecycleView(component: Component<T>): 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<T>) { + flap.onViewAttachedToWindow(this, component) + } + + override fun onViewDetachedFromWindow(component: Component<T>) { + flap.onViewDetachedFromWindow(this, component) + + } + var stickyHeaderHandler: ((position: Int, itemData: T?) -> Boolean)? = null fun setupStickyHeaderHandler(block: (position: Int, itemData: T?) -> Boolean) { @@ -69,6 +83,6 @@ class FlapPagingDataAdapter<T : Any>(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 53b7a8de..b71109f4 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,29 +26,25 @@ 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<AdapterDelegate<*, *>> = mutableListOf() + override val adapterHooks: MutableList<AdapterHook> = mutableListOf() + override val adapterServices: MutableMap<Class<*>, AdapterService> = mutableMapOf() companion object { private const val TAG = "Flap" - - /** - * 当 Adapter.data 中存在一个 Model 没有对应的 AdapterDelegate.delegate()==true 时抛出 - */ - internal class AdapterDelegateNotFoundException(errorMessage: String) : Exception(errorMessage) } /** - * Components 监听的生命周期对象,一般是 Activity - * 默认取的是 RecyclerView.Context - * - * 如果使用 Fragment 那么需要自行设置 + * 默认使用 RecyclerView.Context,如果在 Fragment 里使用,则需要设置为 Fragment.viewLifecycleOwner */ private var lifecycleOwner: LifecycleOwner? = null private val viewTypeDelegateCache: MutableMap<Int, AdapterDelegate<*, *>?> = mutableMapOf() private val delegateViewTypeCache: MutableMap<AdapterDelegate<*, *>, Int> = mutableMapOf() - var fallbackDelegate: FallbackAdapterDelegate? = null + var fallbackDelegate: FallbackAdapterDelegate /** * 是否使用 ApplicationContext 来创建 LayoutInflater 来创建 View @@ -109,7 +101,6 @@ class Flap : IAdapterHookManager by AdapterHookManager(), IAdapterDelegateManage private fun getDelegateByViewType(viewType: Int): AdapterDelegate<*, *> { return viewTypeDelegateCache[viewType] ?: fallbackDelegate - ?: throw AdapterDelegateNotFoundException("找不到 viewType = $viewType 对应的 Delegate,请先注册,或设置默认的 Delegate") } fun onBindViewHolder( @@ -132,7 +123,6 @@ class Flap : IAdapterHookManager by AdapterHookManager(), IAdapterDelegateManage dispatchOnBindViewHolderEnd(adapter, component, itemData, position, payloads) tryAttachLifecycleOwner(component) } catch (e: Exception) { - e.printStackTrace() FlapDebug.e(TAG, "onBindViewHolder: Error = ", e) } } @@ -183,16 +173,15 @@ class Flap : IAdapterHookManager by AdapterHookManager(), IAdapterDelegateManage var itemViewType: Int - val delegate: AdapterDelegate<*, *>? = adapterDelegates.firstOrNull { - it.delegate(itemData) + val delegate: AdapterDelegate<*, *> = adapterDelegates.firstOrNull { + it.isDelegateFor(itemData) } ?: fallbackDelegate if (delegateViewTypeCache.containsKey(delegate)) { return delegateViewTypeCache[delegate]!! } - itemViewType = delegate?.getItemViewType(itemData) - ?: throw AdapterDelegateNotFoundException("找不到对应的 AdapterDelegate,请先注册或设置默认 AdapterDelegate ,position=$position , itemData=$itemData") + itemViewType = delegate.getItemViewType(itemData) if (itemViewType == 0) { itemViewType = generateItemViewType() @@ -216,17 +205,20 @@ class Flap : IAdapterHookManager by AdapterHookManager(), IAdapterDelegateManage 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 +234,7 @@ class Flap : IAdapterHookManager by AdapterHookManager(), IAdapterDelegateManage } } - 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) @@ -419,7 +411,7 @@ class Flap : IAdapterHookManager by AdapterHookManager(), IAdapterDelegateManage 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) } @@ -434,7 +426,7 @@ class Flap : IAdapterHookManager by AdapterHookManager(), IAdapterDelegateManage /** * 设置是否使用 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/FlapAdapter.kt b/flap/src/main/java/me/yifeiyuan/flap/FlapAdapter.kt index fafdf300..225ca9e4 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<Component<*>>(), 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<Component<*>>(), SwipeDragHelper.Callback, FlapStickyHeaders, FlapApi by flap { companion object { private const val TAG = "FlapAdapter" @@ -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) } diff --git a/flap/src/main/java/me/yifeiyuan/flap/FlapApi.kt b/flap/src/main/java/me/yifeiyuan/flap/FlapApi.kt index 07b1c9a4..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 { +interface FlapApi : FlapRegistry { /** * 通过 Adapter 发送事件 @@ -45,7 +45,7 @@ interface FlapApi { /** * 设置是否使用 ComponentPool 作为缓存池 */ - fun withComponentPoolEnable(enable: Boolean): FlapApi + fun setComponentPoolEnable(enable: Boolean): FlapApi /** * 预加载 @@ -78,7 +78,7 @@ interface FlapApi { 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 e14ebf34..43f94866 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/FlapInitializer.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/FlapInitializer.kt @@ -2,16 +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.AdapterHookManager -import me.yifeiyuan.flap.hook.IAdapterHookManager -import me.yifeiyuan.flap.service.AdapterServiceManager -import me.yifeiyuan.flap.service.IAdapterServiceManager +import me.yifeiyuan.flap.hook.AdapterHook +import me.yifeiyuan.flap.service.AdapterService /** - * FlapInitializer 是 Flap 的初始化器,存放全局的配置,会应用于所有的 FlapAdapter 实例。 + * FlapInitializer ,初始化器,存放全局的配置,会应用于所有的 Flap 实例。 * * - AdapterDelegate * - AdapterHook @@ -24,7 +20,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 : FlapRegistry { + + override val adapterDelegates: MutableList<AdapterDelegate<*, *>> = mutableListOf() + override val adapterHooks: MutableList<AdapterHook> = mutableListOf() + override val adapterServices: MutableMap<Class<*>, AdapterService> = mutableMapOf() /** * 是否使用 application context 来创建 Component diff --git a/flap/src/main/java/me/yifeiyuan/flap/FlapRegistry.kt b/flap/src/main/java/me/yifeiyuan/flap/FlapRegistry.kt new file mode 100644 index 00000000..956d1834 --- /dev/null +++ b/flap/src/main/java/me/yifeiyuan/flap/FlapRegistry.kt @@ -0,0 +1,15 @@ +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. + * @since 3.3.0 + */ +interface FlapRegistry : IAdapterHookManager, IAdapterDelegateManager, IAdapterServiceManager { + +} \ No newline at end of file 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<M, VH : Component<M>> { * @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<Any, Component<Any>> { - 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<T>( private var block: DslComponent<T>.() -> Unit, ) : AdapterDelegate<T, DslComponent<T>> { - override fun delegate(model: Any): Boolean { + override fun isDelegateFor(model: Any): Boolean { return isDelegateFor.invoke(model) } 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. *