Skip to content

Commit ffca6ef

Browse files
committed
LocalDraftUploadStarter: Query in parallel
1 parent f8e6f92 commit ffca6ef

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

WordPress/src/main/java/org/wordpress/android/ui/uploads/LocalDraftUploadStarter.kt

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ import android.arch.lifecycle.OnLifecycleEvent
77
import android.content.Context
88
import kotlinx.coroutines.CoroutineDispatcher
99
import kotlinx.coroutines.CoroutineScope
10+
import kotlinx.coroutines.Dispatchers
1011
import kotlinx.coroutines.coroutineScope
1112
import kotlinx.coroutines.launch
12-
import kotlinx.coroutines.yield
1313
import org.wordpress.android.fluxc.model.SiteModel
1414
import org.wordpress.android.fluxc.store.PostStore
1515
import org.wordpress.android.fluxc.store.SiteStore
1616
import org.wordpress.android.modules.BG_THREAD
1717
import org.wordpress.android.util.NetworkUtilsWrapper
1818
import org.wordpress.android.viewmodel.helpers.ConnectionStatus
19-
import org.wordpress.android.viewmodel.helpers.ConnectionStatus.AVAILABLE
2019
import javax.inject.Inject
2120
import javax.inject.Named
2221
import 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

Comments
 (0)