diff --git a/app/src/main/java/org/koitharu/kotatsu/KotatsuApp.kt b/app/src/main/java/org/koitharu/kotatsu/KotatsuApp.kt index ef5a70858..634f4341c 100644 --- a/app/src/main/java/org/koitharu/kotatsu/KotatsuApp.kt +++ b/app/src/main/java/org/koitharu/kotatsu/KotatsuApp.kt @@ -16,6 +16,7 @@ import org.koin.dsl.module import org.koitharu.kotatsu.core.db.MangaDatabase import org.koitharu.kotatsu.core.db.migrations.Migration1To2 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.PagesCache import org.koitharu.kotatsu.core.local.cookies.PersistentCookieJar @@ -111,5 +112,5 @@ class KotatsuApp : Application() { applicationContext, MangaDatabase::class.java, "kotatsu-db" - ).addMigrations(Migration1To2, Migration2To3) + ).addMigrations(Migration1To2, Migration2To3, Migration3To4) } \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/db/MangaDatabase.kt b/app/src/main/java/org/koitharu/kotatsu/core/db/MangaDatabase.kt index 215684b8f..041f74bc5 100644 --- a/app/src/main/java/org/koitharu/kotatsu/core/db/MangaDatabase.kt +++ b/app/src/main/java/org/koitharu/kotatsu/core/db/MangaDatabase.kt @@ -7,20 +7,22 @@ import org.koitharu.kotatsu.core.db.entity.* @Database( entities = [ MangaEntity::class, TagEntity::class, HistoryEntity::class, MangaTagsEntity::class, - FavouriteCategoryEntity::class, FavouriteEntity::class, MangaPrefsEntity::class + FavouriteCategoryEntity::class, FavouriteEntity::class, MangaPrefsEntity::class, TrackEntity::class ], version = 3 ) 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 } \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/db/TracksDao.kt b/app/src/main/java/org/koitharu/kotatsu/core/db/TracksDao.kt new file mode 100644 index 000000000..5eb087dde --- /dev/null +++ b/app/src/main/java/org/koitharu/kotatsu/core/db/TracksDao.kt @@ -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 + + @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) + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/db/entity/TrackEntity.kt b/app/src/main/java/org/koitharu/kotatsu/core/db/entity/TrackEntity.kt new file mode 100644 index 000000000..d9ee04df9 --- /dev/null +++ b/app/src/main/java/org/koitharu/kotatsu/core/db/entity/TrackEntity.kt @@ -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 +) \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/db/migrations/Migration3To4.kt b/app/src/main/java/org/koitharu/kotatsu/core/db/migrations/Migration3To4.kt new file mode 100644 index 000000000..3e8dc8702 --- /dev/null +++ b/app/src/main/java/org/koitharu/kotatsu/core/db/migrations/Migration3To4.kt @@ -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 )") + } +} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/github/AppVersion.kt b/app/src/main/java/org/koitharu/kotatsu/core/github/AppVersion.kt index ee7d27533..52c61c7c1 100644 --- a/app/src/main/java/org/koitharu/kotatsu/core/github/AppVersion.kt +++ b/app/src/main/java/org/koitharu/kotatsu/core/github/AppVersion.kt @@ -10,10 +10,4 @@ data class AppVersion( val url: String, val apkSize: Long, val apkUrl: String -) : Parcelable { - - fun isGreaterThen(version: String) { - val thisParts = name.substringBeforeLast('-').split('.') - val parts = version.substringBeforeLast('-').split('.') - } -} \ No newline at end of file +) : Parcelable \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/domain/MangaDataRepository.kt b/app/src/main/java/org/koitharu/kotatsu/domain/MangaDataRepository.kt index 360aac965..861ed5710 100644 --- a/app/src/main/java/org/koitharu/kotatsu/domain/MangaDataRepository.kt +++ b/app/src/main/java/org/koitharu/kotatsu/domain/MangaDataRepository.kt @@ -14,7 +14,7 @@ class MangaDataRepository : KoinComponent { private val db: MangaDatabase by inject() suspend fun savePreferences(mangaId: Long, mode: ReaderMode) { - db.preferencesDao().upsert( + db.preferencesDao.upsert( MangaPrefsEntity( mangaId = mangaId, mode = mode.id @@ -23,14 +23,14 @@ class MangaDataRepository : KoinComponent { } 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? { - return db.mangaDao().find(mangaId)?.toManga() + return db.mangaDao.find(mangaId)?.toManga() } 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)) } } \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/domain/favourites/FavouritesRepository.kt b/app/src/main/java/org/koitharu/kotatsu/domain/favourites/FavouritesRepository.kt index b3e520875..a714cdeee 100644 --- a/app/src/main/java/org/koitharu/kotatsu/domain/favourites/FavouritesRepository.kt +++ b/app/src/main/java/org/koitharu/kotatsu/domain/favourites/FavouritesRepository.kt @@ -18,17 +18,17 @@ class FavouritesRepository : KoinComponent { private val db: MangaDatabase by inject() suspend fun getAllManga(offset: Int): List { - 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()) } } suspend fun getAllCategories(): List { - val entities = db.favouriteCategoriesDao().findAll("created_at") + val entities = db.favouriteCategoriesDao.findAll("created_at") return entities.map { it.toFavouriteCategory() } } suspend fun getCategories(mangaId: Long): List { - val entities = db.favouritesDao().find(mangaId)?.categories + val entities = db.favouritesDao.find(mangaId)?.categories return entities?.map { it.toFavouriteCategory() }.orEmpty() } @@ -38,25 +38,25 @@ class FavouritesRepository : KoinComponent { createdAt = System.currentTimeMillis(), categoryId = 0 ) - val id = db.favouriteCategoriesDao().insert(entity) + val id = db.favouriteCategoriesDao.insert(entity) return entity.toFavouriteCategory(id) } suspend fun removeCategory(id: Long) { - db.favouriteCategoriesDao().delete(id) + db.favouriteCategoriesDao.delete(id) } suspend fun addToCategory(manga: Manga, categoryId: Long) { val tags = manga.tags.map(TagEntity.Companion::fromMangaTag) - db.tagsDao().upsert(tags) - db.mangaDao().upsert(MangaEntity.from(manga), tags) + db.tagsDao.upsert(tags) + db.mangaDao.upsert(MangaEntity.from(manga), tags) val entity = FavouriteEntity(manga.id, categoryId, System.currentTimeMillis()) - db.favouritesDao().add(entity) + db.favouritesDao.add(entity) notifyFavouritesChanged(manga.id) } suspend fun removeFromCategory(manga: Manga, categoryId: Long) { - db.favouritesDao().delete(categoryId, manga.id) + db.favouritesDao.delete(categoryId, manga.id) notifyFavouritesChanged(manga.id) } diff --git a/app/src/main/java/org/koitharu/kotatsu/domain/history/HistoryRepository.kt b/app/src/main/java/org/koitharu/kotatsu/domain/history/HistoryRepository.kt index a7f638cf0..fc7df11ce 100644 --- a/app/src/main/java/org/koitharu/kotatsu/domain/history/HistoryRepository.kt +++ b/app/src/main/java/org/koitharu/kotatsu/domain/history/HistoryRepository.kt @@ -17,15 +17,15 @@ class HistoryRepository : KoinComponent { private val db: MangaDatabase by inject() suspend fun getList(offset: Int, limit: Int = 20): List { - 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()) } } suspend fun addOrUpdate(manga: Manga, chapterId: Long, page: Int, scroll: Float) { val tags = manga.tags.map(TagEntity.Companion::fromMangaTag) - db.tagsDao().upsert(tags) - db.mangaDao().upsert(MangaEntity.from(manga), tags) - db.historyDao().upsert( + db.tagsDao.upsert(tags) + db.mangaDao.upsert(MangaEntity.from(manga), tags) + db.historyDao.upsert( HistoryEntity( mangaId = manga.id, createdAt = System.currentTimeMillis(), @@ -39,7 +39,7 @@ class HistoryRepository : KoinComponent { } suspend fun getOne(manga: Manga): MangaHistory? { - return db.historyDao().find(manga.id)?.let { + return db.historyDao.find(manga.id)?.let { MangaHistory( createdAt = Date(it.createdAt), updatedAt = Date(it.updatedAt), @@ -51,12 +51,12 @@ class HistoryRepository : KoinComponent { } suspend fun clear() { - db.historyDao().clear() + db.historyDao.clear() notifyHistoryChanged() } suspend fun delete(manga: Manga) { - db.historyDao().delete(manga.id) + db.historyDao.delete(manga.id) notifyHistoryChanged() } @@ -65,8 +65,8 @@ class HistoryRepository : KoinComponent { * Useful for replacing saved manga on deleting it with remove source */ suspend fun deleteOrSwap(manga: Manga, alternative: Manga?) { - if (alternative == null || db.mangaDao().update(MangaEntity.from(alternative)) <= 0) { - db.historyDao().delete(manga.id) + if (alternative == null || db.mangaDao.update(MangaEntity.from(alternative)) <= 0) { + db.historyDao.delete(manga.id) notifyHistoryChanged() } }