Load dynamic calendar drawable with IO dispatcher

This commit is contained in:
MM20 2022-08-03 22:14:13 +02:00
parent b63ebb9e18
commit 54111a93be
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389

View File

@ -5,6 +5,8 @@ import android.graphics.drawable.AdaptiveIconDrawable
import androidx.core.content.res.ResourcesCompat import androidx.core.content.res.ResourcesCompat
import androidx.lifecycle.Transformations import androidx.lifecycle.Transformations
import de.mm20.launcher2.icons.transformations.LauncherIconTransformation import de.mm20.launcher2.icons.transformations.LauncherIconTransformation
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.time.Instant import java.time.Instant
import java.time.ZoneId import java.time.ZoneId
@ -19,7 +21,7 @@ internal class DynamicCalendarIcon(
if (resourceIds.size < 31) throw IllegalArgumentException("DynamicCalendarIcon resourceIds must at least have 31 items") if (resourceIds.size < 31) throw IllegalArgumentException("DynamicCalendarIcon resourceIds must at least have 31 items")
} }
override suspend fun getIcon(time: Long): StaticLauncherIcon { override suspend fun getIcon(time: Long): StaticLauncherIcon = withContext(Dispatchers.IO) {
val day = Instant.ofEpochMilli(time).atZone(ZoneId.systemDefault()).dayOfMonth val day = Instant.ofEpochMilli(time).atZone(ZoneId.systemDefault()).dayOfMonth
val resId = resourceIds[day - 1] val resId = resourceIds[day - 1]
@ -27,7 +29,7 @@ internal class DynamicCalendarIcon(
ResourcesCompat.getDrawable(resources, resId, null) ResourcesCompat.getDrawable(resources, resId, null)
} catch (e: Resources.NotFoundException) { } catch (e: Resources.NotFoundException) {
null null
} ?: return StaticLauncherIcon( } ?: return@withContext StaticLauncherIcon(
foregroundLayer = TextLayer(day.toString()), foregroundLayer = TextLayer(day.toString()),
backgroundLayer = ColorLayer() backgroundLayer = ColorLayer()
) )
@ -41,7 +43,7 @@ internal class DynamicCalendarIcon(
backgroundLayer = ColorLayer() backgroundLayer = ColorLayer()
) )
} else if (drawable is AdaptiveIconDrawable) { } else if (drawable is AdaptiveIconDrawable) {
return StaticLauncherIcon( return@withContext StaticLauncherIcon(
foregroundLayer = drawable.foreground?.let { foregroundLayer = drawable.foreground?.let {
StaticIconLayer( StaticIconLayer(
icon = it, icon = it,
@ -66,7 +68,7 @@ internal class DynamicCalendarIcon(
for (transformation in transformations) { for (transformation in transformations) {
icon = transformation.transform(icon) icon = transformation.transform(icon)
} }
return icon return@withContext icon
} }
override fun setTransformations(transformations: List<LauncherIconTransformation>) { override fun setTransformations(transformations: List<LauncherIconTransformation>) {