Simple-Calendar/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/WidgetMonthlyConfigureActiv...

238 lines
9.1 KiB
Kotlin
Raw Normal View History

2018-11-09 17:12:02 +01:00
package com.simplemobiletools.calendar.pro.activities
import android.annotation.SuppressLint
import android.app.Activity
import android.appwidget.AppWidgetManager
import android.content.Context
import android.content.Intent
import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.widget.LinearLayout
import com.simplemobiletools.calendar.pro.databinding.DayMonthlyNumberViewBinding
import com.simplemobiletools.calendar.pro.databinding.TopNavigationBinding
import com.simplemobiletools.calendar.pro.databinding.WidgetConfigMonthlyBinding
2018-11-09 17:12:02 +01:00
import com.simplemobiletools.calendar.pro.extensions.addDayEvents
import com.simplemobiletools.calendar.pro.extensions.config
import com.simplemobiletools.calendar.pro.extensions.isWeekendIndex
2018-11-09 17:12:02 +01:00
import com.simplemobiletools.calendar.pro.helpers.MonthlyCalendarImpl
import com.simplemobiletools.calendar.pro.helpers.MyWidgetMonthlyProvider
import com.simplemobiletools.calendar.pro.interfaces.MonthlyCalendar
import com.simplemobiletools.calendar.pro.models.DayMonthly
2017-01-09 22:32:10 +01:00
import com.simplemobiletools.commons.dialogs.ColorPickerDialog
2022-04-06 23:49:04 +02:00
import com.simplemobiletools.commons.extensions.*
2023-01-07 23:31:20 +01:00
import com.simplemobiletools.commons.helpers.IS_CUSTOMIZING_COLORS
import com.simplemobiletools.commons.helpers.LOWER_ALPHA
import org.joda.time.DateTime
class WidgetMonthlyConfigureActivity : SimpleActivity(), MonthlyCalendar {
2017-09-10 16:50:07 +02:00
private var mDays: List<DayMonthly>? = null
private var mBgAlpha = 0f
private var mWidgetId = 0
private var mBgColorWithoutTransparency = 0
private var mBgColor = 0
private var mTextColor = 0
private val binding by viewBinding(WidgetConfigMonthlyBinding::inflate)
private val topNavigationBinding by lazy { TopNavigationBinding.bind(binding.root) }
public override fun onCreate(savedInstanceState: Bundle?) {
useDynamicTheme = false
super.onCreate(savedInstanceState)
setResult(Activity.RESULT_CANCELED)
setContentView(binding.root)
initVariables()
2023-01-07 23:31:20 +01:00
val isCustomizingColors = intent.extras?.getBoolean(IS_CUSTOMIZING_COLORS) ?: false
mWidgetId = intent.extras?.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID) ?: AppWidgetManager.INVALID_APPWIDGET_ID
2023-01-07 23:31:20 +01:00
if (mWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID && !isCustomizingColors) {
finish()
2023-01-07 23:31:20 +01:00
}
2023-01-07 23:31:20 +01:00
val primaryColor = getProperPrimaryColor()
binding.apply {
configSave.setOnClickListener { saveConfig() }
configBgColor.setOnClickListener { pickBackgroundColor() }
configTextColor.setOnClickListener { pickTextColor() }
configBgSeekbar.setColors(mTextColor, primaryColor, primaryColor)
}
}
private fun initVariables() {
mBgColor = config.widgetBgColor
2023-01-09 16:49:38 +01:00
mBgAlpha = Color.alpha(mBgColor) / 255f
mBgColorWithoutTransparency = Color.rgb(Color.red(mBgColor), Color.green(mBgColor), Color.blue(mBgColor))
binding.configBgSeekbar.apply {
2023-01-09 16:49:38 +01:00
progress = (mBgAlpha * 100).toInt()
onSeekBarChangeListener { progress ->
mBgAlpha = progress / 100f
updateBackgroundColor()
}
2023-01-07 23:31:20 +01:00
}
2023-01-09 16:49:38 +01:00
updateBackgroundColor()
2023-01-07 23:31:20 +01:00
mTextColor = config.widgetTextColor
2023-09-04 12:13:10 +02:00
if (mTextColor == resources.getColor(com.simplemobiletools.commons.R.color.default_widget_text_color) && config.isUsingSystemTheme) {
mTextColor = resources.getColor(com.simplemobiletools.commons.R.color.you_primary_color, theme)
2023-01-07 23:31:20 +01:00
}
updateTextColor()
2023-01-07 23:31:20 +01:00
MonthlyCalendarImpl(this, this).updateMonthlyCalendar(DateTime().withDayOfMonth(1))
}
2017-10-07 17:17:47 +02:00
private fun saveConfig() {
storeWidgetColors()
requestWidgetUpdate()
Intent().apply {
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mWidgetId)
setResult(Activity.RESULT_OK, this)
}
finish()
}
private fun storeWidgetColors() {
2017-01-10 19:48:02 +01:00
config.apply {
widgetBgColor = mBgColor
2023-01-07 23:31:20 +01:00
widgetTextColor = mTextColor
}
}
2017-10-07 17:17:47 +02:00
private fun pickBackgroundColor() {
2018-04-11 12:24:24 +02:00
ColorPickerDialog(this, mBgColorWithoutTransparency) { wasPositivePressed, color ->
2018-05-01 20:37:39 +02:00
if (wasPositivePressed) {
mBgColorWithoutTransparency = color
2023-01-07 23:31:20 +01:00
updateBackgroundColor()
2018-05-01 20:37:39 +02:00
}
2017-01-09 22:32:10 +01:00
}
}
2017-10-07 17:17:47 +02:00
private fun pickTextColor() {
2018-04-11 12:24:24 +02:00
ColorPickerDialog(this, mTextColor) { wasPositivePressed, color ->
2018-05-01 20:37:39 +02:00
if (wasPositivePressed) {
2023-01-07 23:31:20 +01:00
mTextColor = color
updateTextColor()
2018-05-01 20:37:39 +02:00
updateDays()
}
2017-01-09 22:32:10 +01:00
}
}
private fun requestWidgetUpdate() {
2016-11-27 18:50:47 +01:00
Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, MyWidgetMonthlyProvider::class.java).apply {
2016-11-17 20:05:31 +01:00
putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, intArrayOf(mWidgetId))
sendBroadcast(this)
}
}
2023-01-07 23:31:20 +01:00
private fun updateTextColor() {
topNavigationBinding.topLeftArrow.applyColorFilter(mTextColor)
topNavigationBinding.topRightArrow.applyColorFilter(mTextColor)
topNavigationBinding.topValue.setTextColor(mTextColor)
binding.configTextColor.setFillWithStroke(mTextColor, mTextColor)
binding.configSave.setTextColor(getProperPrimaryColor().getContrastColor())
updateLabels()
}
2023-01-07 23:31:20 +01:00
private fun updateBackgroundColor() {
mBgColor = mBgColorWithoutTransparency.adjustAlpha(mBgAlpha)
binding.configCalendar.root.background.applyColorFilter(mBgColor)
binding.configBgColor.setFillWithStroke(mBgColor, mBgColor)
binding.configSave.backgroundTintList = ColorStateList.valueOf(getProperPrimaryColor())
}
private fun updateDays() {
val daysLength = mDays!!.size
binding.configCalendar.apply {
if (config.showWeekNumbers) {
firstRow.weekNum.setTextColor(mTextColor)
firstRow.weekNum.beVisible()
arrayOf(weekNum0, weekNum1, weekNum2, weekNum3, weekNum4, weekNum5).forEachIndexed { index, textView ->
textView.apply {
@SuppressLint("SetTextI18n")
text = "${mDays!![index * 7 + 3].weekOfYear}:"
setTextColor(mTextColor)
beVisible()
}
2017-01-02 20:53:33 +01:00
}
}
val dividerMargin = resources.displayMetrics.density.toInt()
val dayViews = arrayOf(
day0, day1, day2, day3, day4, day5, day6, day7, day8, day9, day10, day11, day12, day13,
day14, day15, day16, day17, day18, day19, day20, day21, day22, day23, day24, day25, day26, day27,
day28, day29, day30, day31, day32, day33, day34, day35, day36, day37, day38, day39, day40, day41
)
for (i in 0 until daysLength) {
val day = mDays!![i]
val dayTextColor = if (config.highlightWeekends && day.isWeekend) {
config.highlightWeekendsColor
} else {
mTextColor
}
dayViews[i].apply {
removeAllViews()
addDayNumber(dayTextColor, day, this)
context.addDayEvents(day, this, resources, dividerMargin)
}
}
}
}
private fun addDayNumber(rawTextColor: Int, day: DayMonthly, linearLayout: LinearLayout) {
var textColor = rawTextColor
if (!day.isThisMonth) {
textColor = textColor.adjustAlpha(LOWER_ALPHA)
}
DayMonthlyNumberViewBinding.inflate(layoutInflater).apply {
root.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
linearLayout.addView(root)
dayMonthlyNumberBackground.beVisibleIf(day.isToday)
dayMonthlyNumberId.apply {
setTextColor(textColor)
text = day.value.toString()
gravity = Gravity.TOP or Gravity.CENTER_HORIZONTAL
}
if (day.isToday) {
dayMonthlyNumberBackground.setColorFilter(getProperPrimaryColor())
dayMonthlyNumberId.setTextColor(getProperPrimaryColor().getContrastColor())
}
}
}
override fun updateMonthlyCalendar(context: Context, month: String, days: ArrayList<DayMonthly>, checkedEvents: Boolean, currTargetDate: DateTime) {
runOnUiThread {
mDays = days
topNavigationBinding.topValue.text = month
updateDays()
}
}
private fun updateLabels() {
2023-01-07 23:31:20 +01:00
val weekendsTextColor = config.highlightWeekendsColor
binding.configCalendar.firstRow.apply {
arrayOf(label0, label1, label2, label3, label4, label5, label6).forEachIndexed { index, textView ->
val textColor = if (config.highlightWeekends && isWeekendIndex(index)) {
2023-01-07 23:31:20 +01:00
weekendsTextColor
} else {
mTextColor
}
textView.setTextColor(textColor)
2017-01-02 20:53:33 +01:00
}
}
}
}