Manga.kt 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.ReaderOrientation
  6. import eu.kanade.tachiyomi.ui.reader.setting.ReadingMode
  7. import tachiyomi.core.metadata.comicinfo.ComicInfo
  8. import tachiyomi.core.metadata.comicinfo.ComicInfoPublishingStatus
  9. import tachiyomi.core.preference.TriState
  10. import tachiyomi.domain.chapter.model.Chapter
  11. import tachiyomi.domain.manga.model.Manga
  12. import uy.kohesive.injekt.Injekt
  13. import uy.kohesive.injekt.api.get
  14. // TODO: move these into the domain model
  15. val Manga.readingMode: Long
  16. get() = viewerFlags and ReadingMode.MASK.toLong()
  17. val Manga.readerOrientation: Long
  18. get() = viewerFlags and ReaderOrientation.MASK.toLong()
  19. val Manga.downloadedFilter: TriState
  20. get() {
  21. if (forceDownloaded()) return TriState.ENABLED_IS
  22. return when (downloadedFilterRaw) {
  23. Manga.CHAPTER_SHOW_DOWNLOADED -> TriState.ENABLED_IS
  24. Manga.CHAPTER_SHOW_NOT_DOWNLOADED -> TriState.ENABLED_NOT
  25. else -> TriState.DISABLED
  26. }
  27. }
  28. fun Manga.chaptersFiltered(): Boolean {
  29. return unreadFilter != TriState.DISABLED ||
  30. downloadedFilter != TriState.DISABLED ||
  31. bookmarkedFilter != TriState.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, categories: List<String>?) = ComicInfo(
  90. title = ComicInfo.Title(chapter.name),
  91. series = ComicInfo.Series(manga.title),
  92. number = chapter.chapterNumber.takeIf { it >= 0 }?.let {
  93. if ((it.rem(1) == 0.0)) {
  94. ComicInfo.Number(it.toInt().toString())
  95. } else {
  96. ComicInfo.Number(it.toString())
  97. }
  98. },
  99. web = ComicInfo.Web(chapterUrl),
  100. summary = manga.description?.let { ComicInfo.Summary(it) },
  101. writer = manga.author?.let { ComicInfo.Writer(it) },
  102. penciller = manga.artist?.let { ComicInfo.Penciller(it) },
  103. translator = chapter.scanlator?.let { ComicInfo.Translator(it) },
  104. genre = manga.genre?.let { ComicInfo.Genre(it.joinToString()) },
  105. publishingStatus = ComicInfo.PublishingStatusTachiyomi(
  106. ComicInfoPublishingStatus.toComicInfoValue(manga.status),
  107. ),
  108. categories = categories?.let { ComicInfo.CategoriesTachiyomi(it.joinToString()) },
  109. inker = null,
  110. colorist = null,
  111. letterer = null,
  112. coverArtist = null,
  113. tags = null,
  114. )