Parallelize feed properly

This commit is contained in:
Jobobby04 2023-02-07 14:03:27 -05:00
parent f93cf29df4
commit a19be83f99
2 changed files with 64 additions and 56 deletions

View File

@ -32,6 +32,8 @@ import eu.kanade.tachiyomi.util.system.logcat
import exh.savedsearches.models.FeedSavedSearch import exh.savedsearches.models.FeedSavedSearch
import exh.savedsearches.models.SavedSearch import exh.savedsearches.models.SavedSearch
import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.collectLatest
@ -227,41 +229,43 @@ open class FeedScreenModel(
*/ */
private fun getFeed(feedSavedSearch: List<FeedItemUI>) { private fun getFeed(feedSavedSearch: List<FeedItemUI>) {
coroutineScope.launch { coroutineScope.launch {
feedSavedSearch.forEach { itemUI -> feedSavedSearch.map { itemUI ->
val page = try { async {
if (itemUI.source != null) { val page = try {
withContext(coroutineDispatcher) { if (itemUI.source != null) {
if (itemUI.savedSearch == null) { withContext(coroutineDispatcher) {
itemUI.source.fetchLatestUpdates(1) if (itemUI.savedSearch == null) {
} else { itemUI.source.fetchLatestUpdates(1)
itemUI.source.fetchSearchManga( } else {
1, itemUI.source.fetchSearchManga(
itemUI.savedSearch.query.orEmpty(), 1,
getFilterList(itemUI.savedSearch, itemUI.source), itemUI.savedSearch.query.orEmpty(),
) getFilterList(itemUI.savedSearch, itemUI.source),
}.awaitSingle() )
}.mangas }.awaitSingle()
} else { }.mangas
} else {
emptyList()
}
} catch (e: Exception) {
emptyList() emptyList()
} }
} catch (e: Exception) {
emptyList()
}
val result = itemUI.copy( val result = withIOContext {
results = page.map { itemUI.copy(
withIOContext { results = page.map {
networkToLocalManga.await(it.toDomainManga(itemUI.source!!.id)) networkToLocalManga.await(it.toDomainManga(itemUI.source!!.id))
} },
}, )
) }
mutableState.update { state -> mutableState.update { state ->
state.copy( state.copy(
items = state.items?.map { if (it.feed.id == result.feed.id) result else it }, items = state.items?.map { if (it.feed.id == result.feed.id) result else it },
) )
}
} }
} }.awaitAll()
} }
} }

View File

@ -31,6 +31,8 @@ import eu.kanade.tachiyomi.util.system.logcat
import exh.savedsearches.models.FeedSavedSearch import exh.savedsearches.models.FeedSavedSearch
import exh.savedsearches.models.SavedSearch import exh.savedsearches.models.SavedSearch
import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
@ -127,35 +129,37 @@ open class SourceFeedScreenModel(
*/ */
private fun getFeed(feedSavedSearch: List<SourceFeedUI>) { private fun getFeed(feedSavedSearch: List<SourceFeedUI>) {
coroutineScope.launch { coroutineScope.launch {
feedSavedSearch.forEach { sourceFeed -> feedSavedSearch.map { sourceFeed ->
val page = try { async {
withContext(coroutineDispatcher) { val page = try {
when (sourceFeed) { withContext(coroutineDispatcher) {
is SourceFeedUI.Browse -> source.fetchPopularManga(1) when (sourceFeed) {
is SourceFeedUI.Latest -> source.fetchLatestUpdates(1) is SourceFeedUI.Browse -> source.fetchPopularManga(1)
is SourceFeedUI.SourceSavedSearch -> source.fetchSearchManga( is SourceFeedUI.Latest -> source.fetchLatestUpdates(1)
page = 1, is SourceFeedUI.SourceSavedSearch -> source.fetchSearchManga(
query = sourceFeed.savedSearch.query.orEmpty(), page = 1,
filters = getFilterList(sourceFeed.savedSearch, source), query = sourceFeed.savedSearch.query.orEmpty(),
) filters = getFilterList(sourceFeed.savedSearch, source),
}.awaitSingle() )
}.mangas }.awaitSingle()
} catch (e: Exception) { }.mangas
emptyList() } catch (e: Exception) {
} emptyList()
}
val titles = page.map { val titles = withIOContext {
withIOContext { page.map {
networkToLocalManga.await(it.toDomainManga(source.id)) networkToLocalManga.await(it.toDomainManga(source.id))
}
}
mutableState.update { state ->
state.copy(
items = state.items.map { item -> if (item.id == sourceFeed.id) sourceFeed.withResults(titles) else item },
)
} }
} }
}.awaitAll()
mutableState.update { state ->
state.copy(
items = state.items.map { item -> if (item.id == sourceFeed.id) sourceFeed.withResults(titles) else item },
)
}
}
} }
} }