diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/SelectEventTypeColorDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/SelectEventTypeColorDialog.kt new file mode 100644 index 000000000..2af69d1a7 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/SelectEventTypeColorDialog.kt @@ -0,0 +1,61 @@ +package com.simplemobiletools.calendar.dialogs + +import android.app.Activity +import android.graphics.Color +import android.support.v7.app.AlertDialog +import android.view.ViewGroup +import android.widget.RadioButton +import android.widget.RadioGroup +import com.simplemobiletools.calendar.R +import com.simplemobiletools.calendar.extensions.config +import com.simplemobiletools.calendar.helpers.CalDAVHandler +import com.simplemobiletools.calendar.models.EventType +import com.simplemobiletools.commons.extensions.setBackgroundWithStroke +import com.simplemobiletools.commons.extensions.setupDialogStuff +import kotlinx.android.synthetic.main.dialog_select_radio_group.view.* +import kotlinx.android.synthetic.main.radio_button_with_color.view.* + +class SelectEventTypeColorDialog(val activity: Activity, val eventType: EventType, val callback: (color: Int) -> Unit) { + private val dialog: AlertDialog? + private val radioGroup: RadioGroup + private var wasInit = false + private val colors = CalDAVHandler(activity).getAvailableCalDAVCalendarColors(eventType) + + init { + val view = activity.layoutInflater.inflate(R.layout.dialog_select_radio_group, null) as ViewGroup + radioGroup = view.dialog_radio_group + + colors.forEachIndexed { index, value -> + addRadioButton(index, value) + } + + wasInit = true + dialog = AlertDialog.Builder(activity) + .create().apply { + activity.setupDialogStuff(view, this) + } + } + + 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 = String.format("#%06X", 0xFFFFFF and color) + isChecked = color == eventType.color + id = colorKey + } + + if (color != Color.TRANSPARENT) + view.dialog_radio_color.setBackgroundWithStroke(color, activity.config.backgroundColor) + + view.setOnClickListener { viewClicked(colorKey) } + radioGroup.addView(view, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)) + } + + private fun viewClicked(colorKey: Int) { + if (!wasInit) + return + + callback.invoke(colors[colorKey]) + dialog?.dismiss() + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/UpdateEventTypeDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/UpdateEventTypeDialog.kt index 23ad23966..010d58ece 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/UpdateEventTypeDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/UpdateEventTypeDialog.kt @@ -26,9 +26,16 @@ class UpdateEventTypeDialog(val activity: Activity, var eventType: EventType? = setupColor(type_color) type_title.setText(eventType!!.title) type_color.setOnClickListener { - ColorPickerDialog(activity, eventType!!.color) { - eventType!!.color = it - setupColor(type_color) + if (eventType?.caldavCalendarId == 0) { + ColorPickerDialog(activity, eventType!!.color) { + eventType!!.color = it + setupColor(type_color) + } + } else { + SelectEventTypeColorDialog(activity, eventType!!) { + eventType!!.color = it + setupColor(type_color) + } } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVHandler.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVHandler.kt index cb43fd2cb..a56bebf1d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVHandler.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/CalDAVHandler.kt @@ -73,7 +73,9 @@ class CalDAVHandler(val context: Context) { } private fun fillCalendarContentValues(eventType: EventType): ContentValues { + val colorKey = getEventTypeColorKey(eventType) return ContentValues().apply { + put(CalendarContract.Calendars.CALENDAR_COLOR_KEY, colorKey) put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, eventType.title) } } @@ -94,9 +96,10 @@ class CalDAVHandler(val context: Context) { cursor?.close() } - return insertNewColor(eventType) + return -1 } + // it doesnt work properly, needs better SyncAdapter handling private fun insertNewColor(eventType: EventType): Int { val maxId = getMaxColorId(eventType) + 1 @@ -143,6 +146,28 @@ class CalDAVHandler(val context: Context) { return maxId } + fun getAvailableCalDAVCalendarColors(eventType: EventType): ArrayList { + val colors = ArrayList() + val uri = CalendarContract.Colors.CONTENT_URI + val projection = arrayOf(CalendarContract.Colors.COLOR) + val selection = "${CalendarContract.Colors.COLOR_TYPE} = ? AND ${CalendarContract.Colors.ACCOUNT_NAME} = ?" + val selectionArgs = arrayOf(CalendarContract.Colors.TYPE_CALENDAR.toString(), eventType.caldavEmail) + + var cursor: Cursor? = null + try { + cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null) + if (cursor != null && cursor.moveToFirst()) { + do { + colors.add(cursor.getIntValue(CalendarContract.Colors.COLOR)) + } while (cursor.moveToNext()) + } + } finally { + cursor?.close() + } + + return colors + } + private fun fetchCalDAVCalendarEvents(calendarId: Int, eventTypeId: Int) { val importIdsMap = HashMap() val fetchedEventIds = ArrayList()