Skip to content

Commit

Permalink
Rename and add comment
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanCheen committed Dec 2, 2022
1 parent b3b5cae commit dde35ce
Show file tree
Hide file tree
Showing 16 changed files with 139 additions and 137 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2018-present 程序亦非猿
Copyright 2018-present 程序亦非猿(AlanCheen)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
9 changes: 4 additions & 5 deletions app/src/main/java/me/yifeiyuan/flapdev/FlapApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import androidx.core.view.get
import androidx.multidex.MultiDexApplication
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import me.yifeiyuan.flap.FlapDebug
import me.yifeiyuan.flap.FlapInitializer
import me.yifeiyuan.flap.delegate.FallbackAdapterDelegate
import me.yifeiyuan.flap.delegate.FallbackComponent
Expand Down Expand Up @@ -69,16 +68,16 @@ class FlapApplication : MultiDexApplication() {
private fun initFlap() {

val dslAdapterHook = adapterHook {
onCreateViewHolderStart { adapter, viewType ->
onPreCreateViewHolder { adapter, viewType ->

}
onCreateViewHolderEnd { adapter, viewType, component ->
onPostCreateViewHolder { adapter, viewType, component ->

}
onBindViewHolderStart { adapter, component, data, position, payloads ->
onPreBindViewHolder { adapter, component, data, position, payloads ->

}
onBindViewHolderEnd { adapter, component, data, position, payloads ->
onPostBindViewHolder { adapter, component, data, position, payloads ->
Log.d("dslAdapterHook", "onBindViewHolderEnd() called with: adapter = $adapter, component = $component, data = $data, position = $position, payloads = $payloads")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import me.yifeiyuan.flap.decoration.SpaceItemDecoration
import me.yifeiyuan.flap.ext.doOnBindViewHolderEnd
import me.yifeiyuan.flap.ext.doOnCreateViewHolderEnd
import me.yifeiyuan.flap.ext.doOnPostBind
import me.yifeiyuan.flap.ext.doOnPostCreate
import me.yifeiyuan.flapdev.R
import me.yifeiyuan.flapdev.toPixel
import kotlin.random.Random
Expand All @@ -33,12 +31,12 @@ class ItemDecorationTestcase : BaseTestcaseFragment() {

recyclerView.setBackgroundColor(Color.parseColor("#16000000"))

adapter.doOnCreateViewHolderEnd { adapter, viewType, component ->
adapter.doOnPostCreate { adapter, viewType, component ->
val preLP = component.itemView.layoutParams
preLP.height = Random.nextInt(requireActivity().toPixel(50), requireActivity().toPixel(150))
}

adapter.doOnBindViewHolderEnd { adapter, component, data, position, payloads ->
adapter.doOnPostBind { adapter, component, data, position, payloads ->
if (recyclerView.layoutManager is StaggeredGridLayoutManager) {
val index = (component.itemView.layoutParams as StaggeredGridLayoutManager.LayoutParams).spanIndex
Log.d("StaggeredGridL", "onInit() called with: position = $position, spanIndex = $index")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import me.yifeiyuan.flap.FlapAdapter
import me.yifeiyuan.flap.ext.doOnCreateViewHolderEnd
import me.yifeiyuan.flap.ext.doOnPostCreate
import me.yifeiyuan.flapdev.R
import me.yifeiyuan.flapdev.Scrollable
import me.yifeiyuan.flapdev.components.SimpleTextModel
Expand Down Expand Up @@ -47,7 +47,7 @@ class ViewPager2Testcase : Fragment(), Scrollable {
* java.lang.IllegalStateException: Pages must fill the whole ViewPager2 (use match_parent)
* at androidx.viewpager2.widget.ViewPager2$4.onChildViewAttachedToWindow(ViewPager2.java:270)
*/
doOnCreateViewHolderEnd { adapter, viewType, component ->
doOnPostCreate { adapter, viewType, component ->
component.itemView.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ abstract class BaseAdapterAnimation : AdapterHook {
var interpolator: Interpolator = LinearInterpolator()
private var lastPosition = -1

override fun onBindViewHolderEnd(adapter: RecyclerView.Adapter<*>, component: Component<*>, data: Any, position: Int, payloads: MutableList<Any>) {
super.onBindViewHolderEnd(adapter, component, data, position, payloads)
override fun onPostBindViewHolder(adapter: RecyclerView.Adapter<*>, component: Component<*>, data: Any, position: Int, payloads: MutableList<Any>) {
super.onPostBindViewHolder(adapter, component, data, position, payloads)
val adapterPosition = component.adapterPosition
if (!isFirstOnly || adapterPosition > lastPosition) {
val animator = createAnimator(component.itemView, component, data, position)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ class FlapPagingDataAdapter<T : Any>(private val flap: Flap = Flap(), diffCallba
}

override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
flap.onAttachedToRecyclerView(this, recyclerView)
flap.onAdapterAttachedToRecyclerView(this, recyclerView)
}

override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
flap.onDetachedFromRecyclerView(this, recyclerView)
flap.onAdapterDetachedFromRecyclerView(this, recyclerView)
}

override fun onViewAttachedToWindow(component: Component<T>) {
Expand Down
74 changes: 30 additions & 44 deletions flap/src/main/java/me/yifeiyuan/flap/Flap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class Flap : FlapApi {
registerAdapterHook(it)
}

lateinit var componentPool: ComponentPool

lateinit var bindingRecyclerView: RecyclerView
lateinit var bindingContext: Context

init {
adapterHooks.addAll(FlapInitializer.adapterHooks)
adapterDelegates.addAll(FlapInitializer.adapterDelegates)
Expand All @@ -82,7 +87,7 @@ class Flap : FlapApi {
private fun dispatchOnCreateViewHolderStart(adapter: RecyclerView.Adapter<*>, viewType: Int) {
try {
adapterHooks.forEach {
it.onCreateViewHolderStart(adapter, viewType)
it.onPreCreateViewHolder(adapter, viewType)
}
} catch (e: Exception) {
e.printStackTrace()
Expand All @@ -92,7 +97,7 @@ class Flap : FlapApi {
private fun dispatchOnCreateViewHolderEnd(adapter: RecyclerView.Adapter<*>, viewType: Int, component: Component<*>) {
try {
adapterHooks.forEach {
it.onCreateViewHolderEnd(adapter, viewType, component)
it.onPostCreateViewHolder(adapter, viewType, component)
}
} catch (e: Exception) {
e.printStackTrace()
Expand Down Expand Up @@ -147,7 +152,7 @@ class Flap : FlapApi {
payloads: MutableList<Any>) {
try {
adapterHooks.forEach {
it.onBindViewHolderStart(adapter, component, itemData, position, payloads)
it.onPreBindViewHolder(adapter, component, itemData, position, payloads)
}
} catch (e: Exception) {
e.printStackTrace()
Expand All @@ -162,7 +167,7 @@ class Flap : FlapApi {
payloads: MutableList<Any>) {
try {
adapterHooks.forEach {
it.onBindViewHolderEnd(adapter, component, data, position, payloads)
it.onPostBindViewHolder(adapter, component, data, position, payloads)
}
} catch (e: Exception) {
e.printStackTrace()
Expand Down Expand Up @@ -207,17 +212,24 @@ class Flap : FlapApi {

/**
* 会优先于 FlapComponentPool.putRecycledView 被调用
* @see RecyclerView.Adapter.onViewRecycled
*/
fun onViewRecycled(adapter: RecyclerView.Adapter<*>, component: Component<*>) {
val delegate = getDelegateByViewType(component.itemViewType)
delegate.onViewRecycled(adapter, component)
}

/**
* @see RecyclerView.Adapter.onFailedToRecycleView
*/
fun onFailedToRecycleView(adapter: RecyclerView.Adapter<*>, component: Component<*>): Boolean {
val delegate = getDelegateByViewType(component.itemViewType)
return delegate.onFailedToRecycleView(adapter, component)
}

/**
* @see RecyclerView.Adapter.onViewAttachedToWindow
*/
fun onViewAttachedToWindow(adapter: RecyclerView.Adapter<*>, component: Component<*>) {
val delegate = getDelegateByViewType(component.itemViewType)
delegate.onViewAttachedToWindow(adapter, component)
Expand All @@ -234,6 +246,9 @@ class Flap : FlapApi {
}
}

/**
* @see RecyclerView.Adapter.onViewDetachedFromWindow
*/
fun onViewDetachedFromWindow(adapter: RecyclerView.Adapter<*>, component: Component<*>) {
val delegate = getDelegateByViewType(component.itemViewType)
delegate.onViewDetachedFromWindow(adapter, component)
Expand All @@ -255,12 +270,15 @@ class Flap : FlapApi {
*/
private var useComponentPool = true

fun onAttachedToRecyclerView(adapter: RecyclerView.Adapter<*>, recyclerView: RecyclerView) {
handleOnAttachedToRecyclerView(recyclerView)
dispatchOnAttachedToRecyclerView(adapter, recyclerView)
/**
* @see RecyclerView.Adapter.onAttachedToRecyclerView
*/
fun onAdapterAttachedToRecyclerView(adapter: RecyclerView.Adapter<*>, recyclerView: RecyclerView) {
handleOnAdapterAttachedToRecyclerView(recyclerView)
dispatchOnAdapterAttachedToRecyclerView(adapter, recyclerView)
}

private fun handleOnAttachedToRecyclerView(recyclerView: RecyclerView) {
private fun handleOnAdapterAttachedToRecyclerView(recyclerView: RecyclerView) {
bindingRecyclerView = recyclerView
bindingContext = recyclerView.context

Expand All @@ -282,7 +300,7 @@ class Flap : FlapApi {
}
}

private fun dispatchOnAttachedToRecyclerView(adapter: RecyclerView.Adapter<*>, recyclerView: RecyclerView) {
private fun dispatchOnAdapterAttachedToRecyclerView(adapter: RecyclerView.Adapter<*>, recyclerView: RecyclerView) {
try {
adapterHooks.forEach {
it.onAttachedToRecyclerView(adapter, recyclerView)
Expand All @@ -292,12 +310,7 @@ class Flap : FlapApi {
}
}

lateinit var componentPool: ComponentPool

lateinit var bindingRecyclerView: RecyclerView
lateinit var bindingContext: Context

fun onDetachedFromRecyclerView(adapter: RecyclerView.Adapter<*>, recyclerView: RecyclerView) {
fun onAdapterDetachedFromRecyclerView(adapter: RecyclerView.Adapter<*>, recyclerView: RecyclerView) {
if (this::componentPool.isInitialized) {
bindingContext.applicationContext.unregisterComponentCallbacks(componentPool)
}
Expand Down Expand Up @@ -345,10 +358,6 @@ class Flap : FlapApi {
}
}

/**
* 设置是否启用预加载
* 需要先调用 doOnPreload 开启才有效。
*/
override fun setPreloadEnable(enable: Boolean, direction: Int) = apply {
when (direction) {
PreloadHook.SCROLL_UP -> {
Expand All @@ -375,18 +384,10 @@ class Flap : FlapApi {
registerAdapterHook(it)
}

/**
* 设置点击事件监听
* @see doOnItemLongClick
*/
override fun doOnItemClick(onItemClick: OnItemClickListener?) = apply {
itemClicksHelper.onItemClickListener = onItemClick
}

/**
* 设置长按事件监听
* @see doOnItemClick
*/
override fun doOnItemLongClick(onItemLongClick: OnItemLongClickListener?) = apply {
itemClicksHelper.onItemLongClickListener = onItemLongClick
}
Expand All @@ -401,31 +402,19 @@ class Flap : FlapApi {

private var paramProvider: ExtraParamsProvider? = null

/**
* 提供 Component 从 Adapter 获取参数的方法
*
* @return key 对应的参数,如果类型不匹配,则会为 null
*/
@Suppress("UNCHECKED_CAST")
override fun <P> getParam(key: String): P? {
return paramProvider?.getParam(key) as? P?
}

override fun withParamProvider(block: (key: String) -> Any?) = apply {
paramProvider = ExtraParamsProviderWrapper(block)
override fun withParamProvider(provider: (key: String) -> Any?) = apply {
paramProvider = ExtraParamsProviderWrapper(provider)
}

/**
* 设置 Component 绑定的 LifecycleOwner
* 会尝试去获取 recyclerView.context 作为 LifecycleOwner
*/
override fun withLifecycleOwner(lifecycleOwner: LifecycleOwner) = apply {
this.lifecycleOwner = lifecycleOwner
}

/**
* 设置是否使用 ComponentPool 作为缓存池
*/
override fun setComponentPoolEnable(enable: Boolean) = apply {
useComponentPool = enable
}
Expand Down Expand Up @@ -461,9 +450,6 @@ class Flap : FlapApi {
eventObservers[eventName] = EventObserverWrapper(block)
}

/**
* 观察所有的事件
*/
override fun observerEvents(block: (Event<*>) -> Unit) = apply {
allEventsObserver = object : EventObserver {
override fun onEvent(event: Event<*>) {
Expand Down
7 changes: 2 additions & 5 deletions flap/src/main/java/me/yifeiyuan/flap/FlapAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ package me.yifeiyuan.flap

import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
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.util.*

Expand Down Expand Up @@ -115,11 +112,11 @@ open class FlapAdapter(private val flap: Flap = Flap(), private val flapInitBloc
}

override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
flap.onAttachedToRecyclerView(this, recyclerView)
flap.onAdapterAttachedToRecyclerView(this, recyclerView)
}

override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
flap.onDetachedFromRecyclerView(this, recyclerView)
flap.onAdapterDetachedFromRecyclerView(this, recyclerView)
}

override fun onViewRecycled(component: Component<*>) {
Expand Down
Loading

0 comments on commit dde35ce

Please sign in to comment.