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 1 commit
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 async jobs, plays the role like light-weight threads.
XinyueZ marked this conversation as resolved.
Show resolved Hide resolved

[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://github.com/Kotlin/kotlinx.coroutines
XinyueZ marked this conversation as resolved.
Show resolved Hide resolved

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.supportLibraryVersion"
implementation "com.google.code.gson:gson:$rootProject.gsonVersion"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$rootProject.kotlinVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.coroutines"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutines"
XinyueZ marked this conversation as resolved.
Show resolved Hide resolved

// Testing dependencies
androidTestImplementation "androidx.arch.core:core-testing:$rootProject.coreTestingVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,17 @@

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

import com.google.samples.apps.sunflower.utilities.runOnIoThread

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

fun createGardenPlanting(plantId: String) {
runOnIoThread {
val gardenPlanting = GardenPlanting(plantId)
gardenPlantingDao.insertGardenPlanting(gardenPlanting)
}
val gardenPlanting = GardenPlanting(plantId)
XinyueZ marked this conversation as resolved.
Show resolved Hide resolved
gardenPlantingDao.insertGardenPlanting(gardenPlanting)
}

fun removeGardenPlanting(gardenPlanting: GardenPlanting) {
runOnIoThread {
gardenPlantingDao.deleteGardenPlanting(gardenPlanting)
}
gardenPlantingDao.deleteGardenPlanting(gardenPlanting)
XinyueZ marked this conversation as resolved.
Show resolved Hide resolved
}

fun getGardenPlantingForPlant(plantId: String) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ package com.google.samples.apps.sunflower.viewmodels

import androidx.lifecycle.LiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
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.Dispatchers.IO
import kotlinx.coroutines.launch

/**
* The ViewModel used in [PlantDetailFragment].
Expand All @@ -31,7 +32,7 @@ class PlantDetailViewModel(
plantRepository: PlantRepository,
private val gardenPlantingRepository: GardenPlantingRepository,
private val plantId: String
) : ViewModel() {
) : ScopedViewModel() {

val isPlanted: LiveData<Boolean>
val plant: LiveData<Plant>
Expand All @@ -48,7 +49,7 @@ class PlantDetailViewModel(
plant = plantRepository.getPlant(plantId)
}

fun addPlantToGarden() {
fun addPlantToGarden() = launch(IO) {
gardenPlantingRepository.createGardenPlanting(plantId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@
* limitations under the License.
*/

package com.google.samples.apps.sunflower.utilities
package com.google.samples.apps.sunflower.viewmodels
XinyueZ marked this conversation as resolved.
Show resolved Hide resolved

import java.util.concurrent.Executors
import androidx.lifecycle.ViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlin.coroutines.CoroutineContext

private val IO_EXECUTOR = Executors.newSingleThreadExecutor()
abstract class ScopedViewModel : ViewModel(), CoroutineScope {
XinyueZ marked this conversation as resolved.
Show resolved Hide resolved
private val job = Job()

/**
* Utility method to run blocks on a dedicated background thread, used for io/database work.
*/
fun runOnIoThread(f: () -> Unit) {
IO_EXECUTOR.execute(f)
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + job

override fun onCleared() {
job.cancel()
}
}
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'
coroutines = "1.0.0"
XinyueZ marked this conversation as resolved.
Show resolved Hide resolved
espressoVersion = '3.1.0-alpha4'
glideVersion = '4.8.0'
gradleVersion = '3.2.1'
Expand Down