2023-01-26 23:53:24 +01:00
|
|
|
package tachiyomi.presentation.widget
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import androidx.glance.appwidget.GlanceAppWidgetManager
|
|
|
|
import androidx.lifecycle.LifecycleCoroutineScope
|
|
|
|
import kotlinx.coroutines.flow.distinctUntilChanged
|
|
|
|
import kotlinx.coroutines.flow.drop
|
|
|
|
import kotlinx.coroutines.flow.launchIn
|
|
|
|
import kotlinx.coroutines.flow.onEach
|
2023-02-21 01:02:38 +01:00
|
|
|
import tachiyomi.domain.updates.interactor.GetUpdates
|
2023-01-26 23:53:24 +01:00
|
|
|
|
2023-01-27 20:49:57 +01:00
|
|
|
class TachiyomiWidgetManager(
|
2023-02-21 01:02:38 +01:00
|
|
|
private val getUpdates: GetUpdates,
|
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-02-21 01:02:38 +01:00
|
|
|
getUpdates.subscribe(
|
|
|
|
read = false,
|
|
|
|
after = UpdatesGridGlanceWidget.DateLimit.timeInMillis,
|
|
|
|
)
|
2023-01-26 23:53:24 +01:00
|
|
|
.drop(1)
|
|
|
|
.distinctUntilChanged()
|
|
|
|
.onEach {
|
|
|
|
val manager = GlanceAppWidgetManager(this)
|
|
|
|
if (manager.getGlanceIds(UpdatesGridGlanceWidget::class.java).isNotEmpty()) {
|
|
|
|
UpdatesGridGlanceWidget().loadData(it)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.launchIn(scope)
|
|
|
|
}
|
|
|
|
}
|