From a3ab8746bf8b1cfca628431e76f9c63ecd973010 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 3 Jul 2022 16:12:31 -0400 Subject: [PATCH] More domain model usage --- .../kanade/domain/category/model/Category.kt | 4 +++- .../data/database/models/Category.kt | 11 --------- .../data/library/LibraryUpdateNotifier.kt | 8 +++---- .../data/library/LibraryUpdateService.kt | 16 ++++++++----- .../data/notification/NotificationReceiver.kt | 4 ++-- .../ui/library/LibraryCategoryAdapter.kt | 2 +- .../ui/library/LibraryCategoryView.kt | 17 +++++++------- .../tachiyomi/ui/library/LibraryController.kt | 20 ++++++++-------- .../tachiyomi/ui/library/LibraryPresenter.kt | 23 ++++++++++--------- .../ui/library/LibrarySelectionEvent.kt | 5 ++-- 10 files changed, 52 insertions(+), 58 deletions(-) diff --git a/app/src/main/java/eu/kanade/domain/category/model/Category.kt b/app/src/main/java/eu/kanade/domain/category/model/Category.kt index 3171b4664..6c1473ff1 100644 --- a/app/src/main/java/eu/kanade/domain/category/model/Category.kt +++ b/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() diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Category.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Category.kt index 9781f0139..806a36671 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Category.kt +++ b/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? { diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateNotifier.kt b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateNotifier.kt index 0fe4733c2..645cc37c2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateNotifier.kt +++ b/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) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt index abafc364e..bb370c7f4 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt +++ b/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() - val newUpdates = CopyOnWriteArrayList>>() + val newUpdates = CopyOnWriteArrayList>>() val skippedUpdates = CopyOnWriteArrayList>() val failedUpdates = CopyOnWriteArrayList>() 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, ) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationReceiver.kt b/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationReceiver.kt index 59ad5aff4..a64a6a34f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationReceiver.kt +++ b/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 diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt index 11246b4e0..2851426a2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt +++ b/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. diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt index c0264564a..f7319beaf 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt +++ b/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() } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt index d6c252666..6fc1d9f0d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt +++ b/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() + val selectedMangas = mutableSetOf() /** * 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, addCategories: List, removeCategories: List) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt index b7b9759cf..392ec7d7b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt +++ b/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): Collection { + suspend fun getCommonCategories(mangas: List): Collection { 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): Collection { + suspend fun getMixCategories(mangas: List): Collection { 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) { + fun downloadUnreadChapters(mangas: List) { 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, read: Boolean) { + fun markReadStatus(mangas: List, 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) { + private fun deleteChapters(manga: Manga, chapters: List) { sourceManager.get(manga.source)?.let { source -> - downloadManager.deleteChapters(chapters, manga, source) + downloadManager.deleteChapters(chapters, manga.toDbManga(), source) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySelectionEvent.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySelectionEvent.kt index 58f89a4aa..7b93e2502 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibrarySelectionEvent.kt +++ b/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() }