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.SavedSearch
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
@ -227,41 +229,43 @@ open class FeedScreenModel(
*/
private fun getFeed(feedSavedSearch: List<FeedItemUI>) {
coroutineScope.launch {
feedSavedSearch.forEach { itemUI ->
val page = try {
if (itemUI.source != null) {
withContext(coroutineDispatcher) {
if (itemUI.savedSearch == null) {
itemUI.source.fetchLatestUpdates(1)
} else {
itemUI.source.fetchSearchManga(
1,
itemUI.savedSearch.query.orEmpty(),
getFilterList(itemUI.savedSearch, itemUI.source),
)
}.awaitSingle()
}.mangas
} else {
feedSavedSearch.map { itemUI ->
async {
val page = try {
if (itemUI.source != null) {
withContext(coroutineDispatcher) {
if (itemUI.savedSearch == null) {
itemUI.source.fetchLatestUpdates(1)
} else {
itemUI.source.fetchSearchManga(
1,
itemUI.savedSearch.query.orEmpty(),
getFilterList(itemUI.savedSearch, itemUI.source),
)
}.awaitSingle()
}.mangas
} else {
emptyList()
}
} catch (e: Exception) {
emptyList()
}
} catch (e: Exception) {
emptyList()
}
val result = itemUI.copy(
results = page.map {
withIOContext {
networkToLocalManga.await(it.toDomainManga(itemUI.source!!.id))
}
},
)
val result = withIOContext {
itemUI.copy(
results = page.map {
networkToLocalManga.await(it.toDomainManga(itemUI.source!!.id))
},
)
}
mutableState.update { state ->
state.copy(
items = state.items?.map { if (it.feed.id == result.feed.id) result else it },
)
mutableState.update { state ->
state.copy(
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.SavedSearch
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
@ -127,35 +129,37 @@ open class SourceFeedScreenModel(
*/
private fun getFeed(feedSavedSearch: List<SourceFeedUI>) {
coroutineScope.launch {
feedSavedSearch.forEach { sourceFeed ->
val page = try {
withContext(coroutineDispatcher) {
when (sourceFeed) {
is SourceFeedUI.Browse -> source.fetchPopularManga(1)
is SourceFeedUI.Latest -> source.fetchLatestUpdates(1)
is SourceFeedUI.SourceSavedSearch -> source.fetchSearchManga(
page = 1,
query = sourceFeed.savedSearch.query.orEmpty(),
filters = getFilterList(sourceFeed.savedSearch, source),
)
}.awaitSingle()
}.mangas
} catch (e: Exception) {
emptyList()
}
feedSavedSearch.map { sourceFeed ->
async {
val page = try {
withContext(coroutineDispatcher) {
when (sourceFeed) {
is SourceFeedUI.Browse -> source.fetchPopularManga(1)
is SourceFeedUI.Latest -> source.fetchLatestUpdates(1)
is SourceFeedUI.SourceSavedSearch -> source.fetchSearchManga(
page = 1,
query = sourceFeed.savedSearch.query.orEmpty(),
filters = getFilterList(sourceFeed.savedSearch, source),
)
}.awaitSingle()
}.mangas
} catch (e: Exception) {
emptyList()
}
val titles = page.map {
withIOContext {
networkToLocalManga.await(it.toDomainManga(source.id))
val titles = withIOContext {
page.map {
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 },
)
}
}
mutableState.update { state ->
state.copy(
items = state.items.map { item -> if (item.id == sourceFeed.id) sourceFeed.withResults(titles) else item },
)
}
}
}.awaitAll()
}
}