From ab9f21a561ffe8287bef852d06754e0c41b2e1ed Mon Sep 17 00:00:00 2001 From: StageGuard Date: Sat, 7 Feb 2026 23:05:32 +0800 Subject: [PATCH 1/2] Add fallback search. --- .../subject/SubjectSearchRepository.kt | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/app/shared/app-data/src/commonMain/kotlin/data/repository/subject/SubjectSearchRepository.kt b/app/shared/app-data/src/commonMain/kotlin/data/repository/subject/SubjectSearchRepository.kt index d8c1308cdb..04060d44c3 100644 --- a/app/shared/app-data/src/commonMain/kotlin/data/repository/subject/SubjectSearchRepository.kt +++ b/app/shared/app-data/src/commonMain/kotlin/data/repository/subject/SubjectSearchRepository.kt @@ -9,6 +9,7 @@ package me.him188.ani.app.data.repository.subject +import androidx.collection.MutableIntList import androidx.paging.Pager import androidx.paging.PagingConfig import androidx.paging.PagingData @@ -36,6 +37,7 @@ import me.him188.ani.datasources.api.topic.UnifiedCollectionType import me.him188.ani.datasources.bangumi.models.BangumiSubjectType import me.him188.ani.datasources.bangumi.models.search.BangumiSort import me.him188.ani.utils.logging.logger +import me.him188.ani.utils.logging.warn import kotlin.coroutines.CoroutineContext import kotlin.coroutines.cancellation.CancellationException @@ -111,7 +113,45 @@ class SubjectSearchRepository( } catch (e: CancellationException) { throw e } catch (e: Exception) { - LoadResult.Error(RepositoryException.wrapOrThrowCancellation(e)) + try { + Companion.logger.warn(e) { "Failed to search by ANI api, trying fallback Bangumi api." } + + val res = bangumiSubjectSearchService.searchSubjectIds( + searchQuery.keywords, + useNewApi = useNewApi(), + offset = offset, + limit = params.loadSize, + filters = filters, + sort = searchQuery.sort.toBangumiSort(), + ) + + val filtered = if (ignoreDoneAndDropped()) { + val excludedIds = subjectCollectionRepository.getSubjectIdsByCollectionType( + types = listOf(UnifiedCollectionType.DONE, UnifiedCollectionType.DROPPED), + ).first() + + MutableIntList().apply { + res.forEach { if (it !in excludedIds) add(it) } + } + } else { + res + } + + val subjectInfos = filterSubjectsBySort( + subjectService.batchGetSubjectDetails(filtered), + searchQuery.sort, + ) + + return@withContext LoadResult.Page( + subjectInfos, + prevKey = if (offset == 0) null else offset, + nextKey = if (subjectInfos.isEmpty()) null else offset + params.loadSize, + ) + } catch (e: CancellationException) { + throw e + } catch (e: Exception) { + LoadResult.Error(RepositoryException.wrapOrThrowCancellation(e)) + } } } From 1ad2a80e020898e300930c598eb237f4c32e726c Mon Sep 17 00:00:00 2001 From: StageGuard Date: Thu, 26 Feb 2026 00:05:14 +0800 Subject: [PATCH 2/2] Revert search API. --- .../subject/SubjectSearchRepository.kt | 57 +++---------------- 1 file changed, 9 insertions(+), 48 deletions(-) diff --git a/app/shared/app-data/src/commonMain/kotlin/data/repository/subject/SubjectSearchRepository.kt b/app/shared/app-data/src/commonMain/kotlin/data/repository/subject/SubjectSearchRepository.kt index 04060d44c3..68a8d6c2d3 100644 --- a/app/shared/app-data/src/commonMain/kotlin/data/repository/subject/SubjectSearchRepository.kt +++ b/app/shared/app-data/src/commonMain/kotlin/data/repository/subject/SubjectSearchRepository.kt @@ -22,7 +22,6 @@ import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.withContext import me.him188.ani.app.data.models.schedule.AnimeSeasonId import me.him188.ani.app.data.models.schedule.yearMonths -import me.him188.ani.app.data.network.AniSubjectSearchService import me.him188.ani.app.data.network.BangumiSearchFilters import me.him188.ani.app.data.network.BangumiSubjectSearchService import me.him188.ani.app.data.network.BatchSubjectDetails @@ -37,13 +36,11 @@ import me.him188.ani.datasources.api.topic.UnifiedCollectionType import me.him188.ani.datasources.bangumi.models.BangumiSubjectType import me.him188.ani.datasources.bangumi.models.search.BangumiSort import me.him188.ani.utils.logging.logger -import me.him188.ani.utils.logging.warn import kotlin.coroutines.CoroutineContext import kotlin.coroutines.cancellation.CancellationException class SubjectSearchRepository( private val bangumiSubjectSearchService: BangumiSubjectSearchService, - private val aniSubjectSearchService: AniSubjectSearchService, private val subjectCollectionRepository: SubjectCollectionRepository, private val subjectService: SubjectService, defaultDispatcher: CoroutineContext = Dispatchers.Default, @@ -81,7 +78,7 @@ class SubjectSearchRepository( val offset = params.key ?: return@withContext LoadResult.Error(IllegalArgumentException("Key is null")) return@withContext try { - val res = /*bangumiSubjectSearchService.searchSubjectIds*/aniSubjectSearchService.searchSubjects( + val res = bangumiSubjectSearchService.searchSubjectIds( searchQuery.keywords, useNewApi = useNewApi(), offset = offset, @@ -90,20 +87,23 @@ class SubjectSearchRepository( sort = searchQuery.sort.toBangumiSort(), ) - val filtered = if (ignoreDoneAndDropped()) { + val filteredIds = if (ignoreDoneAndDropped()) { val excludedIds = subjectCollectionRepository.getSubjectIdsByCollectionType( types = listOf(UnifiedCollectionType.DONE, UnifiedCollectionType.DROPPED), ).first() - buildList { - res.forEach { if (it.subjectInfo.subjectId !in excludedIds) add(it) } + MutableIntList().apply { + res.forEach { if (it !in excludedIds) add(it) } } } else { res } // 在分页源中直接过滤掉不符合条件的数据 #2380 - val subjectInfos = filterSubjectsBySort(filtered, searchQuery.sort) + val subjectInfos = filterSubjectsBySort( + subjectService.batchGetSubjectDetails(filteredIds), + searchQuery.sort, + ) return@withContext LoadResult.Page( subjectInfos, @@ -113,45 +113,7 @@ class SubjectSearchRepository( } catch (e: CancellationException) { throw e } catch (e: Exception) { - try { - Companion.logger.warn(e) { "Failed to search by ANI api, trying fallback Bangumi api." } - - val res = bangumiSubjectSearchService.searchSubjectIds( - searchQuery.keywords, - useNewApi = useNewApi(), - offset = offset, - limit = params.loadSize, - filters = filters, - sort = searchQuery.sort.toBangumiSort(), - ) - - val filtered = if (ignoreDoneAndDropped()) { - val excludedIds = subjectCollectionRepository.getSubjectIdsByCollectionType( - types = listOf(UnifiedCollectionType.DONE, UnifiedCollectionType.DROPPED), - ).first() - - MutableIntList().apply { - res.forEach { if (it !in excludedIds) add(it) } - } - } else { - res - } - - val subjectInfos = filterSubjectsBySort( - subjectService.batchGetSubjectDetails(filtered), - searchQuery.sort, - ) - - return@withContext LoadResult.Page( - subjectInfos, - prevKey = if (offset == 0) null else offset, - nextKey = if (subjectInfos.isEmpty()) null else offset + params.loadSize, - ) - } catch (e: CancellationException) { - throw e - } catch (e: Exception) { - LoadResult.Error(RepositoryException.wrapOrThrowCancellation(e)) - } + LoadResult.Error(RepositoryException.wrapOrThrowCancellation(e)) } } @@ -273,4 +235,3 @@ class SubjectSearchRepository( private fun SubjectType.toBangumiSubjectType(): BangumiSubjectType = when (this) { SubjectType.ANIME -> BangumiSubjectType.Anime } -