Skip to content
Open
Changes from all commits
Commits
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
29 changes: 10 additions & 19 deletions Sources/RRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ public class RRule: NSObject {
@objc
public static func ruleFromString(_ string: String) -> RecurrenceRule? {
let string = string.trimmingCharacters(in: .whitespaces)
let rules = string.components(separatedBy: ";").compactMap { (rule) -> String? in
guard let range = string.range(of: "RRULE:"), range.lowerBound == string.startIndex else {
return nil
}
let ruleString = String(string.suffix(from: range.upperBound))
let rules = ruleString.components(separatedBy: ";").compactMap { (rule) -> String? in
if rule.isEmpty {
return nil
}
Expand All @@ -44,7 +48,6 @@ public class RRule: NSObject {

let recurrenceRule = RecurrenceRule(frequency: .daily)
var ruleFrequency: RecurrenceFrequency?
var byDayValue = ""
for rule in rules {
let ruleComponents = rule.components(separatedBy: "=")
guard ruleComponents.count == 2 else {
Expand Down Expand Up @@ -146,7 +149,6 @@ public class RRule: NSObject {
// These variables will define the weekdays where the recurrence will be applied.
// In the RFC documentation, it is specified as BYDAY, but was renamed to avoid the ambiguity of that argument.
let byweekday = ruleValue.components(separatedBy: ",").compactMap({ (string) -> EKWeekday? in
byDayValue = string
return EKWeekday.weekdayFromSymbol(string)
})
recurrenceRule.byweekday = byweekday.sorted(by: <)
Expand Down Expand Up @@ -179,33 +181,22 @@ public class RRule: NSObject {
return nil
}
recurrenceRule.frequency = frequency
guard recurrenceRule.frequency == .monthly &&
recurrenceRule.bymonthday.isEmpty &&
recurrenceRule.bysetpos.isEmpty &&
!byDayValue.isEmpty
else { return recurrenceRule }

let offsetString = byDayValue.components(separatedBy: CharacterSet.decimalDigits.inverted).joined()
if offsetString.isEmpty {
return recurrenceRule
}
var offset = Int(offsetString)!
if byDayValue.starts(with: "-") {
offset = -1 * offset
}
recurrenceRule.bysetpos = [offset]
recurrenceRule.byweekday = [EKWeekday.weekdayFromSymbol(String(byDayValue.suffix(2)))!]
return recurrenceRule
}

public static func stringFromRule(_ rule: RecurrenceRule) -> String {
var rruleString = ""
var rruleString = "RRULE:"

rruleString += "FREQ=\(rule.frequency.toString());"

let interval = max(1, rule.interval)
rruleString += "INTERVAL=\(interval);"

rruleString += "WKST=\(rule.firstDayOfWeek.toSymbol());"

rruleString += "DTSTART=\(dateFormatter.string(from: rule.startDate as Date));"

if let endDate = rule.recurrenceEnd?.endDate {
rruleString += "UNTIL=\(dateFormatter.string(from: endDate));"
} else if let count = rule.recurrenceEnd?.occurrenceCount {
Expand Down