mihon/presentation-widget/src/main/java/tachiyomi/presentation/widget/WidgetManager.kt

38 lines
1.3 KiB
Kotlin
Raw Normal View History

package tachiyomi.presentation.widget
import android.content.Context
2023-05-28 04:59:21 +02:00
import androidx.glance.appwidget.updateAll
import androidx.lifecycle.LifecycleCoroutineScope
2023-05-28 04:59:21 +02:00
import eu.kanade.tachiyomi.core.security.SecurityPreferences
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
2023-05-28 04:59:21 +02:00
import logcat.LogPriority
import tachiyomi.core.util.system.logcat
import tachiyomi.domain.updates.interactor.GetUpdates
class WidgetManager(
private val getUpdates: GetUpdates,
2023-05-28 04:59:21 +02:00
private val securityPreferences: SecurityPreferences,
) {
fun Context.init(scope: LifecycleCoroutineScope) {
2023-05-28 04:59:21 +02:00
combine(
getUpdates.subscribe(read = false, after = BaseUpdatesGridGlanceWidget.DateLimit.timeInMillis),
2023-05-28 04:59:21 +02:00
securityPreferences.useAuthenticator().changes(),
transform = { a, _ -> a },
)
.distinctUntilChanged()
.onEach {
2023-05-28 04:59:21 +02:00
try {
UpdatesGridGlanceWidget().updateAll(this)
UpdatesGridCoverScreenGlanceWidget().updateAll(this)
2023-05-28 04:59:21 +02:00
} catch (e: Exception) {
logcat(LogPriority.ERROR, e) { "Failed to update widget" }
}
}
.launchIn(scope)
}
}