AntsyLich a246d897de Adjust distinct checker in WidgetManager and run on default dispatcher
Co-authored-by: p
(cherry picked from commit 9b8ab6acc25a5f99c9c5eebf9cc250975931c57c)
2024-10-14 14:47:59 -04:00

44 lines
1.6 KiB
Kotlin

package tachiyomi.presentation.widget
import android.content.Context
import androidx.glance.appwidget.updateAll
import androidx.lifecycle.LifecycleCoroutineScope
import eu.kanade.tachiyomi.core.security.SecurityPreferences
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import logcat.LogPriority
import tachiyomi.core.common.util.system.logcat
import tachiyomi.domain.updates.interactor.GetUpdates
class WidgetManager(
private val getUpdates: GetUpdates,
private val securityPreferences: SecurityPreferences,
) {
fun Context.init(scope: LifecycleCoroutineScope) {
combine(
getUpdates.subscribe(read = false, after = BaseUpdatesGridGlanceWidget.DateLimit.toEpochMilli()),
securityPreferences.useAuthenticator().changes(),
transform = { a, b -> a to b },
)
.distinctUntilChanged { old, new ->
old.second == new.second &&
old.first.map { it.chapterId }.toSet() == new.first.map { it.chapterId }.toSet()
}
.onEach {
try {
UpdatesGridGlanceWidget().updateAll(this)
UpdatesGridCoverScreenGlanceWidget().updateAll(this)
} catch (e: Exception) {
logcat(LogPriority.ERROR, e) { "Failed to update widget" }
}
}
.flowOn(Dispatchers.Default)
.launchIn(scope)
}
}