@@ -7,16 +7,15 @@ import android.arch.lifecycle.OnLifecycleEvent
77import android.content.Context
88import kotlinx.coroutines.CoroutineDispatcher
99import kotlinx.coroutines.CoroutineScope
10+ import kotlinx.coroutines.Dispatchers
1011import kotlinx.coroutines.coroutineScope
1112import kotlinx.coroutines.launch
12- import kotlinx.coroutines.yield
1313import org.wordpress.android.fluxc.model.SiteModel
1414import org.wordpress.android.fluxc.store.PostStore
1515import org.wordpress.android.fluxc.store.SiteStore
1616import org.wordpress.android.modules.BG_THREAD
1717import org.wordpress.android.util.NetworkUtilsWrapper
1818import org.wordpress.android.viewmodel.helpers.ConnectionStatus
19- import org.wordpress.android.viewmodel.helpers.ConnectionStatus.AVAILABLE
2019import javax.inject.Inject
2120import javax.inject.Named
2221import javax.inject.Singleton
@@ -52,36 +51,30 @@ class LocalDraftUploadStarter @Inject constructor(
5251 init {
5352 // Since this class is meant to be a Singleton, it should be fine (I think) to use observeForever in here.
5453 connectionStatus.observeForever {
55- if (it == AVAILABLE ) {
56- queueUploadForAllSites()
57- }
54+ queueUploadForAllSites()
5855 }
5956 }
6057
6158 private fun queueUploadForAllSites () = launch {
6259 val sites = siteStore.sites
63- // TODO there should be an actual queue instead of calling this directly
6460 upload(sites = sites)
6561 }
6662
6763 fun queueUpload (site : SiteModel ) = launch {
68- // TODO there should be an actual queue instead of calling this directly
6964 upload(sites = listOf (site))
7065 }
7166
7267 private suspend fun upload (sites : List <SiteModel >) = coroutineScope {
73- sites.forEach { site ->
74- if (! networkUtilsWrapper.isNetworkAvailable()) {
75- return @coroutineScope
76- }
77-
78- yield ()
68+ if (! networkUtilsWrapper.isNetworkAvailable()) {
69+ return @coroutineScope
70+ }
7971
80- uploadSite(site = site)
72+ sites.forEach {
73+ upload(scope = this , site = it)
8174 }
8275 }
8376
84- private fun uploadSite ( site : SiteModel ) {
77+ private fun upload ( scope : CoroutineScope , site : SiteModel ) = scope.launch( Dispatchers . IO ) {
8578 postStore.getLocalDraftPosts(site)
8679 .filterNot { UploadService .isPostUploadingOrQueued(it) }
8780 .forEach { localDraft ->
0 commit comments