Skip to content

Commit

Permalink
ViewBinding DSL, AdapterDelegateLayoutContainer bugfix (#82)
Browse files Browse the repository at this point in the history
* https://youtrack.jetbrains.com/issue/KT-28617

* view binding dsl

* view type overriding

* readme.md update

* note about DataBinding in readme, updated sample

* Revert "view type overriding"

This reverts commit be02fb2
  • Loading branch information
RomanPozdeev authored Mar 9, 2020
1 parent 7cf0ea3 commit 4da596f
Show file tree
Hide file tree
Showing 21 changed files with 886 additions and 21 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ implementation 'com.hannesdorfmann:adapterdelegates4-kotlin-dsl:4.2.0'
// If you use Kotlin Android Extensions and synthetic properties (alternative to findViewById())
implementation 'com.hannesdorfmann:adapterdelegates4-kotlin-dsl-layoutcontainer:4.2.0'
// If you use ViewBinding
implementation 'com.hannesdorfmann:adapterdelegates4-kotlin-dsl-viewbinding:4.2.0'
```

## How to use it
Expand Down Expand Up @@ -57,6 +60,21 @@ fun catAdapterDelegate(itemClickedListener : (Cat) -> Unit) = adapterDelegateLay
}
```

In case you want to use ViewBinding\DataBinding use `adapterDelegateViewBinding` instead of `adapterDelegate` like this:

```kotlin
fun cat2AdapterDelegate() = adapterDelegateViewBinding<Cat, DisplayableItem, ItemCatBinding>(
{ layoutInflater, root -> ItemCatBinding.inflate(layoutInflater, root, false) }
) {
binding.name.setOnClickListener {
Log.d("Click", "Click on $item")
}
bind {
binding.name.text = item.name
}
}
```

You have to specify if a specific AdapterDelegate is responsible for a specific item.
Per default this is done with an `instanceof` check like `Cat instanceof Animal`.
You can override this if you want to handle it in a custom way by setting the `on` lambda
Expand Down
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ android {
disable 'GoogleAppIndexingWarning'
abortOnError false
}

viewBinding {
enabled = true
}
}


