From 3d56751b18ff08cf0c42250844538b4f508b708f Mon Sep 17 00:00:00 2001 From: Naveen Date: Sat, 8 Apr 2023 17:03:36 +0530 Subject: [PATCH 01/16] Allow changing CalDAV event color --- .../calendar/pro/activities/EventActivity.kt | 30 +++++++++- .../pro/dialogs/EditEventTypeDialog.kt | 2 +- .../pro/dialogs/SelectEventTypeColorDialog.kt | 23 +++++--- .../calendar/pro/fragments/WeekFragment.kt | 8 ++- .../calendar/pro/helpers/CalDAVHelper.kt | 46 +++++++-------- .../calendar/pro/helpers/EventsHelper.kt | 5 +- app/src/main/res/layout/activity_event.xml | 57 ++++++++++++++++++- 7 files changed, 133 insertions(+), 38 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt index b5dc7156c..d6bacc8ab 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt @@ -10,6 +10,7 @@ import android.graphics.drawable.LayerDrawable import android.net.Uri import android.os.Bundle import android.provider.CalendarContract.Attendees +import android.provider.CalendarContract.Colors import android.provider.ContactsContract.CommonDataKinds import android.provider.ContactsContract.CommonDataKinds.StructuredName import android.provider.ContactsContract.Data @@ -825,6 +826,19 @@ class EventActivity : SimpleActivity() { } } + private fun showEventColorDialog() { + hideKeyboard() + ensureBackgroundThread { + val eventType = eventsHelper.getEventTypeWithCalDAVCalendarId(calendarId = mEventCalendarId)!! + runOnUiThread { + SelectEventTypeColorDialog(activity = this, eventType = eventType, selectedColor = mEvent.color, colorType = Colors.TYPE_EVENT) { color -> + mEvent.color = color + event_caldav_color.setFillWithStroke(color, getProperBackgroundColor()) + } + } + } + } + private fun checkReminderTexts() { updateReminder1Text() updateReminder2Text() @@ -936,6 +950,9 @@ class EventActivity : SimpleActivity() { event_caldav_calendar_image.beVisible() event_caldav_calendar_holder.beVisible() event_caldav_calendar_divider.beVisible() + event_caldav_color_image.beVisible() + event_caldav_color_holder.beVisible() + event_caldav_color_divider.beVisible() val calendars = calDAVHelper.getCalDAVCalendars("", true).filter { it.canWrite() && config.getSyncedCalendarIdsAsList().contains(it.id) @@ -959,6 +976,7 @@ class EventActivity : SimpleActivity() { updateAvailabilityImage() } } + event_caldav_color_holder.setOnClickListener { showEventColorDialog() } } else { updateCurrentCalendarInfo(null) } @@ -974,6 +992,9 @@ class EventActivity : SimpleActivity() { event_caldav_calendar_divider.beVisibleIf(currentCalendar == null) event_caldav_calendar_email.beGoneIf(currentCalendar == null) event_caldav_calendar_color.beGoneIf(currentCalendar == null) + event_caldav_color_image.beGoneIf(currentCalendar == null) + event_caldav_color_holder.beGoneIf(currentCalendar == null) + event_caldav_color_divider.beGoneIf(currentCalendar == null) if (currentCalendar == null) { mEventCalendarId = STORED_LOCALLY_ONLY @@ -1002,6 +1023,13 @@ class EventActivity : SimpleActivity() { event_caldav_calendar_holder.apply { setPadding(paddingLeft, 0, paddingRight, 0) } + + val eventColor = if (mEvent.color == 0) { + calendarColor + } else { + mEvent.color + } + event_caldav_color.setFillWithStroke(eventColor, getProperBackgroundColor()) } } } @@ -1791,7 +1819,7 @@ class EventActivity : SimpleActivity() { val textColor = getProperTextColor() arrayOf( event_time_image, event_time_zone_image, event_repetition_image, event_reminder_image, event_type_image, event_caldav_calendar_image, - event_reminder_1_type, event_reminder_2_type, event_reminder_3_type, event_attendees_image, event_availability_image + event_reminder_1_type, event_reminder_2_type, event_reminder_3_type, event_attendees_image, event_availability_image, event_caldav_color_image ).forEach { it.applyColorFilter(textColor) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/EditEventTypeDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/EditEventTypeDialog.kt index 0133c3d2f..6bf3d3fb9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/EditEventTypeDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/EditEventTypeDialog.kt @@ -32,7 +32,7 @@ class EditEventTypeDialog(val activity: Activity, var eventType: EventType? = nu } } } else { - SelectEventTypeColorDialog(activity, eventType!!) { + SelectEventTypeColorDialog(activity, eventType!!, eventType!!.color) { eventType!!.color = it setupColor(type_color) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventTypeColorDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventTypeColorDialog.kt index bc451de2d..27e560e77 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventTypeColorDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventTypeColorDialog.kt @@ -1,6 +1,7 @@ package com.simplemobiletools.calendar.pro.dialogs import android.app.Activity +import android.provider.CalendarContract.Colors import android.view.ViewGroup import android.widget.RadioButton import android.widget.RadioGroup @@ -16,11 +17,17 @@ import com.simplemobiletools.commons.extensions.setupDialogStuff import kotlinx.android.synthetic.main.dialog_select_event_type_color.view.* import kotlinx.android.synthetic.main.radio_button_with_color.view.* -class SelectEventTypeColorDialog(val activity: Activity, val eventType: EventType, val callback: (color: Int) -> Unit) { +class SelectEventTypeColorDialog( + val activity: Activity, + val eventType: EventType, + val selectedColor: Int, + val colorType: Int = Colors.TYPE_CALENDAR, + val callback: (color: Int) -> Unit +) { private var dialog: AlertDialog? = null private val radioGroup: RadioGroup private var wasInit = false - private val colors = activity.calDAVHelper.getAvailableCalDAVCalendarColors(eventType) + private val colors = activity.calDAVHelper.getAvailableCalDAVCalendarColors(eventType, colorType) init { val view = activity.layoutInflater.inflate(R.layout.dialog_select_event_type_color, null) as ViewGroup @@ -29,8 +36,8 @@ class SelectEventTypeColorDialog(val activity: Activity, val eventType: EventTyp showCustomColorPicker() } - colors.forEachIndexed { index, value -> - addRadioButton(index, value) + colors.forEach { (color, key) -> + addRadioButton(key.toInt(), color) } wasInit = true @@ -50,22 +57,22 @@ class SelectEventTypeColorDialog(val activity: Activity, val eventType: EventTyp val view = activity.layoutInflater.inflate(R.layout.radio_button_with_color, null) (view.dialog_radio_button as RadioButton).apply { text = if (color == 0) activity.getString(R.string.transparent) else String.format("#%06X", 0xFFFFFF and color) - isChecked = color == eventType.color + isChecked = color == selectedColor id = colorKey } view.dialog_radio_color.setFillWithStroke(color, activity.getProperBackgroundColor()) view.setOnClickListener { - viewClicked(colorKey) + viewClicked(color) } radioGroup.addView(view, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)) } - private fun viewClicked(colorKey: Int) { + private fun viewClicked(color: Int) { if (!wasInit) return - callback(colors[colorKey]) + callback(color) dialog?.dismiss() } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragment.kt index 45e3be372..7418548a9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/WeekFragment.kt @@ -557,9 +557,13 @@ class WeekFragment : Fragment(), WeeklyCalendar { val dayColumn = dayColumns[dayOfWeek] (inflater.inflate(R.layout.week_event_marker, null, false) as ConstraintLayout).apply { - var backgroundColor = eventTypeColors.get(event.eventType, primaryColor) + var backgroundColor = if (event.color == 0) { + eventTypeColors.get(event.eventType, primaryColor) + } else { + event.color + } var textColor = backgroundColor.getContrastColor() - val currentEventWeeklyView = eventTimeRanges[currentDayCode]!!.get(event.id) + val currentEventWeeklyView = eventTimeRanges[currentDayCode]!![event.id] val adjustAlpha = if (event.isTask()) { dimCompletedTasks && event.isTaskCompleted() diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt index adc41c6af..edb8a3d8c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt @@ -5,7 +5,6 @@ import android.content.ContentUris import android.content.ContentValues import android.content.Context import android.provider.CalendarContract.* -import android.util.SparseIntArray import android.widget.Toast import com.google.gson.Gson import com.google.gson.reflect.TypeToken @@ -116,35 +115,25 @@ class CalDAVHelper(val context: Context) { private fun getCalDAVColorKey(eventType: EventType): String? { val colors = getAvailableCalDAVCalendarColors(eventType) - val colorKey = colors.indexOf(eventType.color) - return if (colorKey > 0) { - colorKey.toString() - } else { - null - } + return colors[eventType.color] } @SuppressLint("MissingPermission") - fun getAvailableCalDAVCalendarColors(eventType: EventType): ArrayList { - val colors = SparseIntArray() + fun getAvailableCalDAVCalendarColors(eventType: EventType, colorType: Int = Colors.TYPE_CALENDAR): Map { + val colors = mutableMapOf() val uri = Colors.CONTENT_URI val projection = arrayOf(Colors.COLOR, Colors.COLOR_KEY) val selection = "${Colors.COLOR_TYPE} = ? AND ${Colors.ACCOUNT_NAME} = ?" - val selectionArgs = arrayOf(Colors.TYPE_CALENDAR.toString(), eventType.caldavEmail) + val selectionArgs = arrayOf(colorType.toString(), eventType.caldavEmail) context.queryCursor(uri, projection, selection, selectionArgs) { cursor -> - val colorKey = cursor.getIntValue(Colors.COLOR_KEY) + val colorKey = cursor.getStringValue(Colors.COLOR_KEY) val color = cursor.getIntValue(Colors.COLOR) - colors.put(colorKey, color) + colors[colorKey] = color } - var sortedColors = ArrayList(colors.size()) - (0 until colors.size()).mapTo(sortedColors) { colors[it] } - if (sortedColors.isNotEmpty()) { - sortedColors = sortedColors.distinct() as ArrayList - } - - return sortedColors + return colors.toSortedMap().entries + .associate { (k, v) -> v to k } } @SuppressLint("MissingPermission") @@ -181,7 +170,8 @@ class CalDAVHelper(val context: Context) { Events.EVENT_TIMEZONE, Events.CALENDAR_TIME_ZONE, Events.DELETED, - Events.AVAILABILITY + Events.AVAILABILITY, + Events.EVENT_COLOR ) val selection = "${Events.CALENDAR_ID} = $calendarId" @@ -210,6 +200,7 @@ class CalDAVHelper(val context: Context) { val reminders = getCalDAVEventReminders(id) val attendees = Gson().toJson(getCalDAVEventAttendees(id)) val availability = cursor.getIntValue(Events.AVAILABILITY) + val color = cursor.getIntValue(Events.EVENT_COLOR) if (endTS == 0L) { val duration = cursor.getStringValue(Events.DURATION) ?: "" @@ -230,7 +221,8 @@ class CalDAVHelper(val context: Context) { reminder2?.minutes ?: REMINDER_OFF, reminder3?.minutes ?: REMINDER_OFF, reminder1?.type ?: REMINDER_NOTIFICATION, reminder2?.type ?: REMINDER_NOTIFICATION, reminder3?.type ?: REMINDER_NOTIFICATION, repeatRule.repeatInterval, repeatRule.repeatRule, - repeatRule.repeatLimit, ArrayList(), attendees, importId, eventTimeZone, allDay, eventTypeId, source = source, availability = availability + repeatRule.repeatLimit, ArrayList(), attendees, importId, eventTimeZone, allDay, eventTypeId, + source = source, availability = availability, color = color ) if (event.getIsAllDay()) { @@ -291,7 +283,6 @@ class CalDAVHelper(val context: Context) { existingEvent.apply { this.id = null - color = 0 lastUpdated = 0L repetitionExceptions = ArrayList() } @@ -394,14 +385,23 @@ class CalDAVHelper(val context: Context) { } private fun fillEventContentValues(event: Event): ContentValues { + val calendarId = event.getCalDAVCalendarId() return ContentValues().apply { - put(Events.CALENDAR_ID, event.getCalDAVCalendarId()) + put(Events.CALENDAR_ID, calendarId) put(Events.TITLE, event.title) put(Events.DESCRIPTION, event.description) put(Events.EVENT_LOCATION, event.location) put(Events.STATUS, Events.STATUS_CONFIRMED) put(Events.AVAILABILITY, event.availability) + val eventType = eventsHelper.getEventTypeWithCalDAVCalendarId(calendarId)!! + val colors = getAvailableCalDAVCalendarColors(eventType, Colors.TYPE_EVENT) + if (event.color == 0) { + put(Events.EVENT_COLOR_KEY, "") + } else { + put(Events.EVENT_COLOR_KEY, colors[event.color]) + } + val repeatRule = Parser().getRepeatCode(event) if (repeatRule.isEmpty()) { putNull(Events.RRULE) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt index 31c635149..fc9327c71 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt @@ -337,7 +337,10 @@ class EventsHelper(val context: Context) { } } } - it.color = eventTypeColors.get(it.eventType) ?: context.getProperPrimaryColor() + + if (it.color == 0) { + it.color = eventTypeColors.get(it.eventType) ?: context.getProperPrimaryColor() + } } callback(events) diff --git a/app/src/main/res/layout/activity_event.xml b/app/src/main/res/layout/activity_event.xml index c467c41ef..7a4a229b1 100644 --- a/app/src/main/res/layout/activity_event.xml +++ b/app/src/main/res/layout/activity_event.xml @@ -507,10 +507,63 @@ android:importantForAccessibility="no" /> + + + + + + + + + + + + From 450354ef161f14032e42ccd63914c40fabd4ca0b Mon Sep 17 00:00:00 2001 From: Naveen Date: Sat, 8 Apr 2023 17:04:38 +0530 Subject: [PATCH 02/16] Minor code improvement --- .../simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt index edb8a3d8c..3acc3861a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt @@ -394,11 +394,11 @@ class CalDAVHelper(val context: Context) { put(Events.STATUS, Events.STATUS_CONFIRMED) put(Events.AVAILABILITY, event.availability) - val eventType = eventsHelper.getEventTypeWithCalDAVCalendarId(calendarId)!! - val colors = getAvailableCalDAVCalendarColors(eventType, Colors.TYPE_EVENT) if (event.color == 0) { put(Events.EVENT_COLOR_KEY, "") } else { + val eventType = eventsHelper.getEventTypeWithCalDAVCalendarId(calendarId)!! + val colors = getAvailableCalDAVCalendarColors(eventType, Colors.TYPE_EVENT) put(Events.EVENT_COLOR_KEY, colors[event.color]) } From b2c21c6564112cab45b24bc33ce746bb2e577b09 Mon Sep 17 00:00:00 2001 From: Naveen Date: Sat, 8 Apr 2023 17:32:23 +0530 Subject: [PATCH 03/16] Add default CalDAV event color option --- .../calendar/pro/activities/EventActivity.kt | 22 ++++--- .../pro/dialogs/EditEventTypeDialog.kt | 2 +- .../pro/dialogs/SelectEventColorDialog.kt | 65 +++++++++++++++++++ .../pro/dialogs/SelectEventTypeColorDialog.kt | 12 +--- .../res/layout/dialog_select_event_color.xml | 22 +++++++ 5 files changed, 103 insertions(+), 20 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt create mode 100644 app/src/main/res/layout/dialog_select_event_color.xml diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt index d6bacc8ab..f0564f530 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt @@ -10,7 +10,6 @@ import android.graphics.drawable.LayerDrawable import android.net.Uri import android.os.Bundle import android.provider.CalendarContract.Attendees -import android.provider.CalendarContract.Colors import android.provider.ContactsContract.CommonDataKinds import android.provider.ContactsContract.CommonDataKinds.StructuredName import android.provider.ContactsContract.Data @@ -831,9 +830,9 @@ class EventActivity : SimpleActivity() { ensureBackgroundThread { val eventType = eventsHelper.getEventTypeWithCalDAVCalendarId(calendarId = mEventCalendarId)!! runOnUiThread { - SelectEventTypeColorDialog(activity = this, eventType = eventType, selectedColor = mEvent.color, colorType = Colors.TYPE_EVENT) { color -> + SelectEventColorDialog(activity = this, eventType = eventType, selectedColor = mEvent.color) { color -> mEvent.color = color - event_caldav_color.setFillWithStroke(color, getProperBackgroundColor()) + updateEventColorInfo(eventType.color) } } } @@ -1023,18 +1022,21 @@ class EventActivity : SimpleActivity() { event_caldav_calendar_holder.apply { setPadding(paddingLeft, 0, paddingRight, 0) } - - val eventColor = if (mEvent.color == 0) { - calendarColor - } else { - mEvent.color - } - event_caldav_color.setFillWithStroke(eventColor, getProperBackgroundColor()) + updateEventColorInfo(calendarColor) } } } } + private fun updateEventColorInfo(defaultColor: Int) { + val eventColor = if (mEvent.color == 0) { + defaultColor + } else { + mEvent.color + } + event_caldav_color.setFillWithStroke(eventColor, getProperBackgroundColor()) + } + private fun resetTime() { if (mEventEndDateTime.isBefore(mEventStartDateTime) && mEventStartDateTime.dayOfMonth() == mEventEndDateTime.dayOfMonth() && diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/EditEventTypeDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/EditEventTypeDialog.kt index 6bf3d3fb9..0133c3d2f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/EditEventTypeDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/EditEventTypeDialog.kt @@ -32,7 +32,7 @@ class EditEventTypeDialog(val activity: Activity, var eventType: EventType? = nu } } } else { - SelectEventTypeColorDialog(activity, eventType!!, eventType!!.color) { + SelectEventTypeColorDialog(activity, eventType!!) { eventType!!.color = it setupColor(type_color) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt new file mode 100644 index 000000000..5560350ef --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt @@ -0,0 +1,65 @@ +package com.simplemobiletools.calendar.pro.dialogs + +import android.app.Activity +import android.provider.CalendarContract.Colors +import android.view.ViewGroup +import android.widget.RadioButton +import android.widget.RadioGroup +import androidx.appcompat.app.AlertDialog +import com.simplemobiletools.calendar.pro.R +import com.simplemobiletools.calendar.pro.extensions.calDAVHelper +import com.simplemobiletools.calendar.pro.models.EventType +import com.simplemobiletools.commons.extensions.getAlertDialogBuilder +import com.simplemobiletools.commons.extensions.getProperBackgroundColor +import com.simplemobiletools.commons.extensions.setFillWithStroke +import com.simplemobiletools.commons.extensions.setupDialogStuff +import kotlinx.android.synthetic.main.dialog_select_event_type_color.view.* +import kotlinx.android.synthetic.main.radio_button_with_color.view.* + +class SelectEventColorDialog(val activity: Activity, val eventType: EventType, val selectedColor: Int, val callback: (color: Int) -> Unit) { + private var dialog: AlertDialog? = null + private val radioGroup: RadioGroup + private var wasInit = false + private val colors = activity.calDAVHelper.getAvailableCalDAVCalendarColors(eventType, Colors.TYPE_EVENT) + + init { + val view = activity.layoutInflater.inflate(R.layout.dialog_select_event_color, null) as ViewGroup + radioGroup = view.dialog_select_event_type_color_radio + + addRadioButton(colorKey = colors.values.size.inc(), color = 0) + colors.forEach { (color, key) -> + addRadioButton(key.toInt(), color) + } + + wasInit = true + activity.getAlertDialogBuilder() + .apply { + activity.setupDialogStuff(view, this) { alertDialog -> + dialog = alertDialog + } + } + } + + private fun addRadioButton(colorKey: Int, color: Int) { + val view = activity.layoutInflater.inflate(R.layout.radio_button_with_color, null) + (view.dialog_radio_button as RadioButton).apply { + text = if (color == 0) activity.getString(R.string.default_color) else String.format("#%06X", 0xFFFFFF and color) + isChecked = color == selectedColor + id = colorKey + } + + view.dialog_radio_color.setFillWithStroke(color, activity.getProperBackgroundColor()) + view.setOnClickListener { + viewClicked(color) + } + radioGroup.addView(view, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)) + } + + private fun viewClicked(color: Int) { + if (!wasInit) + return + + callback(color) + dialog?.dismiss() + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventTypeColorDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventTypeColorDialog.kt index 27e560e77..dbf3061f3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventTypeColorDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventTypeColorDialog.kt @@ -17,17 +17,11 @@ import com.simplemobiletools.commons.extensions.setupDialogStuff import kotlinx.android.synthetic.main.dialog_select_event_type_color.view.* import kotlinx.android.synthetic.main.radio_button_with_color.view.* -class SelectEventTypeColorDialog( - val activity: Activity, - val eventType: EventType, - val selectedColor: Int, - val colorType: Int = Colors.TYPE_CALENDAR, - val callback: (color: Int) -> Unit -) { +class SelectEventTypeColorDialog(val activity: Activity, val eventType: EventType, val callback: (color: Int) -> Unit) { private var dialog: AlertDialog? = null private val radioGroup: RadioGroup private var wasInit = false - private val colors = activity.calDAVHelper.getAvailableCalDAVCalendarColors(eventType, colorType) + private val colors = activity.calDAVHelper.getAvailableCalDAVCalendarColors(eventType, Colors.TYPE_CALENDAR) init { val view = activity.layoutInflater.inflate(R.layout.dialog_select_event_type_color, null) as ViewGroup @@ -57,7 +51,7 @@ class SelectEventTypeColorDialog( val view = activity.layoutInflater.inflate(R.layout.radio_button_with_color, null) (view.dialog_radio_button as RadioButton).apply { text = if (color == 0) activity.getString(R.string.transparent) else String.format("#%06X", 0xFFFFFF and color) - isChecked = color == selectedColor + isChecked = color == eventType.color id = colorKey } diff --git a/app/src/main/res/layout/dialog_select_event_color.xml b/app/src/main/res/layout/dialog_select_event_color.xml new file mode 100644 index 000000000..18698e72b --- /dev/null +++ b/app/src/main/res/layout/dialog_select_event_color.xml @@ -0,0 +1,22 @@ + + + + + + + + + From 06a6e3a3f761cdcea155cd83fbbf3b802e4776d2 Mon Sep 17 00:00:00 2001 From: Naveen Date: Sat, 8 Apr 2023 17:41:35 +0530 Subject: [PATCH 04/16] Check event color change on back press --- .../calendar/pro/activities/EventActivity.kt | 12 +++++++++--- .../calendar/pro/helpers/Constants.kt | 1 + 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt index f0564f530..b2bd543ec 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt @@ -76,6 +76,7 @@ class EventActivity : SimpleActivity() { private var mOriginalStartTS = 0L private var mOriginalEndTS = 0L private var mIsNewEvent = true + private var mEventColor = 0 private lateinit var mEventStartDateTime: DateTime private lateinit var mEventEndDateTime: DateTime @@ -164,6 +165,7 @@ class EventActivity : SimpleActivity() { putString(ATTENDEES, getAllAttendees(false)) putInt(AVAILABILITY, mAvailability) + putInt(EVENT_COLOR, mEventColor) putLong(EVENT_TYPE_ID, mEventTypeId) putInt(EVENT_CALENDAR_ID, mEventCalendarId) @@ -196,6 +198,7 @@ class EventActivity : SimpleActivity() { mReminder3Type = getInt(REMINDER_3_TYPE) mAvailability = getInt(AVAILABILITY) + mEventColor = getInt(EVENT_COLOR) mRepeatInterval = getInt(REPEAT_INTERVAL) mRepeatRule = getInt(REPEAT_RULE) @@ -429,6 +432,7 @@ class EventActivity : SimpleActivity() { mEventTypeId != mEvent.eventType || mWasCalendarChanged || mIsAllDayEvent != mEvent.getIsAllDay() || + mEventColor != mEvent.color || hasTimeChanged ) { return true @@ -488,6 +492,7 @@ class EventActivity : SimpleActivity() { mEventTypeId = mEvent.eventType mEventCalendarId = mEvent.getCalDAVCalendarId() mAvailability = mEvent.availability + mEventColor = mEvent.color val token = object : TypeToken>() {}.type mAttendees = Gson().fromJson>(mEvent.attendees, token) ?: ArrayList() @@ -831,7 +836,7 @@ class EventActivity : SimpleActivity() { val eventType = eventsHelper.getEventTypeWithCalDAVCalendarId(calendarId = mEventCalendarId)!! runOnUiThread { SelectEventColorDialog(activity = this, eventType = eventType, selectedColor = mEvent.color) { color -> - mEvent.color = color + mEventColor = color updateEventColorInfo(eventType.color) } } @@ -1029,10 +1034,10 @@ class EventActivity : SimpleActivity() { } private fun updateEventColorInfo(defaultColor: Int) { - val eventColor = if (mEvent.color == 0) { + val eventColor = if (mEventColor == 0) { defaultColor } else { - mEvent.color + mEventColor } event_caldav_color.setFillWithStroke(eventColor, getProperBackgroundColor()) } @@ -1225,6 +1230,7 @@ class EventActivity : SimpleActivity() { source = newSource location = event_location.value availability = mAvailability + color = mEventColor } // recreate the event if it was moved in a different CalDAV calendar diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt index 1a3793d81..b3581a4c0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt @@ -255,6 +255,7 @@ const val AVAILABILITY = "AVAILABILITY" const val EVENT_TYPE_ID = "EVENT_TYPE_ID" const val EVENT_CALENDAR_ID = "EVENT_CALENDAR_ID" const val IS_NEW_EVENT = "IS_NEW_EVENT" +const val EVENT_COLOR = "EVENT_COLOR" // actions const val ACTION_MARK_COMPLETED = "ACTION_MARK_COMPLETED" From 8cc2c85f447130db614e6085ac9b3637be0d8e58 Mon Sep 17 00:00:00 2001 From: Naveen Date: Sat, 8 Apr 2023 18:28:24 +0530 Subject: [PATCH 05/16] Properly highlight selected event color --- .../calendar/pro/activities/EventActivity.kt | 13 ++++++++++--- .../calendar/pro/dialogs/SelectEventColorDialog.kt | 4 ++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt index b2bd543ec..5b5c9e5fe 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt @@ -835,9 +835,16 @@ class EventActivity : SimpleActivity() { ensureBackgroundThread { val eventType = eventsHelper.getEventTypeWithCalDAVCalendarId(calendarId = mEventCalendarId)!! runOnUiThread { - SelectEventColorDialog(activity = this, eventType = eventType, selectedColor = mEvent.color) { color -> - mEventColor = color - updateEventColorInfo(eventType.color) + val selectedColor = if (mEventColor == 0) { + eventType.color + } else { + mEventColor + } + SelectEventColorDialog(activity = this, eventType = eventType, selectedColor = selectedColor) { color -> + if (color != eventType.color) { + mEventColor = color + updateEventColorInfo(eventType.color) + } } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt index 5560350ef..cca78a891 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt @@ -26,7 +26,7 @@ class SelectEventColorDialog(val activity: Activity, val eventType: EventType, v val view = activity.layoutInflater.inflate(R.layout.dialog_select_event_color, null) as ViewGroup radioGroup = view.dialog_select_event_type_color_radio - addRadioButton(colorKey = colors.values.size.inc(), color = 0) + addRadioButton(colorKey = colors.values.size.inc(), color = eventType.color) colors.forEach { (color, key) -> addRadioButton(key.toInt(), color) } @@ -43,7 +43,7 @@ class SelectEventColorDialog(val activity: Activity, val eventType: EventType, v private fun addRadioButton(colorKey: Int, color: Int) { val view = activity.layoutInflater.inflate(R.layout.radio_button_with_color, null) (view.dialog_radio_button as RadioButton).apply { - text = if (color == 0) activity.getString(R.string.default_color) else String.format("#%06X", 0xFFFFFF and color) + text = if (color == eventType.color) activity.getString(R.string.default_color) else String.format("#%06X", 0xFFFFFF and color) isChecked = color == selectedColor id = colorKey } From b5bba45d56ddfe6c868172e5476ccb2d0bc5dc2d Mon Sep 17 00:00:00 2001 From: Naveen Date: Sat, 8 Apr 2023 18:29:04 +0530 Subject: [PATCH 06/16] Add braces to if statements --- .../calendar/pro/dialogs/SelectEventColorDialog.kt | 3 ++- .../calendar/pro/dialogs/SelectEventTypeColorDialog.kt | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt index cca78a891..f0ba2cb6b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt @@ -56,8 +56,9 @@ class SelectEventColorDialog(val activity: Activity, val eventType: EventType, v } private fun viewClicked(color: Int) { - if (!wasInit) + if (!wasInit) { return + } callback(color) dialog?.dismiss() diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventTypeColorDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventTypeColorDialog.kt index dbf3061f3..54b794f9e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventTypeColorDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventTypeColorDialog.kt @@ -63,8 +63,9 @@ class SelectEventTypeColorDialog(val activity: Activity, val eventType: EventTyp } private fun viewClicked(color: Int) { - if (!wasInit) + if (!wasInit) { return + } callback(color) dialog?.dismiss() From a2ea8b660fbaa7e0e981e66c646bb9443f7904dd Mon Sep 17 00:00:00 2001 From: Naveen Date: Sat, 8 Apr 2023 18:29:42 +0530 Subject: [PATCH 07/16] Minor readability improvement --- .../com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt index 3acc3861a..218e43ffe 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt @@ -133,7 +133,7 @@ class CalDAVHelper(val context: Context) { } return colors.toSortedMap().entries - .associate { (k, v) -> v to k } + .associate { (key, color) -> color to key } } @SuppressLint("MissingPermission") From a4072b49929f07e8befcfd0cb758ba6aaf5a53e2 Mon Sep 17 00:00:00 2001 From: Naveen Date: Sat, 8 Apr 2023 21:59:49 +0530 Subject: [PATCH 08/16] Remove calendar color circle --- .../calendar/pro/activities/EventActivity.kt | 2 -- app/src/main/res/layout/activity_event.xml | 11 ----------- 2 files changed, 13 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt index 5b5c9e5fe..ddc73170f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt @@ -1002,7 +1002,6 @@ class EventActivity : SimpleActivity() { event_type_holder.beVisibleIf(currentCalendar == null) event_caldav_calendar_divider.beVisibleIf(currentCalendar == null) event_caldav_calendar_email.beGoneIf(currentCalendar == null) - event_caldav_calendar_color.beGoneIf(currentCalendar == null) event_caldav_color_image.beGoneIf(currentCalendar == null) event_caldav_color_holder.beGoneIf(currentCalendar == null) event_caldav_color_divider.beGoneIf(currentCalendar == null) @@ -1025,7 +1024,6 @@ class EventActivity : SimpleActivity() { val calendarColor = eventsHelper.getEventTypeWithCalDAVCalendarId(currentCalendar.id)?.color ?: currentCalendar.color runOnUiThread { - event_caldav_calendar_color.setFillWithStroke(calendarColor, getProperBackgroundColor()) event_caldav_calendar_name.apply { text = currentCalendar.displayName setPadding(paddingLeft, paddingTop, paddingRight, resources.getDimension(R.dimen.tiny_margin).toInt()) diff --git a/app/src/main/res/layout/activity_event.xml b/app/src/main/res/layout/activity_event.xml index 7a4a229b1..5c722a01b 100644 --- a/app/src/main/res/layout/activity_event.xml +++ b/app/src/main/res/layout/activity_event.xml @@ -586,7 +586,6 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="@dimen/small_margin" - android:layout_toStartOf="@+id/event_caldav_calendar_color" android:ellipsize="end" android:maxLines="1" android:paddingTop="@dimen/medium_margin" @@ -601,7 +600,6 @@ android:layout_height="wrap_content" android:layout_below="@+id/event_caldav_calendar_name" android:layout_marginStart="@dimen/small_margin" - android:layout_toStartOf="@+id/event_caldav_calendar_color" android:ellipsize="end" android:maxLines="1" android:paddingEnd="@dimen/medium_margin" @@ -609,15 +607,6 @@ android:textSize="@dimen/meta_text_size" tools:text="hello@simplemobiletools.com" /> - - Date: Mon, 10 Apr 2023 13:45:07 +0530 Subject: [PATCH 09/16] Fix crash related to color keys Color keys can be a name like 'aliceblue' or 'green' --- .../calendar/pro/dialogs/SelectEventColorDialog.kt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt index f0ba2cb6b..e4590ad2d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt @@ -20,15 +20,15 @@ class SelectEventColorDialog(val activity: Activity, val eventType: EventType, v private var dialog: AlertDialog? = null private val radioGroup: RadioGroup private var wasInit = false - private val colors = activity.calDAVHelper.getAvailableCalDAVCalendarColors(eventType, Colors.TYPE_EVENT) + private val colors = activity.calDAVHelper.getAvailableCalDAVCalendarColors(eventType, Colors.TYPE_EVENT).keys init { val view = activity.layoutInflater.inflate(R.layout.dialog_select_event_color, null) as ViewGroup radioGroup = view.dialog_select_event_type_color_radio - addRadioButton(colorKey = colors.values.size.inc(), color = eventType.color) - colors.forEach { (color, key) -> - addRadioButton(key.toInt(), color) + addRadioButton(index = colors.size.inc(), color = eventType.color) + colors.forEachIndexed { index, color -> + addRadioButton(index, color) } wasInit = true @@ -40,12 +40,12 @@ class SelectEventColorDialog(val activity: Activity, val eventType: EventType, v } } - private fun addRadioButton(colorKey: Int, color: Int) { + private fun addRadioButton(index: Int, color: Int) { val view = activity.layoutInflater.inflate(R.layout.radio_button_with_color, null) (view.dialog_radio_button as RadioButton).apply { text = if (color == eventType.color) activity.getString(R.string.default_color) else String.format("#%06X", 0xFFFFFF and color) isChecked = color == selectedColor - id = colorKey + id = index } view.dialog_radio_color.setFillWithStroke(color, activity.getProperBackgroundColor()) From c3ad086806c4c1e67a9d57077bb8afa55a592e90 Mon Sep 17 00:00:00 2001 From: Naveen Date: Tue, 11 Apr 2023 13:49:30 +0530 Subject: [PATCH 10/16] Redesign event color picker - Use circular color buttons instead of radio buttons - Use recyclerview for efficiency and speed - Sort color list using a color comparator - Darken colors to ensure white text is visible on top of them --- .../calendar/pro/activities/EventActivity.kt | 13 ++-- .../pro/adapters/CheckableColorAdapter.kt | 55 +++++++++++++++ .../pro/dialogs/SelectEventColorDialog.kt | 67 ++++++------------- .../pro/dialogs/SelectEventTypeColorDialog.kt | 10 +-- .../calendar/pro/helpers/CalDAVHelper.kt | 34 ++++++++-- .../pro/helpers/HsvColorComparator.kt | 40 +++++++++++ .../pro/views/AutoGridLayoutManager.kt | 34 ++++++++++ app/src/main/res/layout/activity_event.xml | 2 +- .../res/layout/checkable_color_button.xml | 12 ++++ .../res/layout/dialog_select_event_color.xml | 40 ++++++----- 10 files changed, 224 insertions(+), 83 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/calendar/pro/adapters/CheckableColorAdapter.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/HsvColorComparator.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/calendar/pro/views/AutoGridLayoutManager.kt create mode 100644 app/src/main/res/layout/checkable_color_button.xml diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt index ddc73170f..ac1457b1c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt @@ -10,6 +10,7 @@ import android.graphics.drawable.LayerDrawable import android.net.Uri import android.os.Bundle import android.provider.CalendarContract.Attendees +import android.provider.CalendarContract.Colors import android.provider.ContactsContract.CommonDataKinds import android.provider.ContactsContract.CommonDataKinds.StructuredName import android.provider.ContactsContract.Data @@ -834,16 +835,18 @@ class EventActivity : SimpleActivity() { hideKeyboard() ensureBackgroundThread { val eventType = eventsHelper.getEventTypeWithCalDAVCalendarId(calendarId = mEventCalendarId)!! + val eventColorsMap = calDAVHelper.getAvailableCalDAVCalendarColors(eventType, Colors.TYPE_EVENT) + val eventColors = eventColorsMap.keys.toIntArray() runOnUiThread { - val selectedColor = if (mEventColor == 0) { + val currentColor = if (mEventColor == 0) { eventType.color } else { mEventColor } - SelectEventColorDialog(activity = this, eventType = eventType, selectedColor = selectedColor) { color -> - if (color != eventType.color) { - mEventColor = color - updateEventColorInfo(eventType.color) + SelectEventColorDialog(activity = this, colors = eventColors, currentColor = currentColor) { newColor -> + if (newColor != currentColor) { + mEventColor = newColor + updateEventColorInfo(defaultColor = eventType.color) } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/adapters/CheckableColorAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/adapters/CheckableColorAdapter.kt new file mode 100644 index 000000000..523696159 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/adapters/CheckableColorAdapter.kt @@ -0,0 +1,55 @@ +package com.simplemobiletools.calendar.pro.adapters + +import android.annotation.SuppressLint +import android.app.Activity +import android.content.res.ColorStateList +import android.graphics.Color +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView +import com.simplemobiletools.calendar.pro.R +import com.simplemobiletools.commons.extensions.applyColorFilter +import kotlinx.android.synthetic.main.checkable_color_button.view.* + +class CheckableColorAdapter(private val activity: Activity, private val colors: IntArray, var currentColor: Int) : + RecyclerView.Adapter() { + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CheckableColorViewHolder { + val itemView = LayoutInflater.from(activity) + .inflate(R.layout.checkable_color_button, parent, false) + return CheckableColorViewHolder(itemView) + } + + override fun onBindViewHolder(holder: CheckableColorViewHolder, position: Int) { + val color = colors[position] + holder.bindView(color = color, checked = color == currentColor) + } + + override fun getItemCount() = colors.size + + @SuppressLint("NotifyDataSetChanged") + private fun updateSelection(color: Int) { + currentColor = color + notifyDataSetChanged() + } + + inner class CheckableColorViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { + + fun bindView(color: Int, checked: Boolean) { + itemView.checkable_color_button.apply { + backgroundTintList = ColorStateList.valueOf(color) + setOnClickListener { + updateSelection(color) + } + + if (checked) { + setImageResource(R.drawable.ic_check_vector) + applyColorFilter(Color.WHITE) + } else { + setImageDrawable(null) + } + } + } + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt index e4590ad2d..6ccf9dd41 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt @@ -1,66 +1,39 @@ package com.simplemobiletools.calendar.pro.dialogs import android.app.Activity -import android.provider.CalendarContract.Colors import android.view.ViewGroup -import android.widget.RadioButton -import android.widget.RadioGroup -import androidx.appcompat.app.AlertDialog import com.simplemobiletools.calendar.pro.R -import com.simplemobiletools.calendar.pro.extensions.calDAVHelper -import com.simplemobiletools.calendar.pro.models.EventType +import com.simplemobiletools.calendar.pro.adapters.CheckableColorAdapter +import com.simplemobiletools.calendar.pro.views.AutoGridLayoutManager import com.simplemobiletools.commons.extensions.getAlertDialogBuilder -import com.simplemobiletools.commons.extensions.getProperBackgroundColor -import com.simplemobiletools.commons.extensions.setFillWithStroke import com.simplemobiletools.commons.extensions.setupDialogStuff -import kotlinx.android.synthetic.main.dialog_select_event_type_color.view.* -import kotlinx.android.synthetic.main.radio_button_with_color.view.* +import kotlinx.android.synthetic.main.dialog_select_event_color.view.* -class SelectEventColorDialog(val activity: Activity, val eventType: EventType, val selectedColor: Int, val callback: (color: Int) -> Unit) { - private var dialog: AlertDialog? = null - private val radioGroup: RadioGroup - private var wasInit = false - private val colors = activity.calDAVHelper.getAvailableCalDAVCalendarColors(eventType, Colors.TYPE_EVENT).keys +class SelectEventColorDialog(val activity: Activity, val colors: IntArray, var currentColor: Int, val callback: (color: Int) -> Unit) { init { val view = activity.layoutInflater.inflate(R.layout.dialog_select_event_color, null) as ViewGroup - radioGroup = view.dialog_select_event_type_color_radio - - addRadioButton(index = colors.size.inc(), color = eventType.color) - colors.forEachIndexed { index, color -> - addRadioButton(index, color) + val colorAdapter = CheckableColorAdapter(activity, colors, currentColor) + view.color_grid.apply { + layoutManager = AutoGridLayoutManager( + context = activity, + itemWidth = activity.resources.getDimensionPixelSize(R.dimen.smaller_icon_size) + ) + adapter = colorAdapter } - wasInit = true activity.getAlertDialogBuilder() .apply { - activity.setupDialogStuff(view, this) { alertDialog -> - dialog = alertDialog + setPositiveButton(R.string.ok) { dialog, _ -> + callback(colorAdapter.currentColor) + dialog?.dismiss() } + setNeutralButton(R.string.default_calendar_color) { dialog, _ -> + callback(0) + dialog?.dismiss() + } + + activity.setupDialogStuff(view, this, R.string.event_color) } } - - private fun addRadioButton(index: Int, color: Int) { - val view = activity.layoutInflater.inflate(R.layout.radio_button_with_color, null) - (view.dialog_radio_button as RadioButton).apply { - text = if (color == eventType.color) activity.getString(R.string.default_color) else String.format("#%06X", 0xFFFFFF and color) - isChecked = color == selectedColor - id = index - } - - view.dialog_radio_color.setFillWithStroke(color, activity.getProperBackgroundColor()) - view.setOnClickListener { - viewClicked(color) - } - radioGroup.addView(view, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)) - } - - private fun viewClicked(color: Int) { - if (!wasInit) { - return - } - - callback(color) - dialog?.dismiss() - } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventTypeColorDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventTypeColorDialog.kt index 54b794f9e..0467bb229 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventTypeColorDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventTypeColorDialog.kt @@ -21,7 +21,7 @@ class SelectEventTypeColorDialog(val activity: Activity, val eventType: EventTyp private var dialog: AlertDialog? = null private val radioGroup: RadioGroup private var wasInit = false - private val colors = activity.calDAVHelper.getAvailableCalDAVCalendarColors(eventType, Colors.TYPE_CALENDAR) + private val colors = activity.calDAVHelper.getAvailableCalDAVCalendarColors(eventType, Colors.TYPE_CALENDAR).keys init { val view = activity.layoutInflater.inflate(R.layout.dialog_select_event_type_color, null) as ViewGroup @@ -30,8 +30,8 @@ class SelectEventTypeColorDialog(val activity: Activity, val eventType: EventTyp showCustomColorPicker() } - colors.forEach { (color, key) -> - addRadioButton(key.toInt(), color) + colors.forEachIndexed { index, color -> + addRadioButton(index, color) } wasInit = true @@ -47,12 +47,12 @@ class SelectEventTypeColorDialog(val activity: Activity, val eventType: EventTyp } } - private fun addRadioButton(colorKey: Int, color: Int) { + private fun addRadioButton(index: Int, color: Int) { val view = activity.layoutInflater.inflate(R.layout.radio_button_with_color, null) (view.dialog_radio_button as RadioButton).apply { text = if (color == 0) activity.getString(R.string.transparent) else String.format("#%06X", 0xFFFFFF and color) isChecked = color == eventType.color - id = colorKey + id = index } view.dialog_radio_color.setFillWithStroke(color, activity.getProperBackgroundColor()) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt index 218e43ffe..5a0a3cf53 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt @@ -4,6 +4,7 @@ import android.annotation.SuppressLint import android.content.ContentUris import android.content.ContentValues import android.content.Context +import android.graphics.Color import android.provider.CalendarContract.* import android.widget.Toast import com.google.gson.Gson @@ -19,6 +20,7 @@ import org.joda.time.DateTimeZone import org.joda.time.format.DateTimeFormat import java.util.* import kotlin.math.max +import kotlin.math.min @SuppressLint("MissingPermission") class CalDAVHelper(val context: Context) { @@ -118,9 +120,18 @@ class CalDAVHelper(val context: Context) { return colors[eventType.color] } + // darkens the given color to ensure that white text is clearly visible on top of it + private fun getDisplayColorFromColor(color: Int): Int { + val hsv = FloatArray(3) + Color.colorToHSV(color, hsv) + hsv[1] = min(hsv[1] * SATURATION_ADJUST, 1.0f) + hsv[2] = hsv[2] * INTENSITY_ADJUST + return Color.HSVToColor(hsv) + } + @SuppressLint("MissingPermission") fun getAvailableCalDAVCalendarColors(eventType: EventType, colorType: Int = Colors.TYPE_CALENDAR): Map { - val colors = mutableMapOf() + val colors = mutableMapOf() val uri = Colors.CONTENT_URI val projection = arrayOf(Colors.COLOR, Colors.COLOR_KEY) val selection = "${Colors.COLOR_TYPE} = ? AND ${Colors.ACCOUNT_NAME} = ?" @@ -129,11 +140,10 @@ class CalDAVHelper(val context: Context) { context.queryCursor(uri, projection, selection, selectionArgs) { cursor -> val colorKey = cursor.getStringValue(Colors.COLOR_KEY) val color = cursor.getIntValue(Colors.COLOR) - colors[colorKey] = color + val displayColor = getDisplayColorFromColor(color) + colors[displayColor] = colorKey } - - return colors.toSortedMap().entries - .associate { (key, color) -> color to key } + return colors.toSortedMap(HsvColorComparator()) } @SuppressLint("MissingPermission") @@ -200,7 +210,12 @@ class CalDAVHelper(val context: Context) { val reminders = getCalDAVEventReminders(id) val attendees = Gson().toJson(getCalDAVEventAttendees(id)) val availability = cursor.getIntValue(Events.AVAILABILITY) - val color = cursor.getIntValue(Events.EVENT_COLOR) + val color = cursor.getIntValueOrNull(Events.EVENT_COLOR) + val displayColor = if (color != null) { + getDisplayColorFromColor(color) + } else { + 0 + } if (endTS == 0L) { val duration = cursor.getStringValue(Events.DURATION) ?: "" @@ -222,7 +237,7 @@ class CalDAVHelper(val context: Context) { reminder1?.type ?: REMINDER_NOTIFICATION, reminder2?.type ?: REMINDER_NOTIFICATION, reminder3?.type ?: REMINDER_NOTIFICATION, repeatRule.repeatInterval, repeatRule.repeatRule, repeatRule.repeatLimit, ArrayList(), attendees, importId, eventTimeZone, allDay, eventTypeId, - source = source, availability = availability, color = color + source = source, availability = availability, color = displayColor ) if (event.getIsAllDay()) { @@ -534,4 +549,9 @@ class CalDAVHelper(val context: Context) { private fun getCalDAVEventImportId(calendarId: Int, eventId: Long) = "$CALDAV-$calendarId-$eventId" private fun refreshCalDAVCalendar(event: Event) = context.refreshCalDAVCalendars(event.getCalDAVCalendarId().toString(), false) + + companion object { + private const val INTENSITY_ADJUST = 0.8f + private const val SATURATION_ADJUST = 1.3f + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/HsvColorComparator.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/HsvColorComparator.kt new file mode 100644 index 000000000..3e83c2b68 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/HsvColorComparator.kt @@ -0,0 +1,40 @@ +package com.simplemobiletools.calendar.pro.helpers + +import android.graphics.Color + +/** + * A color comparator which compares based on hue, saturation, and value. + * Source: AOSP Color picker, https://cs.android.com/android/platform/superproject/+/master:frameworks/opt/colorpicker/src/com/android/colorpicker/HsvColorComparator.java + */ +class HsvColorComparator : Comparator { + override fun compare(lhs: Int?, rhs: Int?): Int { + val hsv = FloatArray(3) + Color.colorToHSV(lhs!!, hsv) + val hue1 = hsv[0] + val sat1 = hsv[1] + val val1 = hsv[2] + val hsv2 = FloatArray(3) + Color.colorToHSV(rhs!!, hsv2) + val hue2 = hsv2[0] + val sat2 = hsv2[1] + val val2 = hsv2[2] + if (hue1 < hue2) { + return 1 + } else if (hue1 > hue2) { + return -1 + } else { + if (sat1 < sat2) { + return 1 + } else if (sat1 > sat2) { + return -1 + } else { + if (val1 < val2) { + return 1 + } else if (val1 > val2) { + return -1 + } + } + } + return 0 + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/views/AutoGridLayoutManager.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/views/AutoGridLayoutManager.kt new file mode 100644 index 000000000..2428a85d2 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/views/AutoGridLayoutManager.kt @@ -0,0 +1,34 @@ +package com.simplemobiletools.calendar.pro.views + +import android.content.Context +import androidx.recyclerview.widget.GridLayoutManager +import androidx.recyclerview.widget.RecyclerView +import kotlin.math.max + +/** + * RecyclerView GridLayoutManager but with automatic spanCount calculation + * @param itemWidth: Grid item width in pixels. Will be used to calculate span count. + */ +class AutoGridLayoutManager( + context: Context, + private var itemWidth: Int +) : GridLayoutManager(context, 1) { + + init { + require(itemWidth >= 0) + } + + override fun onLayoutChildren(recycler: RecyclerView.Recycler?, state: RecyclerView.State?) { + val width = width + val height = height + if (itemWidth > 0 && width > 0 && height > 0) { + val totalSpace = if (orientation == VERTICAL) { + width - paddingRight - paddingLeft + } else { + height - paddingTop - paddingBottom + } + spanCount = max(1, totalSpace / itemWidth) + } + super.onLayoutChildren(recycler, state) + } +} diff --git a/app/src/main/res/layout/activity_event.xml b/app/src/main/res/layout/activity_event.xml index 5c722a01b..5f5382cf9 100644 --- a/app/src/main/res/layout/activity_event.xml +++ b/app/src/main/res/layout/activity_event.xml @@ -536,7 +536,7 @@ android:layout_toStartOf="@+id/event_caldav_color" android:paddingTop="@dimen/normal_margin" android:paddingBottom="@dimen/normal_margin" - android:text="@string/change_color" + android:text="@string/event_color" android:textSize="@dimen/day_text_size" /> + diff --git a/app/src/main/res/layout/dialog_select_event_color.xml b/app/src/main/res/layout/dialog_select_event_color.xml index 18698e72b..014aceeca 100644 --- a/app/src/main/res/layout/dialog_select_event_color.xml +++ b/app/src/main/res/layout/dialog_select_event_color.xml @@ -1,22 +1,26 @@ - + android:layout_height="wrap_content" + android:paddingHorizontal="@dimen/activity_margin" + android:paddingTop="@dimen/big_margin" + android:paddingBottom="@dimen/medium_margin"> - + - - - - + From 8506513871f26b214f6b84a9e2cfa05cbccdeb8c Mon Sep 17 00:00:00 2001 From: Naveen Date: Tue, 11 Apr 2023 14:33:59 +0530 Subject: [PATCH 11/16] Fix spacing between grid items --- .../pro/dialogs/SelectEventColorDialog.kt | 7 +++-- .../res/layout/checkable_color_button.xml | 27 ++++++++++++------- .../res/layout/dialog_select_event_color.xml | 2 +- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt index 6ccf9dd41..3135fe687 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt @@ -15,10 +15,9 @@ class SelectEventColorDialog(val activity: Activity, val colors: IntArray, var c val view = activity.layoutInflater.inflate(R.layout.dialog_select_event_color, null) as ViewGroup val colorAdapter = CheckableColorAdapter(activity, colors, currentColor) view.color_grid.apply { - layoutManager = AutoGridLayoutManager( - context = activity, - itemWidth = activity.resources.getDimensionPixelSize(R.dimen.smaller_icon_size) - ) + val width = activity.resources.getDimensionPixelSize(R.dimen.smaller_icon_size) + val spacing = activity.resources.getDimensionPixelSize(R.dimen.small_margin) * 2 + layoutManager = AutoGridLayoutManager(context = activity, itemWidth = width + spacing) adapter = colorAdapter } diff --git a/app/src/main/res/layout/checkable_color_button.xml b/app/src/main/res/layout/checkable_color_button.xml index 42a3d34cb..d135d47c3 100644 --- a/app/src/main/res/layout/checkable_color_button.xml +++ b/app/src/main/res/layout/checkable_color_button.xml @@ -1,12 +1,19 @@ - + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:padding="@dimen/small_margin"> + + + + + diff --git a/app/src/main/res/layout/dialog_select_event_color.xml b/app/src/main/res/layout/dialog_select_event_color.xml index 014aceeca..86ff4f649 100644 --- a/app/src/main/res/layout/dialog_select_event_color.xml +++ b/app/src/main/res/layout/dialog_select_event_color.xml @@ -5,7 +5,7 @@ android:id="@+id/color_grid_holder" android:layout_width="match_parent" android:layout_height="wrap_content" - android:paddingHorizontal="@dimen/activity_margin" + android:paddingHorizontal="@dimen/normal_margin" android:paddingTop="@dimen/big_margin" android:paddingBottom="@dimen/medium_margin"> From 885f551c83b48b5bf9dd839276f1703d75cd42ac Mon Sep 17 00:00:00 2001 From: Naveen Date: Tue, 11 Apr 2023 15:23:08 +0530 Subject: [PATCH 12/16] Hide event colors setting when there are no colors available --- .../calendar/pro/activities/EventActivity.kt | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt index ac1457b1c..d0b5d94b8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt @@ -835,8 +835,7 @@ class EventActivity : SimpleActivity() { hideKeyboard() ensureBackgroundThread { val eventType = eventsHelper.getEventTypeWithCalDAVCalendarId(calendarId = mEventCalendarId)!! - val eventColorsMap = calDAVHelper.getAvailableCalDAVCalendarColors(eventType, Colors.TYPE_EVENT) - val eventColors = eventColorsMap.keys.toIntArray() + val eventColors = getEventColors(eventType) runOnUiThread { val currentColor = if (mEventColor == 0) { eventType.color @@ -964,9 +963,6 @@ class EventActivity : SimpleActivity() { event_caldav_calendar_image.beVisible() event_caldav_calendar_holder.beVisible() event_caldav_calendar_divider.beVisible() - event_caldav_color_image.beVisible() - event_caldav_color_holder.beVisible() - event_caldav_color_divider.beVisible() val calendars = calDAVHelper.getCalDAVCalendars("", true).filter { it.canWrite() && config.getSyncedCalendarIdsAsList().contains(it.id) @@ -1005,9 +1001,6 @@ class EventActivity : SimpleActivity() { event_type_holder.beVisibleIf(currentCalendar == null) event_caldav_calendar_divider.beVisibleIf(currentCalendar == null) event_caldav_calendar_email.beGoneIf(currentCalendar == null) - event_caldav_color_image.beGoneIf(currentCalendar == null) - event_caldav_color_holder.beGoneIf(currentCalendar == null) - event_caldav_color_divider.beGoneIf(currentCalendar == null) if (currentCalendar == null) { mEventCalendarId = STORED_LOCALLY_ONLY @@ -1020,11 +1013,20 @@ class EventActivity : SimpleActivity() { event_caldav_calendar_holder.apply { setPadding(paddingLeft, mediumMargin, paddingRight, mediumMargin) } + event_caldav_color_image.beGone() + event_caldav_color_holder.beGone() + event_caldav_color_divider.beGone() } else { event_caldav_calendar_email.text = currentCalendar.accountName ensureBackgroundThread { - val calendarColor = eventsHelper.getEventTypeWithCalDAVCalendarId(currentCalendar.id)?.color ?: currentCalendar.color + val eventType = eventsHelper.getEventTypeWithCalDAVCalendarId(currentCalendar.id) + val calendarColor = eventType?.color ?: currentCalendar.color + val canCustomizeColors = if (eventType != null) { + getEventColors(eventType).isNotEmpty() + } else { + false + } runOnUiThread { event_caldav_calendar_name.apply { @@ -1035,7 +1037,13 @@ class EventActivity : SimpleActivity() { event_caldav_calendar_holder.apply { setPadding(paddingLeft, 0, paddingRight, 0) } - updateEventColorInfo(calendarColor) + + event_caldav_color_image.beVisibleIf(canCustomizeColors) + event_caldav_color_holder.beVisibleIf(canCustomizeColors) + event_caldav_color_divider.beVisibleIf(canCustomizeColors) + if (canCustomizeColors) { + updateEventColorInfo(calendarColor) + } } } } @@ -1050,6 +1058,10 @@ class EventActivity : SimpleActivity() { event_caldav_color.setFillWithStroke(eventColor, getProperBackgroundColor()) } + private fun getEventColors(eventType: EventType): IntArray { + return calDAVHelper.getAvailableCalDAVCalendarColors(eventType, Colors.TYPE_EVENT).keys.toIntArray() + } + private fun resetTime() { if (mEventEndDateTime.isBefore(mEventStartDateTime) && mEventStartDateTime.dayOfMonth() == mEventEndDateTime.dayOfMonth() && From 4a3fbf194b07a317889662b89604aced35f4519c Mon Sep 17 00:00:00 2001 From: Naveen Date: Tue, 11 Apr 2023 15:29:16 +0530 Subject: [PATCH 13/16] Automatically dismiss when a color is selected --- .../pro/adapters/CheckableColorAdapter.kt | 6 ++---- .../pro/dialogs/SelectEventColorDialog.kt | 16 ++++++++++------ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/adapters/CheckableColorAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/adapters/CheckableColorAdapter.kt index 523696159..a77584e32 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/adapters/CheckableColorAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/adapters/CheckableColorAdapter.kt @@ -1,6 +1,5 @@ package com.simplemobiletools.calendar.pro.adapters -import android.annotation.SuppressLint import android.app.Activity import android.content.res.ColorStateList import android.graphics.Color @@ -12,7 +11,7 @@ import com.simplemobiletools.calendar.pro.R import com.simplemobiletools.commons.extensions.applyColorFilter import kotlinx.android.synthetic.main.checkable_color_button.view.* -class CheckableColorAdapter(private val activity: Activity, private val colors: IntArray, var currentColor: Int) : +class CheckableColorAdapter(private val activity: Activity, private val colors: IntArray, var currentColor: Int, val callback: (color: Int) -> Unit) : RecyclerView.Adapter() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CheckableColorViewHolder { @@ -28,10 +27,9 @@ class CheckableColorAdapter(private val activity: Activity, private val colors: override fun getItemCount() = colors.size - @SuppressLint("NotifyDataSetChanged") private fun updateSelection(color: Int) { currentColor = color - notifyDataSetChanged() + callback(color) } inner class CheckableColorViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt index 3135fe687..8bd64cc98 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt @@ -2,6 +2,7 @@ package com.simplemobiletools.calendar.pro.dialogs import android.app.Activity import android.view.ViewGroup +import androidx.appcompat.app.AlertDialog import com.simplemobiletools.calendar.pro.R import com.simplemobiletools.calendar.pro.adapters.CheckableColorAdapter import com.simplemobiletools.calendar.pro.views.AutoGridLayoutManager @@ -10,10 +11,15 @@ import com.simplemobiletools.commons.extensions.setupDialogStuff import kotlinx.android.synthetic.main.dialog_select_event_color.view.* class SelectEventColorDialog(val activity: Activity, val colors: IntArray, var currentColor: Int, val callback: (color: Int) -> Unit) { + private var dialog: AlertDialog? = null init { val view = activity.layoutInflater.inflate(R.layout.dialog_select_event_color, null) as ViewGroup - val colorAdapter = CheckableColorAdapter(activity, colors, currentColor) + val colorAdapter = CheckableColorAdapter(activity, colors, currentColor) { color -> + callback(color) + dialog?.dismiss() + } + view.color_grid.apply { val width = activity.resources.getDimensionPixelSize(R.dimen.smaller_icon_size) val spacing = activity.resources.getDimensionPixelSize(R.dimen.small_margin) * 2 @@ -23,16 +29,14 @@ class SelectEventColorDialog(val activity: Activity, val colors: IntArray, var c activity.getAlertDialogBuilder() .apply { - setPositiveButton(R.string.ok) { dialog, _ -> - callback(colorAdapter.currentColor) - dialog?.dismiss() - } setNeutralButton(R.string.default_calendar_color) { dialog, _ -> callback(0) dialog?.dismiss() } - activity.setupDialogStuff(view, this, R.string.event_color) + activity.setupDialogStuff(view, this, R.string.event_color) { + dialog = it + } } } } From 85e05f07a9ecebfebfecad4edb7ff91b47ee489f Mon Sep 17 00:00:00 2001 From: Naveen Date: Tue, 11 Apr 2023 17:28:23 +0530 Subject: [PATCH 14/16] Update event type color dialog --- .../pro/dialogs/EditEventTypeDialog.kt | 5 +- .../pro/dialogs/SelectEventColorDialog.kt | 4 +- .../pro/dialogs/SelectEventTypeColorDialog.kt | 65 +++++-------------- .../calendar/pro/helpers/CalDAVHelper.kt | 3 +- ...vent_color.xml => dialog_select_color.xml} | 0 .../layout/dialog_select_event_type_color.xml | 45 ------------- 6 files changed, 26 insertions(+), 96 deletions(-) rename app/src/main/res/layout/{dialog_select_event_color.xml => dialog_select_color.xml} (100%) delete mode 100644 app/src/main/res/layout/dialog_select_event_type_color.xml diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/EditEventTypeDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/EditEventTypeDialog.kt index 0133c3d2f..1b73dd378 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/EditEventTypeDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/EditEventTypeDialog.kt @@ -4,6 +4,7 @@ import android.app.Activity import android.widget.ImageView import androidx.appcompat.app.AlertDialog import com.simplemobiletools.calendar.pro.R +import com.simplemobiletools.calendar.pro.extensions.calDAVHelper import com.simplemobiletools.calendar.pro.extensions.eventsHelper import com.simplemobiletools.calendar.pro.helpers.OTHER_EVENT import com.simplemobiletools.calendar.pro.models.EventType @@ -32,7 +33,9 @@ class EditEventTypeDialog(val activity: Activity, var eventType: EventType? = nu } } } else { - SelectEventTypeColorDialog(activity, eventType!!) { + val currentColor = eventType!!.color + val colors = activity.calDAVHelper.getAvailableCalDAVCalendarColors(eventType!!).keys.toIntArray() + SelectEventTypeColorDialog(activity, colors = colors, currentColor = currentColor) { eventType!!.color = it setupColor(type_color) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt index 8bd64cc98..68a9a395c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventColorDialog.kt @@ -8,13 +8,13 @@ import com.simplemobiletools.calendar.pro.adapters.CheckableColorAdapter import com.simplemobiletools.calendar.pro.views.AutoGridLayoutManager import com.simplemobiletools.commons.extensions.getAlertDialogBuilder import com.simplemobiletools.commons.extensions.setupDialogStuff -import kotlinx.android.synthetic.main.dialog_select_event_color.view.* +import kotlinx.android.synthetic.main.dialog_select_color.view.* class SelectEventColorDialog(val activity: Activity, val colors: IntArray, var currentColor: Int, val callback: (color: Int) -> Unit) { private var dialog: AlertDialog? = null init { - val view = activity.layoutInflater.inflate(R.layout.dialog_select_event_color, null) as ViewGroup + val view = activity.layoutInflater.inflate(R.layout.dialog_select_color, null) as ViewGroup val colorAdapter = CheckableColorAdapter(activity, colors, currentColor) { color -> callback(color) dialog?.dismiss() diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventTypeColorDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventTypeColorDialog.kt index 0467bb229..281a1f3ec 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventTypeColorDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventTypeColorDialog.kt @@ -1,44 +1,38 @@ package com.simplemobiletools.calendar.pro.dialogs import android.app.Activity -import android.provider.CalendarContract.Colors import android.view.ViewGroup -import android.widget.RadioButton -import android.widget.RadioGroup import androidx.appcompat.app.AlertDialog import com.simplemobiletools.calendar.pro.R -import com.simplemobiletools.calendar.pro.extensions.calDAVHelper -import com.simplemobiletools.calendar.pro.models.EventType +import com.simplemobiletools.calendar.pro.adapters.CheckableColorAdapter +import com.simplemobiletools.calendar.pro.views.AutoGridLayoutManager import com.simplemobiletools.commons.dialogs.ColorPickerDialog import com.simplemobiletools.commons.extensions.getAlertDialogBuilder -import com.simplemobiletools.commons.extensions.getProperBackgroundColor -import com.simplemobiletools.commons.extensions.setFillWithStroke import com.simplemobiletools.commons.extensions.setupDialogStuff -import kotlinx.android.synthetic.main.dialog_select_event_type_color.view.* -import kotlinx.android.synthetic.main.radio_button_with_color.view.* +import kotlinx.android.synthetic.main.dialog_select_color.view.* -class SelectEventTypeColorDialog(val activity: Activity, val eventType: EventType, val callback: (color: Int) -> Unit) { +class SelectEventTypeColorDialog(val activity: Activity, val colors: IntArray, var currentColor: Int, val callback: (color: Int) -> Unit) { private var dialog: AlertDialog? = null - private val radioGroup: RadioGroup - private var wasInit = false - private val colors = activity.calDAVHelper.getAvailableCalDAVCalendarColors(eventType, Colors.TYPE_CALENDAR).keys init { - val view = activity.layoutInflater.inflate(R.layout.dialog_select_event_type_color, null) as ViewGroup - radioGroup = view.dialog_select_event_type_color_radio - view.dialog_select_event_type_other_value.setOnClickListener { - showCustomColorPicker() + val view = activity.layoutInflater.inflate(R.layout.dialog_select_color, null) as ViewGroup + val colorAdapter = CheckableColorAdapter(activity, colors, currentColor) { color -> + callback(color) + dialog?.dismiss() } - colors.forEachIndexed { index, color -> - addRadioButton(index, color) + view.color_grid.apply { + val width = activity.resources.getDimensionPixelSize(R.dimen.smaller_icon_size) + val spacing = activity.resources.getDimensionPixelSize(R.dimen.small_margin) * 2 + layoutManager = AutoGridLayoutManager(context = activity, itemWidth = width + spacing) + adapter = colorAdapter } - wasInit = true activity.getAlertDialogBuilder() + .setNegativeButton(R.string.cancel, null) .apply { - activity.setupDialogStuff(view, this) { alertDialog -> - dialog = alertDialog + activity.setupDialogStuff(view, this, R.string.color) { + dialog = it } if (colors.isEmpty()) { @@ -47,35 +41,12 @@ class SelectEventTypeColorDialog(val activity: Activity, val eventType: EventTyp } } - private fun addRadioButton(index: Int, color: Int) { - val view = activity.layoutInflater.inflate(R.layout.radio_button_with_color, null) - (view.dialog_radio_button as RadioButton).apply { - text = if (color == 0) activity.getString(R.string.transparent) else String.format("#%06X", 0xFFFFFF and color) - isChecked = color == eventType.color - id = index - } - - view.dialog_radio_color.setFillWithStroke(color, activity.getProperBackgroundColor()) - view.setOnClickListener { - viewClicked(color) - } - radioGroup.addView(view, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)) - } - - private fun viewClicked(color: Int) { - if (!wasInit) { - return - } - - callback(color) - dialog?.dismiss() - } - private fun showCustomColorPicker() { - ColorPickerDialog(activity, eventType.color) { wasPositivePressed, color -> + ColorPickerDialog(activity, currentColor) { wasPositivePressed, color -> if (wasPositivePressed) { callback(color) } + dialog?.dismiss() } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt index 5a0a3cf53..9f5bb195f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHelper.kt @@ -85,8 +85,9 @@ class CalDAVHelper(val context: Context) { val accountType = cursor.getStringValue(Calendars.ACCOUNT_TYPE) val ownerName = cursor.getStringValue(Calendars.OWNER_ACCOUNT) ?: "" val color = cursor.getIntValue(Calendars.CALENDAR_COLOR) + val displayColor = getDisplayColorFromColor(color) val accessLevel = cursor.getIntValue(Calendars.CALENDAR_ACCESS_LEVEL) - val calendar = CalDAVCalendar(id, displayName, accountName, accountType, ownerName, color, accessLevel) + val calendar = CalDAVCalendar(id, displayName, accountName, accountType, ownerName, displayColor, accessLevel) calendars.add(calendar) } diff --git a/app/src/main/res/layout/dialog_select_event_color.xml b/app/src/main/res/layout/dialog_select_color.xml similarity index 100% rename from app/src/main/res/layout/dialog_select_event_color.xml rename to app/src/main/res/layout/dialog_select_color.xml diff --git a/app/src/main/res/layout/dialog_select_event_type_color.xml b/app/src/main/res/layout/dialog_select_event_type_color.xml deleted file mode 100644 index a4960b141..000000000 --- a/app/src/main/res/layout/dialog_select_event_type_color.xml +++ /dev/null @@ -1,45 +0,0 @@ - - - - - - - - - - - - - From 2452f050a99830b92370d3a98f99676b3321deab Mon Sep 17 00:00:00 2001 From: Tibor Kaputa Date: Tue, 11 Apr 2023 15:21:07 +0200 Subject: [PATCH 15/16] adding some spacing --- .../calendar/pro/activities/EventActivity.kt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt index d0b5d94b8..e067183de 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt @@ -842,6 +842,7 @@ class EventActivity : SimpleActivity() { } else { mEventColor } + SelectEventColorDialog(activity = this, colors = eventColors, currentColor = currentColor) { newColor -> if (newColor != currentColor) { mEventColor = newColor @@ -986,7 +987,10 @@ class EventActivity : SimpleActivity() { updateAvailabilityImage() } } - event_caldav_color_holder.setOnClickListener { showEventColorDialog() } + + event_caldav_color_holder.setOnClickListener { + showEventColorDialog() + } } else { updateCurrentCalendarInfo(null) } @@ -1013,6 +1017,7 @@ class EventActivity : SimpleActivity() { event_caldav_calendar_holder.apply { setPadding(paddingLeft, mediumMargin, paddingRight, mediumMargin) } + event_caldav_color_image.beGone() event_caldav_color_holder.beGone() event_caldav_color_divider.beGone() From 888f69ddf3750cc66f46078b5f7a158fe0efffb1 Mon Sep 17 00:00:00 2001 From: Tibor Kaputa Date: Tue, 11 Apr 2023 15:21:33 +0200 Subject: [PATCH 16/16] merging 2 lines --- .../calendar/pro/adapters/CheckableColorAdapter.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/adapters/CheckableColorAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/adapters/CheckableColorAdapter.kt index a77584e32..9305aed01 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/adapters/CheckableColorAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/adapters/CheckableColorAdapter.kt @@ -15,8 +15,7 @@ class CheckableColorAdapter(private val activity: Activity, private val colors: RecyclerView.Adapter() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CheckableColorViewHolder { - val itemView = LayoutInflater.from(activity) - .inflate(R.layout.checkable_color_button, parent, false) + val itemView = LayoutInflater.from(activity).inflate(R.layout.checkable_color_button, parent, false) return CheckableColorViewHolder(itemView) }