Skip to content

Commit 7f6cbbb

Browse files
committed
couple widget corrections
1 parent ff7d406 commit 7f6cbbb

File tree

3 files changed

+20
-35
lines changed

3 files changed

+20
-35
lines changed

app/src/main/kotlin/com/simplemobiletools/notes/activities/WidgetConfigureActivity.kt

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,17 @@ package com.simplemobiletools.notes.activities
22

33
import android.app.Activity
44
import android.appwidget.AppWidgetManager
5-
import android.content.Context
65
import android.content.Intent
76
import android.graphics.Color
87
import android.os.Bundle
98
import android.support.v7.app.AppCompatActivity
109
import android.util.TypedValue
1110
import android.widget.RemoteViews
1211
import android.widget.SeekBar
13-
import com.simplemobiletools.notes.*
12+
import com.simplemobiletools.notes.R
1413
import com.simplemobiletools.notes.extensions.getTextSize
14+
import com.simplemobiletools.notes.helpers.Config
1515
import com.simplemobiletools.notes.helpers.MyWidgetProvider
16-
import com.simplemobiletools.notes.helpers.PREFS_KEY
17-
import com.simplemobiletools.notes.helpers.WIDGET_BG_COLOR
18-
import com.simplemobiletools.notes.helpers.WIDGET_TEXT_COLOR
1916
import kotlinx.android.synthetic.main.widget_config.*
2017
import yuku.ambilwarna.AmbilWarnaDialog
2118

@@ -50,8 +47,8 @@ class WidgetConfigureActivity : AppCompatActivity() {
5047
}
5148

5249
private fun initVariables() {
53-
val prefs = getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE)
54-
mBgColor = prefs.getInt(WIDGET_BG_COLOR, 1)
50+
val config = Config.newInstance(this)
51+
mBgColor = config.widgetBgColor
5552
if (mBgColor == 1) {
5653
mBgColor = Color.BLACK
5754
mBgAlpha = .2f
@@ -66,7 +63,7 @@ class WidgetConfigureActivity : AppCompatActivity() {
6663
}
6764
updateBackgroundColor()
6865

69-
mTextColor = prefs.getInt(WIDGET_TEXT_COLOR, resources.getColor(R.color.color_primary))
66+
mTextColor = config.widgetTextColor
7067
updateTextColor()
7168
}
7269

@@ -86,8 +83,9 @@ class WidgetConfigureActivity : AppCompatActivity() {
8683
}
8784

8885
private fun storeWidgetBackground() {
89-
getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE).apply {
90-
edit().putInt(WIDGET_BG_COLOR, mBgColor).putInt(WIDGET_TEXT_COLOR, mTextColor).apply()
86+
Config.newInstance(this).apply {
87+
widgetBgColor = mBgColor
88+
widgetTextColor = mTextColor
9189
}
9290
}
9391

@@ -112,31 +110,27 @@ class WidgetConfigureActivity : AppCompatActivity() {
112110
}
113111

114112
fun pickBackgroundColor() {
115-
val dialog = AmbilWarnaDialog(this, mBgColorWithoutTransparency, object : AmbilWarnaDialog.OnAmbilWarnaListener {
113+
AmbilWarnaDialog(this, mBgColorWithoutTransparency, object : AmbilWarnaDialog.OnAmbilWarnaListener {
116114
override fun onCancel(dialog: AmbilWarnaDialog) {
117115
}
118116

119117
override fun onOk(dialog: AmbilWarnaDialog, color: Int) {
120118
mBgColorWithoutTransparency = color
121119
updateBackgroundColor()
122120
}
123-
})
124-
125-
dialog.show()
121+
}).show()
126122
}
127123

128124
fun pickTextColor() {
129-
val dialog = AmbilWarnaDialog(this, mTextColor, object : AmbilWarnaDialog.OnAmbilWarnaListener {
125+
AmbilWarnaDialog(this, mTextColor, object : AmbilWarnaDialog.OnAmbilWarnaListener {
130126
override fun onCancel(dialog: AmbilWarnaDialog) {
131127
}
132128

133129
override fun onOk(dialog: AmbilWarnaDialog, color: Int) {
134130
mTextColor = color
135131
updateTextColor()
136132
}
137-
})
138-
139-
dialog.show()
133+
}).show()
140134
}
141135

