fix #269, allow using different colors per widget

This commit is contained in:
tibbi 2019-08-19 23:24:47 +02:00
parent 93c6079fa9
commit 21a5c99315
3 changed files with 17 additions and 11 deletions

View File

@ -11,6 +11,7 @@ import com.google.gson.reflect.TypeToken
import com.simplemobiletools.commons.extensions.adjustAlpha import com.simplemobiletools.commons.extensions.adjustAlpha
import com.simplemobiletools.commons.extensions.setText import com.simplemobiletools.commons.extensions.setText
import com.simplemobiletools.commons.extensions.setTextSize import com.simplemobiletools.commons.extensions.setTextSize
import com.simplemobiletools.commons.helpers.WIDGET_TEXT_COLOR
import com.simplemobiletools.notes.pro.R import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.R.id.checklist_title import com.simplemobiletools.notes.pro.R.id.checklist_title
import com.simplemobiletools.notes.pro.R.id.widget_text_holder import com.simplemobiletools.notes.pro.R.id.widget_text_holder
@ -23,7 +24,7 @@ import com.simplemobiletools.notes.pro.models.Note
class WidgetAdapter(val context: Context, val intent: Intent) : RemoteViewsService.RemoteViewsFactory { class WidgetAdapter(val context: Context, val intent: Intent) : RemoteViewsService.RemoteViewsFactory {
private val textIds = arrayOf(R.id.widget_text_left, R.id.widget_text_center, R.id.widget_text_right) private val textIds = arrayOf(R.id.widget_text_left, R.id.widget_text_center, R.id.widget_text_right)
private var widgetTextColor = context.config.widgetTextColor private var widgetTextColor = DEFAULT_WIDGET_TEXT_COLOR
private var note: Note? = null private var note: Note? = null
private var checklistItems = ArrayList<ChecklistItem>() private var checklistItems = ArrayList<ChecklistItem>()
@ -87,7 +88,7 @@ class WidgetAdapter(val context: Context, val intent: Intent) : RemoteViewsServi
override fun getItemId(position: Int) = position.toLong() override fun getItemId(position: Int) = position.toLong()
override fun onDataSetChanged() { override fun onDataSetChanged() {
widgetTextColor = context.config.widgetTextColor widgetTextColor = intent.getIntExtra(WIDGET_TEXT_COLOR, DEFAULT_WIDGET_TEXT_COLOR)
val noteId = intent.getLongExtra(NOTE_ID, 0L) val noteId = intent.getLongExtra(NOTE_ID, 0L)
note = context.notesDB.getNoteWithId(noteId) note = context.notesDB.getNoteWithId(noteId)
if (note?.type == TYPE_CHECKLIST) { if (note?.type == TYPE_CHECKLIST) {

View File

@ -9,10 +9,10 @@ import android.net.Uri
import android.widget.RemoteViews import android.widget.RemoteViews
import com.simplemobiletools.commons.extensions.getLaunchIntent import com.simplemobiletools.commons.extensions.getLaunchIntent
import com.simplemobiletools.commons.extensions.setBackgroundColor import com.simplemobiletools.commons.extensions.setBackgroundColor
import com.simplemobiletools.commons.helpers.WIDGET_TEXT_COLOR
import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.notes.pro.R import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.activities.SplashActivity import com.simplemobiletools.notes.pro.activities.SplashActivity
import com.simplemobiletools.notes.pro.extensions.config
import com.simplemobiletools.notes.pro.extensions.widgetsDB import com.simplemobiletools.notes.pro.extensions.widgetsDB
import com.simplemobiletools.notes.pro.models.Widget import com.simplemobiletools.notes.pro.models.Widget
import com.simplemobiletools.notes.pro.services.WidgetService import com.simplemobiletools.notes.pro.services.WidgetService
@ -28,24 +28,26 @@ class MyWidgetProvider : AppWidgetProvider() {
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) { override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
super.onUpdate(context, appWidgetManager, appWidgetIds) super.onUpdate(context, appWidgetManager, appWidgetIds)
ensureBackgroundThread { ensureBackgroundThread {
context.widgetsDB.getWidgets().forEach { for (widgetId in appWidgetIds) {
val widget = context.widgetsDB.getWidgetWithWidgetId(widgetId) ?: continue
val views = RemoteViews(context.packageName, R.layout.widget) val views = RemoteViews(context.packageName, R.layout.widget)
views.setBackgroundColor(R.id.notes_widget_holder, context.config.widgetBgColor) views.setBackgroundColor(R.id.notes_widget_holder, widget.widgetBgColor)
setupAppOpenIntent(context, views, R.id.notes_widget_holder, it) setupAppOpenIntent(context, views, R.id.notes_widget_holder, widget)
Intent(context, WidgetService::class.java).apply { Intent(context, WidgetService::class.java).apply {
putExtra(NOTE_ID, it.noteId) putExtra(NOTE_ID, widget.noteId)
putExtra(WIDGET_TEXT_COLOR, widget.widgetTextColor)
data = Uri.parse(this.toUri(Intent.URI_INTENT_SCHEME)) data = Uri.parse(this.toUri(Intent.URI_INTENT_SCHEME))
views.setRemoteAdapter(R.id.notes_widget_listview, this) views.setRemoteAdapter(R.id.notes_widget_listview, this)
} }
val startActivityIntent = context.getLaunchIntent() ?: Intent(context, SplashActivity::class.java) val startActivityIntent = context.getLaunchIntent() ?: Intent(context, SplashActivity::class.java)
startActivityIntent.putExtra(OPEN_NOTE_ID, it.noteId) startActivityIntent.putExtra(OPEN_NOTE_ID, widget.noteId)
val startActivityPendingIntent = PendingIntent.getActivity(context, it.widgetId, startActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT) val startActivityPendingIntent = PendingIntent.getActivity(context, widgetId, startActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT)
views.setPendingIntentTemplate(R.id.notes_widget_listview, startActivityPendingIntent) views.setPendingIntentTemplate(R.id.notes_widget_listview, startActivityPendingIntent)
appWidgetManager.updateAppWidget(it.widgetId, views) appWidgetManager.updateAppWidget(widgetId, views)
appWidgetManager.notifyAppWidgetViewDataChanged(it.widgetId, R.id.notes_widget_listview) appWidgetManager.notifyAppWidgetViewDataChanged(widgetId, R.id.notes_widget_listview)
} }
} }
} }

View File

@ -11,6 +11,9 @@ interface WidgetsDao {
@Query("SELECT * FROM widgets") @Query("SELECT * FROM widgets")
fun getWidgets(): List<Widget> fun getWidgets(): List<Widget>
@Query("SELECT * FROM widgets WHERE widget_id = :widgetId")
fun getWidgetWithWidgetId(widgetId: Int): Widget?
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertOrUpdate(widget: Widget): Long fun insertOrUpdate(widget: Widget): Long