-
Notifications
You must be signed in to change notification settings - Fork 4
[jtx rewrite] Add Handlers for Recurrence Fields #410
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
Open
ArnyminerZ
wants to merge
8
commits into
main
Choose a base branch
from
409-jtx-rewrite-add-handlers-for-recurrence-fields
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.
+596
−3
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3fb2913
Add Handlers for Recurrence Fields
ArnyminerZ e644566
Convert into Instant
ArnyminerZ 4b7fc55
Improve fallback behavior
ArnyminerZ ba38538
Ignore empty recurrences
ArnyminerZ a80c07b
Fix RDATEs not being implemented
ArnyminerZ 2413535
Add specific handling for `Z` timezone -> UTC
ArnyminerZ adadc23
Potential fix for pull request finding
ArnyminerZ 046f920
With TZID handling
ArnyminerZ 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
146 changes: 146 additions & 0 deletions
146
lib/src/main/kotlin/at/bitfire/synctools/mapping/jtx/handler/RecurrenceFieldsHandler.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,146 @@ | ||
| /* | ||
| * 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.jtx.handler | ||
|
|
||
| import android.content.Entity | ||
| import at.bitfire.synctools.icalendar.plusAssign | ||
| import at.techbee.jtx.JtxContract | ||
| import at.techbee.jtx.JtxContract.JtxICalObject.TZ_ALLDAY | ||
| import net.fortuna.ical4j.model.DateList | ||
| import net.fortuna.ical4j.model.ParameterList | ||
| import net.fortuna.ical4j.model.component.CalendarComponent | ||
| import net.fortuna.ical4j.model.parameter.TzId | ||
| import net.fortuna.ical4j.model.parameter.Value | ||
| import net.fortuna.ical4j.model.property.DateListProperty | ||
| import net.fortuna.ical4j.model.property.ExDate | ||
| import net.fortuna.ical4j.model.property.RDate | ||
| import net.fortuna.ical4j.model.property.RRule | ||
| import net.fortuna.ical4j.model.property.RecurrenceId | ||
| import java.time.Instant | ||
| import java.time.LocalDate | ||
| import java.time.LocalDateTime | ||
| import java.time.ZoneId | ||
| import java.time.ZoneOffset | ||
| import java.time.ZonedDateTime | ||
| import java.time.temporal.Temporal | ||
| import java.util.logging.Level | ||
| import java.util.logging.Logger | ||
|
|
||
| /** | ||
| * Handler for recurrence-related fields of a jtx Board content provider data row. | ||
| */ | ||
| class RecurrenceFieldsHandler : JtxFieldHandler { | ||
|
|
||
| private val logger | ||
| get() = Logger.getLogger(javaClass.name) | ||
|
|
||
| override fun process(from: Entity, main: Entity, to: CalendarComponent) { | ||
| if (from === main) { | ||
| processMain(from, to) | ||
| } else { | ||
| processException(from, to) | ||
| } | ||
| } | ||
|
|
||
| private fun processMain(from: Entity, to: CalendarComponent) { | ||
| val values = from.entityValues | ||
| val dtstartTimezone = values.getAsString(JtxContract.JtxICalObject.DTSTART_TIMEZONE) | ||
|
|
||
| values.getAsString(JtxContract.JtxICalObject.RRULE)?.takeIf { it.isNotBlank() }?.let { rruleString -> | ||
| try { | ||
| to += RRule<Temporal>(rruleString) | ||
| } catch (e: Exception) { | ||
| logger.log(Level.WARNING, "Couldn't parse RRULE, ignoring", e) | ||
| } | ||
| } | ||
|
|
||
| values.getAsString(JtxContract.JtxICalObject.RDATE)?.takeIf { it.isNotBlank() }?.let { rdateString -> | ||
| try { | ||
| val timestamps = JtxContract.getLongListFromString(rdateString) | ||
| if (timestamps.isNotEmpty()) { | ||
| to += timestampsToProperty(timestamps, dtstartTimezone) { RDate(it) } | ||
| } | ||
|
ArnyminerZ marked this conversation as resolved.
|
||
| } catch (e: Exception) { | ||
| logger.log(Level.WARNING, "Couldn't parse RDATE, ignoring", e) | ||
| } | ||
| } | ||
|
|
||
| values.getAsString(JtxContract.JtxICalObject.EXDATE)?.takeIf { it.isNotBlank() }?.let { exdateString -> | ||
| try { | ||
| val timestamps = JtxContract.getLongListFromString(exdateString) | ||
| if (timestamps.isNotEmpty()) { | ||
| to += timestampsToProperty(timestamps, dtstartTimezone) { ExDate(it) } | ||
| } | ||
| } catch (e: Exception) { | ||
| logger.log(Level.WARNING, "Couldn't parse EXDATE, ignoring", e) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun processException(from: Entity, to: CalendarComponent) { | ||
| val values = from.entityValues | ||
| val recurid = values.getAsString(JtxContract.JtxICalObject.RECURID) ?: return | ||
| val recuridTimezone = values.getAsString(JtxContract.JtxICalObject.RECURID_TIMEZONE) | ||
|
|
||
| try { | ||
| to += if (recuridTimezone == TZ_ALLDAY || recuridTimezone.isNullOrEmpty() || recuridTimezone == ZoneOffset.UTC.id) { | ||
| RecurrenceId<Temporal>(recurid) | ||
| } else { | ||
| RecurrenceId<Temporal>(ParameterList(listOf(TzId(recuridTimezone))), recurid) | ||
| } | ||
|
ArnyminerZ marked this conversation as resolved.
|
||
| } catch (e: Exception) { | ||
| logger.log(Level.WARNING, "Couldn't parse RECURID, ignoring", e) | ||
| } | ||
| } | ||
|
|
||
| private fun <T: DateListProperty<*>> timestampsToProperty( | ||
| timestamps: List<Long>, | ||
| dtstartTimezone: String?, | ||
| factory: (DateList<*>) -> T | ||
| ): T { | ||
| val dateList = timestampsToDateList(timestamps, dtstartTimezone) | ||
| val property = factory(dateList) | ||
| when (val first = dateList.dates.firstOrNull()) { | ||
| is ZonedDateTime -> property.add<T>(TzId(first.zone.id)) | ||
| is LocalDate -> property.add<T>(Value.DATE) | ||
| } | ||
| return property | ||
| } | ||
|
|
||
| private fun timestampsToDateList(timestamps: List<Long>, dtstartTimezone: String?): DateList<*> = | ||
| when { | ||
| dtstartTimezone == TZ_ALLDAY -> | ||
| DateList(timestamps.map { | ||
| LocalDate.ofInstant(Instant.ofEpochMilli(it), ZoneOffset.UTC) | ||
| }) | ||
| dtstartTimezone == ZoneOffset.UTC.id -> | ||
| DateList(timestamps.map { | ||
| Instant.ofEpochMilli(it) | ||
| }) | ||
| dtstartTimezone.isNullOrEmpty() -> | ||
| DateList(timestamps.map { | ||
| LocalDateTime.ofInstant(Instant.ofEpochMilli(it), ZoneId.systemDefault()) | ||
| }) | ||
| else -> { | ||
| val zone = try { | ||
| ZoneId.of(dtstartTimezone) | ||
| } catch (e: Exception) { | ||
| logger.log(Level.WARNING, "Invalid DTSTART_TIMEZONE '$dtstartTimezone', falling back to UTC", e) | ||
| null | ||
| } | ||
| if (zone != null) { | ||
| DateList(timestamps.map { | ||
| ZonedDateTime.ofInstant(Instant.ofEpochMilli(it), zone) | ||
| }) | ||
| } else { | ||
| DateList(timestamps.map { | ||
| Instant.ofEpochMilli(it) | ||
| }) | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.