Skip to content

Commit 4984fca

Browse files
committed
feat(widget): restore create note button in note list widget header
Restore the widget header row that was removed in a78ee27: the first scrollable entry of the note list widget shows the widget context title (app name, favorites or category) with a create note button. - tapping the title opens the app - tapping the + button creates a new note in the widget category - the + icon is tinted with the account color when the contrast is sufficient - widget_entry_add.xml: cleaned up legacy paddings and hardcoded sizes, fixed content description of the create button - restore the widget_create_note string Click handling relies on the pending intent template being mutable, which is fixed in #3380. Merge that PR first (or together). AI-assistant: Cline Extension 1.x (GLM-5.3) Signed-off-by: Mark <mark-smirnow@yandex.ru>
1 parent a2262b3 commit 4984fca

3 files changed

Lines changed: 64 additions & 22 deletions

File tree

‎app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidgetFactory.kt‎

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package it.niedermann.owncloud.notes.widget.notelist
88

99
import android.appwidget.AppWidgetManager
10+
import android.content.ComponentName
1011
import android.content.Context
1112
import android.content.Intent
1213
import android.os.Bundle
@@ -19,12 +20,14 @@ import androidx.core.net.toUri
1920
import com.nextcloud.android.common.ui.util.PlatformThemeUtil
2021
import it.niedermann.owncloud.notes.R
2122
import it.niedermann.owncloud.notes.edit.EditNoteActivity
23+
import it.niedermann.owncloud.notes.main.MainActivity
2224
import it.niedermann.owncloud.notes.persistence.NotesRepository
2325
import it.niedermann.owncloud.notes.persistence.entity.Account
2426
import it.niedermann.owncloud.notes.persistence.entity.Note
2527
import it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData
2628
import it.niedermann.owncloud.notes.shared.model.ENavigationCategoryType
2729
import it.niedermann.owncloud.notes.shared.model.NavigationCategory
30+
import it.niedermann.owncloud.notes.shared.util.NotesColorUtil
2831

