-
Notifications
You must be signed in to change notification settings - Fork 2
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
rfc2822
wants to merge
4
commits into
main
Choose a base branch
from
new-builders-in-legacy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
lib/src/main/kotlin/at/bitfire/synctools/icalendar/ical4jDateHelpers.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
lib/src/main/kotlin/at/bitfire/synctools/mapping/calendar/AndroidEventBuilder.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...src/main/kotlin/at/bitfire/synctools/mapping/calendar/builder/AndroidEventFieldBuilder.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.