Skip to content
This repository has been archived by the owner on Mar 16, 2021. It is now read-only.

Basic implementation for TI kotlin #150

Merged
merged 9 commits into from
Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -250,6 +253,38 @@ public class HelloWorldActivity extends TiActivity<HelloWorldPresenter, HelloWor

`LoggingInterceptor` is available as module and logs all calls to the view.

### Kotlin

Using Kotlin these days is a no-brainer.
`ThirtyInch` provides some extension methods to improve itself even further!

#### SendToView

When using `sendToView`, repeating `it.*` inside the lambda is quite annoying.
It's clear that the methods are called *on* the view.
With the kotlin extension `deliverToView` the `TiView` will be give over to the lambda as `this`.
```kotlin
class HelloWorldPresenter : TiPresenter<HelloWorldView> {

override fun onCreate() {
// normal java API
sendToView {
it.showText("Hello World")
}

// kotlin extension
deliverToView {
showText("Hello World")
}
}
}

interface HelloWorldView : TiView {
fun showText(text: String)
}
```
Back in the Java days we had to use `it` inside the `sendToView`-lambda.

### [RxJava](https://github.com/ReactiveX/RxJava)

Using RxJava for networking is very often used.
Expand Down
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
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"
}
}

Expand Down
19 changes: 11 additions & 8 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -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"
)
1 change: 1 addition & 0 deletions thirtyinch-kotlin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
38 changes: 38 additions & 0 deletions thirtyinch-kotlin/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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")

testImplementation "junit:junit:$junitVersion"
testImplementation "com.nhaarman:mockito-kotlin:1.5.0"
}

// For uploading to bintray
apply from: '../gradle/bintrayRelease.gradle'
configurePublish {
artifactId = 'thirtyinch-kotlin'
}
21 changes: 21 additions & 0 deletions thirtyinch-kotlin/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 thirtyinch-kotlin/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="net.grandcentrix.thirtyinch.kotlin"/>
Original file line number Diff line number Diff line change
@@ -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.deliverToView { aViewMethod() }
* ```
*/
@SuppressLint("RestrictedApi")
fun <V : TiView> TiPresenter<V>.deliverToView(block: V.() -> Unit) = sendToView { block(it) }
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package net.grandcentrix.thirtyinch.kotlin

import com.nhaarman.mockito_kotlin.*
import net.grandcentrix.thirtyinch.TiPresenter
import net.grandcentrix.thirtyinch.TiView
import org.junit.*
import org.junit.runner.*
import org.junit.runners.*

@RunWith(JUnit4::class)
class TiPresenterTest {

interface View : TiView {
fun aViewMethod()
}

class TestPresenter : TiPresenter<View>()

private val mockView = mock<View>()

@Test
fun `test deliverToView should view as this and call it`() = with(TestPresenter()) {
val tiTestPresenter = test()
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()
}
}