Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example migration of Android application from Java to Kotlin - DO NOT MERGE #9

Open
wants to merge 4 commits into
base: java
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ dependencies {
compile 'io.reactivex.rxjava2:rxandroid:2.0.0'
compile 'com.jakewharton.timber:timber:4.3.0'
compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
kapt 'com.jakewharton:butterknife-compiler:8.4.0'

testCompile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:2.10.0'
testCompile 'org.assertj:assertj-core:3.8.0'
testCompile "com.nhaarman:mockito-kotlin-kt1.1:1.5.0"

androidTestCompile 'com.android.support.test:runner:1.0.1'
androidTestCompile 'com.android.support.test:rules:1.0.1'
Expand Down
29 changes: 0 additions & 29 deletions app/src/main/kotlin/com/example/unittesting/BasePresenter.java

This file was deleted.

23 changes: 23 additions & 0 deletions app/src/main/kotlin/com/example/unittesting/BasePresenter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.unittesting

import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable

open class BasePresenter<T> : Presenter<T> {

var view: T? = null
private val compositeDisposable = CompositeDisposable()

override fun createView(view: T) {
this.view = view
}

fun bindToLifecycle(disposable: Disposable) {
compositeDisposable.add(disposable)
}

override fun destroyView() {
compositeDisposable.clear()
view = null
}
}
8 changes: 0 additions & 8 deletions app/src/main/kotlin/com/example/unittesting/Presenter.java

This file was deleted.

8 changes: 8 additions & 0 deletions app/src/main/kotlin/com/example/unittesting/Presenter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.unittesting

internal interface Presenter<in T> {

fun createView(view: T)

fun destroyView()
}
17 changes: 0 additions & 17 deletions app/src/main/kotlin/com/example/unittesting/ResourceProvider.java

This file was deleted.

11 changes: 11 additions & 0 deletions app/src/main/kotlin/com/example/unittesting/ResourceProvider.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.unittesting

import android.content.res.Resources
import android.support.annotation.StringRes

class ResourceProvider(private var resources: Resources) {

fun getString(@StringRes stringResId: Int): String {
return resources.getString(stringResId)
}
}
21 changes: 0 additions & 21 deletions app/src/main/kotlin/com/example/unittesting/SchedulersFactory.java

This file was deleted.

20 changes: 20 additions & 0 deletions app/src/main/kotlin/com/example/unittesting/SchedulersFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.unittesting

import io.reactivex.Observable
import io.reactivex.ObservableSource
import io.reactivex.ObservableTransformer
import io.reactivex.android.schedulers.AndroidSchedulers

class SchedulersFactory {

fun <T> createMainThreadSchedulerTransformer(): ObservableTransformer<T, T> {
return SchedulersTransformer()
}
}

internal class SchedulersTransformer<T> : ObservableTransformer<T, T> {

override fun apply(upstream: Observable<T>): ObservableSource<T> {
return upstream.observeOn(AndroidSchedulers.mainThread())
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.example.unittesting.login.model

data class LoginCredentials(val login: String, val password: String)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.unittesting.login.model

import io.reactivex.Observable
import io.reactivex.Observable.fromCallable
import timber.log.Timber
import java.util.concurrent.TimeUnit

class LoginRepository {

fun login(login: String, password: String): Observable<Boolean> {
Timber.v("login %s with password %s", login, password)

return fromCallable { CORRECT_LOGIN == login && CORRECT_PASSWORD == password }
.delay(2000, TimeUnit.MILLISECONDS)
}

companion object {
internal val CORRECT_LOGIN = "dbacinski"
internal val CORRECT_PASSWORD = "correct"
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.unittesting.login.model

import io.reactivex.Observable

class LoginUseCase(private val loginRepository: LoginRepository) {

fun loginWithCredentialsWithStatus(credentials: LoginCredentials): Observable<Boolean> {
return loginRepository.login(credentials.login, credentials.password)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.unittesting.login.model

class LoginValidator {

companion object {
val EMPTY = ""
val MIN_PASSWORD_LENGTH = 6
}

fun validateLogin(login: String): Boolean {
return login != EMPTY
}

fun validatePassword(password: String): Boolean {
return password.length >= MIN_PASSWORD_LENGTH
}
}

This file was deleted.

Loading