From 60ac27e40197b21ccff6ecaf05482ff85b9a2c90 Mon Sep 17 00:00:00 2001 From: inorichi Date: Sat, 21 Oct 2017 20:13:41 +0200 Subject: [PATCH] Add library manga class --- .../data/database/mappers/MangaTypeMapping.kt | 12 +++++++++--- .../data/database/models/LibraryManga.kt | 11 +++++++++++ .../tachiyomi/data/database/models/Manga.kt | 6 ------ .../data/database/models/MangaImpl.kt | 8 +------- .../data/database/queries/MangaQueries.kt | 3 ++- .../resolvers/LibraryMangaGetResolver.kt | 19 +++++++++---------- .../data/library/LibraryUpdateService.kt | 13 +++++++------ .../tachiyomi/ui/library/LibraryGridHolder.kt | 3 ++- .../tachiyomi/ui/library/LibraryHolder.kt | 4 ++-- .../tachiyomi/ui/library/LibraryItem.kt | 4 ++-- .../tachiyomi/ui/library/LibraryListHolder.kt | 4 ++-- .../tachiyomi/ui/library/LibraryPresenter.kt | 15 ++++++++------- 12 files changed, 55 insertions(+), 47 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/database/models/LibraryManga.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/mappers/MangaTypeMapping.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/mappers/MangaTypeMapping.kt index 71ca014203..9ad72908fe 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/mappers/MangaTypeMapping.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/mappers/MangaTypeMapping.kt @@ -65,9 +65,8 @@ class MangaPutResolver : DefaultPutResolver() { } } -open class MangaGetResolver : DefaultGetResolver() { - - override fun mapFromCursor(cursor: Cursor): Manga = MangaImpl().apply { +interface BaseMangaGetResolver { + fun mapBaseFromCursor(manga: Manga, cursor: Cursor) = manga.apply { id = cursor.getLong(cursor.getColumnIndex(COL_ID)) source = cursor.getLong(cursor.getColumnIndex(COL_SOURCE)) url = cursor.getString(cursor.getColumnIndex(COL_URL)) @@ -86,6 +85,13 @@ open class MangaGetResolver : DefaultGetResolver() { } } +open class MangaGetResolver : DefaultGetResolver(), BaseMangaGetResolver { + + override fun mapFromCursor(cursor: Cursor): Manga { + return mapBaseFromCursor(MangaImpl(), cursor) + } +} + class MangaDeleteResolver : DefaultDeleteResolver() { override fun mapToDeleteQuery(obj: Manga) = DeleteQuery.builder() diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/models/LibraryManga.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/models/LibraryManga.kt new file mode 100644 index 0000000000..bff35d14e4 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/models/LibraryManga.kt @@ -0,0 +1,11 @@ +package eu.kanade.tachiyomi.data.database.models + +class LibraryManga : MangaImpl() { + + var unread: Int = 0 + + var category: Int = 0 + + var downloadTotal: Int = 0 + +} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Manga.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Manga.kt index 246625abae..1782662dce 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Manga.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Manga.kt @@ -16,12 +16,6 @@ interface Manga : SManga { var chapter_flags: Int - var unread: Int - - var downloadTotal: Int - - var category: Int - fun setChapterOrder(order: Int) { setFlags(order, SORT_MASK) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/models/MangaImpl.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/models/MangaImpl.kt index 64777b0471..977864c249 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/models/MangaImpl.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/models/MangaImpl.kt @@ -1,6 +1,6 @@ package eu.kanade.tachiyomi.data.database.models -class MangaImpl : Manga { +open class MangaImpl : Manga { override var id: Long? = null @@ -32,12 +32,6 @@ class MangaImpl : Manga { override var chapter_flags: Int = 0 - @Transient override var unread: Int = 0 - - @Transient override var downloadTotal: Int = 0 - - @Transient override var category: Int = 0 - override fun equals(other: Any?): Boolean { if (this === other) return true if (other == null || javaClass != other.javaClass) return false diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/MangaQueries.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/MangaQueries.kt index ed5ce5e8ba..17348c5b1b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/MangaQueries.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/MangaQueries.kt @@ -4,6 +4,7 @@ import com.pushtorefresh.storio.sqlite.queries.DeleteQuery import com.pushtorefresh.storio.sqlite.queries.Query import com.pushtorefresh.storio.sqlite.queries.RawQuery import eu.kanade.tachiyomi.data.database.DbProvider +import eu.kanade.tachiyomi.data.database.models.LibraryManga import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.resolvers.LibraryMangaGetResolver import eu.kanade.tachiyomi.data.database.resolvers.MangaFlagsPutResolver @@ -23,7 +24,7 @@ interface MangaQueries : DbProvider { .prepare() fun getLibraryMangas() = db.get() - .listOfObjects(Manga::class.java) + .listOfObjects(LibraryManga::class.java) .withQuery(RawQuery.builder() .query(libraryQuery) .observesTables(MangaTable.TABLE, ChapterTable.TABLE, MangaCategoryTable.TABLE, CategoryTable.TABLE) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/LibraryMangaGetResolver.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/LibraryMangaGetResolver.kt index 4be02fecef..77369827a8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/LibraryMangaGetResolver.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/LibraryMangaGetResolver.kt @@ -1,24 +1,23 @@ package eu.kanade.tachiyomi.data.database.resolvers import android.database.Cursor -import eu.kanade.tachiyomi.data.database.mappers.MangaGetResolver -import eu.kanade.tachiyomi.data.database.models.Manga +import com.pushtorefresh.storio.sqlite.operations.get.DefaultGetResolver +import eu.kanade.tachiyomi.data.database.mappers.BaseMangaGetResolver +import eu.kanade.tachiyomi.data.database.models.LibraryManga import eu.kanade.tachiyomi.data.database.tables.MangaTable -class LibraryMangaGetResolver : MangaGetResolver() { +class LibraryMangaGetResolver : DefaultGetResolver(), BaseMangaGetResolver { companion object { val INSTANCE = LibraryMangaGetResolver() } - override fun mapFromCursor(cursor: Cursor): Manga { - val manga = super.mapFromCursor(cursor) + override fun mapFromCursor(cursor: Cursor): LibraryManga { + val manga = LibraryManga() - val unreadColumn = cursor.getColumnIndex(MangaTable.COL_UNREAD) - manga.unread = cursor.getInt(unreadColumn) - - val categoryColumn = cursor.getColumnIndex(MangaTable.COL_CATEGORY) - manga.category = cursor.getInt(categoryColumn) + mapBaseFromCursor(manga, cursor) + manga.unread = cursor.getInt(cursor.getColumnIndex(MangaTable.COL_UNREAD)) + manga.category = cursor.getInt(cursor.getColumnIndex(MangaTable.COL_CATEGORY)) return manga } 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 01b2b5b600..50a19ef4d5 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 @@ -14,6 +14,7 @@ import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.models.Category 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.download.DownloadManager import eu.kanade.tachiyomi.data.download.DownloadService @@ -231,7 +232,7 @@ class LibraryUpdateService( * @param target the target to update. * @return a list of manga to update */ - fun getMangaToUpdate(intent: Intent, target: Target): List { + fun getMangaToUpdate(intent: Intent, target: Target): List { val categoryId = intent.getIntExtra(KEY_CATEGORY, -1) var listToUpdate = if (categoryId != -1) @@ -262,7 +263,7 @@ class LibraryUpdateService( * @param mangaToUpdate the list to update * @return an observable delivering the progress of each update. */ - fun updateChapterList(mangaToUpdate: List): Observable { + fun updateChapterList(mangaToUpdate: List): Observable { // Initialize the variables holding the progress of the updates. val count = AtomicInteger(0) // List containing new updates @@ -286,7 +287,7 @@ class LibraryUpdateService( // If there's any error, return empty update and continue. .onErrorReturn { failedUpdates.add(manga) - Pair(emptyList(), emptyList()) + Pair(emptyList(), emptyList()) } // Filter out mangas without new chapters (or failed). .filter { pair -> pair.first.isNotEmpty() } @@ -354,7 +355,7 @@ class LibraryUpdateService( * @param mangaToUpdate the list to update * @return an observable delivering the progress of each update. */ - fun updateDetails(mangaToUpdate: List): Observable { + fun updateDetails(mangaToUpdate: List): Observable { // Initialize the variables holding the progress of the updates. val count = AtomicInteger(0) @@ -365,7 +366,7 @@ class LibraryUpdateService( // Update the details of the manga. .concatMap { manga -> val source = sourceManager.get(manga.source) as? HttpSource - ?: return@concatMap Observable.empty() + ?: return@concatMap Observable.empty() source.fetchMangaDetails(manga) .map { networkManga -> @@ -384,7 +385,7 @@ class LibraryUpdateService( * Method that updates the metadata of the connected tracking services. It's called in a * background thread, so it's safe to do heavy operations or network calls here. */ - private fun updateTrackings(mangaToUpdate: List): Observable { + private fun updateTrackings(mangaToUpdate: List): Observable { // Initialize the variables holding the progress of the updates. var count = 0 diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryGridHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryGridHolder.kt index 9b614ea37e..f2dc16f851 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryGridHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryGridHolder.kt @@ -3,6 +3,7 @@ package eu.kanade.tachiyomi.ui.library import android.view.View import com.bumptech.glide.load.engine.DiskCacheStrategy import eu.davidea.flexibleadapter.FlexibleAdapter +import eu.kanade.tachiyomi.data.database.models.LibraryManga import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.glide.GlideApp import eu.kanade.tachiyomi.data.preference.PreferencesHelper @@ -34,7 +35,7 @@ class LibraryGridHolder( * * @param manga the manga to bind. */ - override fun onSetValues(manga: Manga) { + override fun onSetValues(manga: LibraryManga) { // Update the title of the manga. view.title.text = manga.title diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHolder.kt index 2359377da0..9e9275159c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHolder.kt @@ -3,7 +3,7 @@ package eu.kanade.tachiyomi.ui.library import android.view.View import eu.davidea.flexibleadapter.FlexibleAdapter import eu.davidea.viewholders.FlexibleViewHolder -import eu.kanade.tachiyomi.data.database.models.Manga +import eu.kanade.tachiyomi.data.database.models.LibraryManga /** * Generic class used to hold the displayed data of a manga in the library. @@ -23,6 +23,6 @@ abstract class LibraryHolder( * * @param manga the manga to bind. */ - abstract fun onSetValues(manga: Manga) + abstract fun onSetValues(manga: LibraryManga) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryItem.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryItem.kt index 2e78c001d8..25e34f0a2c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryItem.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryItem.kt @@ -9,12 +9,12 @@ import eu.davidea.flexibleadapter.FlexibleAdapter import eu.davidea.flexibleadapter.items.AbstractFlexibleItem import eu.davidea.flexibleadapter.items.IFilterable import eu.kanade.tachiyomi.R -import eu.kanade.tachiyomi.data.database.models.Manga +import eu.kanade.tachiyomi.data.database.models.LibraryManga import eu.kanade.tachiyomi.util.inflate import eu.kanade.tachiyomi.widget.AutofitRecyclerView import kotlinx.android.synthetic.main.catalogue_grid_item.view.* -class LibraryItem(val manga: Manga) : AbstractFlexibleItem(), IFilterable { +class LibraryItem(val manga: LibraryManga) : AbstractFlexibleItem(), IFilterable { override fun getLayoutRes(): Int { return R.layout.catalogue_grid_item diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryListHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryListHolder.kt index a3b60e9322..b36d02f8ba 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryListHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryListHolder.kt @@ -3,7 +3,7 @@ package eu.kanade.tachiyomi.ui.library import android.view.View import com.bumptech.glide.load.engine.DiskCacheStrategy import eu.davidea.flexibleadapter.FlexibleAdapter -import eu.kanade.tachiyomi.data.database.models.Manga +import eu.kanade.tachiyomi.data.database.models.LibraryManga import eu.kanade.tachiyomi.data.glide.GlideApp import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.getOrDefault @@ -34,7 +34,7 @@ class LibraryListHolder( * * @param manga the manga to bind. */ - override fun onSetValues(manga: Manga) { + override fun onSetValues(manga: LibraryManga) { // Update the title of the manga. itemView.title.text = manga.title 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 cb0a6d3231..1a9fcb4187 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 @@ -7,6 +7,7 @@ import com.jakewharton.rxrelay.BehaviorRelay import eu.kanade.tachiyomi.data.cache.CoverCache import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.models.Category +import eu.kanade.tachiyomi.data.database.models.LibraryManga import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.MangaCategory import eu.kanade.tachiyomi.data.download.DownloadManager @@ -98,7 +99,7 @@ class LibraryPresenter( * * @param map the map to filter. */ - private fun applyFilters(map: Map>): Map> { + private fun applyFilters(map: Map>): Map> { // Cached list of downloaded manga directories given a source id. val mangaDirsForSource = mutableMapOf>() @@ -111,7 +112,7 @@ class LibraryPresenter( val filterCompleted = preferences.filterCompleted().getOrDefault() - val filterFn: (Manga) -> Boolean = f@ { manga -> + val filterFn: (LibraryManga) -> Boolean = f@ { manga -> // Filter out manga without source. val source = sourceManager.get(manga.source) ?: return@f false @@ -153,7 +154,7 @@ class LibraryPresenter( * * @param map the map to filter. */ - private fun addDownloadTotal(map: Map>): Map> { + private fun addDownloadTotal(map: Map>): Map> { // Cached list of downloaded manga directories given a source id. if (preferences.downloadBadge().getOrDefault()) { val mangaDirsForSource = mutableMapOf>() @@ -195,7 +196,7 @@ class LibraryPresenter( * * @param map the map to sort. */ - private fun applySort(map: Map>): Map> { + private fun applySort(map: Map>): Map> { val sortingMode = preferences.librarySortingMode().getOrDefault() val lastReadManga by lazy { @@ -207,7 +208,7 @@ class LibraryPresenter( db.getTotalChapterManga().executeAsBlocking().associate { it.id!! to counter++ } } - val sortFn: (Manga, Manga) -> Int = { manga1, manga2 -> + val sortFn: (LibraryManga, LibraryManga) -> Int = { manga1, manga2 -> when (sortingMode) { LibrarySort.ALPHA -> manga1.title.compareTo(manga2.title) LibrarySort.LAST_READ -> { @@ -245,7 +246,7 @@ class LibraryPresenter( * * @return an observable of the categories and its manga. */ - private fun getLibraryObservable(): Observable, Map>>> { + private fun getLibraryObservable(): Observable, Map>>> { return Observable.combineLatest(getCategoriesObservable(), getLibraryMangasObservable(), { dbCategories, libraryManga -> val categories = if (libraryManga.containsKey(0)) @@ -273,7 +274,7 @@ class LibraryPresenter( * @return an observable containing a map with the category id as key and a list of manga as the * value. */ - private fun getLibraryMangasObservable(): Observable>> { + private fun getLibraryMangasObservable(): Observable>> { return db.getLibraryMangas().asRxObservable() .map { list -> list.groupBy { it.category } } }