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() {
hideKeyboard()
SelectEventTypeDialog(this, mEventTypeId) {
mEventTypeId = it
SelectEventTypeDialog(this, mEventTypeId, false) {
mEventTypeId = it.id
updateEventType()
}
}

View File

@ -355,7 +355,7 @@ class MainActivity : SimpleActivity(), NavigationListener, RefreshRecyclerViewLi
val eventType = EventType(0, holidays, resources.getColor(R.color.default_holidays_color))
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)
if (result != IcsImporter.ImportResult.IMPORT_FAIL) {
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) {
var currEventTypeId = DBHelper.REGULAR_EVENT_TYPE_ID
var currEventTypeCalDAVCalendarId = 0
init {
val view = (activity.layoutInflater.inflate(R.layout.dialog_import_events, null) as ViewGroup).apply {
updateEventType(this)
import_event_type_holder.setOnClickListener {
SelectEventTypeDialog(activity, currEventTypeId) {
currEventTypeId = it
SelectEventTypeDialog(activity, currEventTypeId, true) {
currEventTypeId = it.id
currEventTypeCalDAVCalendarId = it.caldavCalendarId
updateEventType(this)
}
}
@ -36,7 +38,7 @@ class ImportEventsDialog(val activity: SimpleActivity, val path: String, val cal
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
activity.toast(R.string.importing)
Thread {
val result = IcsImporter(activity).importEvents(path, currEventTypeId)
val result = IcsImporter(activity).importEvents(path, currEventTypeId, currEventTypeCalDAVCalendarId)
handleParseResult(result)
dismiss()
}.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 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 dialog: AlertDialog?
@ -33,10 +34,11 @@ class SelectEventTypeDialog(val activity: Activity, val currEventType: Int, val
activity.dbHelper.getEventTypes {
eventTypes = it
activity.runOnUiThread {
eventTypes.filter { it.caldavCalendarId == 0 }.forEach {
addRadioButton(it.getDisplayTitle(), it.id, it.color)
eventTypes.filter { showCalDAVCalendars || it.caldavCalendarId == 0 }.forEach {
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
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)
(view.dialog_radio_button as RadioButton).apply {
text = title
isChecked = typeId == currEventType
id = typeId
text = eventType.getDisplayTitle()
isChecked = eventType.id == currEventType
id = eventType.id
}
if (color != Color.TRANSPARENT)
view.dialog_radio_color.setBackgroundWithStroke(color, activity.config.backgroundColor)
if (eventType.color != Color.TRANSPARENT) {
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))
}
private fun viewClicked(typeId: Int) {
if (!wasInit)
private fun viewClicked(eventType: EventType) {
if (!wasInit) {
return
}
if (typeId == NEW_TYPE_ID) {
if (eventType.id == NEW_TYPE_ID) {
UpdateEventTypeDialog(activity) {
callback(it)
activity.hideKeyboard()
dialog?.dismiss()
}
} else {
callback(typeId)
callback(eventType)
dialog?.dismiss()
}
}

View File

@ -15,7 +15,7 @@ import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.value
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
init {
@ -65,15 +65,15 @@ class UpdateEventTypeDialog(val activity: Activity, var eventType: EventType? =
if (eventType!!.caldavCalendarId != 0)
eventType!!.caldavDisplayName = title
val eventTypeId = if (isNewEvent) {
eventType!!.id = if (isNewEvent) {
activity.dbHelper.insertEventType(eventType!!)
} else {
activity.dbHelper.updateEventType(eventType!!)
}
if (eventTypeId != -1) {
if (eventType!!.id != -1) {
dismiss()
callback(eventTypeId)
callback(eventType!!)
} else {
activity.toast(R.string.editing_calendar_failed)
}

View File

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