@@ -10,6 +10,7 @@ import android.content.Entity
1010import android.provider.CalendarContract
1111import android.provider.CalendarContract.Events
1212import android.provider.CalendarContract.ExtendedProperties
13+ import androidx.annotation.VisibleForTesting
1314import at.bitfire.synctools.icalendar.AssociatedEvents
1415import at.bitfire.synctools.mapping.calendar.processor.AccessLevelProcessor
1516import at.bitfire.synctools.mapping.calendar.processor.AndroidEventFieldProcessor
@@ -23,7 +24,6 @@ import at.bitfire.synctools.mapping.calendar.processor.EndTimeProcessor
2324import at.bitfire.synctools.mapping.calendar.processor.LocationProcessor
2425import at.bitfire.synctools.mapping.calendar.processor.OrganizerProcessor
2526import at.bitfire.synctools.mapping.calendar.processor.OriginalInstanceTimeProcessor
26- import at.bitfire.synctools.mapping.calendar.processor.ProdIdGenerator
2727import at.bitfire.synctools.mapping.calendar.processor.RecurrenceFieldsProcessor
2828import at.bitfire.synctools.mapping.calendar.processor.RemindersProcessor
2929import at.bitfire.synctools.mapping.calendar.processor.SequenceProcessor
@@ -196,36 +196,43 @@ class AndroidEventProcessor(
196196 *
197197 * @return updated sequence (or *null* if sequence was not increased/modified)
198198 */
199- private fun increaseSequence (main : Entity ): Int? {
199+ @VisibleForTesting
200+ internal fun increaseSequence (main : Entity ): Int? {
200201 val mainValues = main.entityValues
202+ val currentSeq = mainValues.getAsInteger(EventsContract .COLUMN_SEQUENCE )
201203
202- val weAreOrganizer : Boolean by lazy {
203- mainValues.getAsInteger( Events . IS_ORGANIZER ) == 1
204- }
205- val groupScheduled : Boolean by lazy {
206- main.subValues.any { it.uri == CalendarContract . Attendees . CONTENT_URI }
204+ if (currentSeq == null ) {
205+ /* First upload, request to set to 0 in calendar provider after upload.
206+ We can let it empty in the Entity because then no SEQUENCE property will be generated,
207+ which is equal to SEQUENCE:0. */
208+ return 0
207209 }
208210
209- val currentSeq = mainValues.getAsInteger(EventsContract .COLUMN_SEQUENCE )
210- return when {
211- currentSeq == null -> {
212- /* First upload, request to set to 0 in calendar provider after upload.
213- We can let it empty in the Entity because then no SEQUENCE property will be generated,
214- which is equal to SEQUENCE:0. */
215- 0
216- }
217- // currentSequence != null for the following branches
218- groupScheduled && weAreOrganizer -> {
211+ val groupScheduled = main.subValues.any { it.uri == CalendarContract .Attendees .CONTENT_URI }
212+ if (groupScheduled) {
213+ val weAreOrganizer = mainValues.getAsInteger(Events .IS_ORGANIZER ) == 1
214+
215+ return if (weAreOrganizer) {
219216 /* Upload of a group-scheduled event and we are the organizer, so we increase the SEQUENCE.
220217 We also have to store it into the Entity so that it the new value will be mapped. */
221218 (currentSeq + 1 ).also { newSeq ->
222219 mainValues.put(EventsContract .COLUMN_SEQUENCE , newSeq)
223220 }
224- }
225- else -> {
226- /* Standard upload (either not group-scheduled or we are not the organizer). We don't
227- increase the SEQUENCE and so we don't have to modify the Entity, too. */
221+ } else
222+ /* Upload of a group-scheduled event and we are not the organizer, so we don't increase the SEQUENCE. */
228223 null
224+
225+ } else /* not group-scheduled */ {
226+ return if (currentSeq == 0 ) {
227+ /* The event was uploaded once and has SEQUENCE of 0 (which is mapped to an empty SEQUENCE property).
228+ We don't need to increase the SEQUENCE because the event is not group-scheduled. */
229+ null
230+ } else {
231+ /* Upload of a non-group-scheduled event where a SEQUENCE > 0 is present. Increase by one after upload.
232+ We also have to store it into the Entity so that it the new value will be mapped. */
233+ (currentSeq + 1 ).also { newSeq ->
234+ mainValues.put(EventsContract .COLUMN_SEQUENCE , newSeq)
235+ }
229236 }
230237 }
231238 }
@@ -239,7 +246,9 @@ class AndroidEventProcessor(
239246 * @return generated data object
240247 */
241248 private fun populateEvent (entity : Entity , main : Entity ): VEvent {
242- val vEvent = VEvent ()
249+ // initialization adds DTSTAMP
250+ val vEvent = VEvent (/* initialise = */ true )
251+
243252 for (processor in fieldProcessors)
244253 processor.process(from = entity, main = main, to = vEvent)
245254 return vEvent
0 commit comments