couple adjustments to the widget config activity

This commit is contained in:
tibbi 2017-11-05 17:48:49 +01:00
parent 5555aa5138
commit 3a5d4e96f1
3 changed files with 63 additions and 59 deletions

View File

@ -2,7 +2,6 @@ package com.simplemobiletools.calculator.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
@ -12,60 +11,41 @@ import android.widget.Button
import android.widget.RemoteViews
import android.widget.SeekBar
import com.simplemobiletools.calculator.R
import com.simplemobiletools.calculator.extensions.config
import com.simplemobiletools.calculator.helpers.MyWidgetProvider
import com.simplemobiletools.commons.dialogs.ColorPickerDialog
import com.simplemobiletools.commons.helpers.PREFS_KEY
import com.simplemobiletools.commons.helpers.WIDGET_BG_COLOR
import com.simplemobiletools.commons.helpers.WIDGET_TEXT_COLOR
import com.simplemobiletools.commons.extensions.adjustAlpha
import com.simplemobiletools.commons.extensions.setBackgroundColor
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.widget_config.*
class WidgetConfigureActivity : AppCompatActivity() {
private var mBgColor: Int = 0
private var mBgColorWithoutTransparency: Int = 0
private var mWidgetId: Int = 0
private var mTextColor: Int = 0
private var mBgAlpha: Float = 0.toFloat()
private val bgSeekbarChangeListener = object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
mBgAlpha = progress.toFloat() / 100.toFloat()
updateBackgroundColor()
}
override fun onStartTrackingTouch(seekBar: SeekBar) {
}
override fun onStopTrackingTouch(seekBar: SeekBar) {
}
}
private var mBgColor = 0
private var mBgColorWithoutTransparency = 0
private var mWidgetId = 0
private var mTextColor = 0
private var mBgAlpha = 0f
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setResult(Activity.RESULT_CANCELED)
setContentView(R.layout.widget_config)
config_save.setOnClickListener { saveConfig() }
config_bg_color.setOnClickListener { pickBackgroundColor() }
config_text_color.setOnClickListener { pickTextColor() }
initVariables()
val intent = intent
val extras = intent.extras
if (extras != null)
mWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID)
if (mWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID)
finish()
config_save.setOnClickListener { saveConfig() }
config_bg_color.setOnClickListener { pickBackgroundColor() }
config_text_color.setOnClickListener { pickTextColor() }
}
private fun initVariables() {
val prefs = getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE)
mBgColor = prefs.getInt(WIDGET_BG_COLOR, 1)
mBgColor = config.widgetBgColor
if (mBgColor == 1) {
mBgColor = Color.BLACK
mBgAlpha = .2f
@ -75,46 +55,49 @@ class WidgetConfigureActivity : AppCompatActivity() {
btn_reset.visibility = View.VISIBLE
mBgColorWithoutTransparency = Color.rgb(Color.red(mBgColor), Color.green(mBgColor), Color.blue(mBgColor))
config_bg_seekbar.setOnSeekBarChangeListener(bgSeekbarChangeListener)
config_bg_seekbar.setOnSeekBarChangeListener(seekbarChangeListener)
config_bg_seekbar.progress = (mBgAlpha * 100).toInt()
updateBackgroundColor()
mTextColor = prefs.getInt(WIDGET_TEXT_COLOR, resources.getColor(R.color.color_primary))
mTextColor = config.widgetTextColor
updateTextColor()
formula.text = "15,937*5"
result.text = "79,685"
}
fun saveConfig() {
private fun saveConfig() {
val appWidgetManager = AppWidgetManager.getInstance(this)
val views = RemoteViews(packageName, R.layout.activity_main)
views.setInt(R.id.calculator_holder, "setBackgroundColor", mBgColor)
views.setBackgroundColor(R.id.calculator_holder, mBgColor)
appWidgetManager.updateAppWidget(mWidgetId, views)
storeWidgetBackground()
storeWidgetColors()
requestWidgetUpdate()
val resultValue = Intent()
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mWidgetId)
setResult(Activity.RESULT_OK, resultValue)
Intent().apply {
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mWidgetId)
setResult(Activity.RESULT_OK, this)
}
finish()
}
private fun storeWidgetBackground() {
val prefs = getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE)
prefs.edit().putInt(WIDGET_BG_COLOR, mBgColor).apply()
prefs.edit().putInt(WIDGET_TEXT_COLOR, mTextColor).apply()
private fun storeWidgetColors() {
config.apply {
widgetBgColor = mBgColor
widgetTextColor = mTextColor
}
}
private fun requestWidgetUpdate() {
val intent = Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, MyWidgetProvider::class.java)
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, intArrayOf(mWidgetId))
sendBroadcast(intent)
Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, MyWidgetProvider::class.java).apply {
putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, intArrayOf(mWidgetId))
sendBroadcast(this)
}
}
private fun updateBackgroundColor() {
mBgColor = adjustAlpha(mBgColorWithoutTransparency, mBgAlpha)
mBgColor = mBgColorWithoutTransparency.adjustAlpha(mBgAlpha)
config_calc.setBackgroundColor(mBgColor)
config_bg_color.setBackgroundColor(mBgColor)
config_save.setBackgroundColor(mBgColor)
@ -128,10 +111,8 @@ class WidgetConfigureActivity : AppCompatActivity() {
result.setTextColor(mTextColor)
formula.setTextColor(mTextColor)
var btn: Button
for (i in viewIds) {
btn = findViewById<Button>(i)
btn.setTextColor(mTextColor)
viewIds.forEach {
(findViewById<Button>(it)).setTextColor(mTextColor)
}
}
@ -149,11 +130,18 @@ class WidgetConfigureActivity : AppCompatActivity() {
}
}
private fun adjustAlpha(color: Int, factor: Float): Int {
val alpha = Math.round(Color.alpha(color) * factor)
val red = Color.red(color)
val green = Color.green(color)
val blue = Color.blue(color)
return Color.argb(alpha, red, green, blue)
private val seekbarChangeListener = object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
mBgAlpha = progress.toFloat() / 100.toFloat()
updateBackgroundColor()
}
override fun onStartTrackingTouch(seekBar: SeekBar) {
}
override fun onStopTrackingTouch(seekBar: SeekBar) {
}
}
}

View File

@ -0,0 +1,6 @@
package com.simplemobiletools.calculator.extensions
import android.content.Context
import com.simplemobiletools.calculator.helpers.Config
val Context.config: Config get() = Config.newInstance(this)

View File

@ -0,0 +1,10 @@
package com.simplemobiletools.calculator.helpers
import android.content.Context
import com.simplemobiletools.commons.helpers.BaseConfig
class Config(context: Context) : BaseConfig(context) {
companion object {
fun newInstance(context: Context) = Config(context)
}
}