MangaRepositoryImpl.kt 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package eu.kanade.data.manga
  2. import eu.kanade.data.DatabaseHandler
  3. import eu.kanade.data.listOfStringsAdapter
  4. import eu.kanade.data.toLong
  5. import eu.kanade.domain.manga.model.Manga
  6. import eu.kanade.domain.manga.model.MangaUpdate
  7. import eu.kanade.domain.manga.repository.MangaRepository
  8. import eu.kanade.tachiyomi.util.system.logcat
  9. import kotlinx.coroutines.flow.Flow
  10. import logcat.LogPriority
  11. class MangaRepositoryImpl(
  12. private val handler: DatabaseHandler,
  13. ) : MangaRepository {
  14. override suspend fun getMangaById(id: Long): Manga {
  15. return handler.awaitOne { mangasQueries.getMangaById(id, mangaMapper) }
  16. }
  17. override fun getFavoritesBySourceId(sourceId: Long): Flow<List<Manga>> {
  18. return handler.subscribeToList { mangasQueries.getFavoriteBySourceId(sourceId, mangaMapper) }
  19. }
  20. override suspend fun getDuplicateLibraryManga(title: String, sourceId: Long): Manga? {
  21. return handler.awaitOneOrNull {
  22. mangasQueries.getDuplicateLibraryManga(title, sourceId, mangaMapper)
  23. }
  24. }
  25. override suspend fun resetViewerFlags(): Boolean {
  26. return try {
  27. handler.await { mangasQueries.resetViewerFlags() }
  28. true
  29. } catch (e: Exception) {
  30. logcat(LogPriority.ERROR, e)
  31. false
  32. }
  33. }
  34. override suspend fun update(update: MangaUpdate): Boolean {
  35. return try {
  36. handler.await {
  37. mangasQueries.update(
  38. source = update.source,
  39. url = update.url,
  40. artist = update.artist,
  41. author = update.author,
  42. description = update.description,
  43. genre = update.genre?.let(listOfStringsAdapter::encode),
  44. title = update.title,
  45. status = update.status,
  46. thumbnailUrl = update.thumbnailUrl,
  47. favorite = update.favorite?.toLong(),
  48. lastUpdate = update.lastUpdate,
  49. initialized = update.initialized?.toLong(),
  50. viewer = update.viewerFlags,
  51. chapterFlags = update.chapterFlags,
  52. coverLastModified = update.coverLastModified,
  53. dateAdded = update.dateAdded,
  54. mangaId = update.id,
  55. )
  56. }
  57. true
  58. } catch (e: Exception) {
  59. logcat(LogPriority.ERROR, e)
  60. false
  61. }
  62. }
  63. }