-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from AlanCheen/feature/dsl-viewbinding
🎨增加 ViewBinding DSL 功能支持
- Loading branch information
Showing
13 changed files
with
214 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
24 changes: 24 additions & 0 deletions
24
...binding/src/androidTest/java/me/yifeiyuan/flap/dsl/viewbinding/ExampleInstrumentedTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="me.yifeiyuan.flap.dsl.viewbinding"> | ||
|
||
</manifest> |
63 changes: 63 additions & 0 deletions
63
...wbinding/src/main/java/me/yifeiyuan/flap/dsl/viewbinding/AdapterDelegateViewBindingDsl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <reified T, reified V : ViewBinding> 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<T, V>.() -> Unit): ViewBindingDslAdapterDelegate<T, V> { | ||
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<T, V : ViewBinding>( | ||
private var modelClass: Class<T>, | ||
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<T, V>.() -> Unit, | ||
) : AdapterDelegate<T, ViewBindingDslComponent<T, V>> { | ||
|
||
override fun delegate(model: Any): Boolean { | ||
return isDelegateFor.invoke(model) | ||
} | ||
|
||
override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup, viewType: Int): ViewBindingDslComponent<T, V> { | ||
val binding = viewBinding(inflater, parent) | ||
val component = ViewBindingDslComponent<T, V>(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<T, V : ViewBinding>(val binding: V) : DslComponent<T>(binding.root) |
17 changes: 17 additions & 0 deletions
17
flap-dsl-viewbinding/src/test/java/me/yifeiyuan/flap/dsl/viewbinding/ExampleUnitTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
include ':flap-dsl-viewbinding' | ||
include ':ktmodule' | ||
include ':othermodule' | ||
include ':flap-gradle-plugin' | ||
|