Skip to content
Branch: master
Find file History
JoseAlcerreca Adds UI and Robolectric tests to the BasicNavigationSample
Change-Id: I7019d498575b2c7bcbe03ce6286ea2fe54c464fc
Latest commit 3fb5bcf Mar 5, 2019
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
.google
app [BasicRxJavaSampleKotlin] Updated to use Completable (#539) Jan 18, 2019
gradle/wrapper
.gitignore Architecture components and Kotlin sample Aug 9, 2017
README.md Add android-architecture-components to Google Samples index (#225) Oct 31, 2017
build.gradle
gradle.properties Architecture components and Kotlin sample Aug 9, 2017
gradlew [BasicRxJavaSampleKotlin] Updated to use Completable (#539) Jan 18, 2019
gradlew.bat [BasicRxJavaSampleKotlin] Updated to use Completable (#539) Jan 18, 2019
settings.gradle Architecture components and Kotlin sample Aug 9, 2017
versions.gradle Adds UI and Robolectric tests to the BasicNavigationSample Mar 27, 2019

README.md

Room & RxJava (Kotlin) Sample

This is an API sample to showcase how to use Room, with RxJava's Flowable objects in Kotlin.

Introduction

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.

License

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.

You can’t perform that action at this time.