Skip to content

Commit

Permalink
change the properties of recurrence rule from optional to required
Browse files Browse the repository at this point in the history
  • Loading branch information
hongxinhope committed Apr 13, 2016
1 parent ea8be37 commit aa0ce7b
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 162 deletions.
128 changes: 55 additions & 73 deletions RRuleSwift/JavaScriptBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,100 +68,82 @@ internal extension RecurrenceRule {
jsonString += "count: \(Generator.endlessRecurrenceCount),"
}

if let bysetpos = bysetpos {
let bysetposStrings = bysetpos.flatMap({ (setpo) -> String? in
guard (-366...366 ~= setpo) && (setpo != 0) else {
return nil
}
return String(setpo)
})
if bysetposStrings.count > 0 {
jsonString += "bysetpos: [\(bysetposStrings.joinWithSeparator(","))],"
let bysetposStrings = bysetpos.flatMap({ (setpo) -> String? in
guard (-366...366 ~= setpo) && (setpo != 0) else {
return nil
}
return String(setpo)
})
if bysetposStrings.count > 0 {
jsonString += "bysetpos: [\(bysetposStrings.joinWithSeparator(","))],"
}

if let byyearday = byyearday {
let byyeardayStrings = byyearday.flatMap({ (yearday) -> String? in
guard (-366...366 ~= yearday) && (yearday != 0) else {
return nil
}
return String(yearday)
})
if byyeardayStrings.count > 0 {
jsonString += "byyearday: [\(byyeardayStrings.joinWithSeparator(","))],"
let byyeardayStrings = byyearday.flatMap({ (yearday) -> String? in
guard (-366...366 ~= yearday) && (yearday != 0) else {
return nil
}
return String(yearday)
})
if byyeardayStrings.count > 0 {
jsonString += "byyearday: [\(byyeardayStrings.joinWithSeparator(","))],"
}

if let bymonth = bymonth {
let bymonthStrings = bymonth.flatMap({ (month) -> String? in
guard 1...12 ~= month else {
return nil
}
return String(month)
})
if bymonthStrings.count > 0 {
jsonString += "bymonth: [\(bymonthStrings.joinWithSeparator(","))],"
let bymonthStrings = bymonth.flatMap({ (month) -> String? in
guard 1...12 ~= month else {
return nil
}
return String(month)
})
if bymonthStrings.count > 0 {
jsonString += "bymonth: [\(bymonthStrings.joinWithSeparator(","))],"
}

if let byweekno = byweekno {
let byweeknoStrings = byweekno.flatMap({ (weekno) -> String? in
guard (-53...53 ~= weekno) && (weekno != 0) else {
return nil
}
return String(weekno)
})
if byweeknoStrings.count > 0 {
jsonString += "byweekno: [\(byweeknoStrings.joinWithSeparator(","))],"
let byweeknoStrings = byweekno.flatMap({ (weekno) -> String? in
guard (-53...53 ~= weekno) && (weekno != 0) else {
return nil
}
return String(weekno)
})
if byweeknoStrings.count > 0 {
jsonString += "byweekno: [\(byweeknoStrings.joinWithSeparator(","))],"
}

if let bymonthday = bymonthday {
let bymonthdayStrings = bymonthday.flatMap({ (monthday) -> String? in
guard (-31...31 ~= monthday) && (monthday != 0) else {
return nil
}
return String(monthday)
})
if bymonthdayStrings.count > 0 {
jsonString += "bymonthday: [\(bymonthdayStrings.joinWithSeparator(","))],"
let bymonthdayStrings = bymonthday.flatMap({ (monthday) -> String? in
guard (-31...31 ~= monthday) && (monthday != 0) else {
return nil
}
return String(monthday)
})
if bymonthdayStrings.count > 0 {
jsonString += "bymonthday: [\(bymonthdayStrings.joinWithSeparator(","))],"
}

if let byweekday = byweekday {
let byweekdayJSSymbols = byweekday.map({ (weekday) -> String in
return weekday.toJSONSymbol()
})
if byweekdayJSSymbols.count > 0 {
jsonString += "byweekday: [\(byweekdayJSSymbols.joinWithSeparator(","))],"
}
let byweekdayJSSymbols = byweekday.map({ (weekday) -> String in
return weekday.toJSONSymbol()
})
if byweekdayJSSymbols.count > 0 {
jsonString += "byweekday: [\(byweekdayJSSymbols.joinWithSeparator(","))],"
}

if let byhour = byhour {
let byhourStrings = byhour.map({ (hour) -> String in
return String(hour)
})
if byhourStrings.count > 0 {
jsonString += "byhour: [\(byhourStrings.joinWithSeparator(","))],"
}
let byhourStrings = byhour.map({ (hour) -> String in
return String(hour)
})
if byhourStrings.count > 0 {
jsonString += "byhour: [\(byhourStrings.joinWithSeparator(","))],"
}

if let byminute = byminute {
let byminuteStrings = byminute.map({ (minute) -> String in
return String(minute)
})
if byminuteStrings.count > 0 {
jsonString += "byminute: [\(byminuteStrings.joinWithSeparator(","))],"
}
let byminuteStrings = byminute.map({ (minute) -> String in
return String(minute)
})
if byminuteStrings.count > 0 {
jsonString += "byminute: [\(byminuteStrings.joinWithSeparator(","))],"
}

if let bysecond = bysecond {
let bysecondStrings = bysecond.map({ (second) -> String in
return String(second)
})
if bysecondStrings.count > 0 {
jsonString += "bysecond: [\(bysecondStrings.joinWithSeparator(","))]"
}
let bysecondStrings = bysecond.map({ (second) -> String in
return String(second)
})
if bysecondStrings.count > 0 {
jsonString += "bysecond: [\(bysecondStrings.joinWithSeparator(","))]"
}

if jsonString.substringFromIndex(jsonString.endIndex.advancedBy(-1)) == "," {
Expand Down
128 changes: 55 additions & 73 deletions RRuleSwift/RRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,100 +191,82 @@ public struct RRule {
rruleString += "COUNT=\(count);"
}

if let bysetpos = rule.bysetpos {
let bysetposStrings = bysetpos.flatMap({ (setpo) -> String? in
guard (-366...366 ~= setpo) && (setpo != 0) else {
return nil
}
return String(setpo)
})
if bysetposStrings.count > 0 {
rruleString += "BYSETPOS=\(bysetposStrings.joinWithSeparator(","));"
let bysetposStrings = rule.bysetpos.flatMap({ (setpo) -> String? in
guard (-366...366 ~= setpo) && (setpo != 0) else {
return nil
}
return String(setpo)
})
if bysetposStrings.count > 0 {
rruleString += "BYSETPOS=\(bysetposStrings.joinWithSeparator(","));"
}

if let byyearday = rule.byyearday {
let byyeardayStrings = byyearday.flatMap({ (yearday) -> String? in
guard (-366...366 ~= yearday) && (yearday != 0) else {
return nil
}
return String(yearday)
})
if byyeardayStrings.count > 0 {
rruleString += "BYYEARDAY=\(byyeardayStrings.joinWithSeparator(","));"
let byyeardayStrings = rule.byyearday.flatMap({ (yearday) -> String? in
guard (-366...366 ~= yearday) && (yearday != 0) else {
return nil
}
return String(yearday)
})
if byyeardayStrings.count > 0 {
rruleString += "BYYEARDAY=\(byyeardayStrings.joinWithSeparator(","));"
}

if let bymonth = rule.bymonth {
let bymonthStrings = bymonth.flatMap({ (month) -> String? in
guard 1...12 ~= month else {
return nil
}
return String(month)
})
if bymonthStrings.count > 0 {
rruleString += "BYMONTH=\(bymonthStrings.joinWithSeparator(","));"
let bymonthStrings = rule.bymonth.flatMap({ (month) -> String? in
guard 1...12 ~= month else {
return nil
}
return String(month)
})
if bymonthStrings.count > 0 {
rruleString += "BYMONTH=\(bymonthStrings.joinWithSeparator(","));"
}

if let byweekno = rule.byweekno {
let byweeknoStrings = byweekno.flatMap({ (weekno) -> String? in
guard (-53...53 ~= weekno) && (weekno != 0) else {
return nil
}
return String(weekno)
})
if byweeknoStrings.count > 0 {
rruleString += "BYWEEKNO=\(byweeknoStrings.joinWithSeparator(","));"
let byweeknoStrings = rule.byweekno.flatMap({ (weekno) -> String? in
guard (-53...53 ~= weekno) && (weekno != 0) else {
return nil
}
return String(weekno)
})
if byweeknoStrings.count > 0 {
rruleString += "BYWEEKNO=\(byweeknoStrings.joinWithSeparator(","));"
}

if let bymonthday = rule.bymonthday {
let bymonthdayStrings = bymonthday.flatMap({ (monthday) -> String? in
guard (-31...31 ~= monthday) && (monthday != 0) else {
return nil
}
return String(monthday)
})
if bymonthdayStrings.count > 0 {
rruleString += "BYMONTHDAY=\(bymonthdayStrings.joinWithSeparator(","));"
let bymonthdayStrings = rule.bymonthday.flatMap({ (monthday) -> String? in
guard (-31...31 ~= monthday) && (monthday != 0) else {
return nil
}
return String(monthday)
})
if bymonthdayStrings.count > 0 {
rruleString += "BYMONTHDAY=\(bymonthdayStrings.joinWithSeparator(","));"
}

if let byweekday = rule.byweekday {
let byweekdaySymbols = byweekday.map({ (weekday) -> String in
return weekday.toSymbol()
})
if byweekdaySymbols.count > 0 {
rruleString += "BYDAY=\(byweekdaySymbols.joinWithSeparator(","));"
}
let byweekdaySymbols = rule.byweekday.map({ (weekday) -> String in
return weekday.toSymbol()
})
if byweekdaySymbols.count > 0 {
rruleString += "BYDAY=\(byweekdaySymbols.joinWithSeparator(","));"
}

if let byhour = rule.byhour {
let byhourStrings = byhour.map({ (hour) -> String in
return String(hour)
})
if byhourStrings.count > 0 {
rruleString += "BYHOUR=\(byhourStrings.joinWithSeparator(","));"
}
let byhourStrings = rule.byhour.map({ (hour) -> String in
return String(hour)
})
if byhourStrings.count > 0 {
rruleString += "BYHOUR=\(byhourStrings.joinWithSeparator(","));"
}

if let byminute = rule.byminute {
let byminuteStrings = byminute.map({ (minute) -> String in
return String(minute)
})
if byminuteStrings.count > 0 {
rruleString += "BYMINUTE=\(byminuteStrings.joinWithSeparator(","));"
}
let byminuteStrings = rule.byminute.map({ (minute) -> String in
return String(minute)
})
if byminuteStrings.count > 0 {
rruleString += "BYMINUTE=\(byminuteStrings.joinWithSeparator(","));"
}

if let bysecond = rule.bysecond {
let bysecondStrings = bysecond.map({ (second) -> String in
return String(second)
})
if bysecondStrings.count > 0 {
rruleString += "BYSECOND=\(bysecondStrings.joinWithSeparator(","));"
}
let bysecondStrings = rule.bysecond.map({ (second) -> String in
return String(second)
})
if bysecondStrings.count > 0 {
rruleString += "BYSECOND=\(bysecondStrings.joinWithSeparator(","));"
}

if rruleString.substringFromIndex(rruleString.endIndex.advancedBy(-1)) == ";" {
Expand Down
18 changes: 9 additions & 9 deletions RRuleSwift/RecurrenceRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,37 +39,37 @@ public struct RecurrenceRule {
/// For example, if a bysetpos of -1 is combined with a RecurrenceFrequency.Monthly frequency, and a byweekday of (EKWeekday.Monday, EKWeekday.Tuesday, EKWeekday.Wednesday, EKWeekday.Thursday, EKWeekday.Friday), will result in the last work day of every month.
///
/// Negative values indicate counting backwards from the end of the recurrence rule’s frequency.
public var bysetpos: [Int]?
public var bysetpos = [Int]()

/// The days of the year associated with the recurrence rule, as an array of integers. Values can be from 1 to 366 and from -1 to -366.
///
/// Negative values indicate counting backwards from the end of the year.
public var byyearday: [Int]?
public var byyearday = [Int]()

/// The months of the year associated with the recurrence rule, as an array of integers. Values can be from 1 to 12.
public var bymonth: [Int]?
public var bymonth = [Int]()

/// The weeks of the year associated with the recurrence rule, as an array of integers. Values can be from 1 to 53 and from -1 to -53. According to ISO8601, the first week of the year is that containing at least four days of the new year.
///
/// Negative values indicate counting backwards from the end of the year.
public var byweekno: [Int]?
public var byweekno = [Int]()

/// The days of the month associated with the recurrence rule, as an array of integers. Values can be from 1 to 31 and from -1 to -31.
///
/// Negative values indicate counting backwards from the end of the month.
public var bymonthday: [Int]?
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 = [EKWeekday]()

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

/// The minutes of the hour associated with the recurrence rule, as an array of integers.
public var byminute: [Int]?
public var byminute = [Int]()

/// The seconds of the minute associated with the recurrence rule, as an array of integers.
public var bysecond: [Int]?
public var bysecond = [Int]()

public init(frequency: RecurrenceFrequency) {
self.frequency = frequency
Expand Down
2 changes: 1 addition & 1 deletion RRuleSwift/Supporting Files/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.0.2</string>
<string>0.0.3</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
4 changes: 2 additions & 2 deletions RRuleSwiftExample/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>0.0.2</string>
<string>0.0.3</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>2</string>
<string>3</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
Expand Down
2 changes: 1 addition & 1 deletion RRuleSwiftExample/RRuleExample.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let dateFormatter: NSDateFormatter = {

let ruleString1 = "RRULE:FREQ=WEEKLY;DTSTART=20160328T070000Z;BYDAY=MO,TU,WE,TH,FR;INTERVAL=1"
if let rule1 = RecurrenceRule(recurrenceWithRRuleString: ruleString1) {
let weekdays = rule1.byweekday!.map({ (weekday) -> Int in
let weekdays = rule1.byweekday.map({ (weekday) -> Int in
return weekday.rawValue
})
}
Expand Down
Loading

0 comments on commit aa0ce7b

Please sign in to comment.