create a config context extension

This commit is contained in:
tibbi 2017-01-15 11:29:04 +01:00
parent bf86fdec64
commit 08bf07f7f4
8 changed files with 17 additions and 18 deletions

View File

@ -23,6 +23,7 @@ import com.simplemobiletools.notes.adapters.NotesPagerAdapter
import com.simplemobiletools.notes.dialogs.NewNoteDialog import com.simplemobiletools.notes.dialogs.NewNoteDialog
import com.simplemobiletools.notes.dialogs.OpenNoteDialog import com.simplemobiletools.notes.dialogs.OpenNoteDialog
import com.simplemobiletools.notes.dialogs.RenameNoteDialog import com.simplemobiletools.notes.dialogs.RenameNoteDialog
import com.simplemobiletools.notes.extensions.config
import com.simplemobiletools.notes.extensions.getTextSize import com.simplemobiletools.notes.extensions.getTextSize
import com.simplemobiletools.notes.helpers.DBHelper import com.simplemobiletools.notes.helpers.DBHelper
import com.simplemobiletools.notes.helpers.TYPE_NOTE import com.simplemobiletools.notes.helpers.TYPE_NOTE

View File

@ -6,6 +6,7 @@ import android.widget.AdapterView
import android.widget.ArrayAdapter import android.widget.ArrayAdapter
import com.simplemobiletools.commons.extensions.updateTextColors import com.simplemobiletools.commons.extensions.updateTextColors
import com.simplemobiletools.notes.R import com.simplemobiletools.notes.R
import com.simplemobiletools.notes.extensions.config
import com.simplemobiletools.notes.extensions.updateWidget import com.simplemobiletools.notes.extensions.updateWidget
import com.simplemobiletools.notes.helpers.DBHelper import com.simplemobiletools.notes.helpers.DBHelper
import com.simplemobiletools.notes.models.Note import com.simplemobiletools.notes.models.Note

View File

@ -2,13 +2,9 @@ package com.simplemobiletools.notes.activities
import android.os.Bundle import android.os.Bundle
import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.notes.helpers.Config
open class SimpleActivity : BaseSimpleActivity() { open class SimpleActivity : BaseSimpleActivity() {
lateinit var config: Config
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
config = Config.newInstance(applicationContext)
} }
} }

View File

