Skip to content

Commit

Permalink
Update to avoid placeholder blinking
Browse files Browse the repository at this point in the history
  • Loading branch information
jpelgrom committed Dec 18, 2024
1 parent 88fc4d4 commit de18b86
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class CameraWidget : AppWidgetProvider() {
}
mainScope.launch {
val views = getWidgetRemoteViews(context, appWidgetId)
appWidgetManager.updateAppWidget(appWidgetId, views)
views?.let { appWidgetManager.updateAppWidget(appWidgetId, it) }
}
}

Expand Down Expand Up @@ -117,27 +117,30 @@ class CameraWidget : AppWidgetProvider() {
}
}

private suspend fun getWidgetRemoteViews(context: Context, appWidgetId: Int): RemoteViews {
private suspend fun getWidgetRemoteViews(context: Context, appWidgetId: Int): RemoteViews? {
val updateCameraIntent = Intent(context, CameraWidget::class.java).apply {
action = UPDATE_IMAGE
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
}

return RemoteViews(context.packageName, R.layout.widget_camera).apply {
val widget = cameraWidgetDao.get(appWidgetId)
if (widget != null) {
var entityPictureUrl: String?
try {
entityPictureUrl = retrieveCameraImageUrl(widget.serverId, widget.entityId)
setViewVisibility(R.id.widgetCameraError, View.GONE)
} catch (e: Exception) {
Log.e(TAG, "Failed to fetch entity or entity does not exist", e)
setViewVisibility(R.id.widgetCameraError, View.VISIBLE)
entityPictureUrl = null
}
val widget = cameraWidgetDao.get(appWidgetId)
var widgetCameraError = false
var url: String? = null
if (widget != null) {
try {
val entityPictureUrl = retrieveCameraImageUrl(widget.serverId, widget.entityId)
val baseUrl = serverManager.getServer(widget.serverId)?.connection?.getUrl().toString().removeSuffix("/")
val url = "$baseUrl$entityPictureUrl"
if (entityPictureUrl == null) {
url = "$baseUrl$entityPictureUrl"
} catch (e: Exception) {
Log.e(TAG, "Failed to fetch entity or entity does not exist", e)
widgetCameraError = true
}
}

val views = RemoteViews(context.packageName, R.layout.widget_camera).apply {
if (widget != null) {
setViewVisibility(R.id.widgetCameraError, if (widgetCameraError) View.VISIBLE else View.GONE)
if (url == null) {
setImageViewResource(
R.id.widgetCameraImage,
R.drawable.app_icon_round
Expand Down Expand Up @@ -197,6 +200,8 @@ class CameraWidget : AppWidgetProvider() {
setOnClickPendingIntent(R.id.widgetCameraPlaceholder, tapWidgetPendingIntent)
}
}
// If there is an url, Coil will call appWidgetManager.updateAppWidget
return if (url == null) views else null
}

private suspend fun retrieveCameraImageUrl(serverId: Int, entityId: String): String? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ class RemoteViewsTarget(
@IdRes private val imageViewResId: Int
) : Target {

override fun onStart(placeholder: Image?) = setDrawable(placeholder)
override fun onStart(placeholder: Image?) {
// Skip if null to avoid blinking (there is no placeholder)
placeholder?.let { setDrawable(it) }
}

override fun onError(error: Image?) = setDrawable(error)

Expand Down

0 comments on commit de18b86

Please sign in to comment.