142136
private val bgSeekbarChangeListener = object : SeekBar.OnSeekBarChangeListener {

app/src/main/kotlin/com/simplemobiletools/notes/helpers/Constants.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@ package com.simplemobiletools.notes.helpers
33
val NOTE_ID = "note_id"
44

55
// shared preferences
6-
val PREFS_KEY = "Notes"
76
val CURRENT_NOTE_ID = "current_note_id"
87
val WIDGET_NOTE_ID = "widget_note_id"
98
val FONT_SIZE = "font_size"
109
val GRAVITY = "gravity"
11-
val WIDGET_BG_COLOR = "widget_bg_color"
12-
val WIDGET_TEXT_COLOR = "widget_text_color"
1310

1411
// gravity
1512
val GRAVITY_LEFT = 0

app/src/main/kotlin/com/simplemobiletools/notes/helpers/MyWidgetProvider.kt

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,31 @@ import android.appwidget.AppWidgetManager
55
import android.appwidget.AppWidgetProvider
66
import android.content.Context
77
import android.content.Intent
8-
import android.content.SharedPreferences
9-
import android.graphics.Color
108
import android.view.View
119
import android.widget.RemoteViews
1210
import com.simplemobiletools.notes.R
1311
import com.simplemobiletools.notes.R.layout.widget
1412
import com.simplemobiletools.notes.activities.MainActivity
15-
import com.simplemobiletools.notes.helpers.DBHelper
1613
import com.simplemobiletools.notes.extensions.getTextSize
1714

1815
class MyWidgetProvider : AppWidgetProvider() {
1916
lateinit var mDb: DBHelper
2017
var textIds = arrayOf(R.id.notes_view_left, R.id.notes_view_center, R.id.notes_view_right)
2118

2219
companion object {
23-
lateinit var mPrefs: SharedPreferences
2420
lateinit var mRemoteViews: RemoteViews
2521
}
2622

2723
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
2824
initVariables(context)
29-
val defaultColor = Color.BLACK
30-
val newBgColor = mPrefs.getInt(WIDGET_BG_COLOR, defaultColor)
31-
val newTextColor = mPrefs.getInt(WIDGET_TEXT_COLOR, Color.WHITE)
25+
val config = Config.newInstance(context)
26+
val widgetBgColor = config.widgetBgColor
27+
val widgetTextColor = config.widgetTextColor
3228

3329
for (id in textIds) {
3430
mRemoteViews.apply {
35-
setInt(id, "setBackgroundColor", newBgColor)
36-
setInt(id, "setTextColor", newTextColor)
31+
setInt(id, "setBackgroundColor", widgetBgColor)
32+
setInt(id, "setTextColor", widgetTextColor)
3733
setFloat(id, "setTextSize", context.getTextSize() / context.resources.displayMetrics.density)
3834
setViewVisibility(id, View.GONE)
3935
}
@@ -42,7 +38,7 @@ class MyWidgetProvider : AppWidgetProvider() {
4238
mRemoteViews.setViewVisibility(getProperTextView(context), View.VISIBLE)
4339

4440
for (widgetId in appWidgetIds) {
45-
updateWidget(appWidgetManager, widgetId, mRemoteViews)
41+
updateWidget(appWidgetManager, widgetId, mRemoteViews, context)
4642
}
4743
super.onUpdate(context, appWidgetManager, appWidgetIds)
4844
}
@@ -56,7 +52,6 @@ class MyWidgetProvider : AppWidgetProvider() {
5652
}
5753

5854
private fun initVariables(context: Context) {
59-
mPrefs = context.getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE)
6055
mDb = DBHelper.newInstance(context)
6156
mRemoteViews = RemoteViews(context.packageName, widget)
6257
setupAppOpenIntent(R.id.notes_holder, context)
@@ -68,9 +63,8 @@ class MyWidgetProvider : AppWidgetProvider() {
6863
mRemoteViews.setOnClickPendingIntent(id, pendingIntent)
6964
}
7065

71-
private fun updateWidget(widgetManager: AppWidgetManager, widgetId: Int, remoteViews: RemoteViews) {
72-
val widgetNoteId = mPrefs.getInt(WIDGET_NOTE_ID, 1)
73-
val note = mDb.getNote(widgetNoteId)
66+
private fun updateWidget(widgetManager: AppWidgetManager, widgetId: Int, remoteViews: RemoteViews, context: Context) {
67+
val note = mDb.getNote(Config.newInstance(context).widgetNoteId)
7468
for (id in textIds)
7569
remoteViews.setTextViewText(id, note?.value ?: "")
7670
widgetManager.updateAppWidget(widgetId, remoteViews)

0 commit comments

Comments
 (0)