Update Glance v1.0.0-beta01 (#9551)
This commit is contained in:
parent
ed5a56be60
commit
a458bd9fdb
@ -128,7 +128,7 @@ class App : Application(), DefaultLifecycleObserver, ImageLoaderFactory {
|
||||
setAppCompatDelegateThemeMode(Injekt.get<UiPreferences>().themeMode().get())
|
||||
|
||||
// Updates widget update
|
||||
with(TachiyomiWidgetManager(Injekt.get())) {
|
||||
with(TachiyomiWidgetManager(Injekt.get(), Injekt.get())) {
|
||||
init(ProcessLifecycleOwner.get().lifecycleScope)
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,7 @@ corektx = "androidx.core:core-ktx:1.11.0-beta01"
|
||||
splashscreen = "androidx.core:core-splashscreen:1.0.0-alpha02"
|
||||
recyclerview = "androidx.recyclerview:recyclerview:1.3.1-rc01"
|
||||
viewpager = "androidx.viewpager:viewpager:1.1.0-alpha01"
|
||||
glance = "androidx.glance:glance-appwidget:1.0.0-alpha03"
|
||||
glance = "androidx.glance:glance-appwidget:1.0.0-beta01"
|
||||
profileinstaller = "androidx.profileinstaller:profileinstaller:1.3.1"
|
||||
|
||||
lifecycle-common = { module = "androidx.lifecycle:lifecycle-common", version.ref = "lifecycle_version" }
|
||||
|
@ -1,29 +1,34 @@
|
||||
package tachiyomi.presentation.widget
|
||||
|
||||
import android.content.Context
|
||||
import androidx.glance.appwidget.GlanceAppWidgetManager
|
||||
import androidx.glance.appwidget.updateAll
|
||||
import androidx.lifecycle.LifecycleCoroutineScope
|
||||
import eu.kanade.tachiyomi.core.security.SecurityPreferences
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.drop
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import logcat.LogPriority
|
||||
import tachiyomi.core.util.system.logcat
|
||||
import tachiyomi.domain.updates.interactor.GetUpdates
|
||||
|
||||
class TachiyomiWidgetManager(
|
||||
private val getUpdates: GetUpdates,
|
||||
private val securityPreferences: SecurityPreferences,
|
||||
) {
|
||||
|
||||
fun Context.init(scope: LifecycleCoroutineScope) {
|
||||
getUpdates.subscribe(
|
||||
read = false,
|
||||
after = UpdatesGridGlanceWidget.DateLimit.timeInMillis,
|
||||
combine(
|
||||
getUpdates.subscribe(read = false, after = UpdatesGridGlanceWidget.DateLimit.timeInMillis),
|
||||
securityPreferences.useAuthenticator().changes(),
|
||||
transform = { a, _ -> a },
|
||||
)
|
||||
.drop(1)
|
||||
.distinctUntilChanged()
|
||||
.onEach {
|
||||
val manager = GlanceAppWidgetManager(this)
|
||||
if (manager.getGlanceIds(UpdatesGridGlanceWidget::class.java).isNotEmpty()) {
|
||||
UpdatesGridGlanceWidget().loadData(it)
|
||||
try {
|
||||
UpdatesGridGlanceWidget().updateAll(this)
|
||||
} catch (e: Exception) {
|
||||
logcat(LogPriority.ERROR, e) { "Failed to update widget" }
|
||||
}
|
||||
}
|
||||
.launchIn(scope)
|
||||
|
@ -4,5 +4,5 @@ import androidx.glance.appwidget.GlanceAppWidget
|
||||
import androidx.glance.appwidget.GlanceAppWidgetReceiver
|
||||
|
||||
class UpdatesGridGlanceReceiver : GlanceAppWidgetReceiver() {
|
||||
override val glanceAppWidget: GlanceAppWidget = UpdatesGridGlanceWidget().apply { loadData() }
|
||||
override val glanceAppWidget: GlanceAppWidget = UpdatesGridGlanceWidget()
|
||||
}
|
||||
|
@ -1,17 +1,18 @@
|
||||
package tachiyomi.presentation.widget
|
||||
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.os.Build
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.core.graphics.drawable.toBitmap
|
||||
import androidx.glance.GlanceId
|
||||
import androidx.glance.GlanceModifier
|
||||
import androidx.glance.ImageProvider
|
||||
import androidx.glance.appwidget.GlanceAppWidget
|
||||
import androidx.glance.appwidget.GlanceAppWidgetManager
|
||||
import androidx.glance.appwidget.SizeMode
|
||||
import androidx.glance.appwidget.appWidgetBackground
|
||||
import androidx.glance.appwidget.updateAll
|
||||
import androidx.glance.appwidget.provideContent
|
||||
import androidx.glance.background
|
||||
import androidx.glance.layout.fillMaxSize
|
||||
import coil.executeBlocking
|
||||
@ -23,8 +24,7 @@ import coil.size.Scale
|
||||
import coil.transform.RoundedCornersTransformation
|
||||
import eu.kanade.tachiyomi.core.security.SecurityPreferences
|
||||
import eu.kanade.tachiyomi.util.system.dpToPx
|
||||
import kotlinx.coroutines.MainScope
|
||||
import tachiyomi.core.util.lang.launchIO
|
||||
import tachiyomi.core.util.lang.withIOContext
|
||||
import tachiyomi.domain.manga.model.MangaCover
|
||||
import tachiyomi.domain.updates.interactor.GetUpdates
|
||||
import tachiyomi.domain.updates.model.UpdatesWithRelations
|
||||
@ -45,33 +45,29 @@ class UpdatesGridGlanceWidget : GlanceAppWidget() {
|
||||
private val app: Application by injectLazy()
|
||||
private val preferences: SecurityPreferences by injectLazy()
|
||||
|
||||
private val coroutineScope = MainScope()
|
||||
|
||||
private var data: List<Pair<Long, Bitmap?>>? = null
|
||||
|
||||
override val sizeMode = SizeMode.Exact
|
||||
|
||||
@Composable
|
||||
override fun Content() {
|
||||
override suspend fun provideGlance(context: Context, id: GlanceId) {
|
||||
val locked = preferences.useAuthenticator().get()
|
||||
if (!locked) loadData()
|
||||
|
||||
provideContent {
|
||||
// If app lock enabled, don't do anything
|
||||
if (preferences.useAuthenticator().get()) {
|
||||
if (locked) {
|
||||
LockedWidget()
|
||||
return
|
||||
return@provideContent
|
||||
}
|
||||
UpdatesWidget(data)
|
||||
}
|
||||
|
||||
fun loadData(list: List<UpdatesWithRelations>? = null) {
|
||||
coroutineScope.launchIO {
|
||||
// Don't show anything when lock is active
|
||||
if (preferences.useAuthenticator().get()) {
|
||||
updateAll(app)
|
||||
return@launchIO
|
||||
}
|
||||
|
||||
private suspend fun loadData(list: List<UpdatesWithRelations>? = null) {
|
||||
withIOContext {
|
||||
val manager = GlanceAppWidgetManager(app)
|
||||
val ids = manager.getGlanceIds(this@UpdatesGridGlanceWidget::class.java)
|
||||
if (ids.isEmpty()) return@launchIO
|
||||
if (ids.isEmpty()) return@withIOContext
|
||||
|
||||
val processList = list
|
||||
?: Injekt.get<GetUpdates>().await(
|
||||
@ -84,7 +80,6 @@ class UpdatesGridGlanceWidget : GlanceAppWidget() {
|
||||
.calculateRowAndColumnCount()
|
||||
|
||||
data = prepareList(processList, rowCount * columnCount)
|
||||
ids.forEach { update(app, it) }
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user