Manga.kt 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package eu.kanade.domain.manga.model
  2. import eu.kanade.domain.base.BasePreferences
  3. import eu.kanade.tachiyomi.data.cache.CoverCache
  4. import eu.kanade.tachiyomi.source.model.SManga
  5. import eu.kanade.tachiyomi.ui.reader.setting.OrientationType
  6. import eu.kanade.tachiyomi.ui.reader.setting.ReadingModeType
  7. import tachiyomi.core.metadata.comicinfo.ComicInfo
  8. import tachiyomi.core.metadata.comicinfo.ComicInfoPublishingStatus
  9. import tachiyomi.domain.chapter.model.Chapter
  10. import tachiyomi.domain.manga.model.Manga
  11. import tachiyomi.domain.manga.model.TriStateFilter
  12. import uy.kohesive.injekt.Injekt
  13. import uy.kohesive.injekt.api.get
  14. // TODO: move these into the domain model
  15. val Manga.readingModeType: Long
  16. get() = viewerFlags and ReadingModeType.MASK.toLong()
  17. val Manga.orientationType: Long
  18. get() = viewerFlags and OrientationType.MASK.toLong()
  19. val Manga.downloadedFilter: TriStateFilter
  20. get() {
  21. if (forceDownloaded()) return TriStateFilter.ENABLED_IS
  22. return when (downloadedFilterRaw) {
  23. Manga.CHAPTER_SHOW_DOWNLOADED -> TriStateFilter.ENABLED_IS
  24. Manga.CHAPTER_SHOW_NOT_DOWNLOADED -> TriStateFilter.ENABLED_NOT
  25. else -> TriStateFilter.DISABLED
  26. }
  27. }
  28. fun Manga.chaptersFiltered(): Boolean {
  29. return unreadFilter != TriStateFilter.DISABLED ||
  30. downloadedFilter != TriStateFilter.DISABLED ||
  31. bookmarkedFilter != TriStateFilter.DISABLED
  32. }
  33. fun Manga.forceDownloaded(): Boolean {
  34. return favorite && Injekt.get<BasePreferences>().downloadedOnly().get()
  35. }
  36. fun Manga.toSManga(): SManga = SManga.create().also {
  37. it.url = url
  38. it.title = title
  39. it.artist = artist
  40. it.author = author
  41. it.description = description
  42. it.genre = genre.orEmpty().joinToString()
  43. it.status = status.toInt()
  44. it.thumbnail_url = thumbnailUrl
  45. it.initialized = initialized
  46. }
  47. fun Manga.copyFrom(other: SManga): Manga {
  48. val author = other.author ?: author
  49. val artist = other.artist ?: artist
  50. val description = other.description ?: description
  51. val genres = if (other.genre != null) {
  52. other.getGenres()
  53. } else {
  54. genre
  55. }
  56. val thumbnailUrl = other.thumbnail_url ?: thumbnailUrl
  57. return this.copy(
  58. author = author,
  59. artist = artist,
  60. description = description,
  61. genre = genres,
  62. thumbnailUrl = thumbnailUrl,
  63. status = other.status.toLong(),
  64. updateStrategy = other.update_strategy,
  65. initialized = other.initialized && initialized,
  66. )
  67. }
  68. fun SManga.toDomainManga(sourceId: Long): Manga {
  69. return Manga.create().copy(
  70. url = url,
  71. title = title,
  72. artist = artist,
  73. author = author,
  74. description = description,
  75. genre = getGenres(),
  76. status = status.toLong(),
  77. thumbnailUrl = thumbnail_url,
  78. updateStrategy = update_strategy,
  79. initialized = initialized,
  80. source = sourceId,
  81. )
  82. }
  83. fun Manga.hasCustomCover(coverCache: CoverCache = Injekt.get()): Boolean {
  84. return coverCache.getCustomCoverFile(id).exists()
  85. }
  86. /**
  87. * Creates a ComicInfo instance based on the manga and chapter metadata.
  88. */
  89. fun getComicInfo(manga: Manga, chapter: Chapter, chapterUrl: String) = ComicInfo(
  90. title = ComicInfo.Title(chapter.name),
  91. series = ComicInfo.Series(manga.title),
  92. number = chapter.chapterNumber.takeIf { it >= 0 }?.let { ComicInfo.Number(it.toString()) },
  93. web = ComicInfo.Web(chapterUrl),
  94. summary = manga.description?.let { ComicInfo.Summary(it) },
  95. writer = manga.author?.let { ComicInfo.Writer(it) },
  96. penciller = manga.artist?.let { ComicInfo.Penciller(it) },
  97. translator = chapter.scanlator?.let { ComicInfo.Translator(it) },
  98. genre = manga.genre?.let { ComicInfo.Genre(it.joinToString()) },
  99. publishingStatus = ComicInfo.PublishingStatusTachiyomi(
  100. ComicInfoPublishingStatus.toComicInfoValue(manga.status),
  101. ),
  102. inker = null,
  103. colorist = null,
  104. letterer = null,
  105. coverArtist = null,
  106. tags = null,
  107. )