2023-01-26 23:53:24 +01:00
|
|
|
package tachiyomi.presentation.widget
|
2022-07-31 17:31:40 +02:00
|
|
|
|
|
|
|
import android.app.Application
|
2023-05-28 04:59:21 +02:00
|
|
|
import android.content.Context
|
2022-07-31 17:31:40 +02:00
|
|
|
import android.graphics.Bitmap
|
|
|
|
import android.os.Build
|
|
|
|
import androidx.core.graphics.drawable.toBitmap
|
2023-05-28 04:59:21 +02:00
|
|
|
import androidx.glance.GlanceId
|
2022-07-31 17:31:40 +02:00
|
|
|
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
|
2023-05-28 04:59:21 +02:00
|
|
|
import androidx.glance.appwidget.provideContent
|
2022-07-31 17:31:40 +02:00
|
|
|
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
|
2023-05-28 04:59:21 +02:00
|
|
|
import tachiyomi.core.util.lang.withIOContext
|
2023-01-22 16:37:13 +01:00
|
|
|
import tachiyomi.domain.manga.model.MangaCover
|
2023-02-21 01:02:38 +01:00
|
|
|
import tachiyomi.domain.updates.interactor.GetUpdates
|
|
|
|
import tachiyomi.domain.updates.model.UpdatesWithRelations
|
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-02-21 21:11:34 +01:00
|
|
|
import tachiyomi.presentation.widget.util.appWidgetBackgroundRadius
|
|
|
|
import tachiyomi.presentation.widget.util.calculateRowAndColumnCount
|
2022-07-31 17:31:40 +02:00
|
|
|
import uy.kohesive.injekt.Injekt
|
|
|
|
import uy.kohesive.injekt.api.get
|
|
|
|
import java.util.Calendar
|
|
|
|
import java.util.Date
|
|
|
|
|
2023-08-28 04:05:52 +02:00
|
|
|
class UpdatesGridGlanceWidget(
|
|
|
|
private val context: Context = Injekt.get<Application>(),
|
|
|
|
private val getUpdates: GetUpdates = Injekt.get(),
|
|
|
|
private val preferences: SecurityPreferences = Injekt.get(),
|
|
|
|
) : GlanceAppWidget() {
|
2022-07-31 17:31:40 +02:00
|
|
|
|
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
|
|
|
|
|
2023-05-28 04:59:21 +02:00
|
|
|
override suspend fun provideGlance(context: Context, id: GlanceId) {
|
|
|
|
val locked = preferences.useAuthenticator().get()
|
|
|
|
if (!locked) loadData()
|
2022-07-31 17:31:40 +02:00
|
|
|
|
2023-05-28 04:59:21 +02:00
|
|
|
provideContent {
|
|
|
|
// If app lock enabled, don't do anything
|
|
|
|
if (locked) {
|
|
|
|
LockedWidget()
|
|
|
|
return@provideContent
|
2022-07-31 17:31:40 +02:00
|
|
|
}
|
2023-05-28 04:59:21 +02:00
|
|
|
UpdatesWidget(data)
|
|
|
|
}
|
|
|
|
}
|
2022-07-31 17:31:40 +02:00
|
|
|
|
2023-08-28 04:05:52 +02:00
|
|
|
private suspend fun loadData() {
|
|
|
|
val manager = GlanceAppWidgetManager(context)
|
|
|
|
val ids = manager.getGlanceIds(this@UpdatesGridGlanceWidget::class.java)
|
|
|
|
if (ids.isEmpty()) return
|
2022-07-31 17:31:40 +02:00
|
|
|
|
2023-08-28 04:05:52 +02:00
|
|
|
withIOContext {
|
|
|
|
val updates = getUpdates.await(
|
|
|
|
read = false,
|
|
|
|
after = DateLimit.timeInMillis,
|
|
|
|
)
|
2022-07-31 17:31:40 +02:00
|
|
|
val (rowCount, columnCount) = ids
|
|
|
|
.flatMap { manager.getAppWidgetSizes(it) }
|
|
|
|
.maxBy { it.height.value * it.width.value }
|
|
|
|
.calculateRowAndColumnCount()
|
|
|
|
|
2023-08-28 04:05:52 +02:00
|
|
|
data = prepareList(updates, rowCount * columnCount)
|
2022-07-31 17:31:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 01:02:38 +01:00
|
|
|
private fun prepareList(processList: List<UpdatesWithRelations>, take: Int): List<Pair<Long, Bitmap?>> {
|
2022-07-31 17:31:40 +02:00
|
|
|
// Resize to cover size
|
|
|
|
val widthPx = CoverWidth.value.toInt().dpToPx
|
|
|
|
val heightPx = CoverHeight.value.toInt().dpToPx
|
2023-08-28 04:05:52 +02:00
|
|
|
val roundPx = context.resources.getDimension(R.dimen.appwidget_inner_radius)
|
2022-07-31 17:31:40 +02:00
|
|
|
return processList
|
|
|
|
.distinctBy { it.mangaId }
|
|
|
|
.take(take)
|
|
|
|
.map { updatesView ->
|
2023-08-28 04:05:52 +02:00
|
|
|
val request = ImageRequest.Builder(context)
|
2022-07-31 17:31:40 +02:00
|
|
|
.data(
|
|
|
|
MangaCover(
|
|
|
|
mangaId = updatesView.mangaId,
|
2023-02-21 01:02:38 +01:00
|
|
|
sourceId = updatesView.sourceId,
|
|
|
|
isMangaFavorite = true,
|
|
|
|
url = updatesView.coverData.url,
|
|
|
|
lastModified = updatesView.coverData.lastModified,
|
2022-07-31 17:31:40 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.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()
|
2023-08-28 04:05:52 +02:00
|
|
|
Pair(updatesView.mangaId, context.imageLoader.executeBlocking(request).drawable?.toBitmap())
|
2022-07-31 17:31:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|