Skip to content

Commit

Permalink
Adds Koin
Browse files Browse the repository at this point in the history
This commit not only adds Koin, but also configures a basic
infrastructure for testing, allowing us to change behaviors as needed.
  • Loading branch information
rafaeltoledo committed May 21, 2018
1 parent 103d256 commit 762aa21
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 7 deletions.
8 changes: 8 additions & 0 deletions app/build.gradle
@@ -1,5 +1,6 @@
apply plugin: 'com.android.application'
apply plugin: 'org.jetbrains.kotlin.android'
apply plugin: 'org.jetbrains.kotlin.kapt'

apply from: "$rootDir/gradle/coverage.gradle"

Expand Down Expand Up @@ -61,12 +62,19 @@ android {
dependencies {
implementation "com.android.support:appcompat-v7:$versions.supportLibrary"

implementation "android.arch.lifecycle:extensions:$versions.archComponents"
kapt "android.arch.lifecycle:compiler:$versions.archComponents"

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$versions.kotlin"

implementation 'com.google.firebase:firebase-core:15.0.2'

implementation "org.koin:koin-android:$versions.koin"
implementation "org.koin:koin-android-architecture:$versions.koin"

testImplementation 'junit:junit:4.12'
testImplementation 'org.robolectric:robolectric:3.8'
testImplementation 'com.google.truth:truth:0.40'

androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
Expand Down
10 changes: 10 additions & 0 deletions app/src/debug/AndroidManifest.xml
@@ -0,0 +1,10 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.rafaeltoledo.social">

<application>
<meta-data
android:name="firebase_analytics_collection_deactivated"
android:value="true" />
</application>

</manifest>
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Expand Up @@ -3,6 +3,7 @@
package="net.rafaeltoledo.social">

<application
android:name=".SocialApp"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand Down
16 changes: 15 additions & 1 deletion app/src/main/kotlin/net/rafaeltoledo/social/MainActivity.kt
@@ -1,5 +1,19 @@
package net.rafaeltoledo.social

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.TextView
import org.koin.android.architecture.ext.viewModel

class MainActivity : AppCompatActivity()
class MainActivity : AppCompatActivity() {

private val mainViewModel: MainViewModel by viewModel()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(TextView(this).apply {
id = R.id.content
text = mainViewModel.getString()
})
}
}
8 changes: 8 additions & 0 deletions app/src/main/kotlin/net/rafaeltoledo/social/MainViewModel.kt
@@ -0,0 +1,8 @@
package net.rafaeltoledo.social

import android.arch.lifecycle.ViewModel

class MainViewModel(private val string: String) : ViewModel() {

fun getString() = string
}
17 changes: 17 additions & 0 deletions app/src/main/kotlin/net/rafaeltoledo/social/SocialApp.kt
@@ -0,0 +1,17 @@
package net.rafaeltoledo.social

import android.app.Application
import net.rafaeltoledo.social.di.firstModule
import net.rafaeltoledo.social.di.viewModelModule
import org.koin.android.ext.android.startKoin

open class SocialApp : Application() {

override fun onCreate() {
super.onCreate()
startKoin(listOf(
viewModelModule,
firstModule
))
}
}
7 changes: 7 additions & 0 deletions app/src/main/kotlin/net/rafaeltoledo/social/di/FirstModule.kt
@@ -0,0 +1,7 @@
package net.rafaeltoledo.social.di

import org.koin.dsl.module.applicationContext

val firstModule = applicationContext {
bean { "Social App" }
}
@@ -0,0 +1,9 @@
package net.rafaeltoledo.social.di

import net.rafaeltoledo.social.MainViewModel
import org.koin.android.architecture.ext.viewModel
import org.koin.dsl.module.applicationContext

val viewModelModule = applicationContext {
viewModel { MainViewModel(get()) }
}
4 changes: 4 additions & 0 deletions app/src/main/res/values/ids.xml
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="content" type="id" />
</resources>
4 changes: 1 addition & 3 deletions app/src/main/res/values/styles.xml
@@ -1,8 +1,6 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorSecondary</item>
Expand Down
16 changes: 14 additions & 2 deletions app/src/test/kotlin/net/rafaeltoledo/social/MainActivityTest.kt
@@ -1,16 +1,28 @@
package net.rafaeltoledo.social

import org.junit.Assert.assertNotNull
import android.widget.TextView
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment

@RunWith(RobolectricTestRunner::class)
class MainActivityTest {

@Test
fun checkIfActivityIsSuccessfullyCreated() {
assertNotNull(Robolectric.setupActivity(MainActivity::class.java))
// Arrange
val newValue = "Test Social App"
val app = RuntimeEnvironment.application as TestSocialApp
app.overrideStringValue(newValue)
val activity = Robolectric.setupActivity(MainActivity::class.java)

// Act - nothing to do

// Assert
val text = activity.findViewById<TextView>(R.id.content)
assertThat(text.text).isEqualTo(newValue)
}
}
18 changes: 18 additions & 0 deletions app/src/test/kotlin/net/rafaeltoledo/social/TestSetup.kt
@@ -0,0 +1,18 @@
package net.rafaeltoledo.social

import org.koin.dsl.module.applicationContext
import org.koin.standalone.StandAloneContext.loadKoinModules

class TestSocialApp : SocialApp() {

fun overrideStringValue(newValue: String) {
// Override value
// This behavior should be explicit in a future version of Koin
// See: https://github.com/Ekito/koin/pull/123
loadKoinModules(listOf(
applicationContext {
bean { newValue }
}
))
}
}
4 changes: 3 additions & 1 deletion build.gradle
Expand Up @@ -4,6 +4,8 @@ buildscript {
'kotlin': '1.2.41',
'supportLibrary': '27.1.1',
'jacoco': '0.8.1',
'archComponents': '1.1.1',
'koin': '0.9.3',
]

repositories {
Expand All @@ -14,7 +16,7 @@ buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$versions.kotlin"
classpath 'com.google.gms:google-services:3.3.0'
classpath 'com.google.gms:google-services:3.3.1'
classpath "org.jacoco:org.jacoco.core:$versions.jacoco"
classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.8.2'
}
Expand Down

0 comments on commit 762aa21

Please sign in to comment.