Skip to content

Commit

Permalink
Fix: crash upon open RecommendsScreen if source is removed
Browse files Browse the repository at this point in the history
  • Loading branch information
cuong-tran committed Jan 24, 2025
1 parent 7e16b56 commit 343cd37
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import eu.kanade.tachiyomi.ui.webview.WebViewScreen
import eu.kanade.tachiyomi.util.system.toast
import exh.md.follows.MangaDexFollowsScreen
import exh.source.isEhBasedSource
import exh.source.isMdBasedSource
import exh.ui.smartsearch.SmartSearchScreen
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.collectLatest
Expand Down Expand Up @@ -408,7 +409,7 @@ data class BrowseSourceScreen(
// KMK -->
onSavedSearchPressDesc = stringResource(KMR.strings.saved_searches_delete),
// KMK <--
openMangaDexRandom = if (screenModel.sourceIsMangaDex) {
openMangaDexRandom = if (screenModel.source.isMdBasedSource()) {
{
screenModel.onMangaDexRandom {
navigator.replace(
Expand All @@ -422,7 +423,7 @@ data class BrowseSourceScreen(
} else {
null
},
openMangaDexFollows = if (screenModel.sourceIsMangaDex) {
openMangaDexFollows = if (screenModel.source.isMdBasedSource()) {
{
navigator.replace(MangaDexFollowsScreen(sourceId))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import eu.kanade.tachiyomi.source.online.all.MangaDex
import eu.kanade.tachiyomi.util.removeCovers
import exh.metadata.metadata.RaisedSearchMetadata
import exh.source.getMainSource
import exh.source.mangaDexSourceIds
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toImmutableList
Expand Down Expand Up @@ -125,8 +124,6 @@ open class BrowseSourceScreenModel(
val startExpanded by uiPreferences.expandFilters().asState(screenModelScope)

private val filterSerializer = FilterSerializer()

val sourceIsMangaDex = sourceId in mangaDexSourceIds
// SY <--

init {
Expand Down
16 changes: 15 additions & 1 deletion app/src/main/java/exh/recs/BrowseRecommendsScreenModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import exh.recs.sources.RecommendationPagingSource
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.runBlocking
import tachiyomi.domain.manga.interactor.GetManga
import tachiyomi.domain.source.service.SourceManager
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get

Expand All @@ -15,12 +16,25 @@ class BrowseRecommendsScreenModel(
sourceId: Long,
private val recommendationSourceName: String,
private val getManga: GetManga = Injekt.get(),
// KMK -->
sourceManager: SourceManager = Injekt.get(),
// KMK <--
) : BrowseSourceScreenModel(sourceId, null) {

val manga = runBlocking { getManga.await(mangaId) }!!

// KMK -->
val source_ = sourceManager.get(sourceId)
?.let { it as CatalogueSource }
// KMK <--

val recommendationSource: RecommendationPagingSource
get() = RecommendationPagingSource.createSources(manga, source as CatalogueSource).first {
get() = RecommendationPagingSource.createSources(
manga,
// KMK -->
source_,
// KMK <--
).first {
it::class.qualifiedName == recommendationSourceName
}

Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/exh/recs/RecommendsScreenModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ open class RecommendsScreenModel(
internal val networkToLocalManga: NetworkToLocalManga = Injekt.get(),
) : StateScreenModel<RecommendsScreenModel.State>(State()) {

val source = sourceManager.getOrStub(sourceId) as CatalogueSource
val source = sourceManager.get(sourceId)
// KMK -->
?.let { it as CatalogueSource }
// KMK <--

private val coroutineDispatcher = Dispatchers.IO.limitedParallelism(5)

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/exh/recs/sources/AniListPagingSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import tachiyomi.core.common.util.system.logcat
import tachiyomi.domain.manga.model.Manga
import tachiyomi.i18n.sy.SYMR

class AniListPagingSource(manga: Manga, source: CatalogueSource) : TrackerRecommendationPagingSource(
class AniListPagingSource(manga: Manga, source: CatalogueSource?) : TrackerRecommendationPagingSource(
"https://graphql.anilist.co/", source, manga,
) {
override val name: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import tachiyomi.core.common.util.system.logcat
import tachiyomi.domain.manga.model.Manga
import tachiyomi.i18n.sy.SYMR

abstract class MangaUpdatesPagingSource(manga: Manga, source: CatalogueSource) : TrackerRecommendationPagingSource(
abstract class MangaUpdatesPagingSource(manga: Manga, source: CatalogueSource?) : TrackerRecommendationPagingSource(
"https://api.mangaupdates.com/v1/", source, manga,
) {
override val name: String
Expand Down Expand Up @@ -98,14 +98,14 @@ abstract class MangaUpdatesPagingSource(manga: Manga, source: CatalogueSource) :
}
}

class MangaUpdatesCommunityPagingSource(manga: Manga, source: CatalogueSource) : MangaUpdatesPagingSource(manga, source) {
class MangaUpdatesCommunityPagingSource(manga: Manga, source: CatalogueSource?) : MangaUpdatesPagingSource(manga, source) {
override val category: StringResource
get() = SYMR.strings.community_recommendations
override val recommendationJsonObjectName: String
get() = "recommendations"
}

class MangaUpdatesSimilarPagingSource(manga: Manga, source: CatalogueSource) : MangaUpdatesPagingSource(manga, source) {
class MangaUpdatesSimilarPagingSource(manga: Manga, source: CatalogueSource?) : MangaUpdatesPagingSource(manga, source) {
override val category: StringResource
get() = SYMR.strings.similar_titles
override val recommendationJsonObjectName: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import tachiyomi.core.common.util.system.logcat
import tachiyomi.domain.manga.model.Manga
import tachiyomi.i18n.sy.SYMR

class MyAnimeListPagingSource(manga: Manga, source: CatalogueSource) : TrackerRecommendationPagingSource(
class MyAnimeListPagingSource(manga: Manga, source: CatalogueSource?) : TrackerRecommendationPagingSource(
"https://api.jikan.moe/v4/", source, manga,
) {
override val name: String
Expand Down
21 changes: 16 additions & 5 deletions app/src/main/java/exh/recs/sources/RecommendationPagingSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import uy.kohesive.injekt.injectLazy
* General class for recommendation sources.
*/
abstract class RecommendationPagingSource(
source: CatalogueSource,
source: CatalogueSource?,
protected val manga: Manga,
) : SourcePagingSource(source) {
// Display name
Expand All @@ -44,20 +44,31 @@ abstract class RecommendationPagingSource(
open val associatedSourceId: Long? = null

companion object {
fun createSources(manga: Manga, source: CatalogueSource): List<RecommendationPagingSource> {
fun createSources(manga: Manga, source: CatalogueSource?): List<RecommendationPagingSource> {
return buildList {
add(AniListPagingSource(manga, source))
add(MangaUpdatesCommunityPagingSource(manga, source))
add(MangaUpdatesSimilarPagingSource(manga, source))
add(MyAnimeListPagingSource(manga, source))

// Only include MangaDex if the delegate sources are enabled and the source is MD-based
if (source.isMdBasedSource() && Injekt.get<DelegateSourcePreferences>().delegateSources().get()) {
if (
// KMK -->
source != null &&
// KMK <--
source.isMdBasedSource() &&
Injekt.get<DelegateSourcePreferences>().delegateSources().get()
) {
add(MangaDexSimilarPagingSource(manga, source.getMainSource() as MangaDex))
}

// Only include Comick if the source manga is from there
if (source.isComickSource()) {
if (
// KMK -->
source != null &&
// KMK <--
source.isComickSource()
) {
add(ComickPagingSource(manga, source))
}
}.sortedWith(compareBy({ it.name }, { it.category.resourceId }))
Expand All @@ -70,7 +81,7 @@ abstract class RecommendationPagingSource(
*/
abstract class TrackerRecommendationPagingSource(
protected val endpoint: String,
source: CatalogueSource,
source: CatalogueSource?,
manga: Manga,
) : RecommendationPagingSource(source, manga) {
private val getTracks: GetTracks by injectLazy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ import exh.metadata.metadata.RaisedSearchMetadata
import tachiyomi.core.common.util.lang.withIOContext
import tachiyomi.domain.source.repository.SourcePagingSourceType

class SourceSearchPagingSource(source: CatalogueSource, val query: String, val filters: FilterList) :
class SourceSearchPagingSource(/* KMK --> */ override val /* KMK <-- */source: CatalogueSource, val query: String, val filters: FilterList) :
SourcePagingSource(source) {
override suspend fun requestNextPage(currentPage: Int): MangasPage {
return source.getSearchManga(currentPage, query, filters)
}
}

class SourcePopularPagingSource(source: CatalogueSource) : SourcePagingSource(source) {
class SourcePopularPagingSource(/* KMK --> */ override val /* KMK <-- */source: CatalogueSource) : SourcePagingSource(source) {
override suspend fun requestNextPage(currentPage: Int): MangasPage {
return source.getPopularManga(currentPage)
}
}

class SourceLatestPagingSource(source: CatalogueSource) : SourcePagingSource(source) {
class SourceLatestPagingSource(/* KMK --> */ override val /* KMK <-- */source: CatalogueSource) : SourcePagingSource(source) {
override suspend fun requestNextPage(currentPage: Int): MangasPage {
return source.getLatestUpdates(currentPage)
}
}

abstract class SourcePagingSource(
protected open val source: CatalogueSource,
protected open val source: CatalogueSource?,
) : SourcePagingSourceType() {

abstract suspend fun requestNextPage(currentPage: Int): MangasPage
Expand Down

0 comments on commit 343cd37

Please sign in to comment.