Expand All @@ -44,6 +48,7 @@ dependencies {
implementation project(':paging')
implementation project(':kotlin-dsl')
implementation project(':kotlin-dsl-layoutcontainer')
implementation project(':kotlin-dsl-viewbinding')
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import com.hannesdorfmann.adapterdelegates4.sample.adapterdelegates.Advertisemen
import com.hannesdorfmann.adapterdelegates4.sample.adapterdelegates.DogAdapterDelegate
import com.hannesdorfmann.adapterdelegates4.sample.adapterdelegates.GeckoAdapterDelegate
import com.hannesdorfmann.adapterdelegates4.sample.adapterdelegates.SnakeListItemAdapterDelegate
import com.hannesdorfmann.adapterdelegates4.sample.dsl.catAdapterDelegate
import com.hannesdorfmann.adapterdelegates4.sample.dsl.cat2AdapterDelegate
import com.hannesdorfmann.adapterdelegates4.sample.model.DisplayableItem

class MainListAdapter(activity: Activity) : ListDelegationAdapter<List<DisplayableItem>>(
AdvertisementAdapterDelegate(activity),
catAdapterDelegate(),
cat2AdapterDelegate(),
DogAdapterDelegate(activity),
GeckoAdapterDelegate(activity),
SnakeListItemAdapterDelegate(activity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package com.hannesdorfmann.adapterdelegates4.sample.dsl

import android.util.Log
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateLayoutContainer
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
import com.hannesdorfmann.adapterdelegates4.sample.R
import com.hannesdorfmann.adapterdelegates4.sample.databinding.ItemCatBinding
import com.hannesdorfmann.adapterdelegates4.sample.model.Cat
import com.hannesdorfmann.adapterdelegates4.sample.model.DisplayableItem
import kotlinx.android.synthetic.main.item_cat.*
import kotlinx.android.synthetic.main.item_cat.name

// Example
fun catAdapterDelegate() = adapterDelegateLayoutContainer<Cat, DisplayableItem>(R.layout.item_cat) {
fun catAdapterDelegate() = adapterDelegateLayoutContainer<Cat,
DisplayableItem>(R.layout.item_cat) {

name.setOnClickListener {
Log.d("Click", "Click on $item")
Expand All @@ -18,3 +21,14 @@ fun catAdapterDelegate() = adapterDelegateLayoutContainer<Cat, DisplayableItem>(
name.text = item.name
}
}

fun cat2AdapterDelegate() = adapterDelegateViewBinding<Cat, DisplayableItem, ItemCatBinding>(
{ layoutInflater, root -> ItemCatBinding.inflate(layoutInflater, root, false) }
) {
binding.name.setOnClickListener {
Log.d("Click", "Click on $item")
}
bind {
binding.name.text = item.name
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
classpath 'com.android.tools.build:gradle:3.6.1'
classpath 'com.vanniktech:gradle-maven-publish-plugin:0.6.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

Expand Down
2 changes: 1 addition & 1 deletion deploy_snapshot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ else
echo "signing.secretKeyRingFile=/home/travis/.gnupg/secring.gpg" >> library/gradle.properties
echo "org.gradle.parallel=false" >> gradle.properties
echo "org.gradle.configureondemand=false" >> gradle.properties
./gradlew --no-daemon :library:uploadArchives :paging:uploadArchives :kotlin-dsl:uploadArchives :kotlin-dsl-layoutcontainer:uploadArchives -Dorg.gradle.parallel=false -Dorg.gradle.configureondemand=false
./gradlew --no-daemon :library:uploadArchives :paging:uploadArchives :kotlin-dsl:uploadArchives :kotlin-dsl-layoutcontainer:uploadArchives :kotlin-dsl-viewbinding:uploadArchives -Dorg.gradle.parallel=false -Dorg.gradle.configureondemand=false
rm key.gpg
git reset --hard
echo "Snapshot deployed!"
Expand Down
9 changes: 6 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

VERSION_NAME=4.2.1-SNAPSHOT
VERSION_CODE=421-SNAPSHOT
VERSION_NAME=4.2.2-SNAPSHOT
VERSION_CODE=422-SNAPSHOT
GROUP=com.hannesdorfmann


Expand All @@ -34,4 +34,7 @@ POM_DEVELOPER_ID=hannesdorfmann
POM_DEVELOPER_NAME=Hannes Dorfmann

signing.keyId=E508C045

# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app's APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Jul 03 19:42:09 CEST 2019
#Tue Feb 04 08:47:41 SAMT 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import androidx.annotation.StringRes
import androidx.recyclerview.widget.RecyclerView
import com.hannesdorfmann.adapterdelegates4.AbsListItemAdapterDelegate
import com.hannesdorfmann.adapterdelegates4.AdapterDelegate
import kotlinx.android.extensions.CacheImplementation
import kotlinx.android.extensions.ContainerOptions
import kotlinx.android.extensions.LayoutContainer

/**
Expand Down Expand Up @@ -115,6 +117,7 @@ internal class DslLayoutContainerListAdapterDelegate<I : T, T>(
*
* @since 4.1.0
*/
@ContainerOptions(cache = CacheImplementation.SPARSE_ARRAY)
class AdapterDelegateLayoutContainerViewHolder<T>(
override val containerView: View
) : RecyclerView.ViewHolder(containerView), LayoutContainer {
Expand Down
1 change: 1 addition & 0 deletions kotlin-dsl-viewbinding/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
59 changes: 59 additions & 0 deletions kotlin-dsl-viewbinding/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

androidExtensions {
experimental = true
}

gradle.ext.isCiServer = System.getenv().containsKey("CI")
logger.warn("Running on CI: ${gradle.ext.isCiServer}")

if (gradle.ext.isCiServer) {
apply plugin: "com.vanniktech.maven.publish"
mavenPublish {
releaseRepositoryUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
snapshotRepositoryUrl = "https://oss.sonatype.org/content/repositories/snapshots/"
}
}

android {
compileSdkVersion rootProject.ext.compileSdk

defaultConfig {
minSdkVersion 14
targetSdkVersion 28
versionCode 2
versionName "4.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

compileOptions {
sourceCompatibility rootProject.ext.javaSourceCompatibility
targetCompatibility rootProject.ext.javaTargetCompatibility
}

libraryVariants.all {
it.generateBuildConfig.enabled = false
}

viewBinding {
enabled = true
}
}

dependencies {
api project(":library")

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-inline:2.21.0'
}

// apply from: 'https://raw.github.com/sockeqwe/gradle-mvn-push/master/gradle-mvn-push.gradle'
// apply from: 'https://raw.githubusercontent.com/Tickaroo/findbugs-script/master/findbugs.gradle'
Empty file.
8 changes: 8 additions & 0 deletions kotlin-dsl-viewbinding/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
POM_NAME = AdapterDelegates-Kotlin-Dsl
POM_ARTIFACT_ID = adapterdelegates4-kotlin-dsl-viewbinding
POM_PACKAGING = aar

# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app's APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
21 changes: 21 additions & 0 deletions kotlin-dsl-viewbinding/proguard-rules.pro
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
1 change: 1 addition & 0 deletions kotlin-dsl-viewbinding/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="com.hannesdorfmann.adapterdelegates4.dsl" />
Loading

0 comments on commit 4da596f

Please sign in to comment.