diff --git a/flap/src/main/java/me/yifeiyuan/flap/Component.kt b/flap/src/main/java/me/yifeiyuan/flap/Component.kt index 0ed37820..a1c9bf36 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/Component.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/Component.kt @@ -5,7 +5,6 @@ package me.yifeiyuan.flap import android.content.Context import android.content.res.ColorStateList import android.graphics.drawable.Drawable -import android.os.Build import android.view.View import androidx.annotation.* import androidx.core.content.ContextCompat diff --git a/flap/src/main/java/me/yifeiyuan/flap/Flap.kt b/flap/src/main/java/me/yifeiyuan/flap/Flap.kt index caa16980..1e3211aa 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/Flap.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/Flap.kt @@ -4,9 +4,15 @@ import android.content.ComponentCallbacks2 import android.content.Context import android.content.res.Configuration 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.hook.AdapterHook +import me.yifeiyuan.flap.hook.AdapterHookManager +import me.yifeiyuan.flap.hook.IAdapterHookManager import me.yifeiyuan.flap.pool.ComponentPool +import me.yifeiyuan.flap.service.AdapterServiceManager +import me.yifeiyuan.flap.service.IAdapterServiceManager /** * Flap 存放全局的配置 @@ -18,7 +24,7 @@ import me.yifeiyuan.flap.pool.ComponentPool * @since 2020/9/22 * @since 3.0.0 */ -object Flap : ComponentCallbacks2, IRegistry { +object Flap : ComponentCallbacks2, IAdapterHookManager by AdapterHookManager(), IAdapterDelegateManager by AdapterDelegateManager(), IAdapterServiceManager by AdapterServiceManager() { /** * 是否使用 application context 来创建 Component @@ -29,46 +35,10 @@ object Flap : ComponentCallbacks2, IRegistry { internal val globalComponentPool = ComponentPool() - internal val globalAdapterDelegates: MutableList> = mutableListOf() - internal var globalDefaultAdapterDelegate: AdapterDelegate<*, *>? = FallbackAdapterDelegate() - internal val globalHooks: MutableList = mutableListOf() - var applicationContext: Context? = null - override fun registerAdapterHook(adapterHook: AdapterHook) { - globalHooks.add(adapterHook) - } - - override fun registerAdapterHooks(vararg adapterHooks: AdapterHook) { - globalHooks.addAll(adapterHooks) - } - - override fun unregisterAdapterHook(adapterHook: AdapterHook) { - globalHooks.remove(adapterHook) - } - - override fun clearAdapterHooks() { - globalHooks.clear() - } - - override fun registerAdapterDelegate(adapterDelegate: AdapterDelegate<*, *>) { - globalAdapterDelegates.add(adapterDelegate) - } - - override fun registerAdapterDelegates(vararg adapterDelegates: AdapterDelegate<*, *>) { - globalAdapterDelegates.addAll(adapterDelegates) - } - - override fun unregisterAdapterDelegate(adapterDelegate: AdapterDelegate<*, *>) { - globalAdapterDelegates.remove(adapterDelegate) - } - - override fun clearAdapterDelegates() { - globalAdapterDelegates.clear() - } - override fun onTrimMemory(level: Int) { globalComponentPool.onTrimMemory(level) } diff --git a/flap/src/main/java/me/yifeiyuan/flap/FlapAdapter.kt b/flap/src/main/java/me/yifeiyuan/flap/FlapAdapter.kt index 33101167..85e30302 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/FlapAdapter.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/FlapAdapter.kt @@ -9,15 +9,17 @@ 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.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.AdapterHook +import me.yifeiyuan.flap.hook.AdapterHookManager +import me.yifeiyuan.flap.hook.IAdapterHookManager import me.yifeiyuan.flap.hook.PreloadHook import me.yifeiyuan.flap.pool.ComponentPool import java.util.* -import me.yifeiyuan.flap.service.AdapterService import me.yifeiyuan.flap.service.IAdapterServiceManager import me.yifeiyuan.flap.service.AdapterServiceManager @@ -32,7 +34,7 @@ import me.yifeiyuan.flap.service.AdapterServiceManager * @since 2020/9/22 * @since 3.0.0 */ -open class FlapAdapter : RecyclerView.Adapter>(), IRegistry, IAdapterServiceManager, SwipeDragHelper.Callback { +open class FlapAdapter : RecyclerView.Adapter>(), IAdapterHookManager by AdapterHookManager(), IAdapterDelegateManager by AdapterDelegateManager(), IAdapterServiceManager by AdapterServiceManager(), SwipeDragHelper.Callback { companion object { private const val TAG = "FlapAdapter" @@ -66,14 +68,10 @@ open class FlapAdapter : RecyclerView.Adapter>(), IRegistry, IAdapt */ private var defaultAdapterDelegate: AdapterDelegate<*, *>? = null - private val adapterDelegates: MutableList> = mutableListOf() - - private val adapterHooks: MutableList = mutableListOf() - /** * RecyclerView 滑动到底部触发预加载 */ - var preloadHook: PreloadHook? = null + private var preloadHook: PreloadHook? = null private val viewTypeDelegateCache: MutableMap?> = mutableMapOf() private val delegateViewTypeCache: MutableMap, Int> = mutableMapOf() @@ -105,11 +103,12 @@ open class FlapAdapter : RecyclerView.Adapter>(), IRegistry, IAdapt lateinit var bindingRecyclerView: RecyclerView lateinit var bindingContext: Context - private val serviceManager = AdapterServiceManager() - init { - adapterHooks.addAll(Flap.globalHooks) - adapterDelegates.addAll(Flap.globalAdapterDelegates) + adapterHooks.addAll(Flap.adapterHooks) + adapterDelegates.addAll(Flap.adapterDelegates) + adapterServices.putAll(Flap.adapterServices) + namedAdapterServices.putAll(Flap.namedAdapterServices) + Flap.globalDefaultAdapterDelegate?.let { defaultAdapterDelegate = it } @@ -117,40 +116,6 @@ open class FlapAdapter : RecyclerView.Adapter>(), IRegistry, IAdapt inflateWithApplicationContext = Flap.inflateWithApplicationContext } - override fun registerAdapterHook(adapterHook: AdapterHook) { - adapterHooks.add(adapterHook) - } - - override fun registerAdapterHooks(vararg adapterHooks: AdapterHook) { - this.adapterHooks.addAll(adapterHooks) - } - - override fun unregisterAdapterHook(adapterHook: AdapterHook) { - adapterHooks.remove(adapterHook) - } - - override fun clearAdapterHooks() { - adapterHooks.clear() - } - - override fun registerAdapterDelegate(adapterDelegate: AdapterDelegate<*, *>) { - adapterDelegates.add(adapterDelegate) - } - - override fun registerAdapterDelegates(vararg delegates: AdapterDelegate<*, *>) { - delegates.forEach { - registerAdapterDelegate(it) - } - } - - override fun unregisterAdapterDelegate(adapterDelegate: AdapterDelegate<*, *>) { - adapterDelegates.remove(adapterDelegate) - } - - override fun clearAdapterDelegates() { - adapterDelegates.clear() - } - open fun setData(newDataList: MutableList) { data.clear() data.addAll(newDataList) @@ -535,30 +500,6 @@ open class FlapAdapter : RecyclerView.Adapter>(), IRegistry, IAdapt emptyViewHelper.emptyView = emptyView } - override fun registerAdapterService(serviceClass: Class) { - serviceManager.registerAdapterService(serviceClass) - } - - override fun registerAdapterService(serviceClass: Class, service: T) { - serviceManager.registerAdapterService(serviceClass, service) - } - - override fun getAdapterService(serviceClass: Class): T? { - return serviceManager.getAdapterService(serviceClass) - } - - override fun registerAdapterService(serviceName: String, serviceClass: Class) { - serviceManager.registerAdapterService(serviceName, serviceClass) - } - - override fun registerAdapterService(serviceName: String, service: T) { - serviceManager.registerAdapterService(serviceName, service) - } - - override fun getAdapterService(serviceName: String): T? { - return serviceManager.getAdapterService(serviceName) - } - fun insertDataAt(position: Int, element: Any, notify: Boolean = true) { this.data.add(position, element) if (notify) { diff --git a/flap/src/main/java/me/yifeiyuan/flap/decoration/LinearItemDecoration.kt b/flap/src/main/java/me/yifeiyuan/flap/decoration/LinearItemDecoration.kt index cd644908..8a92d383 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/decoration/LinearItemDecoration.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/decoration/LinearItemDecoration.kt @@ -6,6 +6,7 @@ import android.graphics.Rect import android.graphics.drawable.Drawable import android.view.View import androidx.recyclerview.widget.RecyclerView +import kotlin.math.roundToInt /** * 可以绘制 Drawable 的 ItemDecoration,可以设置间隔的颜色和大小 @@ -76,7 +77,7 @@ class LinearItemDecoration : RecyclerView.ItemDecoration { for (i in 0 until childCount) { val child: View = parent.getChildAt(i) parent.getDecoratedBoundsWithMargins(child, bounds) - val bottom: Int = bounds.bottom + Math.round(child.translationY) + val bottom: Int = bounds.bottom + child.translationY.roundToInt() var top: Int = bottom - drawable.intrinsicHeight if (isIncludeFirstItemTopEdge && i == 0) { top = 0 @@ -104,7 +105,7 @@ class LinearItemDecoration : RecyclerView.ItemDecoration { for (i in 0 until childCount) { val child: View = parent.getChildAt(i) parent.layoutManager?.getDecoratedBoundsWithMargins(child, bounds) - val right: Int = bounds.right + Math.round(child.translationX) + val right: Int = bounds.right + child.translationX.roundToInt() var left: Int = right - drawable.intrinsicWidth if (isIncludeFirstItemTopEdge && i == 0) { left = 0 diff --git a/flap/src/main/java/me/yifeiyuan/flap/decoration/LinearSpaceItemDecoration.kt b/flap/src/main/java/me/yifeiyuan/flap/decoration/LinearSpaceItemDecoration.kt index 16109627..d5eea1d0 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/decoration/LinearSpaceItemDecoration.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/decoration/LinearSpaceItemDecoration.kt @@ -45,9 +45,7 @@ class LinearSpaceItemDecoration(var space: Int, var orientation: Int = RecyclerV val adapterSize = parent.adapter?.itemCount ?: 0 - val layoutManager = parent.layoutManager - - when (layoutManager) { + when (val layoutManager = parent.layoutManager) { is LinearLayoutManager -> { val isFirst = position == 0 diff --git a/flap/src/main/java/me/yifeiyuan/flap/decoration/SpaceItemDecoration.kt b/flap/src/main/java/me/yifeiyuan/flap/decoration/SpaceItemDecoration.kt index 2547932b..886c1c5c 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/decoration/SpaceItemDecoration.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/decoration/SpaceItemDecoration.kt @@ -40,9 +40,7 @@ class SpaceItemDecoration( val adapterPos = parent.getChildAdapterPosition(view) val adapterSize = parent.adapter?.itemCount ?: 0 - val layoutManager = parent.layoutManager - - when (layoutManager) { + when (val layoutManager = parent.layoutManager) { is StaggeredGridLayoutManager -> { val layoutParams = view.layoutParams as StaggeredGridLayoutManager.LayoutParams diff --git a/flap/src/main/java/me/yifeiyuan/flap/delegate/AdapterDelegateManager.kt b/flap/src/main/java/me/yifeiyuan/flap/delegate/AdapterDelegateManager.kt new file mode 100644 index 00000000..d093b566 --- /dev/null +++ b/flap/src/main/java/me/yifeiyuan/flap/delegate/AdapterDelegateManager.kt @@ -0,0 +1,10 @@ +package me.yifeiyuan.flap.delegate + +/** + * Created by 程序亦非猿 on 2022/9/5. + * + * @since 3.1.0 + */ +class AdapterDelegateManager :IAdapterDelegateManager{ + override val adapterDelegates: MutableList> = mutableListOf() +} \ No newline at end of file diff --git a/flap/src/main/java/me/yifeiyuan/flap/delegate/IAdapterDelegateManager.kt b/flap/src/main/java/me/yifeiyuan/flap/delegate/IAdapterDelegateManager.kt new file mode 100644 index 00000000..deb4c9ee --- /dev/null +++ b/flap/src/main/java/me/yifeiyuan/flap/delegate/IAdapterDelegateManager.kt @@ -0,0 +1,29 @@ +package me.yifeiyuan.flap.delegate + +import androidx.annotation.NonNull + +/** + * Created by 程序亦非猿 on 2022/9/5. + * + * @since 3.1.0 + */ +interface IAdapterDelegateManager { + + val adapterDelegates: MutableList> + + fun registerAdapterDelegate(@NonNull adapterDelegate: AdapterDelegate<*,*>){ + this.adapterDelegates.add(adapterDelegate) + } + + fun registerAdapterDelegates(vararg adapterDelegates: AdapterDelegate<*, *>){ + this.adapterDelegates.addAll(adapterDelegates) + } + + fun unregisterAdapterDelegate(@NonNull adapterDelegate: AdapterDelegate<*,*>){ + this.adapterDelegates.remove(adapterDelegate) + } + + fun clearAdapterDelegates(){ + adapterDelegates.clear() + } +} \ No newline at end of file diff --git a/flap/src/main/java/me/yifeiyuan/flap/dsl/AdapterHookDsl.kt b/flap/src/main/java/me/yifeiyuan/flap/dsl/AdapterHookDsl.kt index 73ef9774..b34bedc0 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/dsl/AdapterHookDsl.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/dsl/AdapterHookDsl.kt @@ -1,3 +1,5 @@ +@file:Suppress("unused") + package me.yifeiyuan.flap.dsl import me.yifeiyuan.flap.Component diff --git a/flap/src/main/java/me/yifeiyuan/flap/ext/SwipeDragHelper.kt b/flap/src/main/java/me/yifeiyuan/flap/ext/SwipeDragHelper.kt index 48ecd1cd..77b9d693 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/ext/SwipeDragHelper.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/ext/SwipeDragHelper.kt @@ -143,29 +143,24 @@ class SwipeDragHelper(private val callback: Callback) : ItemTouchHelper.Callback return false } - fun onItemMoved(listener: OnItemMoveListener): SwipeDragHelper { + fun onItemMoved(listener: OnItemMoveListener) = apply { onMoved = listener - return this } - fun onSwipeStarted(listener: OnSwipeStartedListener): SwipeDragHelper { + fun onSwipeStarted(listener: OnSwipeStartedListener)= apply { onSwipeStarted = listener - return this } - fun onSwipeReleased(listener: OnSwipeReleasedListener): SwipeDragHelper { + fun onSwipeReleased(listener: OnSwipeReleasedListener)= apply { onSwipedReleased = listener - return this } - fun onDragStarted(listener: OnDragStartedListener): SwipeDragHelper { + fun onDragStarted(listener: OnDragStartedListener)= apply { onDragStarted = listener - return this } - fun onDragReleased(listener: OnDragReleasedListener): SwipeDragHelper { + fun onDragReleased(listener: OnDragReleasedListener)= apply { onDragReleased = listener - return this } //Item 被滑动删除了调用 @@ -174,14 +169,12 @@ class SwipeDragHelper(private val callback: Callback) : ItemTouchHelper.Callback callback.onSwiped(viewHolder.adapterPosition) } - fun onItemSwiped(listener: OnItemSwipedListener): SwipeDragHelper { + fun onItemSwiped(listener: OnItemSwipedListener)= apply { onSwiped = listener - return this } - fun attachToRecyclerView(recyclerView: RecyclerView): SwipeDragHelper { + fun attachToRecyclerView(recyclerView: RecyclerView)= apply { itemTouchHelper.attachToRecyclerView(recyclerView) - return this } var swipedViewHolder: RecyclerView.ViewHolder? = null @@ -260,74 +253,63 @@ class SwipeDragHelper(private val callback: Callback) : ItemTouchHelper.Callback /** * 拖动是否可用 */ - fun withDragEnable(enable: Boolean): SwipeDragHelper { + fun withDragEnable(enable: Boolean)= apply { isDragEnable = enable - return this } /** * 滑动删除是否可用 */ - fun withSwipeEnable(enable: Boolean): SwipeDragHelper { + fun withSwipeEnable(enable: Boolean)= apply { isSwipeEnable = enable - return this } /** * 快捷设置 dragFlags 为垂直方向 up or down */ - fun forVerticalList(): SwipeDragHelper { + fun forVerticalList()= apply { this.dragFlags = ItemTouchHelper.UP or ItemTouchHelper.DOWN - return this } /** * 快捷设置 dragFlags 为水平方向 left or right */ - fun forHorizontalList(): SwipeDragHelper { + fun forHorizontalList()= apply { this.dragFlags = ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT - return this } /** * 快捷设置 dragFlags 为全方向 */ - fun forGrid(): SwipeDragHelper { + fun forGrid()= apply { this.dragFlags = ItemTouchHelper.UP or ItemTouchHelper.DOWN or ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT - return this } - fun withDragFlags(dragFlags: Int): SwipeDragHelper { + fun withDragFlags(dragFlags: Int)= apply { this.dragFlags = dragFlags - return this } - fun withSwipeFlags(swipeFlags: Int): SwipeDragHelper { + fun withSwipeFlags(swipeFlags: Int)= apply { this.swipeFlags = swipeFlags - return this } - fun withSwipeThreshold(swipeThreshold: Float): SwipeDragHelper { + fun withSwipeThreshold(swipeThreshold: Float)= apply { this.swipeThreshold = swipeThreshold - return this } - fun withDragThreshold(dragThreshold: Float): SwipeDragHelper { + fun withDragThreshold(dragThreshold: Float)= apply { this.dragThreshold = dragThreshold - return this } /** * 设置被滑动的 item 的背景 */ - fun withSwipeBackground(swipeBackground: Drawable): SwipeDragHelper { + fun withSwipeBackground(swipeBackground: Drawable)= apply { this.swipeBackground = swipeBackground - return this } - fun withSwipeBackgroundColor(color: Int): SwipeDragHelper { + fun withSwipeBackgroundColor(color: Int)= apply { this.swipeBackground = ColorDrawable(color) - return this } interface Callback { diff --git a/flap/src/main/java/me/yifeiyuan/flap/hook/AdapterHookManager.kt b/flap/src/main/java/me/yifeiyuan/flap/hook/AdapterHookManager.kt new file mode 100644 index 00000000..3f2da5f1 --- /dev/null +++ b/flap/src/main/java/me/yifeiyuan/flap/hook/AdapterHookManager.kt @@ -0,0 +1,12 @@ +package me.yifeiyuan.flap.hook + +/** + * Created by 程序亦非猿 on 2022/9/5. + * + * @since 3.1.0 + */ +class AdapterHookManager : IAdapterHookManager { + + override val adapterHooks: MutableList = mutableListOf() + +} \ No newline at end of file diff --git a/flap/src/main/java/me/yifeiyuan/flap/hook/IAdapterHookManager.kt b/flap/src/main/java/me/yifeiyuan/flap/hook/IAdapterHookManager.kt new file mode 100644 index 00000000..6feee0d4 --- /dev/null +++ b/flap/src/main/java/me/yifeiyuan/flap/hook/IAdapterHookManager.kt @@ -0,0 +1,28 @@ +package me.yifeiyuan.flap.hook + +import androidx.annotation.NonNull + +/** + * Created by 程序亦非猿 on 2022/9/5. + * @since 3.1.0 + */ +interface IAdapterHookManager { + + val adapterHooks: MutableList + + fun registerAdapterHook(@NonNull adapterHook: AdapterHook) { + adapterHooks.add(adapterHook) + } + + fun registerAdapterHooks(vararg adapterHooks: AdapterHook) { + this.adapterHooks.addAll(adapterHooks) + } + + fun unregisterAdapterHook(@NonNull adapterHook: AdapterHook) { + adapterHooks.remove(adapterHook) + } + + fun clearAdapterHooks() { + adapterHooks.clear() + } +} \ No newline at end of file diff --git a/flap/src/main/java/me/yifeiyuan/flap/hook/PreloadHook.kt b/flap/src/main/java/me/yifeiyuan/flap/hook/PreloadHook.kt index b151979e..e0fa91dc 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/hook/PreloadHook.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/hook/PreloadHook.kt @@ -1,6 +1,5 @@ package me.yifeiyuan.flap.hook -import androidx.recyclerview.widget.RecyclerView import me.yifeiyuan.flap.Component import me.yifeiyuan.flap.FlapAdapter import me.yifeiyuan.flap.FlapDebug diff --git a/flap/src/main/java/me/yifeiyuan/flap/service/AdapterService.kt b/flap/src/main/java/me/yifeiyuan/flap/service/AdapterService.kt index 0cf96de9..2f8338c6 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/service/AdapterService.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/service/AdapterService.kt @@ -5,6 +5,4 @@ package me.yifeiyuan.flap.service * * @since 3.0.3 */ -interface AdapterService { - -} \ No newline at end of file +interface AdapterService \ No newline at end of file diff --git a/flap/src/main/java/me/yifeiyuan/flap/service/AdapterServiceManager.kt b/flap/src/main/java/me/yifeiyuan/flap/service/AdapterServiceManager.kt index 23476723..544bd3fa 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/service/AdapterServiceManager.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/service/AdapterServiceManager.kt @@ -13,44 +13,40 @@ package me.yifeiyuan.flap.service @Suppress("UNCHECKED_CAST") internal class AdapterServiceManager : IAdapterServiceManager { - private val services = mutableMapOf, AdapterService>() - - /** - * 有名字的 Service - */ - private val namedServices = mutableMapOf() + override val adapterServices: MutableMap, AdapterService> = mutableMapOf() + override val namedAdapterServices: MutableMap = mutableMapOf() override fun registerAdapterService(serviceClass: Class) { try { val service = serviceClass.getConstructor().newInstance() - services[serviceClass] = service + adapterServices[serviceClass] = service } catch (e: Exception) { e.printStackTrace() } } override fun registerAdapterService(serviceClass: Class, service: T) { - services[serviceClass] = service + adapterServices[serviceClass] = service } override fun getAdapterService(serviceClass: Class): T? { - return services[serviceClass] as? T + return adapterServices[serviceClass] as? T } override fun registerAdapterService(serviceName: String, serviceClass: Class) { try { val service = serviceClass.getConstructor().newInstance() - namedServices[serviceName] = service + namedAdapterServices[serviceName] = service } catch (e: Exception) { e.printStackTrace() } } override fun registerAdapterService(serviceName: String, service: T) { - namedServices[serviceName] = service + namedAdapterServices[serviceName] = service } override fun getAdapterService(serviceName: String): T? { - return namedServices[serviceName] as? T + return namedAdapterServices[serviceName] as? T } } \ No newline at end of file diff --git a/flap/src/main/java/me/yifeiyuan/flap/service/IAdapterServiceManager.kt b/flap/src/main/java/me/yifeiyuan/flap/service/IAdapterServiceManager.kt index f9386a08..977634c8 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/service/IAdapterServiceManager.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/service/IAdapterServiceManager.kt @@ -7,6 +7,13 @@ package me.yifeiyuan.flap.service */ interface IAdapterServiceManager { + val adapterServices: MutableMap, AdapterService> + + /** + * 有名字的 Service + */ + val namedAdapterServices: MutableMap + fun registerAdapterService(serviceClass: Class) fun registerAdapterService(serviceClass: Class, service: T) diff --git a/flap/src/main/java/me/yifeiyuan/flap/skeleton/Skeleton.kt b/flap/src/main/java/me/yifeiyuan/flap/skeleton/Skeleton.kt index c13cc17f..028c2feb 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/skeleton/Skeleton.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/skeleton/Skeleton.kt @@ -54,59 +54,56 @@ class Skeleton : OnAdapterDataChangedObserver() { */ var emptyViewHelper: EmptyViewHelper? = null - fun bind(recyclerView: RecyclerView): Skeleton { + fun bind(recyclerView: RecyclerView) = apply { targetRecyclerView = recyclerView targetRecyclerView.adapter?.let { adapter(it) } - return this } - fun adapter(adapter: RecyclerView.Adapter<*>): Skeleton { + fun adapter(adapter: RecyclerView.Adapter<*>) = apply { targetAdapter = adapter - return this } - fun layout(@LayoutRes layoutRes: Int): Skeleton { + fun layout(@LayoutRes layoutRes: Int) = apply { skeletonAdapter.skeletonLayoutRes = layoutRes - return this + } - fun layouts(layouts: ((position: Int) -> Int)): Skeleton { + fun layouts(layouts: ((position: Int) -> Int)) = apply { skeletonAdapter.multiSkeletonLayoutRes = layouts - return this + } - fun count(count: Int): Skeleton { + fun count(count: Int) = apply { skeletonAdapter.skeletonItemCount = count - return this + } - fun shimmer(enable: Boolean): Skeleton { + fun shimmer(enable: Boolean) = apply { skeletonAdapter.shimmer = enable - return this + } - fun autoHide(autoHide: Boolean): Skeleton { + fun autoHide(autoHide: Boolean) = apply { this.autoHide = autoHide if (autoHide) { targetAdapter.registerAdapterDataObserver(this) } - return this + } - fun withEmptyViewHelper(emptyViewHelper: EmptyViewHelper): Skeleton { + fun withEmptyViewHelper(emptyViewHelper: EmptyViewHelper) = apply { this.emptyViewHelper = emptyViewHelper - return this + } /**只展示一次*/ - fun onlyOnce(onlyOnce: Boolean): Skeleton { + fun onlyOnce(onlyOnce: Boolean) = apply { this.onlyOnce = onlyOnce - return this } - fun show(): Skeleton { + fun show() = apply { if (skeletonAdapter.skeletonItemCount < 0) { throw IllegalArgumentException("skeletonCount 不能小于 0!") } @@ -133,7 +130,7 @@ class Skeleton : OnAdapterDataChangedObserver() { return this } - fun hide(): Skeleton { + fun hide() = apply { if (targetRecyclerView.visibility != View.VISIBLE) { targetRecyclerView.visibility = View.VISIBLE } @@ -145,7 +142,6 @@ class Skeleton : OnAdapterDataChangedObserver() { emptyViewHelper?.attachAdapter(targetAdapter) isShowing = false - return this } override fun onAdapterDataChanged() { @@ -153,5 +149,4 @@ class Skeleton : OnAdapterDataChangedObserver() { hide() } } - } \ No newline at end of file diff --git a/flap/src/main/java/me/yifeiyuan/flap/widget/FlapRecyclerView.kt b/flap/src/main/java/me/yifeiyuan/flap/widget/FlapRecyclerView.kt index cd60bafa..47cb06b0 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/widget/FlapRecyclerView.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/widget/FlapRecyclerView.kt @@ -4,7 +4,6 @@ package me.yifeiyuan.flap.widget import android.content.Context import android.util.AttributeSet -import android.util.Log import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleObserver import androidx.lifecycle.LifecycleOwner