Fix suggestions worker processing broken tags

pull/227/head v4.0-beta1
Koitharu 4 years ago
parent 117c4c5978
commit 734765dbdd
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

@ -1,7 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="KtlintProjectConfiguration"> <component name="KtlintProjectConfiguration">
<enableKtlint>false</enableKtlint>
<androidMode>true</androidMode> <androidMode>true</androidMode>
<treatAsErrors>false</treatAsErrors> <treatAsErrors>false</treatAsErrors>
<disabledRules>
<list>
<option value="no-empty-first-line-in-method-block" />
</list>
</disabledRules>
</component> </component>
</project> </project>

@ -11,8 +11,6 @@ import androidx.hilt.work.HiltWorker
import androidx.work.* import androidx.work.*
import dagger.assisted.Assisted import dagger.assisted.Assisted
import dagger.assisted.AssistedInject import dagger.assisted.AssistedInject
import java.util.concurrent.TimeUnit
import kotlin.math.pow
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll import kotlinx.coroutines.awaitAll
@ -21,12 +19,16 @@ import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.parser.MangaRepository import org.koitharu.kotatsu.core.parser.MangaRepository
import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.prefs.AppSettings
import org.koitharu.kotatsu.history.domain.HistoryRepository import org.koitharu.kotatsu.history.domain.HistoryRepository
import org.koitharu.kotatsu.parsers.model.MangaSource
import org.koitharu.kotatsu.parsers.model.MangaTag import org.koitharu.kotatsu.parsers.model.MangaTag
import org.koitharu.kotatsu.parsers.model.SortOrder import org.koitharu.kotatsu.parsers.model.SortOrder
import org.koitharu.kotatsu.suggestions.domain.MangaSuggestion import org.koitharu.kotatsu.suggestions.domain.MangaSuggestion
import org.koitharu.kotatsu.suggestions.domain.SuggestionRepository import org.koitharu.kotatsu.suggestions.domain.SuggestionRepository
import org.koitharu.kotatsu.utils.ext.asArrayList import org.koitharu.kotatsu.utils.ext.asArrayList
import org.koitharu.kotatsu.utils.ext.printStackTraceDebug
import org.koitharu.kotatsu.utils.ext.trySetForeground import org.koitharu.kotatsu.utils.ext.trySetForeground
import java.util.concurrent.TimeUnit
import kotlin.math.pow
@HiltWorker @HiltWorker
class SuggestionsWorker @AssistedInject constructor( class SuggestionsWorker @AssistedInject constructor(
@ -94,14 +96,10 @@ class SuggestionsWorker @AssistedInject constructor(
val dispatcher = Dispatchers.Default.limitedParallelism(MAX_PARALLELISM) val dispatcher = Dispatchers.Default.limitedParallelism(MAX_PARALLELISM)
val rawResults = coroutineScope { val rawResults = coroutineScope {
tagsBySources.flatMap { (source, tags) -> tagsBySources.flatMap { (source, tags) ->
val repo = mangaRepositoryFactory.create(source) val repo = mangaRepositoryFactory.tryCreate(source) ?: return@flatMap emptyList()
tags.map { tag -> tags.map { tag ->
async(dispatcher) { async(dispatcher) {
repo.getList( repo.getListSafe(tag)
offset = 0,
sortOrder = SortOrder.UPDATED,
tags = setOf(tag),
)
} }
} }
}.awaitAll().flatten().asArrayList() }.awaitAll().flatten().asArrayList()
@ -139,6 +137,18 @@ class SuggestionsWorker @AssistedInject constructor(
return (weight / maxWeight).pow(2.0).toFloat() return (weight / maxWeight).pow(2.0).toFloat()
} }
private suspend fun MangaRepository.getListSafe(tag: MangaTag) = runCatching {
getList(offset = 0, sortOrder = SortOrder.UPDATED, tags = setOf(tag))
}.onFailure { error ->
error.printStackTraceDebug()
}.getOrDefault(emptyList())
private fun MangaRepository.Factory.tryCreate(source: MangaSource) = runCatching {
create(source)
}.onFailure { error ->
error.printStackTraceDebug()
}.getOrNull()
companion object { companion object {
private const val TAG = "suggestions" private const val TAG = "suggestions"

Loading…
Cancel
Save