Skip to content

Commit e5a4dab

Browse files
committed
fix: show correct placeholder states in single-note widget
Three distinct states are now handled correctly: - Startup / system update: the widget shows "Loading note…" while the RemoteViewsAdapter connects. updateAppWidget() sets the placeholder visible with that text on every full reset (ACTION_APPWIDGET_UPDATE). - Note refresh (edit or sync): updateSingleNoteWidgets() now sends a custom ACTION_DATA_CHANGED instead of ACTION_APPWIDGET_UPDATE. onReceive() handles this with notifyAppWidgetViewDataChanged() only, leaving the existing RemoteViews intact so the note content updates in place with no blank gap or flash. - Note missing: once onDataSetChanged() confirms getNoteById() returns null, partiallyUpdateAppWidget() sets the placeholder visible with "Note not found". When the note is found the placeholder is hidden via the same mechanism. Fixes #1611 AI-assisted: Claude Code (Sonnet 4.6) Signed-off-by: Jan C. Borchardt <925062+jancborchardt@users.noreply.github.com>
1 parent a44c508 commit e5a4dab

4 files changed

Lines changed: 27 additions & 11 deletions

File tree

app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidget.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import android.appwidget.AppWidgetProvider
1212
import android.content.ComponentName
1313
import android.content.Context
1414
import android.content.Intent
15+
import android.view.View
1516
import android.widget.RemoteViews
1617
import androidx.core.net.toUri
1718
import it.niedermann.owncloud.notes.R
@@ -35,10 +36,16 @@ class SingleNoteWidget : AppWidgetProvider() {
3536
override fun onReceive(context: Context, intent: Intent?) {
3637
super.onReceive(context, intent)
3738
val awm = AppWidgetManager.getInstance(context)
38-
3939
val provider = ComponentName(context, SingleNoteWidget::class.java)
4040
val appWidgetIds = awm.getAppWidgetIds(provider)
41-
updateAppWidget(context, awm, appWidgetIds)
41+
42+
if (intent?.action == ACTION_DATA_CHANGED) {
43+
appWidgetIds.forEach { appWidgetId ->
44+
awm.notifyAppWidgetViewDataChanged(appWidgetId, R.id.single_note_widget_lv)
45+
}
46+
} else {
47+
updateAppWidget(context, awm, appWidgetIds)
48+
}
4249
}
4350

4451
override fun onDeleted(context: Context, appWidgetIds: IntArray) {
@@ -104,19 +111,19 @@ class SingleNoteWidget : AppWidgetProvider() {
104111
R.layout.widget_single_note
105112
).apply {
106113
setPendingIntentTemplate(R.id.single_note_widget_lv, pendingIntent)
107-
setEmptyView(
108-
R.id.single_note_widget_lv,
109-
R.id.widget_single_note_placeholder_tv
110-
)
111114
setRemoteAdapter(R.id.single_note_widget_lv, serviceIntent)
115+
setViewVisibility(R.id.widget_single_note_placeholder_tv, View.VISIBLE)
116+
setTextViewText(R.id.widget_single_note_placeholder_tv, context.getString(R.string.widget_single_note_loading))
112117
}
113118
}
114119

115120
companion object {
121+
private const val ACTION_DATA_CHANGED = "it.niedermann.owncloud.notes.ACTION_WIDGET_DATA_CHANGED"
122+
116123
@JvmStatic
117124
fun updateSingleNoteWidgets(context: Context) {
118125
val intent = Intent(context, SingleNoteWidget::class.java).apply {
119-
setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE)
126+
action = ACTION_DATA_CHANGED
120127
}
121128
context.sendBroadcast(intent)
122129
}

app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidgetFactory.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.content.Context;
1111
import android.content.Intent;
1212
import android.util.Log;
13+
import android.view.View;
1314
import android.widget.RemoteViews;
1415
import android.widget.RemoteViewsService;
1516

@@ -53,9 +54,16 @@ public void onDataSetChanged() {
5354
Log.v(TAG, "Fetch note with id " + noteId);
5455
note = repo.getNoteById(noteId);
5556

57+
final var views = new RemoteViews(context.getPackageName(), R.layout.widget_single_note);
5658
if (note == null) {
5759
Log.e(TAG, "Error: note not found");
60+
views.setViewVisibility(R.id.widget_single_note_placeholder_tv, View.VISIBLE);
61+
views.setTextViewText(R.id.widget_single_note_placeholder_tv,
62+
context.getString(R.string.widget_single_note_note_not_found));
63+
} else {
64+
views.setViewVisibility(R.id.widget_single_note_placeholder_tv, View.GONE);
5865
}
66+
AppWidgetManager.getInstance(context).partiallyUpdateAppWidget(appWidgetId, views);
5967
} else {
6068
Log.w(TAG, "Widget with ID " + appWidgetId + " seems to be not configured yet.");
6169
}
@@ -110,7 +118,6 @@ public RemoteViews getViewAt(int position) {
110118
}
111119

112120

113-
// TODO Set loading view
114121
@Override
115122
public RemoteViews getLoadingView() {
116123
return null;

app/src/main/res/layout/widget_single_note.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
android:layout_height="match_parent"
2626
android:gravity="center"
2727
android:padding="@dimen/spacer_1x"
28-
android:text="@string/widget_single_note_placeholder_tv"
28+
android:text="@string/widget_single_note_note_not_found"
2929
android:textAlignment="center"
30-
android:textColor="@color/widget_foreground" />
30+
android:textColor="@color/widget_foreground"
31+
android:visibility="gone" />
3132

3233
</RelativeLayout>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@
241241
<string name="widget_note_list_title">Note list</string>
242242
<string name="widget_note_list_placeholder">No notes</string>
243243
<string name="widget_single_note_title">Single note</string>
244-
<string name="widget_single_note_placeholder_tv">Note not found</string>
244+
<string name="widget_single_note_loading">Loading note…</string>
245+
<string name="widget_single_note_note_not_found">Note not found</string>
245246
<string name="widget_not_logged_in">Please login to Notes before using this widget</string>
246247
<string name="widget_entry_fav_contentDescription">Star icon is used to denote an item as a favorite</string>
247248

0 commit comments

Comments
 (0)