2932
class NoteListWidgetFactory internal constructor(private val context: Context, intent: Intent) :
3033
RemoteViewsFactory {
@@ -90,7 +93,7 @@ class NoteListWidgetFactory internal constructor(private val context: Context, i
9093
override fun onDestroy() = Unit
9194

9295
override fun getCount(): Int {
93-
return dbNotes.size
96+
return dbNotes.size + 1
9497
}
9598

9699
private fun getEditNoteIntent(bundle: Bundle): Intent {
@@ -125,24 +128,50 @@ class NoteListWidgetFactory internal constructor(private val context: Context, i
125128
return getEditNoteIntent(bundle)
126129
}
127130

128-
override fun getViewAt(position: Int): RemoteViews? {
129-
val note = dbNotes.getOrNull(position) ?: return null
131+
private fun getOpenAppIntent(): Intent {
132+
return Intent(Intent.ACTION_MAIN).setComponent(
133+
ComponentName(context.packageName, MainActivity::class.java.name)
134+
)
135+
}
130136

131-
val openNoteIntent = getOpenNoteIntent(note)
137+
private fun getHeaderRemoteView(widgetData: NotesListWidgetData): RemoteViews {
138+
val localAccount = repo.getAccountById(widgetData.accountId)
139+
val createNoteIntent = getCreateNoteIntent(localAccount)
140+
141+
return RemoteViews(context.packageName, R.layout.widget_entry_add).apply {
142+
setOnClickFillInIntent(R.id.widget_entry_content_tv, getOpenAppIntent())
143+
setOnClickFillInIntent(R.id.widget_entry_fav_icon, createNoteIntent)
144+
setTextViewText(
145+
R.id.widget_entry_content_tv,
146+
getCategoryTitle(context, widgetData.mode, widgetData.category)
147+
)
148+
setImageViewResource(R.id.widget_entry_fav_icon, R.drawable.ic_add_blue_24dp)
149+
setInt(
150+
R.id.widget_entry_fav_icon,
151+
"setColorFilter",
152+
if (NotesColorUtil.contrastRatioIsSufficient(
153+
ContextCompat.getColor(context, R.color.widget_background),
154+
localAccount.color
155+
)
156+
) localAccount.color
157+
else ContextCompat.getColor(context, R.color.widget_foreground)
158+
)
159+
}
160+
}
132161

133-
var createNoteIntent: Intent? = null
134-
data?.let {
135-
val localAccount = repo.getAccountById(it.accountId)
136-
createNoteIntent = getCreateNoteIntent(localAccount)
162+
override fun getViewAt(position: Int): RemoteViews? {
163+
if (position == 0) {
164+
val widgetData = data ?: return null
165+
return getHeaderRemoteView(widgetData)
137166
}
138167

168+
val note = dbNotes.getOrNull(position - 1) ?: return null
169+
170+
val openNoteIntent = getOpenNoteIntent(note)
171+
139172
return RemoteViews(context.packageName, R.layout.widget_entry).apply {
140173
setOnClickFillInIntent(R.id.widget_note_list_entry, openNoteIntent)
141174

142-
createNoteIntent?.let {
143-
setOnClickFillInIntent(R.id.widget_entry_fav_icon, createNoteIntent)
144-
}
145-
146175
setTextViewText(R.id.widget_entry_title, note.title)
147176

148177
if (note.category.isEmpty()) {
@@ -178,7 +207,11 @@ class NoteListWidgetFactory internal constructor(private val context: Context, i
178207
}
179208

180209
override fun getItemId(position: Int): Long {
181-
return dbNotes[position].id
210+
if (position == 0) {
211+
return -1
212+
}
213+
val note = dbNotes.getOrNull(position - 1) ?: return -2
214+
return note.id
182215
}
183216

184217
override fun hasStableIds(): Boolean {
@@ -187,5 +220,17 @@ class NoteListWidgetFactory internal constructor(private val context: Context, i
187220

188221
companion object {
189222
private val TAG: String = NoteListWidgetFactory::class.java.getSimpleName()
223+
224+
private fun getCategoryTitle(context: Context, displayMode: Int, category: String?): String {
225+
return when (displayMode) {
226+
NotesListWidgetData.MODE_DISPLAY_STARRED -> context.getString(R.string.label_favorites)
227+
NotesListWidgetData.MODE_DISPLAY_CATEGORY -> if ("" == category)
228+
context.getString(R.string.action_uncategorized)
229+
else
230+
category
231+
232+
else -> context.getString(R.string.app_name)
233+
} ?: context.getString(R.string.app_name)
234+
}
190235
}
191236
}

‎app/src/main/res/layout/widget_entry_add.xml‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
android:id="@+id/widget_note_list_entry"
1212
android:layout_width="match_parent"
1313
android:layout_height="wrap_content"
14-
android:orientation="horizontal">
14+
android:orientation="horizontal"
15+
android:paddingStart="@dimen/widget_note_list_outer_padding">
1516

1617
<TextView
1718
android:id="@+id/widget_entry_content_tv"
@@ -20,9 +21,7 @@
2021
android:layout_gravity="center_vertical"
2122
android:layout_weight="1"
2223
android:ellipsize="middle"
23-
android:paddingStart="30dp"
2424
android:paddingEnd="@dimen/widget_note_list_outer_padding"
25-
android:paddingBottom="@dimen/spacer_1x"
2625
android:singleLine="true"
2726
android:textColor="@color/widget_foreground"
2827
android:textSize="16sp"
@@ -32,12 +31,9 @@
3231

3332
<ImageView
3433
android:id="@+id/widget_entry_fav_icon"
35-
android:layout_width="40dp"
36-
android:layout_height="match_parent"
34+
android:layout_width="@dimen/widget_note_list_fav_icon_width"
35+
android:layout_height="@dimen/widget_note_list_fav_icon_height"
3736
android:layout_gravity="center_vertical"
38-
android:contentDescription="@string/widget_entry_fav_contentDescription"
39-
android:foregroundGravity="center_vertical"
40-
android:paddingHorizontal="@dimen/spacer_1x"
41-
android:paddingBottom="@dimen/spacer_1x"
37+
android:contentDescription="@string/widget_create_note"
4238
app:srcCompat="@drawable/ic_add_blue_24dp" />
4339
</LinearLayout>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@
245245
<string name="widget_single_note_note_not_found">Note not found</string>
246246
<string name="widget_not_logged_in">Please login to Notes before using this widget</string>
247247
<string name="widget_entry_fav_contentDescription">Star icon is used to denote an item as a favorite</string>
248+
<string name="widget_create_note">Create Note</string>
248249

249250
<string name="activity_select_single_note">Select note</string>
250251

0 commit comments

Comments
 (0)