mirror of
				https://github.com/mihonapp/mihon.git
				synced 2025-11-04 08:08:55 +01:00 
			
		
		
		
	Delayed Tracking Update related fix (#8642)
* Delayed Tracking Update related fix * Lint
This commit is contained in:
		@@ -10,6 +10,15 @@ class GetTracks(
 | 
			
		||||
    private val trackRepository: TrackRepository,
 | 
			
		||||
) {
 | 
			
		||||
 | 
			
		||||
    suspend fun awaitOne(id: Long): Track? {
 | 
			
		||||
        return try {
 | 
			
		||||
            trackRepository.getTrackById(id)
 | 
			
		||||
        } catch (e: Exception) {
 | 
			
		||||
            logcat(LogPriority.ERROR, e)
 | 
			
		||||
            null
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    suspend fun await(mangaId: Long): List<Track> {
 | 
			
		||||
        return try {
 | 
			
		||||
            trackRepository.getTracksByMangaId(mangaId)
 | 
			
		||||
 
 | 
			
		||||
@@ -5,6 +5,8 @@ import kotlinx.coroutines.flow.Flow
 | 
			
		||||
 | 
			
		||||
interface TrackRepository {
 | 
			
		||||
 | 
			
		||||
    suspend fun getTrackById(id: Long): Track?
 | 
			
		||||
 | 
			
		||||
    suspend fun getTracksByMangaId(mangaId: Long): List<Track>
 | 
			
		||||
 | 
			
		||||
    fun getTracksAsFlow(): Flow<List<Track>>
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,78 @@
 | 
			
		||||
package eu.kanade.domain.track.service
 | 
			
		||||
 | 
			
		||||
import android.content.Context
 | 
			
		||||
import androidx.work.BackoffPolicy
 | 
			
		||||
import androidx.work.Constraints
 | 
			
		||||
import androidx.work.CoroutineWorker
 | 
			
		||||
import androidx.work.ExistingWorkPolicy
 | 
			
		||||
import androidx.work.NetworkType
 | 
			
		||||
import androidx.work.OneTimeWorkRequestBuilder
 | 
			
		||||
import androidx.work.WorkManager
 | 
			
		||||
import androidx.work.WorkerParameters
 | 
			
		||||
import eu.kanade.domain.track.interactor.GetTracks
 | 
			
		||||
import eu.kanade.domain.track.interactor.InsertTrack
 | 
			
		||||
import eu.kanade.domain.track.model.toDbTrack
 | 
			
		||||
import eu.kanade.domain.track.store.DelayedTrackingStore
 | 
			
		||||
import eu.kanade.tachiyomi.data.track.TrackManager
 | 
			
		||||
import eu.kanade.tachiyomi.util.lang.withIOContext
 | 
			
		||||
import eu.kanade.tachiyomi.util.system.logcat
 | 
			
		||||
import logcat.LogPriority
 | 
			
		||||
import uy.kohesive.injekt.Injekt
 | 
			
		||||
import uy.kohesive.injekt.api.get
 | 
			
		||||
import java.util.concurrent.TimeUnit
 | 
			
		||||
 | 
			
		||||
class DelayedTrackingUpdateJob(context: Context, workerParams: WorkerParameters) :
 | 
			
		||||
    CoroutineWorker(context, workerParams) {
 | 
			
		||||
 | 
			
		||||
    override suspend fun doWork(): Result {
 | 
			
		||||
        val getTracks = Injekt.get<GetTracks>()
 | 
			
		||||
        val insertTrack = Injekt.get<InsertTrack>()
 | 
			
		||||
 | 
			
		||||
        val trackManager = Injekt.get<TrackManager>()
 | 
			
		||||
        val delayedTrackingStore = Injekt.get<DelayedTrackingStore>()
 | 
			
		||||
 | 
			
		||||
        withIOContext {
 | 
			
		||||
            val tracks = delayedTrackingStore.getItems().mapNotNull {
 | 
			
		||||
                val track = getTracks.awaitOne(it.trackId)
 | 
			
		||||
                if (track == null) {
 | 
			
		||||
                    delayedTrackingStore.remove(it.trackId)
 | 
			
		||||
                }
 | 
			
		||||
                track
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            tracks.forEach { track ->
 | 
			
		||||
                try {
 | 
			
		||||
                    val service = trackManager.getService(track.syncId)
 | 
			
		||||
                    if (service != null && service.isLogged) {
 | 
			
		||||
                        service.update(track.toDbTrack(), true)
 | 
			
		||||
                        insertTrack.await(track)
 | 
			
		||||
                    }
 | 
			
		||||
                    delayedTrackingStore.remove(track.id)
 | 
			
		||||
                } catch (e: Exception) {
 | 
			
		||||
                    logcat(LogPriority.ERROR, e)
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return Result.success()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    companion object {
 | 
			
		||||
        private const val TAG = "DelayedTrackingUpdate"
 | 
			
		||||
 | 
			
		||||
        fun setupTask(context: Context) {
 | 
			
		||||
            val constraints = Constraints.Builder()
 | 
			
		||||
                .setRequiredNetworkType(NetworkType.CONNECTED)
 | 
			
		||||
                .build()
 | 
			
		||||
 | 
			
		||||
            val request = OneTimeWorkRequestBuilder<DelayedTrackingUpdateJob>()
 | 
			
		||||
                .setConstraints(constraints)
 | 
			
		||||
                .setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 20, TimeUnit.SECONDS)
 | 
			
		||||
                .addTag(TAG)
 | 
			
		||||
                .build()
 | 
			
		||||
 | 
			
		||||
            WorkManager.getInstance(context)
 | 
			
		||||
                .enqueueUniqueWork(TAG, ExistingWorkPolicy.REPLACE, request)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,46 @@
 | 
			
		||||
package eu.kanade.domain.track.store
 | 
			
		||||
 | 
			
		||||
import android.content.Context
 | 
			
		||||
import androidx.core.content.edit
 | 
			
		||||
import eu.kanade.domain.track.model.Track
 | 
			
		||||
import eu.kanade.tachiyomi.util.system.logcat
 | 
			
		||||
import logcat.LogPriority
 | 
			
		||||
 | 
			
		||||
class DelayedTrackingStore(context: Context) {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Preference file where queued tracking updates are stored.
 | 
			
		||||
     */
 | 
			
		||||
    private val preferences = context.getSharedPreferences("tracking_queue", Context.MODE_PRIVATE)
 | 
			
		||||
 | 
			
		||||
    fun addItem(track: Track) {
 | 
			
		||||
        val trackId = track.id.toString()
 | 
			
		||||
        val lastChapterRead = preferences.getFloat(trackId, 0f)
 | 
			
		||||
        if (track.lastChapterRead > lastChapterRead) {
 | 
			
		||||
            logcat(LogPriority.DEBUG) { "Queuing track item: $trackId, last chapter read: ${track.lastChapterRead}" }
 | 
			
		||||
            preferences.edit {
 | 
			
		||||
                putFloat(trackId, track.lastChapterRead.toFloat())
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun remove(trackId: Long) {
 | 
			
		||||
        preferences.edit {
 | 
			
		||||
            remove(trackId.toString())
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun getItems(): List<DelayedTrackingItem> {
 | 
			
		||||
        return preferences.all.mapNotNull {
 | 
			
		||||
            DelayedTrackingItem(
 | 
			
		||||
                trackId = it.key.toLong(),
 | 
			
		||||
                lastChapterRead = it.value.toString().toFloat(),
 | 
			
		||||
            )
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    data class DelayedTrackingItem(
 | 
			
		||||
        val trackId: Long,
 | 
			
		||||
        val lastChapterRead: Float,
 | 
			
		||||
    )
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user