diff --git a/MigrationCodelab/app/src/androidTest/java/com/google/samples/apps/sunflower/viewmodels/PlantDetailViewModelTest.kt b/MigrationCodelab/app/src/androidTest/java/com/google/samples/apps/sunflower/viewmodels/PlantDetailViewModelTest.kt index 608454319..fd9062efa 100644 --- a/MigrationCodelab/app/src/androidTest/java/com/google/samples/apps/sunflower/viewmodels/PlantDetailViewModelTest.kt +++ b/MigrationCodelab/app/src/androidTest/java/com/google/samples/apps/sunflower/viewmodels/PlantDetailViewModelTest.kt @@ -24,6 +24,7 @@ import com.google.samples.apps.sunflower.data.GardenPlantingRepository import com.google.samples.apps.sunflower.data.PlantRepository import com.google.samples.apps.sunflower.utilities.getValue import com.google.samples.apps.sunflower.utilities.testPlant +import kotlinx.coroutines.Dispatchers import org.junit.After import org.junit.Assert.assertFalse import org.junit.Before @@ -45,7 +46,8 @@ class PlantDetailViewModelTest { val plantRepo = PlantRepository.getInstance(appDatabase.plantDao()) val gardenPlantingRepo = GardenPlantingRepository.getInstance( - appDatabase.gardenPlantingDao() + appDatabase.gardenPlantingDao(), + Dispatchers.IO ) viewModel = PlantDetailViewModel(plantRepo, gardenPlantingRepo, testPlant.plantId) } diff --git a/MigrationCodelab/app/src/main/java/com/google/samples/apps/sunflower/data/GardenPlantingRepository.kt b/MigrationCodelab/app/src/main/java/com/google/samples/apps/sunflower/data/GardenPlantingRepository.kt index d36bf75f2..6e9f1efa5 100644 --- a/MigrationCodelab/app/src/main/java/com/google/samples/apps/sunflower/data/GardenPlantingRepository.kt +++ b/MigrationCodelab/app/src/main/java/com/google/samples/apps/sunflower/data/GardenPlantingRepository.kt @@ -16,13 +16,20 @@ package com.google.samples.apps.sunflower.data +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext + class GardenPlantingRepository private constructor( - private val gardenPlantingDao: GardenPlantingDao + private val gardenPlantingDao: GardenPlantingDao, + private val ioDispatcher: CoroutineDispatcher ) { suspend fun createGardenPlanting(plantId: String) { - val gardenPlanting = GardenPlanting(plantId) - gardenPlantingDao.insertGardenPlanting(gardenPlanting) + withContext(ioDispatcher) { + val gardenPlanting = GardenPlanting(plantId) + gardenPlantingDao.insertGardenPlanting(gardenPlanting) + } } suspend fun removeGardenPlanting(gardenPlanting: GardenPlanting) { @@ -39,9 +46,11 @@ class GardenPlantingRepository private constructor( // For Singleton instantiation @Volatile private var instance: GardenPlantingRepository? = null - fun getInstance(gardenPlantingDao: GardenPlantingDao) = + fun getInstance(gardenPlantingDao: GardenPlantingDao, ioDispatcher: CoroutineDispatcher) = instance ?: synchronized(this) { - instance ?: GardenPlantingRepository(gardenPlantingDao).also { instance = it } + instance ?: GardenPlantingRepository(gardenPlantingDao, ioDispatcher).also { + instance = it + } } } } diff --git a/MigrationCodelab/app/src/main/java/com/google/samples/apps/sunflower/utilities/InjectorUtils.kt b/MigrationCodelab/app/src/main/java/com/google/samples/apps/sunflower/utilities/InjectorUtils.kt index 0243b6dba..460689a78 100644 --- a/MigrationCodelab/app/src/main/java/com/google/samples/apps/sunflower/utilities/InjectorUtils.kt +++ b/MigrationCodelab/app/src/main/java/com/google/samples/apps/sunflower/utilities/InjectorUtils.kt @@ -24,12 +24,15 @@ import com.google.samples.apps.sunflower.data.PlantRepository import com.google.samples.apps.sunflower.viewmodels.GardenPlantingListViewModelFactory import com.google.samples.apps.sunflower.viewmodels.PlantDetailViewModelFactory import com.google.samples.apps.sunflower.viewmodels.PlantListViewModelFactory +import kotlinx.coroutines.Dispatchers /** * Static methods used to inject classes needed for various Activities and Fragments. */ object InjectorUtils { + private val ioDispatcher = Dispatchers.IO + private fun getPlantRepository(context: Context): PlantRepository { return PlantRepository.getInstance( AppDatabase.getInstance(context.applicationContext).plantDao() @@ -38,7 +41,8 @@ object InjectorUtils { private fun getGardenPlantingRepository(context: Context): GardenPlantingRepository { return GardenPlantingRepository.getInstance( - AppDatabase.getInstance(context.applicationContext).gardenPlantingDao() + AppDatabase.getInstance(context.applicationContext).gardenPlantingDao(), + ioDispatcher ) }