Skip to content
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

Support weekday variation #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Sources/JavaScriptBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ internal extension RecurrenceRule {
}

let byweekdayJSSymbols = byweekday.map({ (weekday) -> String in
return weekday.toJSONSymbol()
return weekday.1.toJSONSymbol()
})
if byweekdayJSSymbols.count > 0 {
jsonString += "byweekday: [\(byweekdayJSSymbols.joined(separator: ","))],"
Expand Down
40 changes: 14 additions & 26 deletions Sources/RRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,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 @@ -145,11 +144,15 @@ public class RRule: NSObject {
if ruleName == "BYDAY" {
// 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)
let byweekday = ruleValue.components(separatedBy: ",").compactMap({ (string) -> (Int?, EKWeekday)? in
guard let parsedSymbol = EKWeekday.weekdayFromSymbol(String(string.suffix(2))) else { return nil }
let specific = Int(string.prefix(string.count - 2))
if let position = specific { recurrenceRule.bysetpos = [position] }
return (specific, parsedSymbol)
})
recurrenceRule.byweekday = byweekday.sorted(by: { (a, b) -> Bool in
return a.1 < b.1
})
recurrenceRule.byweekday = byweekday.sorted(by: <)
}

if ruleName == "BYHOUR" {
Expand Down Expand Up @@ -179,22 +182,6 @@ 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
}

Expand Down Expand Up @@ -262,11 +249,12 @@ public class RRule: NSObject {
rruleString += "BYMONTHDAY=\(bymonthdayStrings.joined(separator: ","));"
}

let byweekdaySymbols = rule.byweekday.map({ (weekday) -> String in
return weekday.toSymbol()
})
if byweekdaySymbols.count > 0 {
rruleString += "BYDAY=\(byweekdaySymbols.joined(separator: ","));"
let byweekdayElements = rule.byweekday.map { weekdayTuple -> String in
let position = rule.bysetpos.map { String($0) }
return "\(position)\(weekdayTuple.1.toSymbol())"
}
if byweekdayElements.count > 0 {
rruleString += "BYDAY=\(byweekdayElements.joined(separator: ","));"
}

let byhourStrings = rule.byhour.map({ (hour) -> String in
Expand Down
2 changes: 1 addition & 1 deletion Sources/RecurrenceRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class RecurrenceRule: NSObject {
public var bymonthday = [Int]()

/// The days of the week associated with the recurrence rule, as an array of EKWeekday objects.
public var byweekday = [EKWeekday]()
public var byweekday = [(Int?, EKWeekday)]()

/// The hours of the day associated with the recurrence rule, as an array of integers.
public var byhour = [Int]()
Expand Down