Skip to content

Commit

Permalink
Architecture components and Kotlin sample
Browse files Browse the repository at this point in the history
  • Loading branch information
florina-muntenescu committed Aug 9, 2017
1 parent 54d49a9 commit 8147662
Show file tree
Hide file tree
Showing 37 changed files with 1,159 additions and 0 deletions.
11 changes: 11 additions & 0 deletions BasicRxJavaSampleKotlin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
*.iml
.gradle
/local.properties
.idea
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.externalNativeBuild
.idea/
25 changes: 25 additions & 0 deletions BasicRxJavaSampleKotlin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Room & RxJava Kotlin Sample

This is an API sample to showcase how to use [Room](https://developer.android
.com/topic/libraries/architecture/room.html), with RxJava's [Flowable](http://reactivex
.io/RxJava/2.x/javadoc/io/reactivex/Flowable.html) objects in Kotlin.

# Functionality
The sample app shows an editable user name, stored in the database.

# Implementation

## Data layer

The database is created using Room and has one entity: a `User`. Room generates the corresponding SQLite table at runtime.
Queries are executed in the `UserDao` class. The user retrieval is done via an observable query implemented using a `Flowable`. Every time the user data is updated, the Flowable object will emit automatically, allowing to update the UI based on the latest data. The Flowable will emit only when the query result contains at least a row. When there is no data to match the query, the Flowable will not emit.

## Presentation layer

The app has a main Activity that displays the data.
The Activity works with a ViewModel to do the following:
* subscribe to the emissions of the user name and updates the UI every time there is a new user name emitted
* notify the ViewModel when the pressed the "Update" and passes the new user name.
The ViewModel works with the data source to get and save the data.

Room guarantees that the observable query will be triggered on a background thread. In the Activity, the Flowable events are set to be received on the main thread, so the UI can be updated. The insert query is synchronous so it's wrapped in a Completable and executed on a background thread. On completion, the Activity is notified on the main thread.
1 change: 1 addition & 0 deletions BasicRxJavaSampleKotlin/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
98 changes: 98 additions & 0 deletions BasicRxJavaSampleKotlin/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion

