diff --git a/app/build.gradle b/app/build.gradle index e052841d..d8d7fc2f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -98,6 +98,7 @@ dependencies { implementation project(':ktmodule') implementation project(':flap') + implementation project(':flap-dsl-viewbinding') implementation project(':flap-annotations') // implementation project(':flap-gradle-plugin') kapt project(':flap-compiler') diff --git a/app/src/main/java/me/yifeiyuan/flapdev/testcases/DSLTestcase.kt b/app/src/main/java/me/yifeiyuan/flapdev/testcases/DSLTestcase.kt index 5e6a9c55..7146c7da 100644 --- a/app/src/main/java/me/yifeiyuan/flapdev/testcases/DSLTestcase.kt +++ b/app/src/main/java/me/yifeiyuan/flapdev/testcases/DSLTestcase.kt @@ -6,12 +6,15 @@ import android.widget.ImageView import me.yifeiyuan.flap.FlapAdapter import me.yifeiyuan.flap.dsl.adapterDelegate import me.yifeiyuan.flap.dsl.adapterHook +import me.yifeiyuan.flap.dsl.viewbinding.adapterDelegateViewBinding import me.yifeiyuan.flap.ext.* 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 +import me.yifeiyuan.ktx.foundation.othermodule.databinding.FlapItemVbBinding +import me.yifeiyuan.ktx.foundation.othermodule.vb.ViewBindingModel private const val TAG = "DSLTestcase" @@ -156,7 +159,30 @@ class DSLTestcase : BaseTestcaseFragment() { // model: TestBinderModel, position: Int, payloads: List, adapter: FlapAdapter -> // } - adapter.registerAdapterDelegates(simpleTextDelegate, simpleImageDelegate, testAllDelegate) + val viewBindingDelegate = adapterDelegateViewBinding( + { layoutInflater, parent -> + FlapItemVbBinding.inflate(layoutInflater, parent, false) + } + ) { + + onBind { model -> + binding.tvContent.text = "这是个用 adapterDelegateViewBinding DSL 处理的组件" + } + + onClick { model, position, adapter -> + toast("viewBindingDelegate onClick() called with: component = $this, model = $model, position = $position") + } + + onResume { + Log.d(TAG, "viewBindingDelegate onResume() called") + } + + onPause { + Log.d(TAG, "viewBindingDelegate onPause() called") + } + } + + adapter.registerAdapterDelegates(simpleTextDelegate, simpleImageDelegate, testAllDelegate, viewBindingDelegate) adapter.registerAdapterHook(hook) } diff --git a/flap-dsl-viewbinding/.gitignore b/flap-dsl-viewbinding/.gitignore new file mode 100644 index 00000000..42afabfd --- /dev/null +++ b/flap-dsl-viewbinding/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/flap-dsl-viewbinding/build.gradle b/flap-dsl-viewbinding/build.gradle new file mode 100644 index 00000000..fe35a5fd --- /dev/null +++ b/flap-dsl-viewbinding/build.gradle @@ -0,0 +1,53 @@ +plugins { + id 'com.android.library' + id 'kotlin-android' +} + +apply plugin: 'com.github.dcendents.android-maven' + +group = 'me.yifeiyuan' + +android { + compileSdkVersion 29 + + defaultConfig { + minSdkVersion 17 + targetSdkVersion 29 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + consumerProguardFiles "consumer-rules.pro" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } +// compileOptions { +// sourceCompatibility JavaVersion.VERSION_1_8 +// targetCompatibility JavaVersion.VERSION_1_8 +// } +// kotlinOptions { +// jvmTarget = '1.8' +// } + + buildFeatures { + viewBinding true + } +} + +dependencies { + + testImplementation 'junit:junit:4.13.2' + androidTestImplementation 'androidx.test.ext:junit:1.1.3' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' + + implementation 'androidx.recyclerview:recyclerview:1.2.1' + implementation "androidx.core:core-ktx:1.6.0" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" + + implementation project(':flap') +} \ No newline at end of file diff --git a/flap-dsl-viewbinding/consumer-rules.pro b/flap-dsl-viewbinding/consumer-rules.pro new file mode 100644 index 00000000..e69de29b diff --git a/flap-dsl-viewbinding/proguard-rules.pro b/flap-dsl-viewbinding/proguard-rules.pro new file mode 100644 index 00000000..481bb434 --- /dev/null +++ b/flap-dsl-viewbinding/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/flap-dsl-viewbinding/src/androidTest/java/me/yifeiyuan/flap/dsl/viewbinding/ExampleInstrumentedTest.kt b/flap-dsl-viewbinding/src/androidTest/java/me/yifeiyuan/flap/dsl/viewbinding/ExampleInstrumentedTest.kt new file mode 100644 index 00000000..6502bc32 --- /dev/null +++ b/flap-dsl-viewbinding/src/androidTest/java/me/yifeiyuan/flap/dsl/viewbinding/ExampleInstrumentedTest.kt @@ -0,0 +1,24 @@ +package me.yifeiyuan.flap.dsl.viewbinding + +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.ext.junit.runners.AndroidJUnit4 + +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.Assert.* + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getInstrumentation().targetContext + assertEquals("me.yifeiyuan.flap.dsl.viewbinding.test", appContext.packageName) + } +} \ No newline at end of file diff --git a/flap-dsl-viewbinding/src/main/AndroidManifest.xml b/flap-dsl-viewbinding/src/main/AndroidManifest.xml new file mode 100644 index 00000000..8ae38096 --- /dev/null +++ b/flap-dsl-viewbinding/src/main/AndroidManifest.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/flap-dsl-viewbinding/src/main/java/me/yifeiyuan/flap/dsl/viewbinding/AdapterDelegateViewBindingDsl.kt b/flap-dsl-viewbinding/src/main/java/me/yifeiyuan/flap/dsl/viewbinding/AdapterDelegateViewBindingDsl.kt new file mode 100644 index 00000000..40dffb7e --- /dev/null +++ b/flap-dsl-viewbinding/src/main/java/me/yifeiyuan/flap/dsl/viewbinding/AdapterDelegateViewBindingDsl.kt @@ -0,0 +1,63 @@ +package me.yifeiyuan.flap.dsl.viewbinding + +import android.view.LayoutInflater +import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView +import androidx.viewbinding.ViewBinding +import me.yifeiyuan.flap.delegate.AdapterDelegate +import me.yifeiyuan.flap.dsl.DslComponent + +/** + * 支持 AdapterDelegate ViewBinding DSL 功能 + * Created by 程序亦非猿 on 2022/9/20. + * @since 3.1.3 + */ +inline fun adapterDelegateViewBinding( + noinline viewBinding: (layoutInflater: LayoutInflater, parent: ViewGroup) -> V, + itemViewType: Int = 0, + noinline isDelegateFor: ((model: Any) -> Boolean) = { m -> m.javaClass == T::class.java }, + itemId: Long = RecyclerView.NO_ID, + noinline componentInitializer: ViewBindingDslComponent.() -> Unit): ViewBindingDslAdapterDelegate { + return ViewBindingDslAdapterDelegate(T::class.java, viewBinding, itemViewType, itemId, isDelegateFor = isDelegateFor, block = componentInitializer) +} + +/** + * 支持 AdapterDelegate ViewBinding DSL 功能 + * Created by 程序亦非猿 on 2022/9/20. + * @since 3.1.3 + */ +class ViewBindingDslAdapterDelegate( + private var modelClass: Class, + private var viewBinding: (layoutInflater: LayoutInflater, parent: ViewGroup) -> V, + private var itemViewType: Int, + private var itemId: Long = RecyclerView.NO_ID, + private var isDelegateFor: ((model: Any) -> Boolean) = { m -> m.javaClass == modelClass }, + private var block: ViewBindingDslComponent.() -> Unit, +) : AdapterDelegate> { + + override fun delegate(model: Any): Boolean { + return isDelegateFor.invoke(model) + } + + override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup, viewType: Int): ViewBindingDslComponent { + val binding = viewBinding(inflater, parent) + val component = ViewBindingDslComponent(binding) + block.invoke(component) + return component + } + + override fun getItemId(model: Any, position: Int): Long { + return itemId + } + + override fun getItemViewType(model: Any): Int { + return itemViewType + } +} + +/** + * 支持 AdapterDelegate ViewBinding DSL 功能 + * Created by 程序亦非猿 on 2022/9/20. + * @since 3.1.3 + */ +class ViewBindingDslComponent(val binding: V) : DslComponent(binding.root) \ No newline at end of file diff --git a/flap-dsl-viewbinding/src/test/java/me/yifeiyuan/flap/dsl/viewbinding/ExampleUnitTest.kt b/flap-dsl-viewbinding/src/test/java/me/yifeiyuan/flap/dsl/viewbinding/ExampleUnitTest.kt new file mode 100644 index 00000000..65b9692f --- /dev/null +++ b/flap-dsl-viewbinding/src/test/java/me/yifeiyuan/flap/dsl/viewbinding/ExampleUnitTest.kt @@ -0,0 +1,17 @@ +package me.yifeiyuan.flap.dsl.viewbinding + +import org.junit.Test + +import org.junit.Assert.* + +/** + * Example local unit test, which will execute on the development machine (host). + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +class ExampleUnitTest { + @Test + fun addition_isCorrect() { + assertEquals(4, 2 + 2) + } +} \ No newline at end of file diff --git a/flap/build.gradle b/flap/build.gradle index 5180ff8a..7a9f2b12 100644 --- a/flap/build.gradle +++ b/flap/build.gradle @@ -15,7 +15,6 @@ android { versionName "1.0" testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' - } buildTypes { diff --git a/flap/src/main/java/me/yifeiyuan/flap/dsl/AdapterDelegateDsl.kt b/flap/src/main/java/me/yifeiyuan/flap/dsl/AdapterDelegateDsl.kt index c4355052..12c8f991 100644 --- a/flap/src/main/java/me/yifeiyuan/flap/dsl/AdapterDelegateDsl.kt +++ b/flap/src/main/java/me/yifeiyuan/flap/dsl/AdapterDelegateDsl.kt @@ -72,7 +72,7 @@ class DslAdapterDelegate( * * @since 3.0.9 */ -class DslComponent(view: View) : Component(view) { +open class DslComponent(view: View) : Component(view) { /** * 更多参数的 onBind diff --git a/settings.gradle b/settings.gradle index 590d8e7f..d674cde3 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,3 +1,4 @@ +include ':flap-dsl-viewbinding' include ':ktmodule' include ':othermodule' include ':flap-gradle-plugin'