Reader: Save reading progress with SQLDelight (#7185)
* Use SQLDelight in reader to update history * Move chapter progress to sqldelight * Review Changes Co-Authored-By: inorichi <len@kanade.eu> * Review Changes 2 Co-authored-by: FourTOne5 <59261191+FourTOne5@users.noreply.github.com> Co-authored-by: inorichi <len@kanade.eu>
This commit is contained in:
3
app/src/main/java/eu/kanade/data/DatabaseUtils.kt
Normal file
3
app/src/main/java/eu/kanade/data/DatabaseUtils.kt
Normal file
@@ -0,0 +1,3 @@
|
||||
package eu.kanade.data
|
||||
|
||||
fun Boolean.toLong() = if (this) 1L else 0L
|
||||
@@ -0,0 +1,36 @@
|
||||
package eu.kanade.data.chapter
|
||||
|
||||
import eu.kanade.data.DatabaseHandler
|
||||
import eu.kanade.data.toLong
|
||||
import eu.kanade.domain.chapter.model.ChapterUpdate
|
||||
import eu.kanade.domain.chapter.repository.ChapterRepository
|
||||
import eu.kanade.tachiyomi.util.system.logcat
|
||||
import logcat.LogPriority
|
||||
|
||||
class ChapterRepositoryImpl(
|
||||
private val databaseHandler: DatabaseHandler,
|
||||
) : ChapterRepository {
|
||||
|
||||
override suspend fun update(chapterUpdate: ChapterUpdate) {
|
||||
try {
|
||||
databaseHandler.await {
|
||||
chaptersQueries.update(
|
||||
chapterUpdate.mangaId,
|
||||
chapterUpdate.url,
|
||||
chapterUpdate.name,
|
||||
chapterUpdate.scanlator,
|
||||
chapterUpdate.read?.toLong(),
|
||||
chapterUpdate.bookmark?.toLong(),
|
||||
chapterUpdate.lastPageRead,
|
||||
chapterUpdate.chapterNumber?.toDouble(),
|
||||
chapterUpdate.sourceOrder,
|
||||
chapterUpdate.dateFetch,
|
||||
chapterUpdate.dateUpload,
|
||||
chapterId = chapterUpdate.id,
|
||||
)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
logcat(LogPriority.ERROR, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,16 +4,17 @@ import eu.kanade.domain.history.model.History
|
||||
import eu.kanade.domain.history.model.HistoryWithRelations
|
||||
import java.util.Date
|
||||
|
||||
val historyMapper: (Long, Long, Date?, Date?) -> History = { id, chapterId, readAt, _ ->
|
||||
val historyMapper: (Long, Long, Date?, Long) -> History = { id, chapterId, readAt, readDuration ->
|
||||
History(
|
||||
id = id,
|
||||
chapterId = chapterId,
|
||||
readAt = readAt,
|
||||
readDuration = readDuration,
|
||||
)
|
||||
}
|
||||
|
||||
val historyWithRelationsMapper: (Long, Long, Long, String, String?, Float, Date?) -> HistoryWithRelations = {
|
||||
historyId, mangaId, chapterId, title, thumbnailUrl, chapterNumber, readAt ->
|
||||
val historyWithRelationsMapper: (Long, Long, Long, String, String?, Float, Date?, Long) -> HistoryWithRelations = {
|
||||
historyId, mangaId, chapterId, title, thumbnailUrl, chapterNumber, readAt, readDuration ->
|
||||
HistoryWithRelations(
|
||||
id = historyId,
|
||||
chapterId = chapterId,
|
||||
@@ -22,5 +23,6 @@ val historyWithRelationsMapper: (Long, Long, Long, String, String?, Float, Date?
|
||||
thumbnailUrl = thumbnailUrl ?: "",
|
||||
chapterNumber = chapterNumber,
|
||||
readAt = readAt,
|
||||
readDuration = readDuration,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import eu.kanade.data.DatabaseHandler
|
||||
import eu.kanade.data.chapter.chapterMapper
|
||||
import eu.kanade.data.manga.mangaMapper
|
||||
import eu.kanade.domain.chapter.model.Chapter
|
||||
import eu.kanade.domain.history.model.HistoryUpdate
|
||||
import eu.kanade.domain.history.model.HistoryWithRelations
|
||||
import eu.kanade.domain.history.repository.HistoryRepository
|
||||
import eu.kanade.domain.manga.model.Manga
|
||||
@@ -89,4 +90,28 @@ class HistoryRepositoryImpl(
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun upsertHistory(historyUpdate: HistoryUpdate) {
|
||||
try {
|
||||
try {
|
||||
handler.await {
|
||||
historyQueries.insert(
|
||||
historyUpdate.chapterId,
|
||||
historyUpdate.readAt,
|
||||
historyUpdate.sessionReadDuration,
|
||||
)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
handler.await {
|
||||
historyQueries.update(
|
||||
historyUpdate.readAt,
|
||||
historyUpdate.sessionReadDuration,
|
||||
historyUpdate.chapterId,
|
||||
)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
logcat(LogPriority.ERROR, throwable = e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user