2023-01-26 23:53:24 +01:00
|
|
|
package tachiyomi.presentation.widget
|
|
|
|
|
|
|
|
import android.content.Context
|
2023-05-28 09:59:21 +07:00
|
|
|
import androidx.glance.appwidget.updateAll
|
2023-01-26 23:53:24 +01:00
|
|
|
import androidx.lifecycle.LifecycleCoroutineScope
|
2023-05-28 09:59:21 +07:00
|
|
|
import eu.kanade.tachiyomi.core.security.SecurityPreferences
|
|
|
|
import kotlinx.coroutines.flow.combine
|
2023-01-26 23:53:24 +01:00
|
|
|
import kotlinx.coroutines.flow.distinctUntilChanged
|
|
|
|
import kotlinx.coroutines.flow.launchIn
|
|
|
|
import kotlinx.coroutines.flow.onEach
|
2023-05-28 09:59:21 +07:00
|
|
|
import logcat.LogPriority
|
|
|
|
import tachiyomi.core.util.system.logcat
|
2023-02-20 19:02:38 -05:00
|
|
|
import tachiyomi.domain.updates.interactor.GetUpdates
|
2023-01-26 23:53:24 +01:00
|
|
|
|
2023-11-11 18:31:27 -05:00
|
|
|
class WidgetManager(
|
2023-02-20 19:02:38 -05:00
|
|
|
private val getUpdates: GetUpdates,
|
2023-05-28 09:59:21 +07:00
|
|
|
private val securityPreferences: SecurityPreferences,
|
2023-01-27 20:49:57 +01:00
|
|
|
) {
|
2023-01-26 23:53:24 +01:00
|
|
|
|
2023-01-27 20:49:57 +01:00
|
|
|
fun Context.init(scope: LifecycleCoroutineScope) {
|
2023-05-28 09:59:21 +07:00
|
|
|
combine(
|
2023-12-08 23:11:53 -05:00
|
|
|
getUpdates.subscribe(read = false, after = BaseUpdatesGridGlanceWidget.DateLimit.toEpochMilli()),
|
2023-05-28 09:59:21 +07:00
|
|
|
securityPreferences.useAuthenticator().changes(),
|
|
|
|
transform = { a, _ -> a },
|
2023-02-20 19:02:38 -05:00
|
|
|
)
|
2023-01-26 23:53:24 +01:00
|
|
|
.distinctUntilChanged()
|
|
|
|
.onEach {
|
2023-05-28 09:59:21 +07:00
|
|
|
try {
|
|
|
|
UpdatesGridGlanceWidget().updateAll(this)
|
2023-09-02 20:37:25 +07:00
|
|
|
UpdatesGridCoverScreenGlanceWidget().updateAll(this)
|
2023-05-28 09:59:21 +07:00
|
|
|
} catch (e: Exception) {
|
|
|
|
logcat(LogPriority.ERROR, e) { "Failed to update widget" }
|
2023-01-26 23:53:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.launchIn(scope)
|
|
|
|
}
|
|
|
|
}
|