More domain model usage

This commit is contained in:
arkon 2022-07-03 16:12:31 -04:00
parent 069bd90c0f
commit a3ab8746bf
10 changed files with 52 additions and 58 deletions

View File

@ -2,6 +2,7 @@ package eu.kanade.domain.category.model
import android.content.Context import android.content.Context
import eu.kanade.tachiyomi.R 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.DisplayModeSetting
import eu.kanade.tachiyomi.ui.library.setting.SortDirectionSetting import eu.kanade.tachiyomi.ui.library.setting.SortDirectionSetting
import eu.kanade.tachiyomi.ui.library.setting.SortModeSetting 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.id = id.toInt()
it.order = order.toInt() it.order = order.toInt()
it.flags = flags.toInt() it.flags = flags.toInt()

View File

@ -1,7 +1,5 @@
package eu.kanade.tachiyomi.data.database.models 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.DisplayModeSetting
import eu.kanade.tachiyomi.ui.library.setting.SortDirectionSetting import eu.kanade.tachiyomi.ui.library.setting.SortDirectionSetting
import eu.kanade.tachiyomi.ui.library.setting.SortModeSetting import eu.kanade.tachiyomi.ui.library.setting.SortModeSetting
@ -33,15 +31,6 @@ interface Category : Serializable {
var sortDirection: Int var sortDirection: Int
get() = flags and SortDirectionSetting.MASK.toInt() get() = flags and SortDirectionSetting.MASK.toInt()
set(mode) = setFlags(mode, 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? { fun Category.toDomainCategory(): DomainCategory? {

View File

@ -13,9 +13,9 @@ import androidx.core.app.NotificationManagerCompat
import coil.imageLoader import coil.imageLoader
import coil.request.ImageRequest import coil.request.ImageRequest
import coil.transform.CircleCropTransformation 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.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.download.Downloader
import eu.kanade.tachiyomi.data.notification.NotificationHandler import eu.kanade.tachiyomi.data.notification.NotificationHandler
import eu.kanade.tachiyomi.data.notification.NotificationReceiver import eu.kanade.tachiyomi.data.notification.NotificationReceiver
@ -294,8 +294,8 @@ class LibraryUpdateNotifier(private val context: Context) {
val displayableChapterNumbers = chapters val displayableChapterNumbers = chapters
.filter { it.isRecognizedNumber } .filter { it.isRecognizedNumber }
.sortedBy { it.chapter_number } .sortedBy { it.chapterNumber }
.map { formatter.format(it.chapter_number) } .map { formatter.format(it.chapterNumber) }
.toSet() .toSet()
return when (displayableChapterNumbers.size) { return when (displayableChapterNumbers.size) {

View File

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

View File

@ -9,14 +9,14 @@ import android.os.Build
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import eu.kanade.domain.chapter.interactor.GetChapter import eu.kanade.domain.chapter.interactor.GetChapter
import eu.kanade.domain.chapter.interactor.UpdateChapter 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.toChapterUpdate
import eu.kanade.domain.chapter.model.toDbChapter import eu.kanade.domain.chapter.model.toDbChapter
import eu.kanade.domain.manga.interactor.GetManga import eu.kanade.domain.manga.interactor.GetManga
import eu.kanade.domain.manga.model.Manga
import eu.kanade.domain.manga.model.toDbManga import eu.kanade.domain.manga.model.toDbManga
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.backup.BackupRestoreService 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.DownloadManager
import eu.kanade.tachiyomi.data.download.DownloadService import eu.kanade.tachiyomi.data.download.DownloadService
import eu.kanade.tachiyomi.data.library.LibraryUpdateService import eu.kanade.tachiyomi.data.library.LibraryUpdateService

View File

@ -1,7 +1,7 @@
package eu.kanade.tachiyomi.ui.library package eu.kanade.tachiyomi.ui.library
import eu.davidea.flexibleadapter.FlexibleAdapter 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. * Adapter storing a list of manga in a certain category.

View File

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

View File

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

View File

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

View File

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