Update Glance v1.0.0-beta01 (#9551)

This commit is contained in:
Ivan Iskandar 2023-05-28 09:59:21 +07:00 committed by GitHub
parent ed5a56be60
commit a458bd9fdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 35 deletions

View File

@ -128,7 +128,7 @@ class App : Application(), DefaultLifecycleObserver, ImageLoaderFactory {
setAppCompatDelegateThemeMode(Injekt.get<UiPreferences>().themeMode().get()) setAppCompatDelegateThemeMode(Injekt.get<UiPreferences>().themeMode().get())
// Updates widget update // Updates widget update
with(TachiyomiWidgetManager(Injekt.get())) { with(TachiyomiWidgetManager(Injekt.get(), Injekt.get())) {
init(ProcessLifecycleOwner.get().lifecycleScope) init(ProcessLifecycleOwner.get().lifecycleScope)
} }

View File

@ -13,7 +13,7 @@ corektx = "androidx.core:core-ktx:1.11.0-beta01"
splashscreen = "androidx.core:core-splashscreen:1.0.0-alpha02" splashscreen = "androidx.core:core-splashscreen:1.0.0-alpha02"
recyclerview = "androidx.recyclerview:recyclerview:1.3.1-rc01" recyclerview = "androidx.recyclerview:recyclerview:1.3.1-rc01"
viewpager = "androidx.viewpager:viewpager:1.1.0-alpha01" 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" profileinstaller = "androidx.profileinstaller:profileinstaller:1.3.1"
lifecycle-common = { module = "androidx.lifecycle:lifecycle-common", version.ref = "lifecycle_version" } lifecycle-common = { module = "androidx.lifecycle:lifecycle-common", version.ref = "lifecycle_version" }

View File

@ -1,29 +1,34 @@
package tachiyomi.presentation.widget package tachiyomi.presentation.widget
import android.content.Context import android.content.Context
import androidx.glance.appwidget.GlanceAppWidgetManager import androidx.glance.appwidget.updateAll
import androidx.lifecycle.LifecycleCoroutineScope 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.distinctUntilChanged
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import logcat.LogPriority
import tachiyomi.core.util.system.logcat
import tachiyomi.domain.updates.interactor.GetUpdates import tachiyomi.domain.updates.interactor.GetUpdates
class TachiyomiWidgetManager( class TachiyomiWidgetManager(
private val getUpdates: GetUpdates, private val getUpdates: GetUpdates,
private val securityPreferences: SecurityPreferences,
) { ) {
fun Context.init(scope: LifecycleCoroutineScope) { fun Context.init(scope: LifecycleCoroutineScope) {
getUpdates.subscribe( combine(
read = false, getUpdates.subscribe(read = false, after = UpdatesGridGlanceWidget.DateLimit.timeInMillis),
after = UpdatesGridGlanceWidget.DateLimit.timeInMillis, securityPreferences.useAuthenticator().changes(),
transform = { a, _ -> a },
) )
.drop(1)
.distinctUntilChanged() .distinctUntilChanged()
.onEach { .onEach {
val manager = GlanceAppWidgetManager(this) try {
if (manager.getGlanceIds(UpdatesGridGlanceWidget::class.java).isNotEmpty()) { UpdatesGridGlanceWidget().updateAll(this)
UpdatesGridGlanceWidget().loadData(it) } catch (e: Exception) {
logcat(LogPriority.ERROR, e) { "Failed to update widget" }
} }
} }
.launchIn(scope) .launchIn(scope)

View File

@ -4,5 +4,5 @@ import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.GlanceAppWidgetReceiver import androidx.glance.appwidget.GlanceAppWidgetReceiver
class UpdatesGridGlanceReceiver : GlanceAppWidgetReceiver() { class UpdatesGridGlanceReceiver : GlanceAppWidgetReceiver() {
override val glanceAppWidget: GlanceAppWidget = UpdatesGridGlanceWidget().apply { loadData() } override val glanceAppWidget: GlanceAppWidget = UpdatesGridGlanceWidget()
} }

View File

@ -1,17 +1,18 @@
package tachiyomi.presentation.widget package tachiyomi.presentation.widget
import android.app.Application import android.app.Application
import android.content.Context
import android.graphics.Bitmap import android.graphics.Bitmap
import android.os.Build import android.os.Build
import androidx.compose.runtime.Composable
import androidx.core.graphics.drawable.toBitmap import androidx.core.graphics.drawable.toBitmap
import androidx.glance.GlanceId
import androidx.glance.GlanceModifier import androidx.glance.GlanceModifier
import androidx.glance.ImageProvider import androidx.glance.ImageProvider
import androidx.glance.appwidget.GlanceAppWidget import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.GlanceAppWidgetManager import androidx.glance.appwidget.GlanceAppWidgetManager
import androidx.glance.appwidget.SizeMode import androidx.glance.appwidget.SizeMode
import androidx.glance.appwidget.appWidgetBackground import androidx.glance.appwidget.appWidgetBackground
import androidx.glance.appwidget.updateAll import androidx.glance.appwidget.provideContent
import androidx.glance.background import androidx.glance.background
import androidx.glance.layout.fillMaxSize import androidx.glance.layout.fillMaxSize
import coil.executeBlocking import coil.executeBlocking
@ -23,8 +24,7 @@ import coil.size.Scale
import coil.transform.RoundedCornersTransformation import coil.transform.RoundedCornersTransformation
import eu.kanade.tachiyomi.core.security.SecurityPreferences import eu.kanade.tachiyomi.core.security.SecurityPreferences
import eu.kanade.tachiyomi.util.system.dpToPx import eu.kanade.tachiyomi.util.system.dpToPx
import kotlinx.coroutines.MainScope import tachiyomi.core.util.lang.withIOContext
import tachiyomi.core.util.lang.launchIO
import tachiyomi.domain.manga.model.MangaCover import tachiyomi.domain.manga.model.MangaCover
import tachiyomi.domain.updates.interactor.GetUpdates import tachiyomi.domain.updates.interactor.GetUpdates
import tachiyomi.domain.updates.model.UpdatesWithRelations import tachiyomi.domain.updates.model.UpdatesWithRelations
@ -45,33 +45,29 @@ class UpdatesGridGlanceWidget : GlanceAppWidget() {
private val app: Application by injectLazy() private val app: Application by injectLazy()
private val preferences: SecurityPreferences by injectLazy() private val preferences: SecurityPreferences by injectLazy()
private val coroutineScope = MainScope()
private var data: List<Pair<Long, Bitmap?>>? = null private var data: List<Pair<Long, Bitmap?>>? = null
override val sizeMode = SizeMode.Exact override val sizeMode = SizeMode.Exact
@Composable override suspend fun provideGlance(context: Context, id: GlanceId) {
override fun Content() { val locked = preferences.useAuthenticator().get()
// If app lock enabled, don't do anything if (!locked) loadData()
if (preferences.useAuthenticator().get()) {
LockedWidget() provideContent {
return // If app lock enabled, don't do anything
if (locked) {
LockedWidget()
return@provideContent
}
UpdatesWidget(data)
} }
UpdatesWidget(data)
} }
fun loadData(list: List<UpdatesWithRelations>? = null) { private suspend fun loadData(list: List<UpdatesWithRelations>? = null) {
coroutineScope.launchIO { withIOContext {
// Don't show anything when lock is active
if (preferences.useAuthenticator().get()) {
updateAll(app)
return@launchIO
}
val manager = GlanceAppWidgetManager(app) val manager = GlanceAppWidgetManager(app)
val ids = manager.getGlanceIds(this@UpdatesGridGlanceWidget::class.java) val ids = manager.getGlanceIds(this@UpdatesGridGlanceWidget::class.java)
if (ids.isEmpty()) return@launchIO if (ids.isEmpty()) return@withIOContext
val processList = list val processList = list
?: Injekt.get<GetUpdates>().await( ?: Injekt.get<GetUpdates>().await(
@ -84,7 +80,6 @@ class UpdatesGridGlanceWidget : GlanceAppWidget() {
.calculateRowAndColumnCount() .calculateRowAndColumnCount()
data = prepareList(processList, rowCount * columnCount) data = prepareList(processList, rowCount * columnCount)
ids.forEach { update(app, it) }
} }
} }