|
|
|
@ -11,6 +11,8 @@ import org.koitharu.kotatsu.parsers.model.SortOrder
|
|
|
|
@Dao
|
|
|
|
@Dao
|
|
|
|
abstract class FavouritesDao {
|
|
|
|
abstract class FavouritesDao {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** SELECT **/
|
|
|
|
|
|
|
|
|
|
|
|
@Transaction
|
|
|
|
@Transaction
|
|
|
|
@Query("SELECT * FROM favourites WHERE deleted_at = 0 GROUP BY manga_id ORDER BY created_at DESC")
|
|
|
|
@Query("SELECT * FROM favourites WHERE deleted_at = 0 GROUP BY manga_id ORDER BY created_at DESC")
|
|
|
|
abstract suspend fun findAll(): List<FavouriteManga>
|
|
|
|
abstract suspend fun findAll(): List<FavouriteManga>
|
|
|
|
@ -61,12 +63,12 @@ abstract class FavouritesDao {
|
|
|
|
)
|
|
|
|
)
|
|
|
|
abstract suspend fun findAllManga(categoryId: Int): List<MangaEntity>
|
|
|
|
abstract suspend fun findAllManga(categoryId: Int): List<MangaEntity>
|
|
|
|
|
|
|
|
|
|
|
|
suspend fun findCovers(categoryId: Long, order: SortOrder, limit: Int): List<String> {
|
|
|
|
suspend fun findCovers(categoryId: Long, order: SortOrder): List<String> {
|
|
|
|
val orderBy = getOrderBy(order)
|
|
|
|
val orderBy = getOrderBy(order)
|
|
|
|
@Language("RoomSql") val query = SimpleSQLiteQuery(
|
|
|
|
@Language("RoomSql") val query = SimpleSQLiteQuery(
|
|
|
|
"SELECT m.cover_url FROM favourites AS f LEFT JOIN manga AS m ON f.manga_id = m.manga_id " +
|
|
|
|
"SELECT m.cover_url FROM favourites AS f LEFT JOIN manga AS m ON f.manga_id = m.manga_id " +
|
|
|
|
"WHERE f.category_id = ? AND deleted_at = 0 ORDER BY $orderBy LIMIT ?",
|
|
|
|
"WHERE f.category_id = ? AND deleted_at = 0 ORDER BY $orderBy",
|
|
|
|
arrayOf<Any>(categoryId, limit),
|
|
|
|
arrayOf<Any>(categoryId),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return findCoversImpl(query)
|
|
|
|
return findCoversImpl(query)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -85,25 +87,33 @@ abstract class FavouritesDao {
|
|
|
|
@Query("SELECT DISTINCT category_id FROM favourites WHERE manga_id = :id AND deleted_at = 0")
|
|
|
|
@Query("SELECT DISTINCT category_id FROM favourites WHERE manga_id = :id AND deleted_at = 0")
|
|
|
|
abstract fun observeIds(id: Long): Flow<List<Long>>
|
|
|
|
abstract fun observeIds(id: Long): Flow<List<Long>>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** INSERT **/
|
|
|
|
|
|
|
|
|
|
|
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
|
|
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
|
|
|
abstract suspend fun insert(favourite: FavouriteEntity)
|
|
|
|
abstract suspend fun insert(favourite: FavouriteEntity)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** UPDATE **/
|
|
|
|
|
|
|
|
|
|
|
|
@Update
|
|
|
|
@Update
|
|
|
|
abstract suspend fun update(favourite: FavouriteEntity): Int
|
|
|
|
abstract suspend fun update(favourite: FavouriteEntity): Int
|
|
|
|
|
|
|
|
|
|
|
|
@Query("UPDATE favourites SET deleted_at = :now WHERE manga_id = :mangaId")
|
|
|
|
/** DELETE **/
|
|
|
|
abstract suspend fun delete(mangaId: Long, now: Long = System.currentTimeMillis())
|
|
|
|
|
|
|
|
|
|
|
|
suspend fun delete(mangaId: Long) = setDeletedAt(mangaId, System.currentTimeMillis())
|
|
|
|
|
|
|
|
|
|
|
|
@Query("UPDATE favourites SET deleted_at = :now WHERE manga_id = :mangaId AND category_id = :categoryId")
|
|
|
|
suspend fun delete(mangaId: Long, categoryId: Long) = setDeletedAt(mangaId, categoryId, System.currentTimeMillis())
|
|
|
|
abstract suspend fun delete(categoryId: Long, mangaId: Long, now: Long = System.currentTimeMillis())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
suspend fun recover(mangaId: Long) = delete(mangaId, 0L)
|
|
|
|
suspend fun deleteAll(categoryId: Long) = setDeletedAtAll(categoryId, System.currentTimeMillis())
|
|
|
|
|
|
|
|
|
|
|
|
suspend fun recover(categoryId: Long, mangaId: Long) = delete(categoryId, mangaId, 0L)
|
|
|
|
suspend fun recover(mangaId: Long) = setDeletedAt(mangaId, 0L)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
suspend fun recover(mangaId: Long, categoryId: Long) = setDeletedAt(categoryId, mangaId, 0L)
|
|
|
|
|
|
|
|
|
|
|
|
@Query("DELETE FROM favourites WHERE deleted_at != 0 AND deleted_at < :maxDeletionTime")
|
|
|
|
@Query("DELETE FROM favourites WHERE deleted_at != 0 AND deleted_at < :maxDeletionTime")
|
|
|
|
abstract suspend fun gc(maxDeletionTime: Long)
|
|
|
|
abstract suspend fun gc(maxDeletionTime: Long)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** TOOLS **/
|
|
|
|
|
|
|
|
|
|
|
|
@Transaction
|
|
|
|
@Transaction
|
|
|
|
open suspend fun upsert(entity: FavouriteEntity) {
|
|
|
|
open suspend fun upsert(entity: FavouriteEntity) {
|
|
|
|
if (update(entity) == 0) {
|
|
|
|
if (update(entity) == 0) {
|
|
|
|
@ -118,6 +128,15 @@ abstract class FavouritesDao {
|
|
|
|
@RawQuery
|
|
|
|
@RawQuery
|
|
|
|
protected abstract suspend fun findCoversImpl(query: SupportSQLiteQuery): List<String>
|
|
|
|
protected abstract suspend fun findCoversImpl(query: SupportSQLiteQuery): List<String>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Query("UPDATE favourites SET deleted_at = :deletedAt WHERE manga_id = :mangaId")
|
|
|
|
|
|
|
|
protected abstract suspend fun setDeletedAt(mangaId: Long, deletedAt: Long)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Query("UPDATE favourites SET deleted_at = :deletedAt WHERE manga_id = :mangaId AND category_id = :categoryId")
|
|
|
|
|
|
|
|
abstract suspend fun setDeletedAt(mangaId: Long, categoryId: Long, deletedAt: Long)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Query("UPDATE favourites SET deleted_at = :deletedAt WHERE category_id = :categoryId AND deleted_at = 0")
|
|
|
|
|
|
|
|
protected abstract suspend fun setDeletedAtAll(categoryId: Long, deletedAt: Long)
|
|
|
|
|
|
|
|
|
|
|
|
private fun getOrderBy(sortOrder: SortOrder) = when (sortOrder) {
|
|
|
|
private fun getOrderBy(sortOrder: SortOrder) = when (sortOrder) {
|
|
|
|
SortOrder.RATING -> "rating DESC"
|
|
|
|
SortOrder.RATING -> "rating DESC"
|
|
|
|
SortOrder.NEWEST,
|
|
|
|
SortOrder.NEWEST,
|
|
|
|
|