Update database scheme: tracks table
This commit is contained in:
@@ -16,6 +16,7 @@ import org.koin.dsl.module
|
|||||||
import org.koitharu.kotatsu.core.db.MangaDatabase
|
import org.koitharu.kotatsu.core.db.MangaDatabase
|
||||||
import org.koitharu.kotatsu.core.db.migrations.Migration1To2
|
import org.koitharu.kotatsu.core.db.migrations.Migration1To2
|
||||||
import org.koitharu.kotatsu.core.db.migrations.Migration2To3
|
import org.koitharu.kotatsu.core.db.migrations.Migration2To3
|
||||||
|
import org.koitharu.kotatsu.core.db.migrations.Migration3To4
|
||||||
import org.koitharu.kotatsu.core.local.CbzFetcher
|
import org.koitharu.kotatsu.core.local.CbzFetcher
|
||||||
import org.koitharu.kotatsu.core.local.PagesCache
|
import org.koitharu.kotatsu.core.local.PagesCache
|
||||||
import org.koitharu.kotatsu.core.local.cookies.PersistentCookieJar
|
import org.koitharu.kotatsu.core.local.cookies.PersistentCookieJar
|
||||||
@@ -111,5 +112,5 @@ class KotatsuApp : Application() {
|
|||||||
applicationContext,
|
applicationContext,
|
||||||
MangaDatabase::class.java,
|
MangaDatabase::class.java,
|
||||||
"kotatsu-db"
|
"kotatsu-db"
|
||||||
).addMigrations(Migration1To2, Migration2To3)
|
).addMigrations(Migration1To2, Migration2To3, Migration3To4)
|
||||||
}
|
}
|
||||||
@@ -7,20 +7,22 @@ import org.koitharu.kotatsu.core.db.entity.*
|
|||||||
@Database(
|
@Database(
|
||||||
entities = [
|
entities = [
|
||||||
MangaEntity::class, TagEntity::class, HistoryEntity::class, MangaTagsEntity::class,
|
MangaEntity::class, TagEntity::class, HistoryEntity::class, MangaTagsEntity::class,
|
||||||
FavouriteCategoryEntity::class, FavouriteEntity::class, MangaPrefsEntity::class
|
FavouriteCategoryEntity::class, FavouriteEntity::class, MangaPrefsEntity::class, TrackEntity::class
|
||||||
], version = 3
|
], version = 3
|
||||||
)
|
)
|
||||||
abstract class MangaDatabase : RoomDatabase() {
|
abstract class MangaDatabase : RoomDatabase() {
|
||||||
|
|
||||||
abstract fun historyDao(): HistoryDao
|
abstract val historyDao: HistoryDao
|
||||||
|
|
||||||
abstract fun tagsDao(): TagsDao
|
abstract val tagsDao: TagsDao
|
||||||
|
|
||||||
abstract fun mangaDao(): MangaDao
|
abstract val mangaDao: MangaDao
|
||||||
|
|
||||||
abstract fun favouritesDao(): FavouritesDao
|
abstract val favouritesDao: FavouritesDao
|
||||||
|
|
||||||
abstract fun preferencesDao(): PreferencesDao
|
abstract val preferencesDao: PreferencesDao
|
||||||
|
|
||||||
abstract fun favouriteCategoriesDao(): FavouriteCategoriesDao
|
abstract val favouriteCategoriesDao: FavouriteCategoriesDao
|
||||||
|
|
||||||
|
abstract val tracksDao: TracksDao
|
||||||
}
|
}
|
||||||
32
app/src/main/java/org/koitharu/kotatsu/core/db/TracksDao.kt
Normal file
32
app/src/main/java/org/koitharu/kotatsu/core/db/TracksDao.kt
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package org.koitharu.kotatsu.core.db
|
||||||
|
|
||||||
|
import androidx.room.*
|
||||||
|
import org.koitharu.kotatsu.core.db.entity.TrackEntity
|
||||||
|
|
||||||
|
|
||||||
|
@Dao
|
||||||
|
abstract class TracksDao {
|
||||||
|
|
||||||
|
@Query("SELECT * FROM tracks")
|
||||||
|
abstract suspend fun findAll(): List<TrackEntity>
|
||||||
|
|
||||||
|
@Query("DELETE FROM tracks")
|
||||||
|
abstract suspend fun clear()
|
||||||
|
|
||||||
|
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||||
|
abstract suspend fun insert(entity: TrackEntity): Long
|
||||||
|
|
||||||
|
@Update
|
||||||
|
abstract suspend fun update(entity: TrackEntity): Int
|
||||||
|
|
||||||
|
@Query("DELETE FROM tracks WHERE manga_id = :mangaId")
|
||||||
|
abstract suspend fun delete(mangaId: Long)
|
||||||
|
|
||||||
|
@Transaction
|
||||||
|
open suspend fun upsert(entity: TrackEntity) {
|
||||||
|
if (update(entity) == 0) {
|
||||||
|
insert(entity)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package org.koitharu.kotatsu.core.db.entity
|
||||||
|
|
||||||
|
import androidx.room.ColumnInfo
|
||||||
|
import androidx.room.Entity
|
||||||
|
import androidx.room.ForeignKey
|
||||||
|
import androidx.room.PrimaryKey
|
||||||
|
|
||||||
|
@Entity(
|
||||||
|
tableName = "tracks", foreignKeys = [
|
||||||
|
ForeignKey(
|
||||||
|
entity = MangaEntity::class,
|
||||||
|
parentColumns = ["manga_id"],
|
||||||
|
childColumns = ["manga_id"],
|
||||||
|
onDelete = ForeignKey.CASCADE
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
data class TrackEntity (
|
||||||
|
@PrimaryKey(autoGenerate = false)
|
||||||
|
@ColumnInfo(name = "manga_id") val mangaId: Long,
|
||||||
|
@ColumnInfo(name = "chapters_total") val totalChapters: Int,
|
||||||
|
@ColumnInfo(name = "last_chapter_id") val lastChapterId: Long,
|
||||||
|
@ColumnInfo(name = "chapters_new") val newChapters: Int,
|
||||||
|
@ColumnInfo(name = "last_check") val lastCheck: Long
|
||||||
|
)
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package org.koitharu.kotatsu.core.db.migrations
|
||||||
|
|
||||||
|
import androidx.room.migration.Migration
|
||||||
|
import androidx.sqlite.db.SupportSQLiteDatabase
|
||||||
|
|
||||||
|
object Migration3To4 : Migration(3, 4) {
|
||||||
|
|
||||||
|
override fun migrate(database: SupportSQLiteDatabase) {
|
||||||
|
database.execSQL("CREATE TABLE IF NOT EXISTS tracks (manga_id INTEGER NOT NULL, chapters_total INTEGER NOT NULL, last_chapter_id INTEGER NOT NULL, chapters_new INTEGER NOT NULL, last_check INTEGER NOT NULL, PRIMARY KEY(manga_id), FOREIGN KEY(manga_id) REFERENCES manga(manga_id) ON UPDATE NO ACTION ON DELETE CASCADE )")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,10 +10,4 @@ data class AppVersion(
|
|||||||
val url: String,
|
val url: String,
|
||||||
val apkSize: Long,
|
val apkSize: Long,
|
||||||
val apkUrl: String
|
val apkUrl: String
|
||||||
) : Parcelable {
|
) : Parcelable
|
||||||
|
|
||||||
fun isGreaterThen(version: String) {
|
|
||||||
val thisParts = name.substringBeforeLast('-').split('.')
|
|
||||||
val parts = version.substringBeforeLast('-').split('.')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -14,7 +14,7 @@ class MangaDataRepository : KoinComponent {
|
|||||||
private val db: MangaDatabase by inject()
|
private val db: MangaDatabase by inject()
|
||||||
|
|
||||||
suspend fun savePreferences(mangaId: Long, mode: ReaderMode) {
|
suspend fun savePreferences(mangaId: Long, mode: ReaderMode) {
|
||||||
db.preferencesDao().upsert(
|
db.preferencesDao.upsert(
|
||||||
MangaPrefsEntity(
|
MangaPrefsEntity(
|
||||||
mangaId = mangaId,
|
mangaId = mangaId,
|
||||||
mode = mode.id
|
mode = mode.id
|
||||||
@@ -23,14 +23,14 @@ class MangaDataRepository : KoinComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
suspend fun getReaderMode(mangaId: Long): ReaderMode? {
|
suspend fun getReaderMode(mangaId: Long): ReaderMode? {
|
||||||
return db.preferencesDao().find(mangaId)?.let { ReaderMode.valueOf(it.mode) }
|
return db.preferencesDao.find(mangaId)?.let { ReaderMode.valueOf(it.mode) }
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun findMangaById(mangaId: Long): Manga? {
|
suspend fun findMangaById(mangaId: Long): Manga? {
|
||||||
return db.mangaDao().find(mangaId)?.toManga()
|
return db.mangaDao.find(mangaId)?.toManga()
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun storeManga(manga: Manga) {
|
suspend fun storeManga(manga: Manga) {
|
||||||
db.mangaDao().upsert(MangaEntity.from(manga), manga.tags.map(TagEntity.Companion::fromMangaTag))
|
db.mangaDao.upsert(MangaEntity.from(manga), manga.tags.map(TagEntity.Companion::fromMangaTag))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -18,17 +18,17 @@ class FavouritesRepository : KoinComponent {
|
|||||||
private val db: MangaDatabase by inject()
|
private val db: MangaDatabase by inject()
|
||||||
|
|
||||||
suspend fun getAllManga(offset: Int): List<Manga> {
|
suspend fun getAllManga(offset: Int): List<Manga> {
|
||||||
val entities = db.favouritesDao().findAll(offset, 20, "created_at")
|
val entities = db.favouritesDao.findAll(offset, 20, "created_at")
|
||||||
return entities.map { it.manga.toManga(it.tags.map(TagEntity::toMangaTag).toSet()) }
|
return entities.map { it.manga.toManga(it.tags.map(TagEntity::toMangaTag).toSet()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun getAllCategories(): List<FavouriteCategory> {
|
suspend fun getAllCategories(): List<FavouriteCategory> {
|
||||||
val entities = db.favouriteCategoriesDao().findAll("created_at")
|
val entities = db.favouriteCategoriesDao.findAll("created_at")
|
||||||
return entities.map { it.toFavouriteCategory() }
|
return entities.map { it.toFavouriteCategory() }
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun getCategories(mangaId: Long): List<FavouriteCategory> {
|
suspend fun getCategories(mangaId: Long): List<FavouriteCategory> {
|
||||||
val entities = db.favouritesDao().find(mangaId)?.categories
|
val entities = db.favouritesDao.find(mangaId)?.categories
|
||||||
return entities?.map { it.toFavouriteCategory() }.orEmpty()
|
return entities?.map { it.toFavouriteCategory() }.orEmpty()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,25 +38,25 @@ class FavouritesRepository : KoinComponent {
|
|||||||
createdAt = System.currentTimeMillis(),
|
createdAt = System.currentTimeMillis(),
|
||||||
categoryId = 0
|
categoryId = 0
|
||||||
)
|
)
|
||||||
val id = db.favouriteCategoriesDao().insert(entity)
|
val id = db.favouriteCategoriesDao.insert(entity)
|
||||||
return entity.toFavouriteCategory(id)
|
return entity.toFavouriteCategory(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun removeCategory(id: Long) {
|
suspend fun removeCategory(id: Long) {
|
||||||
db.favouriteCategoriesDao().delete(id)
|
db.favouriteCategoriesDao.delete(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun addToCategory(manga: Manga, categoryId: Long) {
|
suspend fun addToCategory(manga: Manga, categoryId: Long) {
|
||||||
val tags = manga.tags.map(TagEntity.Companion::fromMangaTag)
|
val tags = manga.tags.map(TagEntity.Companion::fromMangaTag)
|
||||||
db.tagsDao().upsert(tags)
|
db.tagsDao.upsert(tags)
|
||||||
db.mangaDao().upsert(MangaEntity.from(manga), tags)
|
db.mangaDao.upsert(MangaEntity.from(manga), tags)
|
||||||
val entity = FavouriteEntity(manga.id, categoryId, System.currentTimeMillis())
|
val entity = FavouriteEntity(manga.id, categoryId, System.currentTimeMillis())
|
||||||
db.favouritesDao().add(entity)
|
db.favouritesDao.add(entity)
|
||||||
notifyFavouritesChanged(manga.id)
|
notifyFavouritesChanged(manga.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun removeFromCategory(manga: Manga, categoryId: Long) {
|
suspend fun removeFromCategory(manga: Manga, categoryId: Long) {
|
||||||
db.favouritesDao().delete(categoryId, manga.id)
|
db.favouritesDao.delete(categoryId, manga.id)
|
||||||
notifyFavouritesChanged(manga.id)
|
notifyFavouritesChanged(manga.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,15 +17,15 @@ class HistoryRepository : KoinComponent {
|
|||||||
private val db: MangaDatabase by inject()
|
private val db: MangaDatabase by inject()
|
||||||
|
|
||||||
suspend fun getList(offset: Int, limit: Int = 20): List<Manga> {
|
suspend fun getList(offset: Int, limit: Int = 20): List<Manga> {
|
||||||
val entities = db.historyDao().findAll(offset, limit)
|
val entities = db.historyDao.findAll(offset, limit)
|
||||||
return entities.map { it.manga.toManga(it.tags.map(TagEntity::toMangaTag).toSet()) }
|
return entities.map { it.manga.toManga(it.tags.map(TagEntity::toMangaTag).toSet()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun addOrUpdate(manga: Manga, chapterId: Long, page: Int, scroll: Float) {
|
suspend fun addOrUpdate(manga: Manga, chapterId: Long, page: Int, scroll: Float) {
|
||||||
val tags = manga.tags.map(TagEntity.Companion::fromMangaTag)
|
val tags = manga.tags.map(TagEntity.Companion::fromMangaTag)
|
||||||
db.tagsDao().upsert(tags)
|
db.tagsDao.upsert(tags)
|
||||||
db.mangaDao().upsert(MangaEntity.from(manga), tags)
|
db.mangaDao.upsert(MangaEntity.from(manga), tags)
|
||||||
db.historyDao().upsert(
|
db.historyDao.upsert(
|
||||||
HistoryEntity(
|
HistoryEntity(
|
||||||
mangaId = manga.id,
|
mangaId = manga.id,
|
||||||
createdAt = System.currentTimeMillis(),
|
createdAt = System.currentTimeMillis(),
|
||||||
@@ -39,7 +39,7 @@ class HistoryRepository : KoinComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
suspend fun getOne(manga: Manga): MangaHistory? {
|
suspend fun getOne(manga: Manga): MangaHistory? {
|
||||||
return db.historyDao().find(manga.id)?.let {
|
return db.historyDao.find(manga.id)?.let {
|
||||||
MangaHistory(
|
MangaHistory(
|
||||||
createdAt = Date(it.createdAt),
|
createdAt = Date(it.createdAt),
|
||||||
updatedAt = Date(it.updatedAt),
|
updatedAt = Date(it.updatedAt),
|
||||||
@@ -51,12 +51,12 @@ class HistoryRepository : KoinComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
suspend fun clear() {
|
suspend fun clear() {
|
||||||
db.historyDao().clear()
|
db.historyDao.clear()
|
||||||
notifyHistoryChanged()
|
notifyHistoryChanged()
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun delete(manga: Manga) {
|
suspend fun delete(manga: Manga) {
|
||||||
db.historyDao().delete(manga.id)
|
db.historyDao.delete(manga.id)
|
||||||
notifyHistoryChanged()
|
notifyHistoryChanged()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,8 +65,8 @@ class HistoryRepository : KoinComponent {
|
|||||||
* Useful for replacing saved manga on deleting it with remove source
|
* Useful for replacing saved manga on deleting it with remove source
|
||||||
*/
|
*/
|
||||||
suspend fun deleteOrSwap(manga: Manga, alternative: Manga?) {
|
suspend fun deleteOrSwap(manga: Manga, alternative: Manga?) {
|
||||||
if (alternative == null || db.mangaDao().update(MangaEntity.from(alternative)) <= 0) {
|
if (alternative == null || db.mangaDao.update(MangaEntity.from(alternative)) <= 0) {
|
||||||
db.historyDao().delete(manga.id)
|
db.historyDao.delete(manga.id)
|
||||||
notifyHistoryChanged()
|
notifyHistoryChanged()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user