Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove error message on null template #2735

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
}
}
Expand Down
Expand Up @@ -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)
Expand Down
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
}
Expand Down
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.i("TemplateTile", "Exception while rendering template", e)
jpelgrom marked this conversation as resolved.
Show resolved Hide resolved
// 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()
Expand Down