Fix progress percent computatiion #327

This commit is contained in:
Koitharu
2023-03-21 20:03:57 +02:00
parent 527e11e65b
commit b45147563a
4 changed files with 42 additions and 18 deletions

View File

@@ -265,7 +265,7 @@ class DetailsViewModel @Inject constructor(
fun markChapterAsCurrent(chapterId: Long) {
launchJob(Dispatchers.Default) {
val manga = checkNotNull(delegate.manga.value)
val chapters = checkNotNull(manga.chapters)
val chapters = checkNotNull(manga.getChapters(selectedBranchValue))
val chapterIndex = chapters.indexOfFirst { it.id == chapterId }
check(chapterIndex in chapters.indices) { "Chapter not found" }
val percent = chapterIndex / chapters.size.toFloat()

View File

@@ -381,7 +381,8 @@ class ReaderViewModel @Inject constructor(
}
private fun computePercent(chapterId: Long, pageIndex: Int): Float {
val chapters = manga?.chapters ?: return PROGRESS_NONE
val branch = chapters[chapterId]?.branch
val chapters = manga?.getChapters(branch) ?: return PROGRESS_NONE
val chaptersCount = chapters.size
val chapterIndex = chapters.indexOfFirst { x -> x.id == chapterId }
val pagesCount = chaptersLoader.getPagesCount(chapterId)

View File

@@ -113,7 +113,8 @@ class TrackingRepository @Inject constructor(
db.withTransaction {
val track = getOrCreateTrack(updates.manga.id).mergeWith(updates)
db.tracksDao.upsert(track)
if (updates.isValid && updates.newChapters.isNotEmpty()) {
if (updates.isValid /*&& updates.newChapters.isNotEmpty()*/) {
updatePercent(updates)
val logEntity = TrackLogEntity(
mangaId = updates.manga.id,
chapters = updates.newChapters.joinToString("\n") { x -> x.name },
@@ -177,6 +178,21 @@ class TrackingRepository @Inject constructor(
)
}
private suspend fun updatePercent(updates: MangaUpdates) {
val history = db.historyDao.find(updates.manga.id) ?: return
val chapters = updates.manga.chapters
if (chapters.isNullOrEmpty()) {
return
}
val chapterIndex = chapters.indexOfFirst { it.id == history.chapterId }
if (chapterIndex < 0) {
return
}
val position = (chapters.size - updates.newChapters.size) * history.percent
val newPercent = position / chapters.size.toFloat()
db.historyDao.update(history.copy(percent = newPercent))
}
private fun TrackEntity.mergeWith(updates: MangaUpdates): TrackEntity {
val chapters = updates.manga.chapters.orEmpty()
return TrackEntity(

View File

@@ -195,9 +195,7 @@ class TrackWorker @AssistedInject constructor(
builder.setDefaults(defaults)
}
}
withContext(Dispatchers.Main) {
notificationManager.notify(TAG, id, builder.build())
}
notificationManager.notify(TAG, id, builder.build())
}
override suspend fun getForegroundInfo(): ForegroundInfo {
@@ -214,13 +212,17 @@ class TrackWorker @AssistedInject constructor(
channel.enableLights(false)
notificationManager.createNotificationChannel(channel)
}
val notification = NotificationCompat.Builder(applicationContext, WORKER_CHANNEL_ID).setContentTitle(title)
.setPriority(NotificationCompat.PRIORITY_MIN).setDefaults(0)
.setColor(ContextCompat.getColor(applicationContext, R.color.blue_primary_dark)).setSilent(true)
.setProgress(0, 0, true).setSmallIcon(android.R.drawable.stat_notify_sync)
.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_DEFERRED).setOngoing(true).build()
val notification = NotificationCompat.Builder(applicationContext, WORKER_CHANNEL_ID)
.setContentTitle(title)
.setPriority(NotificationCompat.PRIORITY_MIN)
.setDefaults(0)
.setColor(ContextCompat.getColor(applicationContext, R.color.blue_primary_dark))
.setSilent(true)
.setProgress(0, 0, true)
.setSmallIcon(android.R.drawable.stat_notify_sync)
.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_DEFERRED)
.setOngoing(true)
.build()
return ForegroundInfo(WORKER_NOTIFICATION_ID, notification)
}
@@ -243,16 +245,21 @@ class TrackWorker @AssistedInject constructor(
fun setup(context: Context) {
val constraints = Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build()
val request =
PeriodicWorkRequestBuilder<TrackWorker>(4, TimeUnit.HOURS).setConstraints(constraints).addTag(TAG)
.setBackoffCriteria(BackoffPolicy.LINEAR, 30, TimeUnit.MINUTES).build()
val request = PeriodicWorkRequestBuilder<TrackWorker>(4, TimeUnit.HOURS)
.setConstraints(constraints)
.addTag(TAG)
.setBackoffCriteria(BackoffPolicy.LINEAR, 30, TimeUnit.MINUTES)
.build()
WorkManager.getInstance(context).enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.KEEP, request)
}
fun startNow(context: Context) {
val constraints = Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build()
val request = OneTimeWorkRequestBuilder<TrackWorker>().setConstraints(constraints).addTag(TAG_ONESHOT)
.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST).build()
val request = OneTimeWorkRequestBuilder<TrackWorker>()
.setConstraints(constraints)
.addTag(TAG_ONESHOT)
.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
.build()
WorkManager.getInstance(context).enqueue(request)
}