Skip to content

Commit

Permalink
optimize initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
hongxinhope committed Apr 10, 2016
1 parent f50248e commit ea8be37
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 25 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#RRuleSwift
Swift library for working with recurrence rules of calendar dates.
Swift rrule library for working with recurrence rules of calendar dates.

![Example](Gif/RRuleSwiftExample.gif "RRuleSwiftExample")

RRuleSwift is based on [rrule.js](https://github.com/jkbrzt/rrule).

##How To Get Started
###Carthage
Specify "RRuleSwift" in your Cartfile:
Specify "RRuleSwift" in your ```Cartfile```:
```ogdl
github "teambition/RRuleSwift"
```

###Usage
##### Initialization
```swift
var recurrenceRule = RecurrenceRule(recurrenceWithFrequency: .Daily)
var recurrenceRule = RecurrenceRule(frequency: .Daily)
recurrenceRule.calendar = ...
recurrenceRule.frequency = ...
recurrenceRule.interval = ...
Expand All @@ -36,7 +36,7 @@ recurrenceRule.bysecond = ...
##### Rule form string
```swift
let ruleString = "RRULE:FREQ=MONTHLY;DTSTART=20160404T021000Z;COUNT=5;INTERVAL=2;WKST=MO;BYDAY=MO,TU"
let rule = RecurrenceRule.ruleWithString(ruleString)
let rule = RecurrenceRule(recurrenceWithRRuleString: ruleString)
```

##### String form rule
Expand All @@ -49,7 +49,7 @@ print(ruleString)
##### Occurrence generator
```swift
let ruleString = "RRULE:FREQ=YEARLY;COUNT=5;WKST=MO"
if let rule = RecurrenceRule.ruleWithString(ruleString) {
if let rule = RecurrenceRule(recurrenceWithRRuleString: ruleString) {
let allDates = rule.allOccurrences()
print(allDates)
/*
Expand Down
2 changes: 1 addition & 1 deletion RRuleSwift/RRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public struct RRule {
return rule
}

var recurrenceRule = RecurrenceRule(recurrenceWithFrequency: .Daily)
var recurrenceRule = RecurrenceRule(frequency: .Daily)
var ruleFrequency: RecurrenceFrequency?
for rule in rules {
let ruleComponents = rule.componentsSeparatedByString("=")
Expand Down
16 changes: 10 additions & 6 deletions RRuleSwift/RecurrenceRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,19 @@ public struct RecurrenceRule {
/// The seconds of the minute associated with the recurrence rule, as an array of integers.
public var bysecond: [Int]?

public init(recurrenceWithFrequency type: RecurrenceFrequency) {
frequency = type
public init(frequency: RecurrenceFrequency) {
self.frequency = frequency
}

public func toRRuleString() -> String {
return RRule.stringFromRule(self)
public init?(recurrenceWithRRuleString rruleString: String) {
if let recurrenceRule = RRule.ruleFromString(rruleString) {
self = recurrenceRule
} else {
return nil
}
}

public static func ruleWithString(rruleString: String) -> RecurrenceRule? {
return RRule.ruleFromString(rruleString)
public func toRRuleString() -> String {
return RRule.stringFromRule(self)
}
}
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.1</string>
<string>0.0.2</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.1</string>
<string>0.0.2</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<string>2</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
Expand Down
4 changes: 2 additions & 2 deletions RRuleSwiftExample/RRuleExample.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ let dateFormatter: NSDateFormatter = {
}()

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

let ruleString2 = "RRULE:FREQ=YEARLY;COUNT=5;WKST=MO"
if let rule2 = RecurrenceRule.ruleWithString(ruleString2) {
if let rule2 = RecurrenceRule(recurrenceWithRRuleString: ruleString2) {
let allDates = rule2.allOccurrences().map({ (date) -> String in
return dateFormatter.stringFromDate(date)
})
Expand Down
8 changes: 4 additions & 4 deletions RRuleSwiftExample/RRuleExample.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=5&amp;CharacterRangeLoc=782&amp;EndingColumnNumber=14&amp;EndingLineNumber=23&amp;StartingColumnNumber=9&amp;StartingLineNumber=23&amp;Timestamp=481611081.530649"
documentLocation = "#CharacterRangeLen=5&amp;CharacterRangeLoc=806&amp;EndingColumnNumber=14&amp;EndingLineNumber=23&amp;StartingColumnNumber=9&amp;StartingLineNumber=23&amp;Timestamp=481970869.121125"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=8&amp;CharacterRangeLoc=779&amp;EndingColumnNumber=17&amp;EndingLineNumber=23&amp;StartingColumnNumber=9&amp;StartingLineNumber=23&amp;Timestamp=481611081.530841"
documentLocation = "#CharacterRangeLen=8&amp;CharacterRangeLoc=803&amp;EndingColumnNumber=17&amp;EndingLineNumber=23&amp;StartingColumnNumber=9&amp;StartingLineNumber=23&amp;Timestamp=481970869.12189"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=12&amp;CharacterRangeLoc=1052&amp;EndingColumnNumber=21&amp;EndingLineNumber=29&amp;StartingColumnNumber=9&amp;StartingLineNumber=29&amp;Timestamp=481611081.530989"
documentLocation = "#CharacterRangeLen=12&amp;CharacterRangeLoc=1076&amp;EndingColumnNumber=21&amp;EndingLineNumber=29&amp;StartingColumnNumber=9&amp;StartingLineNumber=29&amp;Timestamp=481970869.1224"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=8&amp;CharacterRangeLoc=561&amp;EndingColumnNumber=17&amp;EndingLineNumber=16&amp;StartingColumnNumber=9&amp;StartingLineNumber=16&amp;Timestamp=481610961.05331"
documentLocation = "#CharacterRangeLen=8&amp;CharacterRangeLoc=573&amp;EndingColumnNumber=17&amp;EndingLineNumber=16&amp;StartingColumnNumber=9&amp;StartingLineNumber=16&amp;Timestamp=481970863.875966"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
Expand Down
8 changes: 4 additions & 4 deletions RRuleSwiftExample/RRuleExampleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class RRuleExampleViewController: UIViewController {
@IBOutlet weak var textView: UITextView!
@IBOutlet weak var tableView: UITableView!

private var rule = RecurrenceRule(recurrenceWithFrequency: .Daily) {
private var rule = RecurrenceRule(frequency: .Daily) {
didSet {
textView.text = rule.toRRuleString()
tableView.reloadData()
Expand All @@ -67,13 +67,13 @@ class RRuleExampleViewController: UIViewController {
navigationItem.title = "RRuleSwift Example"
tableView.tableFooterView = UIView()
tableView.separatorStyle = .None
rule = RecurrenceRule(recurrenceWithFrequency: .Daily)
rule = RecurrenceRule(frequency: .Daily)
textView.delegate = self
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Reset", style: .Plain, target: self, action: #selector(resetButtonTapped(_:)))
}

func resetButtonTapped(sender: UIBarButtonItem) {
rule = RecurrenceRule(recurrenceWithFrequency: .Daily)
rule = RecurrenceRule(frequency: .Daily)
}
}

Expand Down Expand Up @@ -370,7 +370,7 @@ extension RRuleExampleViewController: UITextViewDelegate {

if text == "\n" {
textView.resignFirstResponder()
rule = RecurrenceRule.ruleWithString(textView.text) ?? RecurrenceRule(recurrenceWithFrequency: .Daily)
rule = RecurrenceRule(recurrenceWithRRuleString: textView.text) ?? RecurrenceRule(frequency: .Daily)
return false
}
return true
Expand Down

0 comments on commit ea8be37

Please sign in to comment.