I have a widget that can render either one month or two months widget (based on size availability). On API 33, it allows me to resize, but on API 32, it does not render 1 month widget.
class ConsolidatorAppWidget: GlanceAppWidget() {
private val isDebugOverride = false // Set to true to have only one widget size to reduce logging clutter
/**
* Used to dynamically configure Widget as user is resizing it.
*/
override val sizeMode: SizeMode = SizeMode.Responsive(
if (isDebugOverride && isDebug()) setOf(ONE_MONTH_WIDGET) else setOf(ONE_MONTH_WIDGET, TWO_MONTH_WIDGET)
)
/**
* DataStore implementation is provided by Glance to communicate between app and widget.
*/
override val stateDefinition: GlanceStateDefinition<*>
get() = PreferencesGlanceStateDefinition
override suspend fun provideGlance(context: Context, id: GlanceId) {
provideContent {
val prefs = currentState<Preferences>()
val firstMonthState = prefs.getFirstMonth()
prefs.toMutablePreferences().setFirstMonth(firstMonthState)
val calendarEventsMap = prefs.getCalendarData().createMonthToDayEventsMap()
val size = LocalSize.current
calendarWidgetLog(
"Content: size: $size, calendarEventsMap with last entry ${
calendarEventsMap.entries.lastOrNull()?.value?.get(
0
)?.startDateTime
}"
)
when (size) {
ONE_MONTH_WIDGET -> CalendarAppWidgetUI(firstMonthState, 1, calendarEventsMap)
TWO_MONTH_WIDGET -> CalendarAppWidgetUI(firstMonthState, 2, calendarEventsMap)
}
}
}
companion object {
private val ONE_MONTH_WIDGET = DpSize(200.dp, 200.dp)
private val TWO_MONTH_WIDGET = DpSize(200.dp, 425.dp)
}
}