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 package org.koitharu.kotatsu.core.model
import android.os.Parcelable import android.os.Parcelable
import java.util.*
import kotlinx.parcelize.Parcelize import kotlinx.parcelize.Parcelize
import org.koitharu.kotatsu.parsers.model.Manga import org.koitharu.kotatsu.parsers.model.Manga
import java.util.*
data class MangaTracking( data class MangaTracking(
val manga: Manga, val manga: Manga,

View File

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

View File

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