Skip to content

Instantly share code, notes, and snippets.

@KatieBarnett
Created November 18, 2024 10:37
Show Gist options
  • Save KatieBarnett/ad15425caaedea9729b377ec2fc9238a to your computer and use it in GitHub Desktop.
Save KatieBarnett/ad15425caaedea9729b377ec2fc9238a to your computer and use it in GitHub Desktop.
Custom GlanceStateDefinition - Custom GlanceStateDefinition fetching the state
class QuoteWidget : GlanceAppWidget(errorUiLayout = R.layout.widget_error_layout) {
override val stateDefinition = QuoteWidgetStateDefinition
override suspend fun provideGlance(context: Context, id: GlanceId) {
provideContent {
// Fetch the state
val widgetState = currentState<WidgetState>()
when (widgetState) {
is WidgetState.Available -> QuoteWidgetContent(
displayText = widgetState.quote.text,
topic = widgetState.topicName,
)
WidgetState.Loading -> QuoteWidgetLoading()
is WidgetState.Unavailable -> QuoteWidgetError(
message = widgetState.message,
)
}
// Use the state
...
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment