fix #316, allow importing ics events directly in a caldav calendar

This commit is contained in:
tibbi 2018-01-19 18:07:06 +01:00
parent 339e307bf6
commit a9ac3a955f
6 changed files with 41 additions and 31 deletions

View File

@ -385,8 +385,8 @@ class EventActivity : SimpleActivity() {
private fun showEventTypeDialog() { private fun showEventTypeDialog() {
hideKeyboard() hideKeyboard()
SelectEventTypeDialog(this, mEventTypeId) { SelectEventTypeDialog(this, mEventTypeId, false) {
mEventTypeId = it mEventTypeId = it.id
updateEventType() updateEventType()
} }
} }

View File

@ -355,7 +355,7 @@ class MainActivity : SimpleActivity(), NavigationListener, RefreshRecyclerViewLi
val eventType = EventType(0, holidays, resources.getColor(R.color.default_holidays_color)) val eventType = EventType(0, holidays, resources.getColor(R.color.default_holidays_color))
eventTypeId = dbHelper.insertEventType(eventType) eventTypeId = dbHelper.insertEventType(eventType)
} }
val result = IcsImporter(this).importEvents(it as String, eventTypeId) val result = IcsImporter(this).importEvents(it as String, eventTypeId, 0)
handleParseResult(result) handleParseResult(result)
if (result != IcsImporter.ImportResult.IMPORT_FAIL) { if (result != IcsImporter.ImportResult.IMPORT_FAIL) {
runOnUiThread { runOnUiThread {

View File

@ -16,13 +16,15 @@ import kotlinx.android.synthetic.main.dialog_import_events.view.*
class ImportEventsDialog(val activity: SimpleActivity, val path: String, val callback: (refreshView: Boolean) -> Unit) { class ImportEventsDialog(val activity: SimpleActivity, val path: String, val callback: (refreshView: Boolean) -> Unit) {
var currEventTypeId = DBHelper.REGULAR_EVENT_TYPE_ID var currEventTypeId = DBHelper.REGULAR_EVENT_TYPE_ID
var currEventTypeCalDAVCalendarId = 0
init { init {
val view = (activity.layoutInflater.inflate(R.layout.dialog_import_events, null) as ViewGroup).apply { val view = (activity.layoutInflater.inflate(R.layout.dialog_import_events, null) as ViewGroup).apply {
updateEventType(this) updateEventType(this)
import_event_type_holder.setOnClickListener { import_event_type_holder.setOnClickListener {
SelectEventTypeDialog(activity, currEventTypeId) { SelectEventTypeDialog(activity, currEventTypeId, true) {
currEventTypeId = it currEventTypeId = it.id
currEventTypeCalDAVCalendarId = it.caldavCalendarId
updateEventType(this) updateEventType(this)
} }
} }
@ -36,7 +38,7 @@ class ImportEventsDialog(val activity: SimpleActivity, val path: String, val cal
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
activity.toast(R.string.importing) activity.toast(R.string.importing)
Thread { Thread {
val result = IcsImporter(activity).importEvents(path, currEventTypeId) val result = IcsImporter(activity).importEvents(path, currEventTypeId, currEventTypeCalDAVCalendarId)
handleParseResult(result) handleParseResult(result)
dismiss() dismiss()
}.start() }.start()

View File

@ -18,7 +18,8 @@ import kotlinx.android.synthetic.main.dialog_select_radio_group.view.*
import kotlinx.android.synthetic.main.radio_button_with_color.view.* import kotlinx.android.synthetic.main.radio_button_with_color.view.*
import java.util.* import java.util.*
class SelectEventTypeDialog(val activity: Activity, val currEventType: Int, val callback: (checkedId: Int) -> Unit) { class SelectEventTypeDialog(val activity: Activity, val currEventType: Int, val showCalDAVCalendars: Boolean,
val callback: (eventType: EventType) -> Unit) {
private val NEW_TYPE_ID = -2 private val NEW_TYPE_ID = -2
private val dialog: AlertDialog? private val dialog: AlertDialog?
@ -33,10 +34,11 @@ class SelectEventTypeDialog(val activity: Activity, val currEventType: Int, val
activity.dbHelper.getEventTypes { activity.dbHelper.getEventTypes {
eventTypes = it eventTypes = it
activity.runOnUiThread { activity.runOnUiThread {
eventTypes.filter { it.caldavCalendarId == 0 }.forEach { eventTypes.filter { showCalDAVCalendars || it.caldavCalendarId == 0 }.forEach {
addRadioButton(it.getDisplayTitle(), it.id, it.color) addRadioButton(it)
} }
addRadioButton(activity.getString(R.string.add_new_type), NEW_TYPE_ID, Color.TRANSPARENT) val newEventType = EventType(NEW_TYPE_ID, activity.getString(R.string.add_new_type), Color.TRANSPARENT, 0)
addRadioButton(newEventType)
wasInit = true wasInit = true
activity.updateTextColors(view.dialog_radio_holder) activity.updateTextColors(view.dialog_radio_holder)
} }
@ -48,33 +50,35 @@ class SelectEventTypeDialog(val activity: Activity, val currEventType: Int, val
} }
} }
private fun addRadioButton(title: String, typeId: Int, color: Int) { private fun addRadioButton(eventType: EventType) {
val view = activity.layoutInflater.inflate(R.layout.radio_button_with_color, null) val view = activity.layoutInflater.inflate(R.layout.radio_button_with_color, null)
(view.dialog_radio_button as RadioButton).apply { (view.dialog_radio_button as RadioButton).apply {
text = title text = eventType.getDisplayTitle()
isChecked = typeId == currEventType isChecked = eventType.id == currEventType
id = typeId id = eventType.id
} }
if (color != Color.TRANSPARENT) if (eventType.color != Color.TRANSPARENT) {
view.dialog_radio_color.setBackgroundWithStroke(color, activity.config.backgroundColor) view.dialog_radio_color.setBackgroundWithStroke(eventType.color, activity.config.backgroundColor)
}
view.setOnClickListener { viewClicked(typeId) } view.setOnClickListener { viewClicked(eventType) }
radioGroup.addView(view, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)) radioGroup.addView(view, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))
} }
private fun viewClicked(typeId: Int) { private fun viewClicked(eventType: EventType) {
if (!wasInit) if (!wasInit) {
return return
}
if (typeId == NEW_TYPE_ID) { if (eventType.id == NEW_TYPE_ID) {
UpdateEventTypeDialog(activity) { UpdateEventTypeDialog(activity) {
callback(it) callback(it)
activity.hideKeyboard() activity.hideKeyboard()
dialog?.dismiss() dialog?.dismiss()
} }
} else { } else {
callback(typeId) callback(eventType)
dialog?.dismiss() dialog?.dismiss()
} }
} }

View File

@ -15,7 +15,7 @@ import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.value import com.simplemobiletools.commons.extensions.value
import kotlinx.android.synthetic.main.dialog_event_type.view.* import kotlinx.android.synthetic.main.dialog_event_type.view.*
class UpdateEventTypeDialog(val activity: Activity, var eventType: EventType? = null, val callback: (eventTypeId: Int) -> Unit) { class UpdateEventTypeDialog(val activity: Activity, var eventType: EventType? = null, val callback: (eventType: EventType) -> Unit) {
var isNewEvent = eventType == null var isNewEvent = eventType == null
init { init {
@ -65,15 +65,15 @@ class UpdateEventTypeDialog(val activity: Activity, var eventType: EventType? =
if (eventType!!.caldavCalendarId != 0) if (eventType!!.caldavCalendarId != 0)
eventType!!.caldavDisplayName = title eventType!!.caldavDisplayName = title
val eventTypeId = if (isNewEvent) { eventType!!.id = if (isNewEvent) {
activity.dbHelper.insertEventType(eventType!!) activity.dbHelper.insertEventType(eventType!!)
} else { } else {
activity.dbHelper.updateEventType(eventType!!) activity.dbHelper.updateEventType(eventType!!)
} }
if (eventTypeId != -1) { if (eventType!!.id != -1) {
dismiss() dismiss()
callback(eventTypeId) callback(eventType!!)
} else { } else {
activity.toast(R.string.editing_calendar_failed) activity.toast(R.string.editing_calendar_failed)
} }

View File

@ -39,7 +39,7 @@ class IcsImporter(val activity: SimpleActivity) {
private var eventsImported = 0 private var eventsImported = 0
private var eventsFailed = 0 private var eventsFailed = 0
fun importEvents(path: String, defaultEventType: Int): ImportResult { fun importEvents(path: String, defaultEventType: Int, calDAVCalendarId: Int): ImportResult {
try { try {
val existingEvents = activity.dbHelper.getEventsWithImportIds() val existingEvents = activity.dbHelper.getEventsWithImportIds()
var prevLine = "" var prevLine = ""
@ -112,30 +112,34 @@ class IcsImporter(val activity: SimpleActivity) {
curReminderMinutes.add(curReminderTriggerMinutes) curReminderMinutes.add(curReminderTriggerMinutes)
} }
} else if (line == END_EVENT) { } else if (line == END_EVENT) {
if (curStart != -1 && curEnd == -1) if (curStart != -1 && curEnd == -1) {
curEnd = curStart curEnd = curStart
}
if (curTitle.isEmpty() || curStart == -1) if (curTitle.isEmpty() || curStart == -1) {
continue continue
}
val eventToUpdate = existingEvents.firstOrNull { curImportId.isNotEmpty() && curImportId == it.importId } val eventToUpdate = existingEvents.firstOrNull { curImportId.isNotEmpty() && curImportId == it.importId }
if (eventToUpdate != null && eventToUpdate.lastUpdated >= curLastModified) { if (eventToUpdate != null && eventToUpdate.lastUpdated >= curLastModified) {
continue continue
} }
val source = if (calDAVCalendarId == 0) SOURCE_IMPORTED_ICS else "$CALDAV-$calDAVCalendarId"
val event = Event(0, curStart, curEnd, curTitle, curDescription, curReminderMinutes.getOrElse(0, { -1 }), val event = Event(0, curStart, curEnd, curTitle, curDescription, curReminderMinutes.getOrElse(0, { -1 }),
curReminderMinutes.getOrElse(1, { -1 }), curReminderMinutes.getOrElse(2, { -1 }), curRepeatInterval, curReminderMinutes.getOrElse(1, { -1 }), curReminderMinutes.getOrElse(2, { -1 }), curRepeatInterval,
curImportId, curFlags, curRepeatLimit, curRepeatRule, curEventType, lastUpdated = curLastModified, curImportId, curFlags, curRepeatLimit, curRepeatRule, curEventType, lastUpdated = curLastModified,
source = SOURCE_IMPORTED_ICS, location = curLocation) source = source, location = curLocation)
if (event.getIsAllDay() && curEnd > curStart) { if (event.getIsAllDay() && curEnd > curStart) {
event.endTS -= DAY event.endTS -= DAY
} }
if (eventToUpdate == null) { if (eventToUpdate == null) {
activity.dbHelper.insert(event, false) { activity.dbHelper.insert(event, true) {
for (exceptionTS in curRepeatExceptions) { for (exceptionTS in curRepeatExceptions) {
activity.dbHelper.addEventRepeatException(it, exceptionTS, false) activity.dbHelper.addEventRepeatException(it, exceptionTS, true)
} }
existingEvents.add(event) existingEvents.add(event)
} }