Remove error message on null template (#2735)

* Remove error message on null template

 - Null is a valid value for a template so the app should reflect that instead of suggesting there is an error, which may cause users to start looking in the wrong place.

* Log.e instead of Log.i
This commit is contained in:
Joris Pelgröm 2022-08-03 23:59:00 +02:00 committed by GitHub
parent 7f1677a2dd
commit 9fc98e464e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 21 deletions

View File

@ -8,6 +8,7 @@ import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.fasterxml.jackson.databind.JsonMappingException
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.google.android.gms.wearable.CapabilityClient
@ -112,12 +113,13 @@ class SettingsWearViewModel @Inject constructor(
viewModelScope.launch {
try {
templateTileContentRendered.value =
integrationUseCase.renderTemplate(template, mapOf()) ?: getApplication<Application>().getString(
commonR.string.template_error
)
integrationUseCase.renderTemplate(template, mapOf()).toString()
} catch (e: Exception) {
Log.e(TAG, "Exception while rendering template", e)
// JsonMappingException suggests that template is not a String (= error)
templateTileContentRendered.value = getApplication<Application>().getString(
commonR.string.template_render_error
if (e.cause is JsonMappingException) commonR.string.template_error
else commonR.string.template_render_error
)
}
}

View File

@ -91,17 +91,12 @@ class TemplateWidget : BaseWidgetProvider() {
// Content
var renderedTemplate: String? = templateWidgetDao.get(appWidgetId)?.lastUpdate ?: "Loading"
try {
renderedTemplate = integrationUseCase.renderTemplate(widget.template, mapOf())
if (renderedTemplate != null) {
templateWidgetDao.updateTemplateWidgetLastUpdate(
appWidgetId,
renderedTemplate
)
setViewVisibility(R.id.widgetTemplateError, View.GONE)
} else {
Log.e(TAG, "Template returned null: ${widget.template}")
setViewVisibility(R.id.widgetTemplateError, View.VISIBLE)
}
renderedTemplate = integrationUseCase.renderTemplate(widget.template, mapOf()).toString()
templateWidgetDao.updateTemplateWidgetLastUpdate(
appWidgetId,
renderedTemplate
)
setViewVisibility(R.id.widgetTemplateError, View.GONE)
} catch (e: Exception) {
Log.e(TAG, "Unable to render template: ${widget.template}", e)
setViewVisibility(R.id.widgetTemplateError, View.VISIBLE)

View File

@ -7,6 +7,7 @@ import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.text.Html.fromHtml
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
@ -16,6 +17,7 @@ import androidx.core.graphics.toColorInt
import androidx.core.view.isVisible
import androidx.core.widget.doAfterTextChanged
import androidx.lifecycle.lifecycleScope
import com.fasterxml.jackson.databind.JsonMappingException
import com.google.android.material.color.DynamicColors
import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.common.data.integration.IntegrationRepository
@ -213,10 +215,15 @@ class TemplateWidgetConfigureActivity : BaseWidgetConfigureActivity() {
var enabled: Boolean
withContext(Dispatchers.IO) {
try {
templateText = integrationUseCase.renderTemplate(template, mapOf()) ?: getString(commonR.string.template_error)
templateText = integrationUseCase.renderTemplate(template, mapOf()).toString()
enabled = true
} catch (e: Exception) {
templateText = getString(commonR.string.template_render_error)
Log.e(TAG, "Exception while rendering template", e)
// JsonMappingException suggests that template is not a String (= error)
templateText = getString(
if (e.cause is JsonMappingException) commonR.string.template_error
else commonR.string.template_render_error
)
enabled = false
}
}

View File

@ -11,6 +11,7 @@ import android.text.style.ForegroundColorSpan
import android.text.style.RelativeSizeSpan
import android.text.style.StyleSpan
import android.text.style.UnderlineSpan
import android.util.Log
import androidx.core.content.getSystemService
import androidx.core.text.HtmlCompat.FROM_HTML_MODE_LEGACY
import androidx.core.text.HtmlCompat.fromHtml
@ -32,6 +33,7 @@ import androidx.wear.tiles.TileBuilders.Tile
import androidx.wear.tiles.TileService
import androidx.wear.tiles.TimelineBuilders.Timeline
import androidx.wear.tiles.TimelineBuilders.TimelineEntry
import com.fasterxml.jackson.databind.JsonMappingException
import com.google.common.util.concurrent.ListenableFuture
import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.R
@ -69,11 +71,12 @@ class TemplateTile : TileService() {
val template = integrationUseCase.getTemplateTileContent()
val renderedText = try {
integrationUseCase.renderTemplate(template, mapOf()) ?: getString(
commonR.string.template_error
)
integrationUseCase.renderTemplate(template, mapOf()).toString()
} catch (e: Exception) {
getString(commonR.string.template_render_error)
Log.e("TemplateTile", "Exception while rendering template", e)
// JsonMappingException suggests that template is not a String (= error)
if (e.cause is JsonMappingException) getString(commonR.string.template_error)
else getString(commonR.string.template_render_error)
}
Tile.Builder()