@@ -7,8 +7,10 @@ import android.content.ComponentName
77import android.content.Context
88import android.content.Intent
99import android.util.Log
10+ import android.view.View
1011import android.widget.RemoteViews
1112import net.activitywatch.android.R
13+ import net.activitywatch.android.watcher.UsageStatsWatcher
1214
1315private const val TAG = " CategoryTimeWidget"
1416private const val ACTION_REFRESH = " net.activitywatch.android.widget.ACTION_REFRESH"
@@ -36,12 +38,27 @@ class CategoryTimeWidgetProvider : AppWidgetProvider() {
3638 super .onReceive(context, intent)
3739
3840 if (intent.action == ACTION_REFRESH ) {
39- Log .d(TAG , " Refresh button clicked - updating all widgets" )
41+ Log .d(TAG , " Refresh button clicked - re-parsing usage events and updating widgets" )
4042
4143 val appWidgetManager = AppWidgetManager .getInstance(context)
4244 val componentName = ComponentName (context, CategoryTimeWidgetProvider ::class .java)
4345 val appWidgetIds = appWidgetManager.getAppWidgetIds(componentName)
4446
47+ // Show loading indicator first
48+ for (appWidgetId in appWidgetIds) {
49+ showLoadingState(context, appWidgetManager, appWidgetId)
50+ }
51+
52+ // Re-parse usage events
53+ try {
54+ val usageStatsWatcher = UsageStatsWatcher (context)
55+ usageStatsWatcher.sendHeartbeats()
56+ Log .d(TAG , " Triggered usage events re-parsing" )
57+ } catch (e: Exception ) {
58+ Log .e(TAG , " Error re-parsing usage events" , e)
59+ }
60+
61+ // Then update with fresh data
4562 for (appWidgetId in appWidgetIds) {
4663 updateWidgetWithRefreshButton(context, appWidgetManager, appWidgetId)
4764 }
@@ -59,6 +76,21 @@ class CategoryTimeWidgetProvider : AppWidgetProvider() {
5976 }
6077
6178 companion object {
79+ /* *
80+ * Show loading state - hide refresh button and show spinner
81+ */
82+ private fun showLoadingState (
83+ context : Context ,
84+ appWidgetManager : AppWidgetManager ,
85+ appWidgetId : Int
86+ ) {
87+ val views = RemoteViews (context.packageName, R .layout.widget_category_time)
88+ views.setViewVisibility(R .id.widget_refresh_button, View .GONE )
89+ views.setViewVisibility(R .id.widget_loading_indicator, View .VISIBLE )
90+ appWidgetManager.updateAppWidget(appWidgetId, views)
91+ Log .d(TAG , " Showing loading indicator for widget $appWidgetId " )
92+ }
93+
6294 /* *
6395 * Update widget and set up the refresh button click handler
6496 */
@@ -70,9 +102,13 @@ class CategoryTimeWidgetProvider : AppWidgetProvider() {
70102 // First update the widget data
71103 CategoryTimeWidgetWorker .updateSingleWidget(context, appWidgetManager, appWidgetId)
72104
73- // Then set up the refresh button click handler
105+ // Then set up the refresh button click handler and hide loading
74106 val views = RemoteViews (context.packageName, R .layout.widget_category_time)
75107
108+ // Hide loading indicator and show refresh button
109+ views.setViewVisibility(R .id.widget_loading_indicator, View .GONE )
110+ views.setViewVisibility(R .id.widget_refresh_button, View .VISIBLE )
111+
76112 val refreshIntent = Intent (context, CategoryTimeWidgetProvider ::class .java).apply {
77113 action = ACTION_REFRESH
78114 }
0 commit comments