Skip to content

Commit 0581b56

Browse files
committed
animation + reparsing
1 parent 4a9d9c0 commit 0581b56

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

mobile/src/main/java/net/activitywatch/android/widget/CategoryTimeWidgetProvider.kt

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import android.content.ComponentName
77
import android.content.Context
88
import android.content.Intent
99
import android.util.Log
10+
import android.view.View
1011
import android.widget.RemoteViews
1112
import net.activitywatch.android.R
13+
import net.activitywatch.android.watcher.UsageStatsWatcher
1214

1315
private const val TAG = "CategoryTimeWidget"
1416
private 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
}

mobile/src/main/res/layout/widget_category_time.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,21 @@
214214

215215
<ImageButton
216216
android:id="@+id/widget_refresh_button"
217-
android:layout_width="24dp"
218-
android:layout_height="24dp"
217+
android:layout_width="22dp"
218+
android:layout_height="22dp"
219219
android:layout_marginStart="8dp"
220220
android:src="@drawable/ic_refresh"
221221
android:background="@android:color/transparent"
222222
android:contentDescription="Refresh widget" />
223+
224+
<ProgressBar
225+
android:id="@+id/widget_loading_indicator"
226+
android:layout_width="20dp"
227+
android:layout_height="20dp"
228+
android:layout_marginStart="8dp"
229+
android:visibility="gone"
230+
android:indeterminate="true"
231+
style="?android:attr/progressBarStyleSmall" />
223232
</LinearLayout>
224233

225234
</LinearLayout>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
33
android:minWidth="250dp"
4-
android:minHeight="180dp"
4+
android:minHeight="40dp"
55
android:updatePeriodMillis="1800000"
66
android:initialLayout="@layout/widget_category_time"
77
android:resizeMode="horizontal|vertical"
88
android:widgetCategory="home_screen"
9-
android:description="@string/widget_category_time_description" />
9+
android:description="@string/widget_category_time_description" />

0 commit comments

Comments
 (0)