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