Skip to content

Commit

Permalink
Merge pull request #110 from AlanCheen/feature/adapter-hook-dsl
Browse files Browse the repository at this point in the history
Feature/adapter hook dsl
  • Loading branch information
AlanCheen authored Sep 5, 2022
2 parents 8597012 + 5678fce commit 6438f4f
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 21 deletions.
6 changes: 3 additions & 3 deletions app/src/main/java/me/yifeiyuan/flapdev/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ class MainActivity : AppCompatActivity() {
subtitle = "Header、Footer"
replace(HeaderFooterTestcase::class.java)
}
R.id.nav_layout_delegate_dsl -> {
subtitle = "AdapterDelegate DSL"
replace(AdapterDelegateDSLTestcase::class.java)
R.id.nav_flap_dsl -> {
subtitle = "DSL"
replace(DSLTestcase::class.java)
}
R.id.nav_dismiss -> {
subtitle = "滑动删除&拖放"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,35 @@ package me.yifeiyuan.flapdev.testcases
import android.util.Log
import android.view.View
import android.widget.ImageView
import me.yifeiyuan.flap.Component
import me.yifeiyuan.flap.FlapAdapter
import me.yifeiyuan.flap.delegate.AdapterDelegate
import me.yifeiyuan.flap.dsl.adapterDelegate
import me.yifeiyuan.flap.dsl.adapterHook
import me.yifeiyuan.flap.ext.bindButton
import me.yifeiyuan.flap.ext.bindTextView
import me.yifeiyuan.flap.ext.bindView
import me.yifeiyuan.flap.hook.AdapterHook
import me.yifeiyuan.flapdev.R
import me.yifeiyuan.flapdev.components.SimpleImageModel
import me.yifeiyuan.flapdev.components.SimpleTextModel
import me.yifeiyuan.flapdev.components.TestAllModel
import me.yifeiyuan.flapdev.mockMultiTypeModels

private const val TAG = "LayoutDelegateDSLTest"
private const val TAG = "DSLTestcase"

/**
* 测试 AdapterDelegate DSL
* 测试 DSL
*
* Created by 程序亦非猿 on 2022/8/4.
*/
class AdapterDelegateDSLTestcase : BaseTestcaseFragment() {
class DSLTestcase : BaseTestcaseFragment() {

override fun onInit(view: View) {
super.onInit(view)

recyclerView.addItemDecoration(linearItemDecoration)

val simpleTextDelegate = adapterDelegate<SimpleTextModel>(R.layout.flap_item_simple_text) {
onBind { model ->
bindTextView(R.id.tv_content) {
Expand Down Expand Up @@ -126,15 +132,6 @@ class AdapterDelegateDSLTestcase : BaseTestcaseFragment() {
}
}

// val simpleImageDelegate = LayoutAdapterDelegate(SimpleImageModel::class.java,
// R.layout.flap_item_simple_image) { model ->
// bindView<ImageView>(R.id.logo) {
// setOnClickListener {
// toast("simpleImageDelegate clicked")
// }
// }
// }

val simpleImageDelegate = adapterDelegate<SimpleImageModel>(R.layout.flap_item_simple_image) {

onBind { model, position, payloads, adapter ->
Expand All @@ -149,7 +146,22 @@ class AdapterDelegateDSLTestcase : BaseTestcaseFragment() {
}
}

// val hook2 = object : AdapterHook{
// override fun onBindViewHolderEnd(adapter: FlapAdapter, delegate: AdapterDelegate<*, *>, component: Component<*>, data: Any, position: Int, payloads: MutableList<Any>) {
//
// }
// }
val hook = adapterHook {
onBindViewHolderEnd { adapter, delegate, component, data, position, payloads ->
Log.d(TAG, "onBindViewHolderEnd() called with: adapter = $adapter, delegate = $delegate, component = $component, data = $data, position = $position, payloads = $payloads")
}
}



adapter.registerAdapterDelegates(simpleTextDelegate, simpleImageDelegate, testAllDelegate)

adapter.registerAdapterHook(hook)
}

override fun createAdapter(): FlapAdapter {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/menu/activity_main_drawer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
android:icon="@drawable/flap_ic_qj"
android:title="FlapDifferAdapter" />
<item
android:id="@+id/nav_layout_delegate_dsl"
android:id="@+id/nav_flap_dsl"
android:icon="@drawable/flap_ic_taozi"
android:title="AdapterDelegate DSL" />
android:title="DSL" />
<item
android:id="@+id/nav_dismiss"
android:icon="@drawable/flap_ic_hlg"
Expand Down
8 changes: 4 additions & 4 deletions flap/src/main/java/me/yifeiyuan/flap/FlapAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -539,12 +539,12 @@ open class FlapAdapter : RecyclerView.Adapter<Component<*>>(), IRegistry, IAdapt
serviceManager.registerAdapterService(serviceClass)
}

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

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

override fun <T : AdapterService> registerAdapterService(serviceName: String, serviceClass: Class<T>) {
Expand Down
82 changes: 82 additions & 0 deletions flap/src/main/java/me/yifeiyuan/flap/dsl/AdapterHookDsl.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package me.yifeiyuan.flap.dsl

import me.yifeiyuan.flap.Component
import me.yifeiyuan.flap.FlapAdapter
import me.yifeiyuan.flap.delegate.AdapterDelegate
import me.yifeiyuan.flap.hook.AdapterHook

/**
* Created by 程序亦非猿 on 2022/9/5.
*
* @since 3.1.0
*/

/**
* @since 3.1.0
*/
fun adapterHook(block: DslAdapterHook.() -> Unit): AdapterHook {
return DslAdapterHook().apply(block)
}

/**
* @since 3.1.0
*/
class DslAdapterHook : AdapterHook {

private var onCreateViewHolderStart: ((adapter: FlapAdapter, delegate: AdapterDelegate<*, *>, viewType: Int) -> Unit)? = null
private var onCreateViewHolderEnd: ((adapter: FlapAdapter, delegate: AdapterDelegate<*, *>, viewType: Int, component: Component<*>) -> Unit)? = null

private var onViewAttachedToWindow: ((adapter: FlapAdapter, delegate: AdapterDelegate<*, *>, component: Component<*>) -> Unit)? = null
private var onViewDetachedFromWindow: ((adapter: FlapAdapter, delegate: AdapterDelegate<*, *>, component: Component<*>) -> Unit)? = null

private var onBindViewHolderStart: ((adapter: FlapAdapter, delegate: AdapterDelegate<*, *>, component: Component<*>, data: Any, position: Int, payloads: MutableList<Any>) -> Unit)? = null
private var onBindViewHolderEnd: ((adapter: FlapAdapter, delegate: AdapterDelegate<*, *>, component: Component<*>, data: Any, position: Int, payloads: MutableList<Any>) -> Unit)? = null

fun onCreateViewHolderStart(block: (adapter: FlapAdapter, delegate: AdapterDelegate<*, *>, viewType: Int) -> Unit) {
onCreateViewHolderStart = block
}

fun onCreateViewHolderEnd(block: (adapter: FlapAdapter, delegate: AdapterDelegate<*, *>, viewType: Int, component: Component<*>) -> Unit) {
onCreateViewHolderEnd = block
}

fun onViewAttachedToWindow(block: (adapter: FlapAdapter, delegate: AdapterDelegate<*, *>, component: Component<*>) -> Unit) {
onViewAttachedToWindow = block
}

fun onViewDetachedFromWindow(block: (adapter: FlapAdapter, delegate: AdapterDelegate<*, *>, component: Component<*>) -> Unit) {
onViewDetachedFromWindow = block
}

fun onBindViewHolderStart(block: (adapter: FlapAdapter, delegate: AdapterDelegate<*, *>, component: Component<*>, data: Any, position: Int, payloads: MutableList<Any>) -> Unit) {
onBindViewHolderStart = block
}

fun onBindViewHolderEnd(block: (adapter: FlapAdapter, delegate: AdapterDelegate<*, *>, component: Component<*>, data: Any, position: Int, payloads: MutableList<Any>) -> Unit) {
onBindViewHolderEnd = block
}

override fun onCreateViewHolderStart(adapter: FlapAdapter, delegate: AdapterDelegate<*, *>, viewType: Int) {
onCreateViewHolderStart?.invoke(adapter, delegate, viewType)
}

override fun onCreateViewHolderEnd(adapter: FlapAdapter, delegate: AdapterDelegate<*, *>, viewType: Int, component: Component<*>) {
onCreateViewHolderEnd?.invoke(adapter, delegate, viewType, component)
}

override fun onBindViewHolderStart(adapter: FlapAdapter, delegate: AdapterDelegate<*, *>, component: Component<*>, data: Any, position: Int, payloads: MutableList<Any>) {
onBindViewHolderStart?.invoke(adapter, delegate, component, data, position, payloads)
}

override fun onBindViewHolderEnd(adapter: FlapAdapter, delegate: AdapterDelegate<*, *>, component: Component<*>, data: Any, position: Int, payloads: MutableList<Any>) {
onBindViewHolderEnd?.invoke(adapter, delegate, component, data, position, payloads)
}

override fun onViewAttachedToWindow(adapter: FlapAdapter, delegate: AdapterDelegate<*, *>, component: Component<*>) {
onViewAttachedToWindow?.invoke(adapter, delegate, component)
}

override fun onViewDetachedFromWindow(adapter: FlapAdapter, delegate: AdapterDelegate<*, *>, component: Component<*>) {
onViewDetachedFromWindow?.invoke(adapter, delegate, component)
}
}

0 comments on commit 6438f4f

Please sign in to comment.