fix uv index widgets not working

This commit is contained in:
PranshulGG committed 2026-08-26 18:19:39 +05:30
1 parent 0a26f51cc1
commit 057b171dc5
3 files changed
+97 -107

No files matched your search

@@ -7,7 +7,6 @@ import androidx.compose.ui.graphics.Color
import androidx.glance.ColorFilter
import androidx.glance.GlanceId
import androidx.glance.GlanceModifier
import androidx.glance.GlanceTheme
import androidx.glance.ImageProvider
import androidx.glance.action.actionStartActivity
import androidx.glance.action.clickable
@@ -28,9 +27,7 @@ import com.pranshulgg.weather_master_app.widgets.WeatherWidgetStateDefinition
import com.pranshulgg.weather_master_app.widgets.WeatherWidgetStateJson
import com.pranshulgg.weather_master_app.widgets.model.WidgetVariant
import com.pranshulgg.weather_master_app.widgets.model.WidgetWeather
import com.pranshulgg.weather_master_app.widgets.pill.ui.WeatherWidgetPill
import com.pranshulgg.weather_master_app.widgets.ui.colors.WidgetColors
import com.pranshulgg.weather_master_app.widgets.ui.colors.WidgetTheme
import com.pranshulgg.weather_master_app.widgets.ui.ReloadButton
import com.pranshulgg.weather_master_app.widgets.uvindex.ui.variants.UvIndexWidgetCompact
import com.pranshulgg.weather_master_app.widgets.uvindex.ui.variants.UvIndexWidgetLarge
import kotlinx.serialization.json.Json
@@ -41,6 +38,7 @@ class UvIndexWidget : GlanceAppWidget() {
override val stateDefinition =
WeatherWidgetStateDefinition
override suspend fun provideGlance(
context: Context,
id: GlanceId
@@ -56,24 +54,26 @@ class UvIndexWidget : GlanceAppWidget() {
Json.decodeFromString<WidgetWeather>(it)
}
val config = widgetState.config
val widgetColors = WidgetColors()
if (state != null) {
Box(
GlanceModifier.fillMaxSize()
.clickable(actionStartActivity<MainActivity>())
) {
when (config.variant) {
WidgetVariant.LARGE -> UvIndexWidgetLarge(
state,
GlanceModifier.appWidgetBackgroundShape(getUvIndex(state.uvIndex ?: 0))
)
Box(
GlanceModifier.fillMaxSize()
.clickable(actionStartActivity<MainActivity>())
) {
when (config.variant) {
WidgetVariant.LARGE -> UvIndexWidgetLarge(
state,
GlanceModifier.appWidgetBackgroundShape(getUvIndex(state?.uvIndex ?: 0))
)
else -> UvIndexWidgetCompact(
state,
GlanceModifier.appWidgetBackgroundShape(getUvIndex(state?.uvIndex ?: 0))
)
else -> UvIndexWidgetCompact(
state,
GlanceModifier.appWidgetBackgroundShape(getUvIndex(state.uvIndex ?: 0))
)
}
}
} else {
ReloadButton()
}
}
}
@@ -26,52 +26,48 @@ import com.pranshulgg.weather_master_app.widgets.uvindex.getUvIndexTextColorForW
@Composable
fun UvIndexWidgetCompact(
state: WidgetWeather?,
state: WidgetWeather,
glanceModifier: GlanceModifier
) {
val context = LocalContext.current
if (state != null) {
val textColor = getUvIndexTextColorForWidget(getUvIndex(state.uvIndex ?: 0))
val textColorVariant = getUvIndexSecondaryTextColorForWidget(getUvIndex(state.uvIndex ?: 0))
val textColor = getUvIndexTextColorForWidget(getUvIndex(state.uvIndex ?: 0))
val textColorVariant = getUvIndexSecondaryTextColorForWidget(getUvIndex(state.uvIndex ?: 0))
Column(
modifier = glanceModifier.fillMaxSize().padding(horizontal = 16.dp),
verticalAlignment = Alignment.CenterVertically
) {
Column(
modifier = glanceModifier.fillMaxSize().padding(horizontal = 16.dp),
verticalAlignment = Alignment.CenterVertically
) {
Text(
context.getString(R.string.weather_current),
style = TextStyle(
color = textColorVariant,
fontSize = 18.sp,
),
)
Row(verticalAlignment = Alignment.Bottom) {
Text(
stringResource(R.string.weather_current),
"${state.uvIndex ?: "No data"}",
style = TextStyle(
color = textColorVariant,
color = textColor,
fontSize = 32.sp,
fontWeight = FontWeight.Bold
),
)
Spacer(GlanceModifier.width(5.dp))
Text(
state.uvIndex?.let {
getUvIndex(it).toLabel(context)
} ?: "No data",
style = TextStyle(
color = textColor,
fontSize = 18.sp,
fontWeight = FontWeight.Medium
),
)
Row(verticalAlignment = Alignment.Bottom) {
Text(
"${state.uvIndex ?: "No data"}",
style = TextStyle(
color = textColor,
fontSize = 32.sp,
fontWeight = FontWeight.Bold
),
)
Spacer(GlanceModifier.width(5.dp))
Text(
state.uvIndex?.let {
getUvIndex(it).toLabel(context)
} ?: "No data",
style = TextStyle(
color = textColor,
fontSize = 18.sp,
fontWeight = FontWeight.Medium
),
)
}
}
} else {
ReloadButton()
}
}
@@ -24,68 +24,62 @@ import com.pranshulgg.weather_master_app.widgets.uvindex.getUvIndexTextColorForW
@Composable
fun UvIndexWidgetLarge(
state: WidgetWeather?,
state: WidgetWeather,
glanceModifier: GlanceModifier
) {
val context = LocalContext.current
if (state != null) {
val textColor = getUvIndexTextColorForWidget(getUvIndex(state.uvIndex ?: 0))
val textColorVariant = getUvIndexSecondaryTextColorForWidget(getUvIndex(state.uvIndex ?: 0))
val textColor = getUvIndexTextColorForWidget(getUvIndex(state.uvIndex ?: 0))
val textColorVariant = getUvIndexSecondaryTextColorForWidget(getUvIndex(state.uvIndex ?: 0))
Column(modifier = glanceModifier.fillMaxSize().padding(16.dp)) {
Text(
stringResource(R.string.weather_current),
style = TextStyle(
color = textColorVariant,
fontSize = 18.sp,
),
)
Text(
state.uvIndex?.let {
getUvIndex(it).toLabel(context)
} ?: "No data",
style = TextStyle(
color = textColor,
fontSize = 22.sp,
fontWeight = FontWeight.Medium
),
)
Text(
"${state.uvIndex ?: "No data"}",
style = TextStyle(
color = textColor,
fontSize = 48.sp,
fontWeight = FontWeight.Bold
),
)
Spacer(GlanceModifier.defaultWeight())
Text(
stringResource(
R.string.widget_weather_today_max_uv,
"${state.daily[0].maxUvIndex}"
),
style = TextStyle(
color = textColorVariant,
fontSize = 20.sp,
),
)
Text(
stringResource(
R.string.widget_weather_today_max_uv_at,
"${state.daily[0].maxUvIndexAt}"
),
style = TextStyle(
color = textColorVariant,
fontSize = 18.sp,
),
)
}
} else {
ReloadButton()
Column(modifier = glanceModifier.fillMaxSize().padding(16.dp)) {
Text(
context.getString(R.string.weather_current),
style = TextStyle(
color = textColorVariant,
fontSize = 18.sp,
),
)
Text(
state.uvIndex?.let {
getUvIndex(it).toLabel(context)
} ?: "No data",
style = TextStyle(
color = textColor,
fontSize = 22.sp,
fontWeight = FontWeight.Medium
),
)
Text(
"${state.uvIndex ?: "No data"}",
style = TextStyle(
color = textColor,
fontSize = 48.sp,
fontWeight = FontWeight.Bold
),
)
Spacer(GlanceModifier.defaultWeight())
Text(
context.getString(R.string.widget_weather_today_max_uv, "${state.daily[0].maxUvIndex}"),
style = TextStyle(
color = textColorVariant,
fontSize = 20.sp,
),
)
Text(
context.getString(
R.string.widget_weather_today_max_uv_at,
"${state.daily[0].maxUvIndexAt}"
),
style = TextStyle(
color = textColorVariant,
fontSize = 18.sp,
),
)
}
}