Improve DelayedTrackerStore (#8109)

* Improve DelayedTrackerStore

* Review changes
This commit is contained in:
AntsyLich 2022-09-29 19:20:07 +06:00 committed by GitHub
parent 8b9a06e298
commit ad84a8c3e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 10 deletions

View File

@ -25,9 +25,9 @@ class DelayedTrackingStore(context: Context) {
}
}
fun clear() {
fun remove(track: Track) {
preferences.edit {
clear()
remove(track.id.toString())
}
}

View File

@ -14,9 +14,8 @@ import eu.kanade.domain.track.interactor.GetTracks
import eu.kanade.domain.track.interactor.InsertTrack
import eu.kanade.domain.track.model.toDbTrack
import eu.kanade.tachiyomi.data.track.TrackManager
import eu.kanade.tachiyomi.util.lang.withIOContext
import eu.kanade.tachiyomi.util.system.logcat
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import logcat.LogPriority
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
@ -33,9 +32,9 @@ class DelayedTrackingUpdateJob(context: Context, workerParams: WorkerParameters)
val trackManager = Injekt.get<TrackManager>()
val delayedTrackingStore = Injekt.get<DelayedTrackingStore>()
withContext(Dispatchers.IO) {
withIOContext {
val tracks = delayedTrackingStore.getItems().mapNotNull {
val manga = getManga.await(it.mangaId) ?: return@withContext
val manga = getManga.await(it.mangaId) ?: return@withIOContext
getTracks.await(manga.id)
.find { track -> track.id == it.trackId }
?.copy(lastChapterRead = it.lastChapterRead.toDouble())
@ -48,12 +47,11 @@ class DelayedTrackingUpdateJob(context: Context, workerParams: WorkerParameters)
service.update(track.toDbTrack(), true)
insertTrack.await(track)
}
delayedTrackingStore.remove(track)
} catch (e: Exception) {
logcat(LogPriority.ERROR, e)
}
}
delayedTrackingStore.clear()
}
return Result.success()

View File

@ -843,12 +843,14 @@ class ReaderPresenter(
// for a while. The view can still be garbage collected.
async {
runCatching {
if (context.isOnline()) {
try {
if (!context.isOnline()) error("Couldn't update tracker as device is offline")
service.update(updatedTrack.toDbTrack(), true)
insertTrack.await(updatedTrack)
} else {
} catch (e: Exception) {
delayedTrackingStore.addItem(updatedTrack)
DelayedTrackingUpdateJob.setupTask(context)
throw e
}
}
}