Skip to content

Add new event Entity builder logic that is used beside legacy builder #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -523,15 +523,6 @@ class LegacyAndroidEventBuilder2Test {
assertEquals(TimeZones.UTC_ID, entity.entityValues.get(Events.EVENT_END_TIMEZONE))
}

@Test
fun testBuildEvent_Summary() {
buildEvent(true) {
summary = "Sample Summary"
}.let { result ->
assertEquals("Sample Summary", result.entityValues.getAsString(Events.TITLE))
}
}

@Test
fun testBuildEvent_Location() {
buildEvent(true) {
Expand Down Expand Up @@ -1290,7 +1281,6 @@ class LegacyAndroidEventBuilder2Test {
assertEquals(1594038600000L, exception.getAsLong(Events.DTSTART))
assertEquals(tzShanghai.id, exception.getAsString(Events.EVENT_TIMEZONE))
assertEquals(0, exception.getAsInteger(Events.ALL_DAY))
assertEquals("Event moved to one hour later", exception.getAsString(Events.TITLE))
}
}
}
Expand Down Expand Up @@ -1318,7 +1308,6 @@ class LegacyAndroidEventBuilder2Test {
assertEquals(1594038600000L, exception.getAsLong(Events.DTSTART))
assertEquals(tzShanghai.id, exception.getAsString(Events.EVENT_TIMEZONE))
assertEquals(0, exception.getAsInteger(Events.ALL_DAY))
assertEquals("Event moved to one hour later", exception.getAsString(Events.TITLE))
}
}
}
Expand All @@ -1345,7 +1334,6 @@ class LegacyAndroidEventBuilder2Test {
assertEquals(1, exception.getAsInteger(Events.ORIGINAL_ALL_DAY))
assertEquals(1594031400000L, exception.getAsLong(Events.DTSTART))
assertEquals(0, exception.getAsInteger(Events.ALL_DAY))
assertEquals("Today not an all-day event", exception.getAsString(Events.TITLE))
}
}
}
Expand All @@ -1372,7 +1360,6 @@ class LegacyAndroidEventBuilder2Test {
assertEquals(1, exception.getAsInteger(Events.ORIGINAL_ALL_DAY))
assertEquals(1594031400000L, exception.getAsLong(Events.DTSTART))
assertEquals(0, exception.getAsInteger(Events.ALL_DAY))
assertEquals("Today not an all-day event", exception.getAsString(Events.TITLE))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* This file is part of bitfireAT/synctools which is released under GPLv3.
* Copyright © All Contributors. See the LICENSE and AUTHOR files in the root directory for details.
* SPDX-License-Identifier: GPL-3.0-or-later
*/

package at.bitfire.synctools.icalendar

import net.fortuna.ical4j.model.Date
import net.fortuna.ical4j.model.DateTime

fun Date.isAllDay() =
this !is DateTime
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import net.fortuna.ical4j.model.ComponentList
import net.fortuna.ical4j.model.Property
import net.fortuna.ical4j.model.PropertyList
import net.fortuna.ical4j.model.component.CalendarComponent
import net.fortuna.ical4j.model.property.RDate
import net.fortuna.ical4j.model.property.RRule
import net.fortuna.ical4j.model.property.RecurrenceId
import net.fortuna.ical4j.model.property.Sequence
import net.fortuna.ical4j.model.property.Uid
Expand Down Expand Up @@ -42,3 +44,6 @@ val CalendarComponent.recurrenceId: RecurrenceId?

val CalendarComponent.sequence: Sequence?
get() = getProperty(Property.SEQUENCE)

fun CalendarComponent.isRecurring() =
getProperties<RRule>(Property.RRULE).isNotEmpty() || getProperties<RDate>(Property.RDATE).isNotEmpty()
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* This file is part of bitfireAT/synctools which is released under GPLv3.
* Copyright © All Contributors. See the LICENSE and AUTHOR files in the root directory for details.
* SPDX-License-Identifier: GPL-3.0-or-later
*/

package at.bitfire.synctools.mapping.calendar

import android.content.ContentValues
import android.content.Entity
import androidx.annotation.VisibleForTesting
import at.bitfire.ical4android.Event
import at.bitfire.synctools.icalendar.AssociatedEvents
import at.bitfire.synctools.icalendar.isRecurring
import at.bitfire.synctools.mapping.calendar.builder.AndroidEventFieldBuilder
import at.bitfire.synctools.mapping.calendar.builder.OriginalInstanceTimeBuilder
import at.bitfire.synctools.mapping.calendar.builder.TitleBuilder
import at.bitfire.synctools.storage.calendar.AndroidCalendar
import at.bitfire.synctools.storage.calendar.EventAndExceptions
import net.fortuna.ical4j.model.component.VEvent

/**
* Maps
*
* - an iCalendar object that contains VEVENTs and exceptions ([AssociatedEvents]) to
* - Android event data rows (including exceptions → [EventAndExceptions]).
*
* The built fields must contain `null` values for empty fields so that they can be used for updates.
*
* _Migration note:_ Until a fields are built by [AndroidEventFieldBuilder]s, the rest is still
* generated by [LegacyAndroidEventBuilder2].
*/
class AndroidEventBuilder(
// AndroidEvent-level fields
private val syncId: String,
private val eTag: String?,
private val scheduleTag: String?,
private val flags: Int,

// for new builders
private val associatedEvents: AssociatedEvents,

// only for legacy builder
private val androidCalendar: AndroidCalendar,
private val event: Event,
private val id: Long?
) {

fun build(): EventAndExceptions? {
// start with values from new builders
val eventAndExceptions = buildNew()
if (eventAndExceptions == null) {
// builder has returned null to indicate that the main Entity must be discarded.
// Without a main Entity, EventAndExceptions can't be generated, so return null here.
return null
}

// merge with legacy EventAndExceptions
val legacyEventAndExceptions = LegacyAndroidEventBuilder2(
androidCalendar, event, id, syncId, eTag, scheduleTag, flags
).build()

// ORIGINAL_INSTANCE_TIME must be set and the same for exceptions to be merged!
// So ORIGINAL_INSTANCE_TIME must always be provided by both the old and the new builder.
eventAndExceptions.mergeFrom(legacyEventAndExceptions)

return eventAndExceptions
}

private fun buildNew(): EventAndExceptions? {
val mainEvent = associatedEvents.main ?: fakeMainEvent(associatedEvents.exceptions)
val mayHaveExceptions = mainEvent.isRecurring()

return EventAndExceptions(
main = buildEvent(from = mainEvent, main = mainEvent) ?: return null,
exceptions =
if (mayHaveExceptions)
associatedEvents.exceptions.mapNotNull { exception ->
buildEvent(from = exception, main = mainEvent)
}
else
emptyList()
)
}

@VisibleForTesting
internal fun buildEvent(from: VEvent, main: VEvent): Entity? {
val result = Entity(ContentValues())
for (builder in getBuilders())
if (!builder.build(from = from, main = main, to = result)) {
// discard entity when builder returns null
return null
}
return result
}

fun getBuilders(): List<AndroidEventFieldBuilder> = listOf(
OriginalInstanceTimeBuilder(),
TitleBuilder()
)

private fun fakeMainEvent(forExceptions: List<VEvent>): VEvent = TODO()

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.provider.CalendarContract.Colors
import android.provider.CalendarContract.Events
import android.provider.CalendarContract.ExtendedProperties
import android.provider.CalendarContract.Reminders
import androidx.annotation.OpenForTesting
import androidx.core.content.contentValuesOf
import at.bitfire.ical4android.Event
import at.bitfire.ical4android.ICalendar
Expand Down Expand Up @@ -60,6 +61,7 @@ import java.util.logging.Logger
* Important: To use recurrence exceptions, you MUST set _SYNC_ID and ORIGINAL_SYNC_ID
* in populateEvent() / buildEvent. Setting _ID and ORIGINAL_ID is not sufficient.
*/
@Deprecated("Use AndroidEventBuilder instead")
class LegacyAndroidEventBuilder2(
private val calendar: AndroidCalendar,
private val event: Event,
Expand Down Expand Up @@ -129,7 +131,8 @@ class LegacyAndroidEventBuilder2(
*
* @param recurrence event to be used as data source; *null*: use this AndroidEvent's main [event] as source
*/
private fun buildEventRow(recurrence: Event?): ContentValues {
@OpenForTesting
internal fun buildEventRow(recurrence: Event?): ContentValues {
// start with object-level (AndroidEvent) fields
val row = contentValuesOf(
Events.CALENDAR_ID to calendar.id,
Expand Down Expand Up @@ -168,33 +171,35 @@ class LegacyAndroidEventBuilder2(
row.put(Events.ORIGINAL_SYNC_ID, syncId)
row.put(Events.ORIGINAL_ALL_DAY, if (DateUtils.isDate(event.dtStart)) 1 else 0)

var recurrenceDate = from.recurrenceId!!.date
val dtStartDate = event.dtStart!!.date
if (recurrenceDate is DateTime && dtStartDate !is DateTime) {
// rewrite RECURRENCE-ID;VALUE=DATE-TIME to VALUE=DATE for all-day events
val localDate = recurrenceDate.toLocalDate()
recurrenceDate = Date(localDate.toIcal4jDate())

} else if (recurrenceDate !is DateTime && dtStartDate is DateTime) {
// rewrite RECURRENCE-ID;VALUE=DATE to VALUE=DATE-TIME for non-all-day-events
val localDate = recurrenceDate.toLocalDate()
// guess time and time zone from DTSTART
val zonedTime = ZonedDateTime.of(
localDate,
dtStartDate.toLocalTime(),
dtStartDate.requireZoneId()
)
recurrenceDate = zonedTime.toIcal4jDateTime()
from.recurrenceId?.date?.let { recurrenceDate ->
var alignedRecurrenceDate = recurrenceDate
val dtStartDate = event.dtStart?.date
if (recurrenceDate is DateTime && dtStartDate !is DateTime) {
// rewrite RECURRENCE-ID;VALUE=DATE-TIME to VALUE=DATE for all-day events
val localDate = recurrenceDate.toLocalDate()
alignedRecurrenceDate = Date(localDate.toIcal4jDate())

} else if (recurrenceDate !is DateTime && dtStartDate is DateTime) {
// rewrite RECURRENCE-ID;VALUE=DATE to VALUE=DATE-TIME for non-all-day-events
val localDate = recurrenceDate.toLocalDate()
// guess time and time zone from DTSTART
val zonedTime = ZonedDateTime.of(
localDate,
dtStartDate.toLocalTime(),
dtStartDate.requireZoneId()
)
alignedRecurrenceDate = zonedTime.toIcal4jDateTime()
}
row.put(Events.ORIGINAL_INSTANCE_TIME, alignedRecurrenceDate.time)
}
row.put(Events.ORIGINAL_INSTANCE_TIME, recurrenceDate.time)
}

// UID, sequence
row.put(Events.UID_2445, from.uid)
row.put(AndroidEvent2.COLUMN_SEQUENCE, from.sequence)

// time fields
row.put(Events.DTSTART, dtStart.date.time)
row.put(Events.DTSTART, dtStart.date?.time)
row.put(Events.ALL_DAY, if (allDay) 1 else 0)
row.put(Events.EVENT_TIMEZONE, AndroidTimeUtils.storageTzId(dtStart))

Expand Down Expand Up @@ -318,7 +323,7 @@ class LegacyAndroidEventBuilder2(
}

AndroidTimeUtils.androidifyTimeZone(dtEnd, tzRegistry)
row.put(Events.DTEND, dtEnd.date.time)
row.put(Events.DTEND, dtEnd.date?.time)
row.put(Events.EVENT_END_TIMEZONE, AndroidTimeUtils.storageTzId(dtEnd))
row.putNull(Events.DURATION)
row.putNull(Events.RRULE)
Expand All @@ -328,7 +333,7 @@ class LegacyAndroidEventBuilder2(
}

// text fields
row.put(Events.TITLE, from.summary)
// Events.TITLE done by TitleBuilder
row.put(Events.EVENT_LOCATION, from.location)
row.put(Events.DESCRIPTION, from.description)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* This file is part of bitfireAT/synctools which is released under GPLv3.
* Copyright © All Contributors. See the LICENSE and AUTHOR files in the root directory for details.
* SPDX-License-Identifier: GPL-3.0-or-later
*/

package at.bitfire.synctools.mapping.calendar.builder

import android.content.Entity
import net.fortuna.ical4j.model.component.VEvent

interface AndroidEventFieldBuilder {

/**
* Maps the given event into the provided [Entity].
*
* If [from] references the same object as [main], this method is called for a main event (not an exception).
* If [from] references another object as [main], this method is called for an exception (not a main event).
*
* So the referential equality operator can be used to see if a main event or an exception is built:
*
* ```
* val buildsMainEvent = from === main
* ```
*
* @param from event to map to
* @param main main event
* @param to destination object where built values are put into
*
* @return whether the resulting entity may be used; `false`: resulting entity should be discarded
*/
fun build(from: VEvent, main: VEvent, to: Entity): Boolean

}
Loading