diff --git a/README.md b/README.md index 422f3b7..55b19d2 100644 --- a/README.md +++ b/README.md @@ -55,13 +55,13 @@ Groups are the building block of Groupie. An individual `Item` (the unit which Kotlin ```kotlin -groupAdapter += HeaderItem() -groupAdapter += CommentItem() +groupAdapter.add(HeaderItem()) +groupAdapter.add(CommentItem()) val section = Section() section.setHeader(HeaderItem()) section.addAll(bodyItems) -groupAdapter += section +groupAdapter.add(section) ``` Java @@ -168,6 +168,33 @@ Finally, in your `Item`, override } ``` +### Add and remove items with `+=` and `-=` in Kotlin + +`groupie-ktx` provides Kotlin Extensions overriding the `plusAssign` and `minusAssign` operations on both group adapters and sections. This enables a cleaner syntax when creating and modifying layouts. + +```gradle +implementation "com.github.lisawray.groupie:groupie:$groupie_version" +implementation "com.github.lisawray.groupie:groupie-ktx:$groupie_version" +``` + +Instances of `.add()`, `.addAll()`, and `.remove()` can be replaced with `+=` and `-=` + +```kotlin +// Without groupie-ktx +mySection.add(HeaderItem()) +mySection.remove(CommentItem()) +groupAdapter.add(mySection) +groupAdapter.remove(DescriptionItem()) + +// With groupie-ktx +mySection += HeaderItem() +mySection -= CommentItem() +groupAdapter += mySection +groupAdapter -= DescriptionItem() +``` + +Note: `groupie-ktx` is included with the deprecated `groupie-kotlin-android-extensions` module. If you depend on `groupie-kotlin-android-extensions`, you do not need to also depend on this module. + ### Note: Items can also declare their own column span and whether they are draggable or swipeable. diff --git a/library-groupie-ktx/.gitignore b/library-groupie-ktx/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/library-groupie-ktx/.gitignore @@ -0,0 +1 @@ +/build diff --git a/library-groupie-ktx/build.gradle b/library-groupie-ktx/build.gradle new file mode 100644 index 0000000..aa37652 --- /dev/null +++ b/library-groupie-ktx/build.gradle @@ -0,0 +1,111 @@ +apply plugin: 'com.android.library' +apply plugin: 'kotlin-android' +apply plugin: 'maven-publish' + + +archivesBaseName = 'groupie-ktx' + + +android { + compileSdkVersion rootProject.sdkVersion + + + defaultConfig { + minSdkVersion rootProject.minimumSdkVersion + targetSdkVersion rootProject.sdkVersion + versionCode 20 + versionName "1.0" + } + + signingConfigs { + release { + } + } + buildTypes { + release { + signingConfig signingConfigs.release + minifyEnabled false + } + } + +} + +dependencies { + implementation project(':library') + implementation "androidx.recyclerview:recyclerview:1.2.1" +} + +tasks.withType(Javadoc).all { + enabled = false +} + + +group = "com.github.lisawray.groupie" +version = "2.10.1" + +task javadoc(type: Javadoc) { + configurations.implementation.canBeResolved(true) + configurations.api.canBeResolved(true) + + failOnError false + + source = android.sourceSets.main.java.srcDirs + classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) + //destinationDir = file("../javadoc/") + classpath += configurations.api +} + +task sourcesJar(type: Jar) { + from android.sourceSets.main.java.srcDirs + archiveClassifier = "sources" +} + +task javadocJar(type: Jar, dependsOn: javadoc) { + classifier = 'javadoc' + from javadoc.destinationDir +} + +artifacts { + archives sourcesJar + archives javadocJar +} + +// Because the components are created only during the afterEvaluate phase, you must +// configure your publications using the afterEvaluate() lifecycle method. +afterEvaluate { + publishing { + publications { + // Creates a Maven publication called "release". + release(MavenPublication) { + // Applies the component for the release build variant. + from components.release + artifact(sourcesJar) + + // You can then customize attributes of the publication as shown below. + groupId = 'com.github.lisawray.groupie' + artifactId = 'groupie-ktx' + version = '2.10.1' + + pom.withXml { + def dependenciesNode = (asNode().get("dependencies") as groovy.util.NodeList).get(0) as groovy.util.Node + def configurationNames = ["implementation", "api"] + + configurationNames.forEach { configurationName -> + configurations[configurationName].allDependencies.forEach { + if (it.group != null && it.version != "unspecified") { + def dependencyNode = dependenciesNode.appendNode("dependency") + dependencyNode.appendNode("groupId", it.group) + dependencyNode.appendNode("artifactId", it.name) + dependencyNode.appendNode("version", it.version) + // dependencyNode.appendNode("scope", configurationName) + } + } + } + } + } + } + } +} + + + diff --git a/library-groupie-ktx/gradle.properties b/library-groupie-ktx/gradle.properties new file mode 100644 index 0000000..2b783c9 --- /dev/null +++ b/library-groupie-ktx/gradle.properties @@ -0,0 +1,6 @@ +POM_NAME=groupie-ktx +POM_DESCRIPTION=Library to help with complex RecyclerViews +POM_BINTRAY_NAME=groupie-ktx +POM_ARTIFACT_ID=groupie-ktx +POM_PACKAGING=aar +POM_VERSION=2.10.1 \ No newline at end of file diff --git a/library-groupie-ktx/src/main/AndroidManifest.xml b/library-groupie-ktx/src/main/AndroidManifest.xml new file mode 100644 index 0000000..1882af6 --- /dev/null +++ b/library-groupie-ktx/src/main/AndroidManifest.xml @@ -0,0 +1,2 @@ + diff --git a/library-kotlin-android-extensions/src/main/java/com/xwray/groupie/groupiex/GroupAdapterExt.kt b/library-groupie-ktx/src/main/java/com/xwray/groupie/groupiex/GroupAdapterExt.kt similarity index 85% rename from library-kotlin-android-extensions/src/main/java/com/xwray/groupie/groupiex/GroupAdapterExt.kt rename to library-groupie-ktx/src/main/java/com/xwray/groupie/groupiex/GroupAdapterExt.kt index 70ba54f..f5e0a5d 100644 --- a/library-kotlin-android-extensions/src/main/java/com/xwray/groupie/groupiex/GroupAdapterExt.kt +++ b/library-groupie-ktx/src/main/java/com/xwray/groupie/groupiex/GroupAdapterExt.kt @@ -4,7 +4,6 @@ import com.xwray.groupie.Group import com.xwray.groupie.GroupAdapter import com.xwray.groupie.GroupieViewHolder -// TODO(zhuinden): move this into its own artifact later: `groupiex` (or rather, `groupie-ktx`) operator fun GroupAdapter.plusAssign(element: Group) = this.add(element) operator fun GroupAdapter.plusAssign(groups: Collection) = this.addAll(groups) operator fun GroupAdapter.minusAssign(element: Group) = this.remove(element) diff --git a/library-kotlin-android-extensions/src/main/java/com/xwray/groupie/groupiex/SectionExt.kt b/library-groupie-ktx/src/main/java/com/xwray/groupie/groupiex/SectionExt.kt similarity index 80% rename from library-kotlin-android-extensions/src/main/java/com/xwray/groupie/groupiex/SectionExt.kt rename to library-groupie-ktx/src/main/java/com/xwray/groupie/groupiex/SectionExt.kt index 44e234f..a3bda23 100644 --- a/library-kotlin-android-extensions/src/main/java/com/xwray/groupie/groupiex/SectionExt.kt +++ b/library-groupie-ktx/src/main/java/com/xwray/groupie/groupiex/SectionExt.kt @@ -3,7 +3,6 @@ package com.xwray.groupie.groupiex import com.xwray.groupie.Group import com.xwray.groupie.Section -// TODO(zhuinden): move this into its own artifact later: `groupiex` (or rather, `groupie-ktx`) operator fun Section.plusAssign(element: Group) = this.add(element) operator fun Section.plusAssign(groups: Collection) = this.addAll(groups) operator fun Section.minusAssign(element: Group) = this.remove(element) diff --git a/library-kotlin-android-extensions/build.gradle b/library-kotlin-android-extensions/build.gradle index b3d3c76..b15c800 100644 --- a/library-kotlin-android-extensions/build.gradle +++ b/library-kotlin-android-extensions/build.gradle @@ -39,6 +39,7 @@ android { dependencies { implementation project(':library') + implementation project(':library-groupie-ktx') implementation "androidx.recyclerview:recyclerview:1.2.1" } diff --git a/settings.gradle b/settings.gradle index 9485eab..dd8de4e 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1 @@ -include ':example-databinding', ':library-databinding', ':library', ':example', ':example-shared', ':library-kotlin-android-extensions', 'example-viewbinding', ':library-viewbinding' +include ':example-databinding', ':library-databinding', ':library', ':example', ':example-shared', ':library-kotlin-android-extensions', ':library-groupie-ktx', 'example-viewbinding', ':library-viewbinding'