Fix wrong tracker notifications

This commit is contained in:
Koitharu
2022-05-31 16:22:30 +03:00
parent a74b623c10
commit ccb31de1ba
3 changed files with 27 additions and 28 deletions

View File

@@ -1,9 +1,9 @@
package org.koitharu.kotatsu.core.model
import android.os.Parcelable
import java.util.*
import kotlinx.parcelize.Parcelize
import org.koitharu.kotatsu.parsers.model.Manga
import java.util.*
data class MangaTracking(
val manga: Manga,

View File

@@ -77,10 +77,11 @@ class TrackingRepository(
suspend fun storeTrackResult(
mangaId: Long,
knownChaptersCount: Int,
lastChapterId: Long,
knownChaptersCount: Int, // how many chapters user already seen
lastChapterId: Long, // in upstream manga
newChapters: List<MangaChapter>,
previousTrackChapterId: Long
previousTrackChapterId: Long, // from previous check
saveTrackLog: Boolean,
) {
db.withTransaction {
val entity = TrackEntity(
@@ -92,14 +93,16 @@ class TrackingRepository(
lastNotifiedChapterId = newChapters.lastOrNull()?.id ?: previousTrackChapterId
)
db.tracksDao.upsert(entity)
val foundChapters = newChapters.takeLastWhile { x -> x.id != previousTrackChapterId }
if (foundChapters.isNotEmpty()) {
val logEntity = TrackLogEntity(
mangaId = mangaId,
chapters = foundChapters.joinToString("\n") { x -> x.name },
createdAt = System.currentTimeMillis()
)
db.trackLogsDao.insert(logEntity)
if (saveTrackLog && previousTrackChapterId != 0L) {
val foundChapters = newChapters.takeLastWhile { x -> x.id != previousTrackChapterId }
if (foundChapters.isNotEmpty()) {
val logEntity = TrackLogEntity(
mangaId = mangaId,
chapters = foundChapters.joinToString("\n") { x -> x.name },
createdAt = System.currentTimeMillis()
)
db.trackLogsDao.insert(logEntity)
}
}
}
}

View File

@@ -14,7 +14,6 @@ import androidx.lifecycle.map
import androidx.work.*
import coil.ImageLoader
import coil.request.ImageRequest
import java.util.concurrent.TimeUnit
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.koin.core.component.KoinComponent
@@ -31,6 +30,7 @@ import org.koitharu.kotatsu.utils.ext.referer
import org.koitharu.kotatsu.utils.ext.toBitmapOrNull
import org.koitharu.kotatsu.utils.ext.trySetForeground
import org.koitharu.kotatsu.utils.progress.Progress
import java.util.concurrent.TimeUnit
class TrackWorker(context: Context, workerParams: WorkerParameters) :
CoroutineWorker(context, workerParams), KoinComponent {
@@ -65,25 +65,18 @@ class TrackWorker(context: Context, workerParams: WorkerParameters) :
setProgress(workData.build())
val chapters = details?.chapters ?: continue
when {
track.knownChaptersCount == -1 -> { // first check
// first check or manga was empty on last check
track.knownChaptersCount <= 0 || track.lastChapterId == 0L -> {
repository.storeTrackResult(
mangaId = track.manga.id,
knownChaptersCount = chapters.size,
lastChapterId = chapters.lastOrNull()?.id ?: 0L,
previousTrackChapterId = 0L,
newChapters = emptyList()
newChapters = emptyList(),
saveTrackLog = false,
)
}
track.knownChaptersCount == 0 && track.lastChapterId == 0L -> { // manga was empty on last check
repository.storeTrackResult(
mangaId = track.manga.id,
knownChaptersCount = 0,
lastChapterId = 0L,
previousTrackChapterId = track.lastNotifiedChapterId,
newChapters = chapters
)
showNotification(details, channelId, chapters)
}
// the same chapters count
chapters.size == track.knownChaptersCount -> {
if (chapters.lastOrNull()?.id == track.lastChapterId) {
// manga was not updated. skip
@@ -97,8 +90,9 @@ class TrackWorker(context: Context, workerParams: WorkerParameters) :
mangaId = track.manga.id,
knownChaptersCount = chapters.size,
lastChapterId = chapters.lastOrNull()?.id ?: 0L,
previousTrackChapterId = track.lastNotifiedChapterId,
newChapters = emptyList()
previousTrackChapterId = 0L,
newChapters = emptyList(),
saveTrackLog = false,
)
} else {
val newChapters = chapters.takeLast(chapters.size - knownChapter + 1)
@@ -107,7 +101,8 @@ class TrackWorker(context: Context, workerParams: WorkerParameters) :
knownChaptersCount = knownChapter + 1,
lastChapterId = track.lastChapterId,
previousTrackChapterId = track.lastNotifiedChapterId,
newChapters = newChapters
newChapters = newChapters,
saveTrackLog = true,
)
showNotification(
details,
@@ -125,6 +120,7 @@ class TrackWorker(context: Context, workerParams: WorkerParameters) :
lastChapterId = track.lastChapterId,
previousTrackChapterId = track.lastNotifiedChapterId,
newChapters = newChapters,
saveTrackLog = true,
)
showNotification(
manga = track.manga,