Skip to content

Commit

Permalink
优化 API 设计
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanCheen committed Nov 28, 2022
1 parent aa9aaf2 commit e14dea1
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ open class BaseTestcaseFragment : Fragment(), Scrollable, IMenuView {
}
}

adapter.setParamProvider {
adapter.withParamProvider {
when (it) {
"intValue" -> {
233
Expand Down
21 changes: 5 additions & 16 deletions flap/src/main/java/me/yifeiyuan/flap/Flap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -183,16 +173,15 @@ class Flap : FlapApi {

var itemViewType: Int

val delegate: AdapterDelegate<*, *>? = adapterDelegates.firstOrNull {
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()
Expand Down Expand Up @@ -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)
}

Expand All @@ -437,7 +426,7 @@ class Flap : FlapApi {
/**
* 设置是否使用 ComponentPool 作为缓存池
*/
override fun withComponentPoolEnable(enable: Boolean) = apply {
override fun setComponentPoolEnable(enable: Boolean) = apply {
useComponentPool = enable
}

Expand Down
6 changes: 3 additions & 3 deletions flap/src/main/java/me/yifeiyuan/flap/FlapApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 发送事件
Expand Down Expand Up @@ -45,7 +45,7 @@ interface FlapApi : Registry {
/**
* 设置是否使用 ComponentPool 作为缓存池
*/
fun withComponentPoolEnable(enable: Boolean): FlapApi
fun setComponentPoolEnable(enable: Boolean): FlapApi

/**
* 预加载
Expand Down Expand Up @@ -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 获取参数的方法
Expand Down
10 changes: 2 additions & 8 deletions flap/src/main/java/me/yifeiyuan/flap/FlapInitializer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<AdapterDelegate<*, *>> = mutableListOf()
override val adapterHooks: MutableList<AdapterHook> = mutableListOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

}
2 changes: 1 addition & 1 deletion flap/src/main/java/me/yifeiyuan/flap/pool/ComponentPool.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit e14dea1

Please sign in to comment.