defaultConfig {
applicationId "com.example.android.observability"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
// Support libraries
implementation "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
implementation "com.android.support:support-v4:$rootProject.supportLibraryVersion"
implementation "com.android.support:design:$rootProject.supportLibraryVersion"
// Architecture components
implementation "android.arch.lifecycle:runtime:$rootProject.architectureComponentsVersion"
implementation "android.arch.lifecycle:extensions:$rootProject.architectureComponentsVersion"
kapt "android.arch.lifecycle:compiler:$rootProject.architectureComponentsVersion"
implementation "android.arch.persistence.room:runtime:$rootProject.architectureComponentsVersion"
kapt "android.arch.persistence.room:compiler:$rootProject.architectureComponentsVersion"
implementation "android.arch.persistence.room:rxjava2:$rootProject.architectureComponentsVersion"

// RxJava
implementation "io.reactivex.rxjava2:rxandroid:$rootProject.rxandroidVersion"
implementation "io.reactivex.rxjava2:rxjava:$rootProject.rxjavaVersion"

// Dependencies for local unit tests
testImplementation "junit:junit:$rootProject.ext.junitVersion"
testImplementation "org.mockito:mockito-all:$rootProject.ext.mockitoVersion"
testImplementation "org.hamcrest:hamcrest-all:$rootProject.ext.hamcrestVersion"
testImplementation "android.arch.core:core-testing:$rootProject.architectureComponentsVersion"
testImplementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"

// Android Testing Support Library's runner and rules
androidTestImplementation "com.android.support.test:runner:$rootProject.ext.runnerVersion"
androidTestImplementation "com.android.support.test:rules:$rootProject.ext.runnerVersion"
androidTestImplementation "android.arch.persistence.room:testing:$rootProject.architectureComponentsVersion"
androidTestImplementation "android.arch.core:core-testing:$rootProject.architectureComponentsVersion"

// Dependencies for Android unit tests
androidTestImplementation "junit:junit:$rootProject.ext.junitVersion"
androidTestImplementation "org.mockito:mockito-core:$rootProject.ext.mockitoVersion"
androidTestImplementation 'com.google.dexmaker:dexmaker:1.2'
androidTestImplementation 'com.google.dexmaker:dexmaker-mockito:1.2'

// Espresso UI Testing
androidTestImplementation "com.android.support.test.espresso:espresso-core:$rootProject.espressoVersion"
androidTestImplementation "com.android.support.test.espresso:espresso-contrib:$rootProject.espressoVersion"
androidTestImplementation "com.android.support.test.espresso:espresso-intents:$rootProject.espressoVersion"

// Resolve conflicts between main and test APK:
androidTestImplementation "com.android.support:support-annotations:$rootProject.supportLibraryVersion"
androidTestImplementation "com.android.support:support-v4:$rootProject.supportLibraryVersion"
androidTestImplementation "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
androidTestImplementation "com.android.support:design:$rootProject.supportLibraryVersion"

// Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}

configurations.all {
resolutionStrategy {
force "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
}
25 changes: 25 additions & 0 deletions BasicRxJavaSampleKotlin/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/florinam/Library/Android/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.android.observability.persistence

import android.arch.core.executor.testing.InstantTaskExecutorRule
import android.arch.persistence.room.Room
import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/**
* Test the implementation of [UserDao]
*/
@RunWith(AndroidJUnit4::class)
class UserDaoTest {

@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()

private lateinit var database: UsersDatabase

@Before
@Throws(Exception::class)
fun initDb() {
// using an in-memory database because the information stored here disappears after test
database = Room.inMemoryDatabaseBuilder(InstrumentationRegistry.getContext(),
UsersDatabase::class.java)
// allowing main thread queries, just for testing
.allowMainThreadQueries()
.build()
}

@After
@Throws(Exception::class)
fun closeDb() {
database.close()
}

@Test
fun getUsersWhenNoUserInserted() {
database.userDao().getUserById("123")
.test()
.assertNoValues()
}

@Test
fun insertAndGetUser() {
// When inserting a new user in the data source
database.userDao().insertUser(USER)

// When subscribing to the emissions of the user
database.userDao().getUserById(USER.id)
.test()
// assertValue asserts that there was only one emission of the user
.assertValue { it.id == USER.id && it.userName == USER.userName }
}

@Test
fun updateAndGetUser() {
// Given that we have a user in the data source
database.userDao().insertUser(USER)

// When we are updating the name of the user
val updatedUser = User(USER.id, "new username")
database.userDao().insertUser(updatedUser)

// When subscribing to the emissions of the user
database.userDao().getUserById(USER.id)
.test()
// assertValue asserts that there was only one emission of the user
.assertValue { it.id == USER.id && it.userName == "new username" }
}

@Test
fun deleteAndGetUser() {
// Given that we have a user in the data source
database.userDao().insertUser(USER)

//When we are deleting all users
database.userDao().deleteAllUsers()
// When subscribing to the emissions of the user
database.userDao().getUserById(USER.id)
.test()
// check that there's no user emitted
.assertNoValues()
}

companion object {
private val USER = User("id", "username")
}
}
37 changes: 37 additions & 0 deletions BasicRxJavaSampleKotlin/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2017 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.observability">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ui.UserActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.android.observability

import android.content.Context
import com.example.android.observability.persistence.UserDao

import com.example.android.observability.persistence.UsersDatabase
import com.example.android.observability.ui.ViewModelFactory

/**
* Enables injection of data sources.
*/
object Injection {

fun provideUserDataSource(context: Context): UserDao {
val database = UsersDatabase.getInstance(context)
return database.userDao()
}

fun provideViewModelFactory(context: Context): ViewModelFactory {
val dataSource = provideUserDataSource(context)
return ViewModelFactory(dataSource)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.android.observability.persistence

import android.arch.persistence.room.ColumnInfo
import android.arch.persistence.room.Entity
import android.arch.persistence.room.PrimaryKey
import java.util.*

@Entity(tableName = "users")
data class User(@PrimaryKey
@ColumnInfo(name = "userid")
val id: String = UUID.randomUUID().toString(),
@ColumnInfo(name = "username")
val userName: String)
Loading

0 comments on commit 8147662

Please sign in to comment.