Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

Use Kotlin Coroutines for IO executor #235

Merged
merged 7 commits into from
Nov 28, 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Libraries Used
* [Layout][35] - Lay out widgets using different algorithms.
* Third party
* [Glide][90] for image loading
* [Kotlin Coroutines][91] for managing background threads with simplified code and reducing needs for callbacks

[0]: https://developer.android.com/jetpack/foundation/
[1]: https://developer.android.com/topic/libraries/support-library/packages#v7-appcompat
Expand All @@ -83,6 +84,7 @@ Libraries Used
[34]: https://developer.android.com/guide/components/fragments
[35]: https://developer.android.com/guide/topics/ui/declaring-layout
[90]: https://bumptech.github.io/glide/
[91]: https://kotlinlang.org/docs/reference/coroutines-overview.html

Upcoming features
-----------------
Expand Down
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ dependencies {
implementation "com.google.android.material:material:$rootProject.materialVersion"
implementation "com.google.code.gson:gson:$rootProject.gsonVersion"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$rootProject.kotlinVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutinesVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.coroutinesVersion"

// Testing dependencies
androidTestImplementation "androidx.arch.core:core-testing:$rootProject.coreTestingVersion"
Expand Down
9 changes: 9 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

# ServiceLoader support
-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {}
-keepnames class kotlinx.coroutines.CoroutineExceptionHandler {}

# Most of volatile fields are updated with AFU and should not be mangled
-keepclassmembernames class kotlinx.** {
volatile <fields>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@

package com.google.samples.apps.sunflower.data

import com.google.samples.apps.sunflower.utilities.runOnIoThread
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext

class GardenPlantingRepository private constructor(
private val gardenPlantingDao: GardenPlantingDao
) {

fun createGardenPlanting(plantId: String) {
runOnIoThread {
suspend fun createGardenPlanting(plantId: String) {
withContext(IO) {
val gardenPlanting = GardenPlanting(plantId)
gardenPlantingDao.insertGardenPlanting(gardenPlanting)
}
}

fun removeGardenPlanting(gardenPlanting: GardenPlanting) {
runOnIoThread {
suspend fun removeGardenPlanting(gardenPlanting: GardenPlanting) {
withContext(IO) {
gardenPlantingDao.deleteGardenPlanting(gardenPlanting)
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import com.google.samples.apps.sunflower.PlantDetailFragment
import com.google.samples.apps.sunflower.data.GardenPlantingRepository
import com.google.samples.apps.sunflower.data.Plant
import com.google.samples.apps.sunflower.data.PlantRepository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch

/**
* The ViewModel used in [PlantDetailFragment].
Expand All @@ -36,6 +40,29 @@ class PlantDetailViewModel(
val isPlanted: LiveData<Boolean>
val plant: LiveData<Plant>

/**
* This is the job for all coroutines started by this ViewModel.
*
* Cancelling this job will cancel all coroutines started by this ViewModel.
*/
private val viewModelJob = Job()

/**
* This is the scope for all coroutines launched by [PlantDetailViewModel].
*
* Since we pass [viewModelJob], you can cancel all coroutines launched by [viewModelScope] by calling
* viewModelJob.cancel(). This is called in [onCleared].
*/
private val viewModelScope = CoroutineScope(Main + viewModelJob)

/**
* Cancel all coroutines when the ViewModel is cleared.
*/
override fun onCleared() {
super.onCleared()
viewModelJob.cancel()
}

init {

/* The getGardenPlantingForPlant method returns a LiveData from querying the database. The
Expand All @@ -49,6 +76,8 @@ class PlantDetailViewModel(
}

fun addPlantToGarden() {
gardenPlantingRepository.createGardenPlanting(plantId)
viewModelScope.launch {
gardenPlantingRepository.createGardenPlanting(plantId)
}
}
}
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ buildscript {
// App dependencies
constraintLayoutVersion = '2.0.0-alpha2'
coreTestingVersion = '2.0.0'
coroutinesVersion = "1.0.1"
espressoVersion = '3.1.0-alpha4'
glideVersion = '4.8.0'
gradleVersion = '3.2.1'
Expand Down