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
|
|
|
|
import tachiyomi.data.DatabaseHandler
|
2023-01-27 20:49:57 +01:00
|
|
|
import uy.kohesive.injekt.Injekt
|
|
|
|
import uy.kohesive.injekt.api.get
|
2023-01-26 23:53:24 +01:00
|
|
|
|
2023-01-27 20:49:57 +01:00
|
|
|
class TachiyomiWidgetManager(
|
|
|
|
private val database: DatabaseHandler = Injekt.get(),
|
|
|
|
) {
|
2023-01-26 23:53:24 +01:00
|
|
|
|
2023-01-27 20:49:57 +01:00
|
|
|
fun Context.init(scope: LifecycleCoroutineScope) {
|
2023-02-18 16:51:06 +01:00
|
|
|
database.subscribeToList {
|
|
|
|
updatesViewQueries.getUpdatesByReadStatus(
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|