From e4419df77be2353f6167b813faada01608dd54a1 Mon Sep 17 00:00:00 2001 From: Philippe Simons Date: Wed, 27 Oct 2021 14:55:49 +0200 Subject: [PATCH] uiScope is not needed, viewModelScope extension is the same (#369) deferred collection is not needed run readCollectionSuspended in a Dispatcher.IO (the client of readCollectionSuspended shouldn't dictate which context to run this) --- .../y20k/transistor/collection/CollectionViewModel.kt | 10 +++------- .../java/org/y20k/transistor/helpers/FileHelper.kt | 9 +++++---- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/org/y20k/transistor/collection/CollectionViewModel.kt b/app/src/main/java/org/y20k/transistor/collection/CollectionViewModel.kt index 1c49e8bd..df7be961 100644 --- a/app/src/main/java/org/y20k/transistor/collection/CollectionViewModel.kt +++ b/app/src/main/java/org/y20k/transistor/collection/CollectionViewModel.kt @@ -21,6 +21,7 @@ import android.content.Intent import android.content.IntentFilter import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.viewModelScope import androidx.localbroadcastmanager.content.LocalBroadcastManager import kotlinx.coroutines.* import org.y20k.transistor.Keys @@ -44,8 +45,6 @@ class CollectionViewModel(application: Application) : AndroidViewModel(applicati val collectionSizeLiveData: MutableLiveData = MutableLiveData() private var modificationDateViewModel: Date = Date() private var collectionChangedReceiver: BroadcastReceiver - private val backgroundJob = Job() - private val uiScope = CoroutineScope(Dispatchers.Main + backgroundJob) /* Init constructor */ @@ -61,7 +60,6 @@ class CollectionViewModel(application: Application) : AndroidViewModel(applicati /* Overrides onCleared */ override fun onCleared() { super.onCleared() - backgroundJob.cancel() LocalBroadcastManager.getInstance(getApplication()).unregisterReceiver(collectionChangedReceiver) } @@ -86,11 +84,9 @@ class CollectionViewModel(application: Application) : AndroidViewModel(applicati /* Reads collection of radio stations from storage using GSON */ private fun loadCollection(context: Context) { LogHelper.v(TAG, "Loading collection of stations from storage") - uiScope.launch { + viewModelScope.launch { // load collection on background thread - val deferred: Deferred = async(Dispatchers.Default) { FileHelper.readCollectionSuspended(getApplication()) } - // wait for result - val collection: Collection = deferred.await() + val collection: Collection = FileHelper.readCollectionSuspended(getApplication()) // get updated modification date modificationDateViewModel = collection.modificationDate // update collection view model diff --git a/app/src/main/java/org/y20k/transistor/helpers/FileHelper.kt b/app/src/main/java/org/y20k/transistor/helpers/FileHelper.kt index 056ba9d0..703cc164 100644 --- a/app/src/main/java/org/y20k/transistor/helpers/FileHelper.kt +++ b/app/src/main/java/org/y20k/transistor/helpers/FileHelper.kt @@ -25,8 +25,10 @@ import androidx.core.content.FileProvider import androidx.core.net.toUri import com.google.gson.Gson import com.google.gson.GsonBuilder +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import org.y20k.transistor.Keys import org.y20k.transistor.core.Collection import org.y20k.transistor.core.Station @@ -310,11 +312,10 @@ object FileHelper { /* Suspend function: Wrapper for readCollection */ - suspend fun readCollectionSuspended(context: Context): Collection { - return suspendCoroutine {cont -> - cont.resume(readCollection(context)) + suspend fun readCollectionSuspended(context: Context): Collection = + withContext(Dispatchers.IO) { + readCollection(context) } - } /* Suspend function: Wrapper for copyFile */