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

Commit

Permalink
uiScope is not needed, viewModelScope extension is the same (#369)
Browse files Browse the repository at this point in the history
deferred collection is not needed
run readCollectionSuspended in a Dispatcher.IO (the client of readCollectionSuspended shouldn't dictate which context to run this)
  • Loading branch information
loki666 authored Oct 27, 2021
1 parent 374c4cd commit e4419df
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,8 +45,6 @@ class CollectionViewModel(application: Application) : AndroidViewModel(applicati
val collectionSizeLiveData: MutableLiveData<Int> = MutableLiveData<Int>()
private var modificationDateViewModel: Date = Date()
private var collectionChangedReceiver: BroadcastReceiver
private val backgroundJob = Job()
private val uiScope = CoroutineScope(Dispatchers.Main + backgroundJob)


/* Init constructor */
Expand All @@ -61,7 +60,6 @@ class CollectionViewModel(application: Application) : AndroidViewModel(applicati
/* Overrides onCleared */
override fun onCleared() {
super.onCleared()
backgroundJob.cancel()
LocalBroadcastManager.getInstance(getApplication()).unregisterReceiver(collectionChangedReceiver)
}

Expand All @@ -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<Collection> = 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
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/java/org/y20k/transistor/helpers/FileHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit e4419df

Please sign in to comment.