Browse Source

More domain model usage

arkon 2 years ago
parent
commit
a3ab8746bf

+ 3 - 1
app/src/main/java/eu/kanade/domain/category/model/Category.kt

@@ -2,6 +2,7 @@ package eu.kanade.domain.category.model
 
 import android.content.Context
 import eu.kanade.tachiyomi.R
+import eu.kanade.tachiyomi.data.database.models.CategoryImpl
 import eu.kanade.tachiyomi.ui.library.setting.DisplayModeSetting
 import eu.kanade.tachiyomi.ui.library.setting.SortDirectionSetting
 import eu.kanade.tachiyomi.ui.library.setting.SortModeSetting
@@ -36,7 +37,8 @@ data class Category(
     }
 }
 
-fun Category.toDbCategory(): DbCategory = DbCategory.create(name).also {
+fun Category.toDbCategory(): DbCategory = CategoryImpl().also {
+    it.name = name
     it.id = id.toInt()
     it.order = order.toInt()
     it.flags = flags.toInt()

+ 0 - 11
app/src/main/java/eu/kanade/tachiyomi/data/database/models/Category.kt

@@ -1,7 +1,5 @@
 package eu.kanade.tachiyomi.data.database.models
 
-import android.content.Context
-import eu.kanade.tachiyomi.R
 import eu.kanade.tachiyomi.ui.library.setting.DisplayModeSetting
 import eu.kanade.tachiyomi.ui.library.setting.SortDirectionSetting
 import eu.kanade.tachiyomi.ui.library.setting.SortModeSetting
@@ -33,15 +31,6 @@ interface Category : Serializable {
     var sortDirection: Int
         get() = flags and SortDirectionSetting.MASK.toInt()
         set(mode) = setFlags(mode, SortDirectionSetting.MASK.toInt())
-
-    companion object {
-
-        fun create(name: String): Category = CategoryImpl().apply {
-            this.name = name
-        }
-
-        fun createDefault(context: Context): Category = create(context.getString(R.string.label_default)).apply { id = 0 }
-    }
 }
 
 fun Category.toDomainCategory(): DomainCategory? {

+ 4 - 4
app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateNotifier.kt

@@ -13,9 +13,9 @@ import androidx.core.app.NotificationManagerCompat
 import coil.imageLoader
 import coil.request.ImageRequest
 import coil.transform.CircleCropTransformation
+import eu.kanade.domain.chapter.model.Chapter
+import eu.kanade.domain.manga.model.Manga
 import eu.kanade.tachiyomi.R
-import eu.kanade.tachiyomi.data.database.models.Chapter
-import eu.kanade.tachiyomi.data.database.models.Manga
 import eu.kanade.tachiyomi.data.download.Downloader
 import eu.kanade.tachiyomi.data.notification.NotificationHandler
 import eu.kanade.tachiyomi.data.notification.NotificationReceiver
@@ -294,8 +294,8 @@ class LibraryUpdateNotifier(private val context: Context) {
 
         val displayableChapterNumbers = chapters
             .filter { it.isRecognizedNumber }
-            .sortedBy { it.chapter_number }
-            .map { formatter.format(it.chapter_number) }
+            .sortedBy { it.chapterNumber }
+            .map { formatter.format(it.chapterNumber) }
             .toSet()
 
         return when (displayableChapterNumbers.size) {

+ 10 - 6
app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt

@@ -27,6 +27,7 @@ import eu.kanade.tachiyomi.data.cache.CoverCache
 import eu.kanade.tachiyomi.data.database.models.Chapter
 import eu.kanade.tachiyomi.data.database.models.LibraryManga
 import eu.kanade.tachiyomi.data.database.models.Manga
+import eu.kanade.tachiyomi.data.database.models.toDomainChapter
 import eu.kanade.tachiyomi.data.database.models.toDomainManga
 import eu.kanade.tachiyomi.data.database.models.toMangaInfo
 import eu.kanade.tachiyomi.data.download.DownloadManager
@@ -305,7 +306,7 @@ class LibraryUpdateService(
         val semaphore = Semaphore(5)
         val progressCount = AtomicInteger(0)
         val currentlyUpdatingManga = CopyOnWriteArrayList<LibraryManga>()
-        val newUpdates = CopyOnWriteArrayList<Pair<LibraryManga, Array<Chapter>>>()
+        val newUpdates = CopyOnWriteArrayList<Pair<DomainManga, Array<DomainChapter>>>()
         val skippedUpdates = CopyOnWriteArrayList<Pair<Manga, String?>>()
         val failedUpdates = CopyOnWriteArrayList<Pair<Manga, String?>>()
         val hasDownloads = AtomicBoolean(false)
@@ -358,8 +359,11 @@ class LibraryUpdateService(
 
                                                         // Convert to the manga that contains new chapters
                                                         newUpdates.add(
-                                                            mangaWithNotif to newDbChapters.sortedByDescending { ch -> ch.source_order }
-                                                                .toTypedArray(),
+                                                            mangaWithNotif.toDomainManga()!! to
+                                                                newDbChapters
+                                                                    .map { it.toDomainChapter()!! }
+                                                                    .sortedByDescending { it.sourceOrder }
+                                                                    .toTypedArray(),
                                                         )
                                                     }
                                                 }
@@ -511,7 +515,7 @@ class LibraryUpdateService(
                 return
             }
 
-            notifier.showProgressNotification(listOf(manga), progressCount++, mangaToUpdate.size)
+            notifier.showProgressNotification(listOf(manga.toDomainManga()!!), progressCount++, mangaToUpdate.size)
 
             // Update the tracking details.
             updateTrackings(manga, loggedServices)
@@ -558,7 +562,7 @@ class LibraryUpdateService(
 
         updatingManga.add(manga)
         notifier.showProgressNotification(
-            updatingManga,
+            updatingManga.map { it.toDomainManga()!! },
             completed.get(),
             mangaToUpdate.size,
         )
@@ -572,7 +576,7 @@ class LibraryUpdateService(
         updatingManga.remove(manga)
         completed.andIncrement
         notifier.showProgressNotification(
-            updatingManga,
+            updatingManga.map { it.toDomainManga()!! },
             completed.get(),
             mangaToUpdate.size,
         )

+ 2 - 2
app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationReceiver.kt

@@ -9,14 +9,14 @@ import android.os.Build
 import androidx.core.content.ContextCompat
 import eu.kanade.domain.chapter.interactor.GetChapter
 import eu.kanade.domain.chapter.interactor.UpdateChapter
+import eu.kanade.domain.chapter.model.Chapter
 import eu.kanade.domain.chapter.model.toChapterUpdate
 import eu.kanade.domain.chapter.model.toDbChapter
 import eu.kanade.domain.manga.interactor.GetManga
+import eu.kanade.domain.manga.model.Manga
 import eu.kanade.domain.manga.model.toDbManga
 import eu.kanade.tachiyomi.R
 import eu.kanade.tachiyomi.data.backup.BackupRestoreService
-import eu.kanade.tachiyomi.data.database.models.Chapter
-import eu.kanade.tachiyomi.data.database.models.Manga
 import eu.kanade.tachiyomi.data.download.DownloadManager
 import eu.kanade.tachiyomi.data.download.DownloadService
 import eu.kanade.tachiyomi.data.library.LibraryUpdateService

+ 1 - 1
app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt

@@ -1,7 +1,7 @@
 package eu.kanade.tachiyomi.ui.library
 
 import eu.davidea.flexibleadapter.FlexibleAdapter
-import eu.kanade.tachiyomi.data.database.models.Manga
+import eu.kanade.domain.manga.model.Manga
 
 /**
  * Adapter storing a list of manga in a certain category.

+ 9 - 8
app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt

@@ -9,8 +9,9 @@ import dev.chrisbanes.insetter.applyInsetter
 import eu.davidea.flexibleadapter.FlexibleAdapter
 import eu.davidea.flexibleadapter.SelectableAdapter
 import eu.kanade.domain.category.model.Category
+import eu.kanade.domain.manga.model.Manga
 import eu.kanade.tachiyomi.R
-import eu.kanade.tachiyomi.data.database.models.Manga
+import eu.kanade.tachiyomi.data.database.models.toDomainManga
 import eu.kanade.tachiyomi.data.library.LibraryUpdateService
 import eu.kanade.tachiyomi.databinding.LibraryCategoryBinding
 import eu.kanade.tachiyomi.ui.main.MainActivity
@@ -143,7 +144,7 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
             .filter { it == category.id }
             .subscribe {
                 adapter.currentItems.forEach { item ->
-                    controller.setSelection(item.manga, true)
+                    controller.setSelection(item.manga.toDomainManga()!!, true)
                 }
                 controller.invalidateActionMode()
             }
@@ -152,7 +153,7 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
             .filter { it == category.id }
             .subscribe {
                 adapter.currentItems.forEach { item ->
-                    controller.toggleSelection(item.manga)
+                    controller.toggleSelection(item.manga.toDomainManga()!!)
                 }
                 controller.invalidateActionMode()
             }
@@ -191,7 +192,7 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
                 val position = adapter.indexOf(manga)
                 if (position != -1 && !adapter.isSelected(position)) {
                     adapter.toggleSelection(position)
-                    (recycler.findViewHolderForItemId(manga.id!!) as? LibraryHolder<*>)?.toggleActivation()
+                    (recycler.findViewHolderForItemId(manga.id) as? LibraryHolder<*>)?.toggleActivation()
                 }
             }
         }
@@ -241,7 +242,7 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
         val position = adapter.indexOf(manga)
         if (position != -1) {
             adapter.toggleSelection(position)
-            (recycler.findViewHolderForItemId(manga.id!!) as? LibraryHolder<*>)?.toggleActivation()
+            (recycler.findViewHolderForItemId(manga.id) as? LibraryHolder<*>)?.toggleActivation()
         }
     }
 
@@ -263,7 +264,7 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
             toggleSelection(position)
             true
         } else {
-            openManga(item.manga)
+            openManga(item.manga.toDomainManga()!!)
             false
         }
     }
@@ -309,7 +310,7 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
     private fun toggleSelection(position: Int) {
         val item = adapter.getItem(position) ?: return
 
-        controller.setSelection(item.manga, !adapter.isSelected(position))
+        controller.setSelection(item.manga.toDomainManga()!!, !adapter.isSelected(position))
         controller.invalidateActionMode()
     }
 
@@ -321,7 +322,7 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
     private fun setSelection(position: Int) {
         val item = adapter.getItem(position) ?: return
 
-        controller.setSelection(item.manga, true)
+        controller.setSelection(item.manga.toDomainManga()!!, true)
         controller.invalidateActionMode()
     }
 }

+ 9 - 11
app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt

@@ -20,7 +20,6 @@ import eu.kanade.domain.category.model.toDbCategory
 import eu.kanade.domain.manga.model.Manga
 import eu.kanade.domain.manga.model.toDbManga
 import eu.kanade.tachiyomi.R
-import eu.kanade.tachiyomi.data.database.models.toDomainManga
 import eu.kanade.tachiyomi.data.library.LibraryUpdateService
 import eu.kanade.tachiyomi.data.preference.PreferencesHelper
 import eu.kanade.tachiyomi.databinding.LibraryControllerBinding
@@ -53,7 +52,6 @@ import rx.android.schedulers.AndroidSchedulers
 import uy.kohesive.injekt.Injekt
 import uy.kohesive.injekt.api.get
 import java.util.concurrent.TimeUnit
-import eu.kanade.tachiyomi.data.database.models.Manga as DbManga
 
 class LibraryController(
     bundle: Bundle? = null,
@@ -78,7 +76,7 @@ class LibraryController(
     /**
      * Currently selected mangas.
      */
-    val selectedMangas = mutableSetOf<DbManga>()
+    val selectedMangas = mutableSetOf<Manga>()
 
     /**
      * Relay to notify the UI of selection updates.
@@ -490,18 +488,18 @@ class LibraryController(
     override fun onDestroyActionMode(mode: ActionMode) {
         // Clear all the manga selections and notify child views.
         selectedMangas.clear()
-        selectionRelay.call(LibrarySelectionEvent.Cleared())
+        selectionRelay.call(LibrarySelectionEvent.Cleared)
 
         (activity as? MainActivity)?.showBottomNav(true)
 
         actionMode = null
     }
 
-    fun openManga(manga: DbManga) {
+    fun openManga(manga: Manga) {
         // Notify the presenter a manga is being opened.
         presenter.onOpenManga()
 
-        router.pushController(MangaController(manga.id!!))
+        router.pushController(MangaController(manga.id))
     }
 
     /**
@@ -510,7 +508,7 @@ class LibraryController(
      * @param manga the manga whose selection has changed.
      * @param selected whether it's now selected or not.
      */
-    fun setSelection(manga: DbManga, selected: Boolean) {
+    fun setSelection(manga: Manga, selected: Boolean) {
         if (selected) {
             if (selectedMangas.add(manga)) {
                 selectionRelay.call(LibrarySelectionEvent.Selected(manga))
@@ -527,7 +525,7 @@ class LibraryController(
      *
      * @param manga the manga whose selection to change.
      */
-    fun toggleSelection(manga: DbManga) {
+    fun toggleSelection(manga: Manga) {
         if (selectedMangas.add(manga)) {
             selectionRelay.call(LibrarySelectionEvent.Selected(manga))
         } else if (selectedMangas.remove(manga)) {
@@ -541,7 +539,7 @@ class LibraryController(
      */
     fun clearSelection() {
         selectedMangas.clear()
-        selectionRelay.call(LibrarySelectionEvent.Cleared())
+        selectionRelay.call(LibrarySelectionEvent.Cleared)
         invalidateActionMode()
     }
 
@@ -568,7 +566,7 @@ class LibraryController(
                 }
             }.toTypedArray()
             launchUI {
-                ChangeMangaCategoriesDialog(this@LibraryController, mangas.map { it.toDomainManga()!! }, categories, preselected)
+                ChangeMangaCategoriesDialog(this@LibraryController, mangas, categories, preselected)
                     .showDialog(router)
             }
         }
@@ -587,7 +585,7 @@ class LibraryController(
     }
 
     private fun showDeleteMangaDialog() {
-        DeleteLibraryMangasDialog(this, selectedMangas.toList().map { it.toDomainManga()!! }).showDialog(router)
+        DeleteLibraryMangasDialog(this, selectedMangas.toList()).showDialog(router)
     }
 
     override fun updateCategoriesForMangas(mangas: List<Manga>, addCategories: List<Category>, removeCategories: List<Category>) {

+ 12 - 11
app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt

@@ -16,6 +16,7 @@ import eu.kanade.domain.manga.interactor.UpdateManga
 import eu.kanade.domain.manga.model.Manga
 import eu.kanade.domain.manga.model.MangaUpdate
 import eu.kanade.domain.manga.model.isLocal
+import eu.kanade.domain.manga.model.toDbManga
 import eu.kanade.domain.track.interactor.GetTracks
 import eu.kanade.tachiyomi.data.cache.CoverCache
 import eu.kanade.tachiyomi.data.database.models.Chapter
@@ -489,10 +490,10 @@ class LibraryPresenter(
      *
      * @param mangas the list of manga.
      */
-    suspend fun getCommonCategories(mangas: List<DbManga>): Collection<Category> {
+    suspend fun getCommonCategories(mangas: List<Manga>): Collection<Category> {
         if (mangas.isEmpty()) return emptyList()
         return mangas.toSet()
-            .map { getCategories.await(it.id!!) }
+            .map { getCategories.await(it.id) }
             .reduce { set1, set2 -> set1.intersect(set2).toMutableList() }
     }
 
@@ -501,9 +502,9 @@ class LibraryPresenter(
      *
      * @param mangas the list of manga.
      */
-    suspend fun getMixCategories(mangas: List<DbManga>): Collection<Category> {
+    suspend fun getMixCategories(mangas: List<Manga>): Collection<Category> {
         if (mangas.isEmpty()) return emptyList()
-        val mangaCategories = mangas.toSet().map { getCategories.await(it.id!!) }
+        val mangaCategories = mangas.toSet().map { getCategories.await(it.id) }
         val common = mangaCategories.reduce { set1, set2 -> set1.intersect(set2).toMutableList() }
         return mangaCategories.flatten().distinct().subtract(common).toMutableList()
     }
@@ -513,14 +514,14 @@ class LibraryPresenter(
      *
      * @param mangas the list of manga.
      */
-    fun downloadUnreadChapters(mangas: List<DbManga>) {
+    fun downloadUnreadChapters(mangas: List<Manga>) {
         mangas.forEach { manga ->
             launchIO {
-                val chapters = getChapterByMangaId.await(manga.id!!)
+                val chapters = getChapterByMangaId.await(manga.id)
                     .filter { !it.read }
                     .map { it.toDbChapter() }
 
-                downloadManager.downloadChapters(manga, chapters)
+                downloadManager.downloadChapters(manga.toDbManga(), chapters)
             }
         }
     }
@@ -530,10 +531,10 @@ class LibraryPresenter(
      *
      * @param mangas the list of manga.
      */
-    fun markReadStatus(mangas: List<DbManga>, read: Boolean) {
+    fun markReadStatus(mangas: List<Manga>, read: Boolean) {
         mangas.forEach { manga ->
             launchIO {
-                val chapters = getChapterByMangaId.await(manga.id!!)
+                val chapters = getChapterByMangaId.await(manga.id)
 
                 val toUpdate = chapters
                     .map { chapter ->
@@ -552,9 +553,9 @@ class LibraryPresenter(
         }
     }
 
-    private fun deleteChapters(manga: DbManga, chapters: List<Chapter>) {
+    private fun deleteChapters(manga: Manga, chapters: List<Chapter>) {
         sourceManager.get(manga.source)?.let { source ->
-            downloadManager.deleteChapters(chapters, manga, source)
+            downloadManager.deleteChapters(chapters, manga.toDbManga(), source)
         }
     }
 

+ 2 - 3
app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySelectionEvent.kt

@@ -1,10 +1,9 @@
 package eu.kanade.tachiyomi.ui.library
 
-import eu.kanade.tachiyomi.data.database.models.Manga
+import eu.kanade.domain.manga.model.Manga
 
 sealed class LibrarySelectionEvent {
-
     class Selected(val manga: Manga) : LibrarySelectionEvent()
     class Unselected(val manga: Manga) : LibrarySelectionEvent()
-    class Cleared : LibrarySelectionEvent()
+    object Cleared : LibrarySelectionEvent()
 }