From 01a0d760b1382fcf060ef0f391979a44aa94ccc7 Mon Sep 17 00:00:00 2001 From: StefMa Date: Fri, 6 Jul 2018 09:52:04 +0200 Subject: [PATCH 1/9] Create new Ti-Kotlin module --- build.gradle | 7 ++-- settings.gradle | 19 +++++----- thirtyinch-kotlin/.gitignore | 1 + thirtyinch-kotlin/build.gradle | 35 +++++++++++++++++++ thirtyinch-kotlin/proguard-rules.pro | 21 +++++++++++ .../src/main/AndroidManifest.xml | 1 + 6 files changed, 74 insertions(+), 10 deletions(-) create mode 100644 thirtyinch-kotlin/.gitignore create mode 100644 thirtyinch-kotlin/build.gradle create mode 100644 thirtyinch-kotlin/proguard-rules.pro create mode 100644 thirtyinch-kotlin/src/main/AndroidManifest.xml diff --git a/build.gradle b/build.gradle index fe70e216..d6e5915c 100644 --- a/build.gradle +++ b/build.gradle @@ -1,13 +1,16 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { + ext.kotlin_version = '1.2.51' + ext.kotlin_version = "1.2.51" repositories { jcenter() google() } dependencies { - classpath 'com.android.tools.build:gradle:3.0.1' - classpath 'com.vanniktech:gradle-android-junit-jacoco-plugin:0.10.0' + classpath "com.android.tools.build:gradle:3.0.1" + classpath "com.vanniktech:gradle-android-junit-jacoco-plugin:0.10.0" + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } diff --git a/settings.gradle b/settings.gradle index 02b95510..e64567a5 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,8 +1,11 @@ -include ':thirtyinch', - ':thirtyinch-logginginterceptor', - ':thirtyinch-plugin', - ':thirtyinch-rx', - ':thirtyinch-rx2', - ':thirtyinch-test', - ':sample', - ':plugin-test' +include( + ":thirtyinch", + ":thirtyinch-logginginterceptor", + ":thirtyinch-plugin", + ":thirtyinch-rx", + ":thirtyinch-rx2", + ":thirtyinch-test", + ":thirtyinch-kotlin", + ":sample", + ":plugin-test" +) diff --git a/thirtyinch-kotlin/.gitignore b/thirtyinch-kotlin/.gitignore new file mode 100644 index 00000000..796b96d1 --- /dev/null +++ b/thirtyinch-kotlin/.gitignore @@ -0,0 +1 @@ +/build diff --git a/thirtyinch-kotlin/build.gradle b/thirtyinch-kotlin/build.gradle new file mode 100644 index 00000000..3f81b36d --- /dev/null +++ b/thirtyinch-kotlin/build.gradle @@ -0,0 +1,35 @@ +apply plugin: "com.android.library" +apply plugin: "org.jetbrains.kotlin.android" + +android { + compileSdkVersion COMPILE_SDK_VERSION + buildToolsVersion BUILD_TOOLS_VERSION + + defaultConfig { + minSdkVersion MIN_SDK_VERSION + targetSdkVersion TARGET_SDK_VERSION + versionCode VERSION_CODE + versionName VERSION_NAME + } + buildTypes { + release { + minifyEnabled false + } + debug { + } + } + lintOptions { + abortOnError false + } +} + +dependencies { + api project(":thirtyinch") + api("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version") +} + +// For uploading to bintray +apply from: '../gradle/bintrayRelease.gradle' +configurePublish { + artifactId = 'thirtyinch-kotlin' +} \ No newline at end of file diff --git a/thirtyinch-kotlin/proguard-rules.pro b/thirtyinch-kotlin/proguard-rules.pro new file mode 100644 index 00000000..f1b42451 --- /dev/null +++ b/thirtyinch-kotlin/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 diff --git a/thirtyinch-kotlin/src/main/AndroidManifest.xml b/thirtyinch-kotlin/src/main/AndroidManifest.xml new file mode 100644 index 00000000..4d150e26 --- /dev/null +++ b/thirtyinch-kotlin/src/main/AndroidManifest.xml @@ -0,0 +1 @@ + From 49761f686835a29f6762c77d9921e179c5d12b4d Mon Sep 17 00:00:00 2001 From: StefMa Date: Fri, 6 Jul 2018 10:11:19 +0200 Subject: [PATCH 2/9] Introduce sendToViewKotlin method --- thirtyinch-kotlin/build.gradle | 3 ++ .../thirtyinch/kotlin/TiPresenter.kt | 21 ++++++++++++ .../thirtyinch/kotlin/TiPresenterTest.kt | 34 +++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 thirtyinch-kotlin/src/main/java/net/grandcentrix/thirtyinch/kotlin/TiPresenter.kt create mode 100644 thirtyinch-kotlin/src/test/java/net/grandcentrix/thirtyinch/kotlin/TiPresenterTest.kt diff --git a/thirtyinch-kotlin/build.gradle b/thirtyinch-kotlin/build.gradle index 3f81b36d..888ce528 100644 --- a/thirtyinch-kotlin/build.gradle +++ b/thirtyinch-kotlin/build.gradle @@ -26,6 +26,9 @@ android { dependencies { api project(":thirtyinch") api("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version") + + testImplementation "junit:junit:$junitVersion" + testImplementation "com.nhaarman:mockito-kotlin:1.5.0" } // For uploading to bintray diff --git a/thirtyinch-kotlin/src/main/java/net/grandcentrix/thirtyinch/kotlin/TiPresenter.kt b/thirtyinch-kotlin/src/main/java/net/grandcentrix/thirtyinch/kotlin/TiPresenter.kt new file mode 100644 index 00000000..2d6f069d --- /dev/null +++ b/thirtyinch-kotlin/src/main/java/net/grandcentrix/thirtyinch/kotlin/TiPresenter.kt @@ -0,0 +1,21 @@ +package net.grandcentrix.thirtyinch.kotlin + +import android.annotation.SuppressLint +import net.grandcentrix.thirtyinch.TiPresenter +import net.grandcentrix.thirtyinch.TiView + +/** + * Will call the given [block] in [TiPresenter.sendToView]. + * + * This have the benefit that we can omit the `it` inside the `sendToView{}` call. + * + * Example: + * ``` + * // Before + * presenter.sendToView { it.aViewMethod() } + * // After + * presenter.sendToViewKotlin { aViewMethod() } + * ``` + */ +@SuppressLint("RestrictedApi") +fun TiPresenter.sendToViewKotlin(block: V.() -> Unit) = sendToView { block.invoke(it) } \ No newline at end of file diff --git a/thirtyinch-kotlin/src/test/java/net/grandcentrix/thirtyinch/kotlin/TiPresenterTest.kt b/thirtyinch-kotlin/src/test/java/net/grandcentrix/thirtyinch/kotlin/TiPresenterTest.kt new file mode 100644 index 00000000..745fab9f --- /dev/null +++ b/thirtyinch-kotlin/src/test/java/net/grandcentrix/thirtyinch/kotlin/TiPresenterTest.kt @@ -0,0 +1,34 @@ +package net.grandcentrix.thirtyinch.kotlin + +import com.nhaarman.mockito_kotlin.mock +import com.nhaarman.mockito_kotlin.verify +import net.grandcentrix.thirtyinch.TiPresenter +import net.grandcentrix.thirtyinch.TiView +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +@RunWith(JUnit4::class) +class TiPresenterTest { + + interface View : TiView { + + fun aViewMethod() + + } + + class TestPresenter : TiPresenter() + + private val mockView = mock() + + @Test + fun `test sendToViewKotlin should view as this and call it`() = with(TestPresenter()) { + val tiTestPresenter = test() + tiTestPresenter.create() + tiTestPresenter.attachView(mockView) + + sendToViewKotlin { aViewMethod() } + + verify(mockView).aViewMethod() + } +} \ No newline at end of file From a87f19750615141f063f6118f5e8f2a171cbe6e7 Mon Sep 17 00:00:00 2001 From: StefMa Date: Fri, 6 Jul 2018 10:12:52 +0200 Subject: [PATCH 3/9] Set kotlin_version only once --- build.gradle | 1 - 1 file changed, 1 deletion(-) diff --git a/build.gradle b/build.gradle index d6e5915c..a3e256c3 100644 --- a/build.gradle +++ b/build.gradle @@ -1,7 +1,6 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = '1.2.51' ext.kotlin_version = "1.2.51" repositories { jcenter() From 2d57d02f1b19ae9c54c69253b8e4eb75086db0b0 Mon Sep 17 00:00:00 2001 From: StefMa Date: Mon, 9 Jul 2018 14:04:34 +0200 Subject: [PATCH 4/9] Rename to --- .../java/net/grandcentrix/thirtyinch/kotlin/TiPresenter.kt | 4 ++-- .../net/grandcentrix/thirtyinch/kotlin/TiPresenterTest.kt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/thirtyinch-kotlin/src/main/java/net/grandcentrix/thirtyinch/kotlin/TiPresenter.kt b/thirtyinch-kotlin/src/main/java/net/grandcentrix/thirtyinch/kotlin/TiPresenter.kt index 2d6f069d..f0c56dd3 100644 --- a/thirtyinch-kotlin/src/main/java/net/grandcentrix/thirtyinch/kotlin/TiPresenter.kt +++ b/thirtyinch-kotlin/src/main/java/net/grandcentrix/thirtyinch/kotlin/TiPresenter.kt @@ -14,8 +14,8 @@ import net.grandcentrix.thirtyinch.TiView * // Before * presenter.sendToView { it.aViewMethod() } * // After - * presenter.sendToViewKotlin { aViewMethod() } + * presenter.sendToViewKt { aViewMethod() } * ``` */ @SuppressLint("RestrictedApi") -fun TiPresenter.sendToViewKotlin(block: V.() -> Unit) = sendToView { block.invoke(it) } \ No newline at end of file +fun TiPresenter.sendToViewKt(block: V.() -> Unit) = sendToView { block.invoke(it) } \ No newline at end of file diff --git a/thirtyinch-kotlin/src/test/java/net/grandcentrix/thirtyinch/kotlin/TiPresenterTest.kt b/thirtyinch-kotlin/src/test/java/net/grandcentrix/thirtyinch/kotlin/TiPresenterTest.kt index 745fab9f..61fa120e 100644 --- a/thirtyinch-kotlin/src/test/java/net/grandcentrix/thirtyinch/kotlin/TiPresenterTest.kt +++ b/thirtyinch-kotlin/src/test/java/net/grandcentrix/thirtyinch/kotlin/TiPresenterTest.kt @@ -27,7 +27,7 @@ class TiPresenterTest { tiTestPresenter.create() tiTestPresenter.attachView(mockView) - sendToViewKotlin { aViewMethod() } + sendToViewKt { aViewMethod() } verify(mockView).aViewMethod() } From 968d89a4d96c4b878e8348d768f28bc0e2cf6f9b Mon Sep 17 00:00:00 2001 From: StefMa Date: Mon, 9 Jul 2018 14:11:57 +0200 Subject: [PATCH 5/9] Add ti-kotlin to Readme --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index ff6eb81f..59a82740 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,9 @@ dependencies { implementation "net.grandcentrix.thirtyinch:thirtyinch-rx2:$thirtyinchVersion" implementation "net.grandcentrix.thirtyinch:thirtyinch-logginginterceptor:$thirtyinchVersion" + + // kotlin extensions + implementation "net.grandcentrix.thirtyinch:thirtyinch-kotlin:$thirtyinchVersion" // CompositeAndroid plugin // When you are using ThirtyInch with the CompositeAndroid extension you have to manually @@ -250,6 +253,30 @@ public class HelloWorldActivity extends TiActivity { + + override fun onCreate() { + sendToViewKt { + showText("Hello World") + } + } + +} + +interface HelloWorldView : TiView { + fun showText(text: String) +} +``` +Back in the Java days we had to use `it` inside the `sendToView`-lambda. +With this extension the `TiView` will be give over to the lambda as `this`. + ### [RxJava](https://github.com/ReactiveX/RxJava) Using RxJava for networking is very often used. From 6ecd5f3f974286cd8501377e213075c3c85aec36 Mon Sep 17 00:00:00 2001 From: StefMa Date: Mon, 9 Jul 2018 15:35:42 +0200 Subject: [PATCH 6/9] Rename to deliverToView --- README.md | 2 +- .../java/net/grandcentrix/thirtyinch/kotlin/TiPresenter.kt | 4 ++-- .../net/grandcentrix/thirtyinch/kotlin/TiPresenterTest.kt | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 59a82740..a48e40c0 100644 --- a/README.md +++ b/README.md @@ -263,7 +263,7 @@ Take a look at the `sendToViewKt` example: class HelloWorldPresenter : TiPresenter { override fun onCreate() { - sendToViewKt { + deliverToView { showText("Hello World") } } diff --git a/thirtyinch-kotlin/src/main/java/net/grandcentrix/thirtyinch/kotlin/TiPresenter.kt b/thirtyinch-kotlin/src/main/java/net/grandcentrix/thirtyinch/kotlin/TiPresenter.kt index f0c56dd3..0c70f8a8 100644 --- a/thirtyinch-kotlin/src/main/java/net/grandcentrix/thirtyinch/kotlin/TiPresenter.kt +++ b/thirtyinch-kotlin/src/main/java/net/grandcentrix/thirtyinch/kotlin/TiPresenter.kt @@ -14,8 +14,8 @@ import net.grandcentrix.thirtyinch.TiView * // Before * presenter.sendToView { it.aViewMethod() } * // After - * presenter.sendToViewKt { aViewMethod() } + * presenter.deliverToView { aViewMethod() } * ``` */ @SuppressLint("RestrictedApi") -fun TiPresenter.sendToViewKt(block: V.() -> Unit) = sendToView { block.invoke(it) } \ No newline at end of file +fun TiPresenter.deliverToView(block: V.() -> Unit) = sendToView { block.invoke(it) } \ No newline at end of file diff --git a/thirtyinch-kotlin/src/test/java/net/grandcentrix/thirtyinch/kotlin/TiPresenterTest.kt b/thirtyinch-kotlin/src/test/java/net/grandcentrix/thirtyinch/kotlin/TiPresenterTest.kt index 61fa120e..a4cd75ea 100644 --- a/thirtyinch-kotlin/src/test/java/net/grandcentrix/thirtyinch/kotlin/TiPresenterTest.kt +++ b/thirtyinch-kotlin/src/test/java/net/grandcentrix/thirtyinch/kotlin/TiPresenterTest.kt @@ -27,7 +27,7 @@ class TiPresenterTest { tiTestPresenter.create() tiTestPresenter.attachView(mockView) - sendToViewKt { aViewMethod() } + deliverToView { aViewMethod() } verify(mockView).aViewMethod() } From b74145a712b42811ff0f22e25975e5b45de67c54 Mon Sep 17 00:00:00 2001 From: Pascal Welsch Date: Tue, 10 Jul 2018 10:52:21 +0200 Subject: [PATCH 7/9] Improve Kotlin docs --- README.md | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index a48e40c0..3a5dc913 100644 --- a/README.md +++ b/README.md @@ -255,19 +255,28 @@ public class HelloWorldActivity extends TiActivity { override fun onCreate() { - deliverToView { - showText("Hello World") - } + // normal java API + sendToView { + it.showText("Hello World") + } + + // kotlin extension + deliverToView { + showText("Hello World") + } } - } interface HelloWorldView : TiView { @@ -275,7 +284,6 @@ interface HelloWorldView : TiView { } ``` Back in the Java days we had to use `it` inside the `sendToView`-lambda. -With this extension the `TiView` will be give over to the lambda as `this`. ### [RxJava](https://github.com/ReactiveX/RxJava) From 73d1fc898d151dc460b4ec7178b57e60501618db Mon Sep 17 00:00:00 2001 From: Pascal Welsch Date: Tue, 10 Jul 2018 10:52:45 +0200 Subject: [PATCH 8/9] Remove unnecessary invoke() --- .../java/net/grandcentrix/thirtyinch/kotlin/TiPresenter.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/thirtyinch-kotlin/src/main/java/net/grandcentrix/thirtyinch/kotlin/TiPresenter.kt b/thirtyinch-kotlin/src/main/java/net/grandcentrix/thirtyinch/kotlin/TiPresenter.kt index 0c70f8a8..bb300779 100644 --- a/thirtyinch-kotlin/src/main/java/net/grandcentrix/thirtyinch/kotlin/TiPresenter.kt +++ b/thirtyinch-kotlin/src/main/java/net/grandcentrix/thirtyinch/kotlin/TiPresenter.kt @@ -7,7 +7,7 @@ import net.grandcentrix.thirtyinch.TiView /** * Will call the given [block] in [TiPresenter.sendToView]. * - * This have the benefit that we can omit the `it` inside the `sendToView{}` call. + * This have the benefit that we can omit the `it` inside the `sendToView { }` call. * * Example: * ``` @@ -18,4 +18,4 @@ import net.grandcentrix.thirtyinch.TiView * ``` */ @SuppressLint("RestrictedApi") -fun TiPresenter.deliverToView(block: V.() -> Unit) = sendToView { block.invoke(it) } \ No newline at end of file +fun TiPresenter.deliverToView(block: V.() -> Unit) = sendToView { block(it) } \ No newline at end of file From 8c31d1261589283f1b4690283e840059e6068c36 Mon Sep 17 00:00:00 2001 From: Pascal Welsch Date: Tue, 10 Jul 2018 10:53:14 +0200 Subject: [PATCH 9/9] Add test for deliverToView without view --- .../thirtyinch/kotlin/TiPresenterTest.kt | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/thirtyinch-kotlin/src/test/java/net/grandcentrix/thirtyinch/kotlin/TiPresenterTest.kt b/thirtyinch-kotlin/src/test/java/net/grandcentrix/thirtyinch/kotlin/TiPresenterTest.kt index a4cd75ea..e8907384 100644 --- a/thirtyinch-kotlin/src/test/java/net/grandcentrix/thirtyinch/kotlin/TiPresenterTest.kt +++ b/thirtyinch-kotlin/src/test/java/net/grandcentrix/thirtyinch/kotlin/TiPresenterTest.kt @@ -1,20 +1,17 @@ package net.grandcentrix.thirtyinch.kotlin -import com.nhaarman.mockito_kotlin.mock -import com.nhaarman.mockito_kotlin.verify +import com.nhaarman.mockito_kotlin.* import net.grandcentrix.thirtyinch.TiPresenter import net.grandcentrix.thirtyinch.TiView -import org.junit.Test -import org.junit.runner.RunWith -import org.junit.runners.JUnit4 +import org.junit.* +import org.junit.runner.* +import org.junit.runners.* @RunWith(JUnit4::class) class TiPresenterTest { interface View : TiView { - fun aViewMethod() - } class TestPresenter : TiPresenter() @@ -22,13 +19,22 @@ class TiPresenterTest { private val mockView = mock() @Test - fun `test sendToViewKotlin should view as this and call it`() = with(TestPresenter()) { + fun `test deliverToView should view as this and call it`() = with(TestPresenter()) { val tiTestPresenter = test() - tiTestPresenter.create() tiTestPresenter.attachView(mockView) deliverToView { aViewMethod() } verify(mockView).aViewMethod() } + + @Test + fun `test deliverToView without attached view`() = with(TestPresenter()) { + val tiTestPresenter = test() + deliverToView { aViewMethod() } + verify(mockView, never()).aViewMethod() + + tiTestPresenter.attachView(mockView) + verify(mockView).aViewMethod() + } } \ No newline at end of file