2023-01-26 23:53:24 +01:00
|
|
|
package tachiyomi.presentation.widget
|
2022-07-31 17:31:40 +02:00
|
|
|
|
|
|
|
import android.app.Application
|
|
|
|
import android.graphics.Bitmap
|
|
|
|
import android.os.Build
|
|
|
|
import androidx.compose.runtime.Composable
|
|
|
|
import androidx.core.graphics.drawable.toBitmap
|
|
|
|
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.background
|
|
|
|
import androidx.glance.layout.fillMaxSize
|
|
|
|
import coil.executeBlocking
|
|
|
|
import coil.imageLoader
|
|
|
|
import coil.request.CachePolicy
|
|
|
|
import coil.request.ImageRequest
|
|
|
|
import coil.size.Precision
|
|
|
|
import coil.size.Scale
|
|
|
|
import coil.transform.RoundedCornersTransformation
|
2022-09-18 19:07:48 +02:00
|
|
|
import eu.kanade.tachiyomi.core.security.SecurityPreferences
|
2022-07-31 17:31:40 +02:00
|
|
|
import eu.kanade.tachiyomi.util.system.dpToPx
|
|
|
|
import kotlinx.coroutines.MainScope
|
2023-01-28 04:31:12 +01:00
|
|
|
import tachiyomi.core.util.lang.launchIO
|
2023-01-21 16:37:07 +01:00
|
|
|
import tachiyomi.data.DatabaseHandler
|
2023-01-22 16:37:13 +01:00
|
|
|
import tachiyomi.domain.manga.model.MangaCover
|
2023-01-27 20:49:57 +01:00
|
|
|
import tachiyomi.presentation.widget.components.CoverHeight
|
|
|
|
import tachiyomi.presentation.widget.components.CoverWidth
|
|
|
|
import tachiyomi.presentation.widget.components.LockedWidget
|
|
|
|
import tachiyomi.presentation.widget.components.UpdatesWidget
|
2023-01-21 16:37:07 +01:00
|
|
|
import tachiyomi.view.UpdatesView
|
2022-07-31 17:31:40 +02:00
|
|
|
import uy.kohesive.injekt.Injekt
|
|
|
|
import uy.kohesive.injekt.api.get
|
|
|
|
import uy.kohesive.injekt.injectLazy
|
|
|
|
import java.util.Calendar
|
|
|
|
import java.util.Date
|
|
|
|
|
|
|
|
class UpdatesGridGlanceWidget : GlanceAppWidget() {
|
|
|
|
private val app: Application by injectLazy()
|
2022-09-18 19:07:48 +02:00
|
|
|
private val preferences: SecurityPreferences by injectLazy()
|
2022-07-31 17:31:40 +02:00
|
|
|
|
|
|
|
private val coroutineScope = MainScope()
|
|
|
|
|
2023-01-27 20:49:57 +01:00
|
|
|
private var data: List<Pair<Long, Bitmap?>>? = null
|
2022-07-31 17:31:40 +02:00
|
|
|
|
|
|
|
override val sizeMode = SizeMode.Exact
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
override fun Content() {
|
2023-01-27 20:49:57 +01:00
|
|
|
// If app lock enabled, don't do anything
|
2022-07-31 17:31:40 +02:00
|
|
|
if (preferences.useAuthenticator().get()) {
|
2023-01-27 20:49:57 +01:00
|
|
|
LockedWidget()
|
|
|
|
return
|
2022-07-31 17:31:40 +02:00
|
|
|
}
|
2023-01-27 20:49:57 +01:00
|
|
|
UpdatesWidget(data)
|
2022-07-31 17:31:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fun loadData(list: List<UpdatesView>? = null) {
|
|
|
|
coroutineScope.launchIO {
|
|
|
|
// Don't show anything when lock is active
|
|
|
|
if (preferences.useAuthenticator().get()) {
|
|
|
|
updateAll(app)
|
|
|
|
return@launchIO
|
|
|
|
}
|
|
|
|
|
|
|
|
val manager = GlanceAppWidgetManager(app)
|
|
|
|
val ids = manager.getGlanceIds(this@UpdatesGridGlanceWidget::class.java)
|
|
|
|
if (ids.isEmpty()) return@launchIO
|
|
|
|
|
|
|
|
val processList = list
|
|
|
|
?: Injekt.get<DatabaseHandler>()
|
|
|
|
.awaitList { updatesViewQueries.updates(after = DateLimit.timeInMillis) }
|
|
|
|
val (rowCount, columnCount) = ids
|
|
|
|
.flatMap { manager.getAppWidgetSizes(it) }
|
|
|
|
.maxBy { it.height.value * it.width.value }
|
|
|
|
.calculateRowAndColumnCount()
|
|
|
|
|
|
|
|
data = prepareList(processList, rowCount * columnCount)
|
|
|
|
ids.forEach { update(app, it) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun prepareList(processList: List<UpdatesView>, take: Int): List<Pair<Long, Bitmap?>> {
|
|
|
|
// Resize to cover size
|
|
|
|
val widthPx = CoverWidth.value.toInt().dpToPx
|
|
|
|
val heightPx = CoverHeight.value.toInt().dpToPx
|
|
|
|
val roundPx = app.resources.getDimension(R.dimen.appwidget_inner_radius)
|
|
|
|
return processList
|
|
|
|
.distinctBy { it.mangaId }
|
|
|
|
.take(take)
|
|
|
|
.map { updatesView ->
|
|
|
|
val request = ImageRequest.Builder(app)
|
|
|
|
.data(
|
|
|
|
MangaCover(
|
|
|
|
mangaId = updatesView.mangaId,
|
|
|
|
sourceId = updatesView.source,
|
|
|
|
isMangaFavorite = updatesView.favorite,
|
|
|
|
url = updatesView.thumbnailUrl,
|
|
|
|
lastModified = updatesView.coverLastModified,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.memoryCachePolicy(CachePolicy.DISABLED)
|
|
|
|
.precision(Precision.EXACT)
|
|
|
|
.size(widthPx, heightPx)
|
|
|
|
.scale(Scale.FILL)
|
|
|
|
.let {
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
|
|
|
|
it.transformations(RoundedCornersTransformation(roundPx))
|
2022-09-11 05:57:03 +02:00
|
|
|
} else {
|
|
|
|
it // Handled by system
|
|
|
|
}
|
2022-07-31 17:31:40 +02:00
|
|
|
}
|
|
|
|
.build()
|
|
|
|
Pair(updatesView.mangaId, app.imageLoader.executeBlocking(request).drawable?.toBitmap())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
val DateLimit: Calendar
|
|
|
|
get() = Calendar.getInstance().apply {
|
|
|
|
time = Date()
|
|
|
|
add(Calendar.MONTH, -3)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-27 20:49:57 +01:00
|
|
|
val ContainerModifier = GlanceModifier
|
2022-07-31 17:31:40 +02:00
|
|
|
.fillMaxSize()
|
|
|
|
.background(ImageProvider(R.drawable.appwidget_background))
|
|
|
|
.appWidgetBackground()
|
|
|
|
.appWidgetBackgroundRadius()
|