Skip to content
This repository was archived by the owner on Jan 11, 2024. It is now read-only.

Commit a4b4e4c

Browse files
committedAug 28, 2020
Use viewModelScope instead of setting up own scope
1 parent e94638a commit a4b4e4c

File tree

5 files changed

+15
-62
lines changed

5 files changed

+15
-62
lines changed
 

‎RecyclerViewHeaders-Starter/app/build.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ dependencies {
6363
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
6464
kapt "androidx.room:room-compiler:$room_version"
6565
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
66+
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
67+
68+
// Kotlin Extensions and Coroutines support for Room
69+
implementation "androidx.room:room-ktx:$room_version"
6670

6771
// Coroutines
6872
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutine_version"

‎RecyclerViewHeaders-Starter/app/src/main/java/com/example/android/trackmysleepquality/database/SleepDatabaseDao.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import androidx.room.Update
3131
interface SleepDatabaseDao {
3232

3333
@Insert
34-
fun insert(night: SleepNight)
34+
suspend fun insert(night: SleepNight)
3535

3636
/**
3737
* When updating a row with a value already set in a column,
@@ -40,23 +40,23 @@ interface SleepDatabaseDao {
4040
* @param night new value to write
4141
*/
4242
@Update
43-
fun update(night: SleepNight)
43+
suspend fun update(night: SleepNight)
4444

4545
/**
4646
* Selects and returns the row that matches the supplied start time, which is our key.
4747
*
4848
* @param key startTimeMilli to match
4949
*/
5050
@Query("SELECT * from daily_sleep_quality_table WHERE nightId = :key")
51-
fun get(key: Long): SleepNight
51+
suspend fun get(key: Long): SleepNight
5252

5353
/**
5454
* Deletes all values from the table.
5555
*
5656
* This does not delete the table, only its contents.
5757
*/
5858
@Query("DELETE FROM daily_sleep_quality_table")
59-
fun clear()
59+
suspend fun clear()
6060

6161
/**
6262
* Selects and returns all rows in the table,
@@ -70,7 +70,7 @@ interface SleepDatabaseDao {
7070
* Selects and returns the latest night.
7171
*/
7272
@Query("SELECT * FROM daily_sleep_quality_table ORDER BY nightId DESC LIMIT 1")
73-
fun getTonight(): SleepNight?
73+
suspend fun getTonight(): SleepNight?
7474

7575
/**
7676
* Selects and returns the night with given nightId.

‎RecyclerViewHeaders-Starter/app/src/main/java/com/example/android/trackmysleepquality/sleepdetail/SleepDetailViewModel.kt

-17
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,6 @@ class SleepDetailViewModel(
4242
*/
4343
val database = dataSource
4444

45-
/** Coroutine setup variables */
46-
47-
/**
48-
* viewModelJob allows us to cancel all coroutines started by this ViewModel.
49-
*/
50-
private val viewModelJob = Job()
51-
5245
private val night: LiveData<SleepNight>
5346

5447
fun getNight() = night
@@ -72,16 +65,6 @@ class SleepDetailViewModel(
7265
val navigateToSleepTracker: LiveData<Boolean?>
7366
get() = _navigateToSleepTracker
7467

75-
/**
76-
* Cancels all coroutines when the ViewModel is cleared, to cleanup any pending work.
77-
*
78-
* onCleared() gets called when the ViewModel is destroyed.
79-
*/
80-
override fun onCleared() {
81-
super.onCleared()
82-
viewModelJob.cancel()
83-
}
84-
8568
/**
8669
* Call this immediately after navigating to [SleepTrackerFragment]
8770
*/

‎RecyclerViewHeaders-Starter/app/src/main/java/com/example/android/trackmysleepquality/sleepquality/SleepQualityViewModel.kt

+5-39
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@ package com.example.android.trackmysleepquality.sleepquality
1919
import androidx.lifecycle.LiveData
2020
import androidx.lifecycle.MutableLiveData
2121
import androidx.lifecycle.ViewModel
22+
import androidx.lifecycle.viewModelScope
2223
import com.example.android.trackmysleepquality.database.SleepDatabaseDao
23-
import kotlinx.coroutines.CoroutineScope
24-
import kotlinx.coroutines.Dispatchers
25-
import kotlinx.coroutines.Job
2624
import kotlinx.coroutines.launch
27-
import kotlinx.coroutines.withContext
2825

2926
/**
3027
* ViewModel for SleepQualityFragment.
@@ -40,25 +37,6 @@ class SleepQualityViewModel(
4037
*/
4138
val database = dataSource
4239

43-
/** Coroutine setup variables */
44-
45-
/**
46-
* viewModelJob allows us to cancel all coroutines started by this ViewModel.
47-
*/
48-
private val viewModelJob = Job()
49-
50-
/**
51-
* A [CoroutineScope] keeps track of all coroutines started by this ViewModel.
52-
*
53-
* Because we pass it [viewModelJob], any coroutine started in this scope can be cancelled
54-
* by calling `viewModelJob.cancel()`
55-
*
56-
* By default, all coroutines started in uiScope will launch in [Dispatchers.Main] which is
57-
* the main thread on Android. This is a sensible default because most coroutines started by
58-
* a [ViewModel] update the UI after performing some processing.
59-
*/
60-
private val uiScope = CoroutineScope(Dispatchers.Main + viewModelJob)
61-
6240
/**
6341
* Variable that tells the fragment whether it should navigate to [SleepTrackerFragment].
6442
*
@@ -73,16 +51,6 @@ class SleepQualityViewModel(
7351
val navigateToSleepTracker: LiveData<Boolean?>
7452
get() = _navigateToSleepTracker
7553

76-
/**
77-
* Cancels all coroutines when the ViewModel is cleared, to cleanup any pending work.
78-
*
79-
* onCleared() gets called when the ViewModel is destroyed.
80-
*/
81-
override fun onCleared() {
82-
super.onCleared()
83-
viewModelJob.cancel()
84-
}
85-
8654
/**
8755
* Call this immediately after navigating to [SleepTrackerFragment]
8856
*/
@@ -96,14 +64,12 @@ class SleepQualityViewModel(
9664
* Then navigates back to the SleepTrackerFragment.
9765
*/
9866
fun onSetSleepQuality(quality: Int) {
99-
uiScope.launch {
67+
viewModelScope.launch {
10068
// IO is a thread pool for running operations that access the disk, such as
10169
// our Room database.
102-
withContext(Dispatchers.IO) {
103-
val tonight = database.get(sleepNightKey)
104-
tonight.sleepQuality = quality
105-
database.update(tonight)
106-
}
70+
val tonight = database.get(sleepNightKey)
71+
tonight.sleepQuality = quality
72+
database.update(tonight)
10773

10874
// Setting this state variable to true will alert the observer and trigger navigation.
10975
_navigateToSleepTracker.value = true

‎RecyclerViewHeaders-Starter/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ buildscript {
2121
ext {
2222
kotlin_version = '1.3.72'
2323
archLifecycleVersion = '1.1.1'
24-
room_version = '2.2.0'
24+
room_version = '2.2.5'
2525
coroutine_version = '1.3.7'
2626
gradleVersion = '4.0.1'
2727
navigationVersion = '1.0.0-alpha07'

0 commit comments

Comments
 (0)