Skip to content

Commit

Permalink
Merge pull request #111 from AlanCheen/feature/managers
Browse files Browse the repository at this point in the history
Feature/managers
  • Loading branch information
AlanCheen authored Sep 6, 2022
2 parents 6438f4f + b1cd489 commit 7791536
Show file tree
Hide file tree
Showing 18 changed files with 155 additions and 191 deletions.
1 change: 0 additions & 1 deletion flap/src/main/java/me/yifeiyuan/flap/Component.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 7 additions & 37 deletions flap/src/main/java/me/yifeiyuan/flap/Flap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 存放全局的配置
Expand All @@ -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
Expand All @@ -29,46 +35,10 @@ object Flap : ComponentCallbacks2, IRegistry {

internal val globalComponentPool = ComponentPool()

internal val globalAdapterDelegates: MutableList<AdapterDelegate<*, *>> = mutableListOf()

internal var globalDefaultAdapterDelegate: AdapterDelegate<*, *>? = FallbackAdapterDelegate()

internal val globalHooks: MutableList<AdapterHook> = 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)
}
Expand Down
81 changes: 11 additions & 70 deletions flap/src/main/java/me/yifeiyuan/flap/FlapAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -32,7 +34,7 @@ import me.yifeiyuan.flap.service.AdapterServiceManager
* @since 2020/9/22
* @since 3.0.0
*/
open class FlapAdapter : RecyclerView.Adapter<Component<*>>(), IRegistry, IAdapterServiceManager, SwipeDragHelper.Callback {
open class FlapAdapter : RecyclerView.Adapter<Component<*>>(), IAdapterHookManager by AdapterHookManager(), IAdapterDelegateManager by AdapterDelegateManager(), IAdapterServiceManager by AdapterServiceManager(), SwipeDragHelper.Callback {

companion object {
private const val TAG = "FlapAdapter"
Expand Down Expand Up @@ -66,14 +68,10 @@ open class FlapAdapter : RecyclerView.Adapter<Component<*>>(), IRegistry, IAdapt
*/
private var defaultAdapterDelegate: AdapterDelegate<*, *>? = null

private val adapterDelegates: MutableList<AdapterDelegate<*, *>> = mutableListOf()

private val adapterHooks: MutableList<AdapterHook> = mutableListOf()

/**
* RecyclerView 滑动到底部触发预加载
*/
var preloadHook: PreloadHook? = null
private var preloadHook: PreloadHook? = null

private val viewTypeDelegateCache: MutableMap<Int, AdapterDelegate<*, *>?> = mutableMapOf()
private val delegateViewTypeCache: MutableMap<AdapterDelegate<*, *>, Int> = mutableMapOf()
Expand Down Expand Up @@ -105,52 +103,19 @@ open class FlapAdapter : RecyclerView.Adapter<Component<*>>(), 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
}

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<Any>) {
data.clear()
data.addAll(newDataList)
Expand Down Expand Up @@ -535,30 +500,6 @@ open class FlapAdapter : RecyclerView.Adapter<Component<*>>(), IRegistry, IAdapt
emptyViewHelper.emptyView = emptyView
}

override fun <T : AdapterService> registerAdapterService(serviceClass: Class<T>) {
serviceManager.registerAdapterService(serviceClass)
}

override fun <T : AdapterService> registerAdapterService(serviceClass: Class<T>, service: T) {
serviceManager.registerAdapterService(serviceClass, service)
}

override fun <T : AdapterService> getAdapterService(serviceClass: Class<T>): T? {
return serviceManager.getAdapterService(serviceClass)
}

override fun <T : AdapterService> registerAdapterService(serviceName: String, serviceClass: Class<T>) {
serviceManager.registerAdapterService(serviceName, serviceClass)
}

override fun <T : AdapterService> registerAdapterService(serviceName: String, service: T) {
serviceManager.registerAdapterService(serviceName, service)
}

override fun <T : AdapterService> getAdapterService(serviceName: String): T? {
return serviceManager.getAdapterService(serviceName)
}

fun insertDataAt(position: Int, element: Any, notify: Boolean = true) {
this.data.add(position, element)
if (notify) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,可以设置间隔的颜色和大小
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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<AdapterDelegate<*, *>> = mutableListOf()
}
Original file line number Diff line number Diff line change
@@ -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<AdapterDelegate<*,*>>

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()
}
}
2 changes: 2 additions & 0 deletions flap/src/main/java/me/yifeiyuan/flap/dsl/AdapterHookDsl.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("unused")

package me.yifeiyuan.flap.dsl

import me.yifeiyuan.flap.Component
Expand Down
Loading

0 comments on commit 7791536

Please sign in to comment.