@ -11,8 +11,8 @@ import android.widget.RemoteViews
import android.widget.SeekBar import android.widget.SeekBar
import com.simplemobiletools.commons.dialogs.ColorPickerDialog import com.simplemobiletools.commons.dialogs.ColorPickerDialog
import com.simplemobiletools.notes.R import com.simplemobiletools.notes.R
import com.simplemobiletools.notes.extensions.config
import com.simplemobiletools.notes.extensions.getTextSize import com.simplemobiletools.notes.extensions.getTextSize
import com.simplemobiletools.notes.helpers.Config
import com.simplemobiletools.notes.helpers.MyWidgetProvider import com.simplemobiletools.notes.helpers.MyWidgetProvider
import kotlinx.android.synthetic.main.widget_config.* import kotlinx.android.synthetic.main.widget_config.*
@ -47,7 +47,6 @@ class WidgetConfigureActivity : AppCompatActivity() {
} }
private fun initVariables() { private fun initVariables() {
val config = Config.newInstance(this)
mBgColor = config.widgetBgColor mBgColor = config.widgetBgColor
if (mBgColor == 1) { if (mBgColor == 1) {
mBgColor = Color.BLACK mBgColor = Color.BLACK
@ -83,7 +82,7 @@ class WidgetConfigureActivity : AppCompatActivity() {
} }
private fun storeWidgetBackground() { private fun storeWidgetBackground() {
Config.newInstance(this).apply { config.apply {
widgetBgColor = mBgColor widgetBgColor = mBgColor
widgetTextColor = mTextColor widgetTextColor = mTextColor
} }

View File

@ -7,8 +7,8 @@ import android.widget.RadioButton
import android.widget.RadioGroup import android.widget.RadioGroup
import com.simplemobiletools.commons.extensions.setupDialogStuff import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.notes.R import com.simplemobiletools.notes.R
import com.simplemobiletools.notes.extensions.config
import com.simplemobiletools.notes.helpers.DBHelper import com.simplemobiletools.notes.helpers.DBHelper
import com.simplemobiletools.notes.helpers.Config
import kotlinx.android.synthetic.main.dialog_radio_group.view.* import kotlinx.android.synthetic.main.dialog_radio_group.view.*
class OpenNoteDialog(val activity: Activity, val callback: (checkedId: Int) -> Unit) : RadioGroup.OnCheckedChangeListener { class OpenNoteDialog(val activity: Activity, val callback: (checkedId: Int) -> Unit) : RadioGroup.OnCheckedChangeListener {
@ -16,7 +16,6 @@ class OpenNoteDialog(val activity: Activity, val callback: (checkedId: Int) -> U
var wasInit = false var wasInit = false
init { init {
val config = Config.newInstance(activity)
val view = activity.layoutInflater.inflate(R.layout.dialog_radio_group, null) val view = activity.layoutInflater.inflate(R.layout.dialog_radio_group, null)
val radioGroup = view.dialog_radio_group val radioGroup = view.dialog_radio_group
radioGroup.setOnCheckedChangeListener(this) radioGroup.setOnCheckedChangeListener(this)
@ -26,7 +25,7 @@ class OpenNoteDialog(val activity: Activity, val callback: (checkedId: Int) -> U
val radioButton = activity.layoutInflater.inflate(R.layout.radio_button, null) as RadioButton val radioButton = activity.layoutInflater.inflate(R.layout.radio_button, null) as RadioButton
radioButton.apply { radioButton.apply {
text = it.title text = it.title
isChecked = it.id == config.currentNoteId isChecked = it.id == activity.config.currentNoteId
id = it.id id = it.id
} }
radioGroup.addView(radioButton, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)) radioGroup.addView(radioButton, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))

View File

@ -5,11 +5,11 @@ import android.content.ComponentName
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.view.Gravity import android.view.Gravity
import com.simplemobiletools.notes.* import com.simplemobiletools.notes.R
import com.simplemobiletools.notes.helpers.* import com.simplemobiletools.notes.helpers.*
fun Context.getTextSize() = fun Context.getTextSize() =
when (Config.newInstance(this).fontSize) { when (config.fontSize) {
FONT_SIZE_SMALL -> resources.getDimension(R.dimen.smaller_text_size) FONT_SIZE_SMALL -> resources.getDimension(R.dimen.smaller_text_size)
FONT_SIZE_LARGE -> resources.getDimension(R.dimen.big_text_size) FONT_SIZE_LARGE -> resources.getDimension(R.dimen.big_text_size)
FONT_SIZE_EXTRA_LARGE -> resources.getDimension(R.dimen.extra_big_text_size) FONT_SIZE_EXTRA_LARGE -> resources.getDimension(R.dimen.extra_big_text_size)
@ -17,7 +17,7 @@ fun Context.getTextSize() =
} }
fun Context.getTextGravity() = fun Context.getTextGravity() =
when (Config.newInstance(this).gravity) { when (config.gravity) {
GRAVITY_CENTER -> Gravity.CENTER_HORIZONTAL GRAVITY_CENTER -> Gravity.CENTER_HORIZONTAL
GRAVITY_RIGHT -> Gravity.RIGHT GRAVITY_RIGHT -> Gravity.RIGHT
else -> Gravity.LEFT else -> Gravity.LEFT
@ -33,3 +33,5 @@ fun Context.updateWidget() {
sendBroadcast(this) sendBroadcast(this)
} }
} }
val Context.config: Config get() = Config.newInstance(this)

View File

@ -10,10 +10,10 @@ import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import com.simplemobiletools.commons.extensions.value import com.simplemobiletools.commons.extensions.value
import com.simplemobiletools.notes.R import com.simplemobiletools.notes.R
import com.simplemobiletools.notes.extensions.config
import com.simplemobiletools.notes.extensions.getTextGravity import com.simplemobiletools.notes.extensions.getTextGravity
import com.simplemobiletools.notes.extensions.getTextSize import com.simplemobiletools.notes.extensions.getTextSize
import com.simplemobiletools.notes.extensions.updateWidget import com.simplemobiletools.notes.extensions.updateWidget
import com.simplemobiletools.notes.helpers.Config
import com.simplemobiletools.notes.helpers.DBHelper import com.simplemobiletools.notes.helpers.DBHelper
import com.simplemobiletools.notes.helpers.NOTE_ID import com.simplemobiletools.notes.helpers.NOTE_ID
import com.simplemobiletools.notes.models.Note import com.simplemobiletools.notes.models.Note
@ -52,7 +52,7 @@ class NoteFragment : Fragment() {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
val config = Config.newInstance(context) val config = context.config
view.notes_view.apply { view.notes_view.apply {
setText(note.value) setText(note.value)
setColors(config.textColor, config.primaryColor, config.backgroundColor) setColors(config.textColor, config.primaryColor, config.backgroundColor)

View File

@ -10,6 +10,7 @@ import android.widget.RemoteViews
import com.simplemobiletools.notes.R import com.simplemobiletools.notes.R
import com.simplemobiletools.notes.R.layout.widget import com.simplemobiletools.notes.R.layout.widget
import com.simplemobiletools.notes.activities.MainActivity import com.simplemobiletools.notes.activities.MainActivity
import com.simplemobiletools.notes.extensions.config
import com.simplemobiletools.notes.extensions.getTextSize import com.simplemobiletools.notes.extensions.getTextSize
class MyWidgetProvider : AppWidgetProvider() { class MyWidgetProvider : AppWidgetProvider() {
@ -22,7 +23,7 @@ class MyWidgetProvider : AppWidgetProvider() {
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) { override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
initVariables(context) initVariables(context)
val config = Config.newInstance(context) val config = context.config
val widgetBgColor = config.widgetBgColor val widgetBgColor = config.widgetBgColor
val widgetTextColor = config.widgetTextColor val widgetTextColor = config.widgetTextColor
@ -44,7 +45,7 @@ class MyWidgetProvider : AppWidgetProvider() {
} }
private fun getProperTextView(context: Context): Int { private fun getProperTextView(context: Context): Int {
return when (Config.newInstance(context).gravity) { return when (context.config.gravity) {
GRAVITY_CENTER -> R.id.notes_view_center GRAVITY_CENTER -> R.id.notes_view_center
GRAVITY_RIGHT -> R.id.notes_view_right GRAVITY_RIGHT -> R.id.notes_view_right
else -> R.id.notes_view_left else -> R.id.notes_view_left
@ -64,7 +65,7 @@ class MyWidgetProvider : AppWidgetProvider() {
} }
private fun updateWidget(widgetManager: AppWidgetManager, widgetId: Int, remoteViews: RemoteViews, context: Context) { private fun updateWidget(widgetManager: AppWidgetManager, widgetId: Int, remoteViews: RemoteViews, context: Context) {
val note = mDb.getNote(Config.newInstance(context).widgetNoteId) val note = mDb.getNote(context.config.widgetNoteId)
for (id in textIds) for (id in textIds)
remoteViews.setTextViewText(id, note?.value ?: "") remoteViews.setTextViewText(id, note?.value ?: "")
widgetManager.updateAppWidget(widgetId, remoteViews) widgetManager.updateAppWidget(widgetId, remoteViews)