@@ -42,26 +42,37 @@ import androidx.glance.layout.Spacer
4242import androidx.glance.layout.fillMaxSize
4343import androidx.glance.layout.size
4444import androidx.glance.layout.wrapContentSize
45+ import kotlinx.coroutines.CoroutineScope
46+ import kotlinx.coroutines.Dispatchers
47+ import kotlinx.coroutines.launch
4548
4649private object SizesPreview {
4750 val medium = 56 .dp
4851}
4952
50- suspend fun updateWidgetPreview (context : Context ) {
53+ /* *
54+ * This is a convenience function for updating the widget preview using Generated Previews.
55+ *
56+ * In a real application, this would be called whenever the widget's state changes.
57+ */
58+ fun updateWidgetPreview (context : Context ) {
59+
5160 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .VANILLA_ICE_CREAM ) {
52- try {
53- val appwidgetManager = AppWidgetManager .getInstance(context)
61+ CoroutineScope (Dispatchers .IO ).launch {
62+ try {
63+ val appwidgetManager = AppWidgetManager .getInstance(context)
5464
55- appwidgetManager.setWidgetPreview(
56- ComponentName (context, JetcasterAppWidgetReceiver ::class .java),
57- AppWidgetProviderInfo .WIDGET_CATEGORY_HOME_SCREEN ,
58- JetcasterAppWidgetPreview ().compose(
59- context,
60- size = DpSize (160 .dp, 64 .dp)
61- ),
62- )
63- } catch (e: Exception ) {
64- Log .e(TAG , e.message, e)
65+ appwidgetManager.setWidgetPreview(
66+ ComponentName (context, JetcasterAppWidgetReceiver ::class .java),
67+ AppWidgetProviderInfo .WIDGET_CATEGORY_HOME_SCREEN ,
68+ JetcasterAppWidgetPreview ().compose(
69+ context,
70+ size = DpSize (160 .dp, 64 .dp)
71+ ),
72+ )
73+ } catch (e: Exception ) {
74+ Log .e(TAG , e.message, e)
75+ }
6576 }
6677 }
6778}
0 commit comments