Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package it.niedermann.owncloud.notes.widget.notelist

import android.appwidget.AppWidgetManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Bundle
Expand All @@ -19,12 +20,14 @@ import androidx.core.net.toUri
import com.nextcloud.android.common.ui.util.PlatformThemeUtil
import it.niedermann.owncloud.notes.R
import it.niedermann.owncloud.notes.edit.EditNoteActivity
import it.niedermann.owncloud.notes.main.MainActivity
import it.niedermann.owncloud.notes.persistence.NotesRepository
import it.niedermann.owncloud.notes.persistence.entity.Account
import it.niedermann.owncloud.notes.persistence.entity.Note
import it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData
import it.niedermann.owncloud.notes.shared.model.ENavigationCategoryType
import it.niedermann.owncloud.notes.shared.model.NavigationCategory
import it.niedermann.owncloud.notes.shared.util.NotesColorUtil

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

override fun getCount(): Int {
return dbNotes.size
return dbNotes.size + 1
}

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

override fun getViewAt(position: Int): RemoteViews? {
val note = dbNotes.getOrNull(position) ?: return null
private fun getOpenAppIntent(): Intent {
return Intent(Intent.ACTION_MAIN).setComponent(
ComponentName(context.packageName, MainActivity::class.java.name)
)
}

val openNoteIntent = getOpenNoteIntent(note)
private fun getHeaderRemoteView(widgetData: NotesListWidgetData): RemoteViews {
val localAccount = repo.getAccountById(widgetData.accountId)
val createNoteIntent = getCreateNoteIntent(localAccount)

return RemoteViews(context.packageName, R.layout.widget_entry_add).apply {
setOnClickFillInIntent(R.id.widget_entry_content_tv, getOpenAppIntent())
setOnClickFillInIntent(R.id.widget_entry_fav_icon, createNoteIntent)
setTextViewText(
R.id.widget_entry_content_tv,
getCategoryTitle(context, widgetData.mode, widgetData.category)
)
setImageViewResource(R.id.widget_entry_fav_icon, R.drawable.ic_add_blue_24dp)
setInt(
R.id.widget_entry_fav_icon,
"setColorFilter",
if (NotesColorUtil.contrastRatioIsSufficient(
ContextCompat.getColor(context, R.color.widget_background),
localAccount.color
)
) localAccount.color
else ContextCompat.getColor(context, R.color.widget_foreground)
)
}
}

var createNoteIntent: Intent? = null
data?.let {
val localAccount = repo.getAccountById(it.accountId)
createNoteIntent = getCreateNoteIntent(localAccount)
override fun getViewAt(position: Int): RemoteViews? {
if (position == 0) {
val widgetData = data ?: return null
return getHeaderRemoteView(widgetData)
}

val note = dbNotes.getOrNull(position - 1) ?: return null

val openNoteIntent = getOpenNoteIntent(note)

return RemoteViews(context.packageName, R.layout.widget_entry).apply {
setOnClickFillInIntent(R.id.widget_note_list_entry, openNoteIntent)

createNoteIntent?.let {
setOnClickFillInIntent(R.id.widget_entry_fav_icon, createNoteIntent)
}

setTextViewText(R.id.widget_entry_title, note.title)

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

override fun getItemId(position: Int): Long {
return dbNotes[position].id
if (position == 0) {
return -1
}
val note = dbNotes.getOrNull(position - 1) ?: return -2
return note.id
}

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

companion object {
private val TAG: String = NoteListWidgetFactory::class.java.getSimpleName()

private fun getCategoryTitle(context: Context, displayMode: Int, category: String?): String {
return when (displayMode) {
NotesListWidgetData.MODE_DISPLAY_STARRED -> context.getString(R.string.label_favorites)
NotesListWidgetData.MODE_DISPLAY_CATEGORY -> if ("" == category)
context.getString(R.string.action_uncategorized)
else
category

else -> context.getString(R.string.app_name)
} ?: context.getString(R.string.app_name)
}
}
}
14 changes: 5 additions & 9 deletions app/src/main/res/layout/widget_entry_add.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
android:id="@+id/widget_note_list_entry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:orientation="horizontal"
android:paddingStart="@dimen/widget_note_list_outer_padding">

<TextView
android:id="@+id/widget_entry_content_tv"
Expand All @@ -20,9 +21,7 @@
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:ellipsize="middle"
android:paddingStart="30dp"
android:paddingEnd="@dimen/widget_note_list_outer_padding"
android:paddingBottom="@dimen/spacer_1x"
android:singleLine="true"
android:textColor="@color/widget_foreground"
android:textSize="16sp"
Expand All @@ -32,12 +31,9 @@

<ImageView
android:id="@+id/widget_entry_fav_icon"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_width="@dimen/widget_note_list_fav_icon_width"
android:layout_height="@dimen/widget_note_list_fav_icon_height"
android:layout_gravity="center_vertical"
android:contentDescription="@string/widget_entry_fav_contentDescription"
android:foregroundGravity="center_vertical"
android:paddingHorizontal="@dimen/spacer_1x"
android:paddingBottom="@dimen/spacer_1x"
android:contentDescription="@string/widget_create_note"
app:srcCompat="@drawable/ic_add_blue_24dp" />
</LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@
<string name="widget_single_note_note_not_found">Note not found</string>
<string name="widget_not_logged_in">Please login to Notes before using this widget</string>
<string name="widget_entry_fav_contentDescription">Star icon is used to denote an item as a favorite</string>
<string name="widget_create_note">Create Note</string>

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

Expand Down