allow updating CalDAV calendar color with the predefined colors

This commit is contained in:
tibbi 2017-08-23 14:43:42 +02:00
parent 7a68938f77
commit 184bb10f72
3 changed files with 97 additions and 4 deletions

View File

@ -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()
}
}

View File

@ -26,9 +26,16 @@ class UpdateEventTypeDialog(val activity: Activity, var eventType: EventType? =
setupColor(type_color) setupColor(type_color)
type_title.setText(eventType!!.title) type_title.setText(eventType!!.title)
type_color.setOnClickListener { type_color.setOnClickListener {
ColorPickerDialog(activity, eventType!!.color) { if (eventType?.caldavCalendarId == 0) {
eventType!!.color = it ColorPickerDialog(activity, eventType!!.color) {
setupColor(type_color) eventType!!.color = it
setupColor(type_color)
}
} else {
SelectEventTypeColorDialog(activity, eventType!!) {
eventType!!.color = it
setupColor(type_color)
}
} }
} }
} }

View File

@ -73,7 +73,9 @@ class CalDAVHandler(val context: Context) {
} }
private fun fillCalendarContentValues(eventType: EventType): ContentValues { private fun fillCalendarContentValues(eventType: EventType): ContentValues {
val colorKey = getEventTypeColorKey(eventType)
return ContentValues().apply { return ContentValues().apply {
put(CalendarContract.Calendars.CALENDAR_COLOR_KEY, colorKey)
put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, eventType.title) put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, eventType.title)
} }
} }
@ -94,9 +96,10 @@ class CalDAVHandler(val context: Context) {
cursor?.close() cursor?.close()
} }
return insertNewColor(eventType) return -1
} }
// it doesnt work properly, needs better SyncAdapter handling
private fun insertNewColor(eventType: EventType): Int { private fun insertNewColor(eventType: EventType): Int {
val maxId = getMaxColorId(eventType) + 1 val maxId = getMaxColorId(eventType) + 1
@ -143,6 +146,28 @@ class CalDAVHandler(val context: Context) {
return maxId return maxId
} }
fun getAvailableCalDAVCalendarColors(eventType: EventType): ArrayList<Int> {
val colors = ArrayList<Int>()
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) { private fun fetchCalDAVCalendarEvents(calendarId: Int, eventTypeId: Int) {
val importIdsMap = HashMap<String, Event>() val importIdsMap = HashMap<String, Event>()
val fetchedEventIds = ArrayList<String>() val fetchedEventIds = ArrayList<String>()