From 926d641f33ced65f36ac30b15a990beb192112e9 Mon Sep 17 00:00:00 2001 From: Fitz Date: Wed, 21 Sep 2022 18:59:12 +0800 Subject: [PATCH 1/2] =?UTF-8?q?:art:=E5=A2=9E=E5=8A=A0=20adapterDelegateDa?= =?UTF-8?q?taBinding=20DSL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 1 + .../flapdev/testcases/DSLTestcase.kt | 13 +++- flap-dsl-databinding/.gitignore | 1 + flap-dsl-databinding/build.gradle | 53 ++++++++++++++ flap-dsl-databinding/consumer-rules.pro | 0 flap-dsl-databinding/proguard-rules.pro | 21 ++++++ .../databinding/ExampleInstrumentedTest.kt | 24 +++++++ .../src/main/AndroidManifest.xml | 5 ++ .../AdapterDelegateDataBindingDsl.kt | 70 +++++++++++++++++++ .../flap/dsl/databinding/ExampleUnitTest.kt | 17 +++++ settings.gradle | 1 + 11 files changed, 205 insertions(+), 1 deletion(-) create mode 100644 flap-dsl-databinding/.gitignore create mode 100644 flap-dsl-databinding/build.gradle create mode 100644 flap-dsl-databinding/consumer-rules.pro create mode 100644 flap-dsl-databinding/proguard-rules.pro create mode 100644 flap-dsl-databinding/src/androidTest/java/me/yifeiyuan/flap/dsl/databinding/ExampleInstrumentedTest.kt create mode 100644 flap-dsl-databinding/src/main/AndroidManifest.xml create mode 100644 flap-dsl-databinding/src/main/java/me/yifeiyuan/flap/dsl/databinding/AdapterDelegateDataBindingDsl.kt create mode 100644 flap-dsl-databinding/src/test/java/me/yifeiyuan/flap/dsl/databinding/ExampleUnitTest.kt diff --git a/app/build.gradle b/app/build.gradle index d8d7fc2f8..ecb19489b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -99,6 +99,7 @@ dependencies { implementation project(':flap') implementation project(':flap-dsl-viewbinding') + implementation project(':flap-dsl-databinding') 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 24c74742c..0c598c095 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.databinding.adapterDelegateDataBinding import me.yifeiyuan.flap.dsl.viewbinding.adapterDelegateViewBinding import me.yifeiyuan.flap.ext.* import me.yifeiyuan.flapdev.R +import me.yifeiyuan.flapdev.components.SimpleDataBindingModel import me.yifeiyuan.flapdev.components.SimpleImageModel import me.yifeiyuan.flapdev.components.SimpleTextModel import me.yifeiyuan.flapdev.components.TestAllModel +import me.yifeiyuan.flapdev.databinding.FlapItemSimpleDatabindingBinding import me.yifeiyuan.flapdev.mockMultiTypeModels import me.yifeiyuan.ktx.foundation.othermodule.databinding.FlapItemVbBinding import me.yifeiyuan.ktx.foundation.othermodule.vb.ViewBindingModel @@ -180,7 +183,15 @@ class DSLTestcase : BaseTestcaseFragment() { } } - adapter.registerAdapterDelegates(simpleTextDelegate, simpleImageDelegate, testAllDelegate, viewBindingDelegate) + val dataBindingDelegate = adapterDelegateDataBinding(R.layout.flap_item_simple_databinding) { + + onBind { model -> + binding.model = model + binding.executePendingBindings() + } + } + + adapter.registerAdapterDelegates(simpleTextDelegate, simpleImageDelegate, testAllDelegate, viewBindingDelegate, dataBindingDelegate) adapter.registerAdapterHook(hook) } diff --git a/flap-dsl-databinding/.gitignore b/flap-dsl-databinding/.gitignore new file mode 100644 index 000000000..42afabfd2 --- /dev/null +++ b/flap-dsl-databinding/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/flap-dsl-databinding/build.gradle b/flap-dsl-databinding/build.gradle new file mode 100644 index 000000000..93ade14f2 --- /dev/null +++ b/flap-dsl-databinding/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 { + dataBinding 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-databinding/consumer-rules.pro b/flap-dsl-databinding/consumer-rules.pro new file mode 100644 index 000000000..e69de29bb diff --git a/flap-dsl-databinding/proguard-rules.pro b/flap-dsl-databinding/proguard-rules.pro new file mode 100644 index 000000000..481bb4348 --- /dev/null +++ b/flap-dsl-databinding/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-databinding/src/androidTest/java/me/yifeiyuan/flap/dsl/databinding/ExampleInstrumentedTest.kt b/flap-dsl-databinding/src/androidTest/java/me/yifeiyuan/flap/dsl/databinding/ExampleInstrumentedTest.kt new file mode 100644 index 000000000..8e19a3af1 --- /dev/null +++ b/flap-dsl-databinding/src/androidTest/java/me/yifeiyuan/flap/dsl/databinding/ExampleInstrumentedTest.kt @@ -0,0 +1,24 @@ +package me.yifeiyuan.flap.dsl.databinding + +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.databinding.test", appContext.packageName) + } +} \ No newline at end of file diff --git a/flap-dsl-databinding/src/main/AndroidManifest.xml b/flap-dsl-databinding/src/main/AndroidManifest.xml new file mode 100644 index 000000000..a2c78fc95 --- /dev/null +++ b/flap-dsl-databinding/src/main/AndroidManifest.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/flap-dsl-databinding/src/main/java/me/yifeiyuan/flap/dsl/databinding/AdapterDelegateDataBindingDsl.kt b/flap-dsl-databinding/src/main/java/me/yifeiyuan/flap/dsl/databinding/AdapterDelegateDataBindingDsl.kt new file mode 100644 index 000000000..2bc31d7fe --- /dev/null +++ b/flap-dsl-databinding/src/main/java/me/yifeiyuan/flap/dsl/databinding/AdapterDelegateDataBindingDsl.kt @@ -0,0 +1,70 @@ +package me.yifeiyuan.flap.dsl.databinding + +import android.view.LayoutInflater +import android.view.ViewGroup +import androidx.annotation.LayoutRes +import androidx.databinding.DataBindingUtil +import androidx.databinding.ViewDataBinding +import androidx.recyclerview.widget.RecyclerView +import me.yifeiyuan.flap.delegate.AdapterDelegate +import me.yifeiyuan.flap.dsl.DslComponent + +/** + * Created by 程序亦非猿 on 2022/9/21. + */ + +/** + * ViewDataBinding? + * 支持 AdapterDelegate ViewBinding DSL 功能 + * Created by 程序亦非猿 on 2022/9/20. + * @since 3.1.3 + */ +inline fun adapterDelegateDataBinding( + @LayoutRes layoutId: Int, + noinline dataBinding: ((layoutInflater: LayoutInflater, parent: ViewGroup) -> D) = { l, p -> DataBindingUtil.inflate(l, layoutId, p, false) }, + noinline isDelegateFor: ((model: Any) -> Boolean) = { m -> m.javaClass == T::class.java }, + itemId: Long = RecyclerView.NO_ID, + noinline componentInitializer: DataBindingDslComponent.() -> Unit): DataBindingDslAdapterDelegate { + return DataBindingDslAdapterDelegate(T::class.java, dataBinding, layoutId, itemId, isDelegateFor, componentInitializer) +} + +/** + * 支持 AdapterDelegate ViewBinding DSL 功能 + * Created by 程序亦非猿 on 2022/9/20. + * @since 3.1.3 + */ +class DataBindingDslAdapterDelegate( + private var modelClass: Class, + private var dataBinding: ((layoutInflater: LayoutInflater, parent: ViewGroup) -> D) = { l, p -> DataBindingUtil.inflate(l, layoutId, p, false) }, + @LayoutRes private var layoutId: Int, + private var itemId: Long = RecyclerView.NO_ID, + private var isDelegateFor: ((model: Any) -> Boolean) = { m -> m.javaClass == modelClass }, + private var block: DataBindingDslComponent.() -> Unit, +) : AdapterDelegate> { + + override fun delegate(model: Any): Boolean { + return isDelegateFor.invoke(model) + } + + override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup, viewType: Int): DataBindingDslComponent { + val viewDataBinding = dataBinding.invoke(inflater, parent) + val component = DataBindingDslComponent(viewDataBinding) + block.invoke(component) + return component + } + + override fun getItemId(model: Any, position: Int): Long { + return itemId + } + + override fun getItemViewType(model: Any): Int { + return layoutId + } +} + +/** + * 支持 AdapterDelegate DataBinding DSL 功能 + * Created by 程序亦非猿 on 2022/9/20. + * @since 3.1.3 + */ +class DataBindingDslComponent(val binding: V) : DslComponent(binding.root) \ No newline at end of file diff --git a/flap-dsl-databinding/src/test/java/me/yifeiyuan/flap/dsl/databinding/ExampleUnitTest.kt b/flap-dsl-databinding/src/test/java/me/yifeiyuan/flap/dsl/databinding/ExampleUnitTest.kt new file mode 100644 index 000000000..cc480576b --- /dev/null +++ b/flap-dsl-databinding/src/test/java/me/yifeiyuan/flap/dsl/databinding/ExampleUnitTest.kt @@ -0,0 +1,17 @@ +package me.yifeiyuan.flap.dsl.databinding + +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/settings.gradle b/settings.gradle index d674cde30..2a3186b24 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,3 +1,4 @@ +include ':flap-dsl-databinding' include ':flap-dsl-viewbinding' include ':ktmodule' include ':othermodule' From ffb6f1eed87ab887eadbd5259d3448a0c8605517 Mon Sep 17 00:00:00 2001 From: Fitz Date: Wed, 21 Sep 2022 21:15:19 +0800 Subject: [PATCH 2/2] =?UTF-8?q?:memo:=20=E6=9B=B4=E6=96=B0=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../databinding/AdapterDelegateDataBindingDsl.kt | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/flap-dsl-databinding/src/main/java/me/yifeiyuan/flap/dsl/databinding/AdapterDelegateDataBindingDsl.kt b/flap-dsl-databinding/src/main/java/me/yifeiyuan/flap/dsl/databinding/AdapterDelegateDataBindingDsl.kt index 2bc31d7fe..aeaf72b3e 100644 --- a/flap-dsl-databinding/src/main/java/me/yifeiyuan/flap/dsl/databinding/AdapterDelegateDataBindingDsl.kt +++ b/flap-dsl-databinding/src/main/java/me/yifeiyuan/flap/dsl/databinding/AdapterDelegateDataBindingDsl.kt @@ -10,14 +10,9 @@ import me.yifeiyuan.flap.delegate.AdapterDelegate import me.yifeiyuan.flap.dsl.DslComponent /** - * Created by 程序亦非猿 on 2022/9/21. - */ - -/** - * ViewDataBinding? - * 支持 AdapterDelegate ViewBinding DSL 功能 + * 支持 AdapterDelegate DataBinding DSL 功能 * Created by 程序亦非猿 on 2022/9/20. - * @since 3.1.3 + * @since 3.1.4 */ inline fun adapterDelegateDataBinding( @LayoutRes layoutId: Int, @@ -29,9 +24,9 @@ inline fun adapterDelegateDataBinding( } /** - * 支持 AdapterDelegate ViewBinding DSL 功能 + * 支持 AdapterDelegate DataBinding DSL 功能 * Created by 程序亦非猿 on 2022/9/20. - * @since 3.1.3 + * @since 3.1.4 */ class DataBindingDslAdapterDelegate( private var modelClass: Class, @@ -65,6 +60,6 @@ class DataBindingDslAdapterDelegate( /** * 支持 AdapterDelegate DataBinding DSL 功能 * Created by 程序亦非猿 on 2022/9/20. - * @since 3.1.3 + * @since 3.1.4 */ class DataBindingDslComponent(val binding: V) : DslComponent(binding.root) \ No newline at end of file