From 7f6cbbb461c27065d45386d6d0ecdd671ed4efeb Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 6 Jan 2017 22:42:46 +0100 Subject: [PATCH] couple widget corrections --- .../activities/WidgetConfigureActivity.kt | 30 ++++++++----------- .../notes/helpers/Constants.kt | 3 -- .../notes/helpers/MyWidgetProvider.kt | 22 +++++--------- 3 files changed, 20 insertions(+), 35 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/activities/WidgetConfigureActivity.kt b/app/src/main/kotlin/com/simplemobiletools/notes/activities/WidgetConfigureActivity.kt index 8610d555..fa03a443 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/activities/WidgetConfigureActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/activities/WidgetConfigureActivity.kt @@ -2,7 +2,6 @@ package com.simplemobiletools.notes.activities import android.app.Activity import android.appwidget.AppWidgetManager -import android.content.Context import android.content.Intent import android.graphics.Color import android.os.Bundle @@ -10,12 +9,10 @@ import android.support.v7.app.AppCompatActivity import android.util.TypedValue import android.widget.RemoteViews import android.widget.SeekBar -import com.simplemobiletools.notes.* +import com.simplemobiletools.notes.R import com.simplemobiletools.notes.extensions.getTextSize +import com.simplemobiletools.notes.helpers.Config import com.simplemobiletools.notes.helpers.MyWidgetProvider -import com.simplemobiletools.notes.helpers.PREFS_KEY -import com.simplemobiletools.notes.helpers.WIDGET_BG_COLOR -import com.simplemobiletools.notes.helpers.WIDGET_TEXT_COLOR import kotlinx.android.synthetic.main.widget_config.* import yuku.ambilwarna.AmbilWarnaDialog @@ -50,8 +47,8 @@ class WidgetConfigureActivity : AppCompatActivity() { } private fun initVariables() { - val prefs = getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE) - mBgColor = prefs.getInt(WIDGET_BG_COLOR, 1) + val config = Config.newInstance(this) + mBgColor = config.widgetBgColor if (mBgColor == 1) { mBgColor = Color.BLACK mBgAlpha = .2f @@ -66,7 +63,7 @@ class WidgetConfigureActivity : AppCompatActivity() { } updateBackgroundColor() - mTextColor = prefs.getInt(WIDGET_TEXT_COLOR, resources.getColor(R.color.color_primary)) + mTextColor = config.widgetTextColor updateTextColor() } @@ -86,8 +83,9 @@ class WidgetConfigureActivity : AppCompatActivity() { } private fun storeWidgetBackground() { - getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE).apply { - edit().putInt(WIDGET_BG_COLOR, mBgColor).putInt(WIDGET_TEXT_COLOR, mTextColor).apply() + Config.newInstance(this).apply { + widgetBgColor = mBgColor + widgetTextColor = mTextColor } } @@ -112,7 +110,7 @@ class WidgetConfigureActivity : AppCompatActivity() { } fun pickBackgroundColor() { - val dialog = AmbilWarnaDialog(this, mBgColorWithoutTransparency, object : AmbilWarnaDialog.OnAmbilWarnaListener { + AmbilWarnaDialog(this, mBgColorWithoutTransparency, object : AmbilWarnaDialog.OnAmbilWarnaListener { override fun onCancel(dialog: AmbilWarnaDialog) { } @@ -120,13 +118,11 @@ class WidgetConfigureActivity : AppCompatActivity() { mBgColorWithoutTransparency = color updateBackgroundColor() } - }) - - dialog.show() + }).show() } fun pickTextColor() { - val dialog = AmbilWarnaDialog(this, mTextColor, object : AmbilWarnaDialog.OnAmbilWarnaListener { + AmbilWarnaDialog(this, mTextColor, object : AmbilWarnaDialog.OnAmbilWarnaListener { override fun onCancel(dialog: AmbilWarnaDialog) { } @@ -134,9 +130,7 @@ class WidgetConfigureActivity : AppCompatActivity() { mTextColor = color updateTextColor() } - }) - - dialog.show() + }).show() } private val bgSeekbarChangeListener = object : SeekBar.OnSeekBarChangeListener { diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/notes/helpers/Constants.kt index 08cbb194..b51b6693 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/helpers/Constants.kt @@ -3,13 +3,10 @@ package com.simplemobiletools.notes.helpers val NOTE_ID = "note_id" // shared preferences -val PREFS_KEY = "Notes" val CURRENT_NOTE_ID = "current_note_id" val WIDGET_NOTE_ID = "widget_note_id" val FONT_SIZE = "font_size" val GRAVITY = "gravity" -val WIDGET_BG_COLOR = "widget_bg_color" -val WIDGET_TEXT_COLOR = "widget_text_color" // gravity val GRAVITY_LEFT = 0 diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/helpers/MyWidgetProvider.kt b/app/src/main/kotlin/com/simplemobiletools/notes/helpers/MyWidgetProvider.kt index 0b368f7b..bd06661f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/helpers/MyWidgetProvider.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/helpers/MyWidgetProvider.kt @@ -5,14 +5,11 @@ import android.appwidget.AppWidgetManager import android.appwidget.AppWidgetProvider import android.content.Context import android.content.Intent -import android.content.SharedPreferences -import android.graphics.Color import android.view.View import android.widget.RemoteViews import com.simplemobiletools.notes.R import com.simplemobiletools.notes.R.layout.widget import com.simplemobiletools.notes.activities.MainActivity -import com.simplemobiletools.notes.helpers.DBHelper import com.simplemobiletools.notes.extensions.getTextSize class MyWidgetProvider : AppWidgetProvider() { @@ -20,20 +17,19 @@ class MyWidgetProvider : AppWidgetProvider() { var textIds = arrayOf(R.id.notes_view_left, R.id.notes_view_center, R.id.notes_view_right) companion object { - lateinit var mPrefs: SharedPreferences lateinit var mRemoteViews: RemoteViews } override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) { initVariables(context) - val defaultColor = Color.BLACK - val newBgColor = mPrefs.getInt(WIDGET_BG_COLOR, defaultColor) - val newTextColor = mPrefs.getInt(WIDGET_TEXT_COLOR, Color.WHITE) + val config = Config.newInstance(context) + val widgetBgColor = config.widgetBgColor + val widgetTextColor = config.widgetTextColor for (id in textIds) { mRemoteViews.apply { - setInt(id, "setBackgroundColor", newBgColor) - setInt(id, "setTextColor", newTextColor) + setInt(id, "setBackgroundColor", widgetBgColor) + setInt(id, "setTextColor", widgetTextColor) setFloat(id, "setTextSize", context.getTextSize() / context.resources.displayMetrics.density) setViewVisibility(id, View.GONE) } @@ -42,7 +38,7 @@ class MyWidgetProvider : AppWidgetProvider() { mRemoteViews.setViewVisibility(getProperTextView(context), View.VISIBLE) for (widgetId in appWidgetIds) { - updateWidget(appWidgetManager, widgetId, mRemoteViews) + updateWidget(appWidgetManager, widgetId, mRemoteViews, context) } super.onUpdate(context, appWidgetManager, appWidgetIds) } @@ -56,7 +52,6 @@ class MyWidgetProvider : AppWidgetProvider() { } private fun initVariables(context: Context) { - mPrefs = context.getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE) mDb = DBHelper.newInstance(context) mRemoteViews = RemoteViews(context.packageName, widget) setupAppOpenIntent(R.id.notes_holder, context) @@ -68,9 +63,8 @@ class MyWidgetProvider : AppWidgetProvider() { mRemoteViews.setOnClickPendingIntent(id, pendingIntent) } - private fun updateWidget(widgetManager: AppWidgetManager, widgetId: Int, remoteViews: RemoteViews) { - val widgetNoteId = mPrefs.getInt(WIDGET_NOTE_ID, 1) - val note = mDb.getNote(widgetNoteId) + private fun updateWidget(widgetManager: AppWidgetManager, widgetId: Int, remoteViews: RemoteViews, context: Context) { + val note = mDb.getNote(Config.newInstance(context).widgetNoteId) for (id in textIds) remoteViews.setTextViewText(id, note?.value ?: "") widgetManager.updateAppWidget(widgetId, remoteViews)