Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
support modal
Browse files Browse the repository at this point in the history
  • Loading branch information
hongxinhope committed Dec 23, 2016
1 parent ae3d752 commit 14e898e
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 28 deletions.
77 changes: 52 additions & 25 deletions RecurrencePicker/RecurrencePicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ open class RecurrencePicker: UITableViewController {
open var backgroundColor: UIColor?
open var separatorColor: UIColor?

fileprivate var isModal: Bool {
return presentingViewController?.presentedViewController == self
|| (navigationController != nil && navigationController?.presentingViewController?.presentedViewController == navigationController && navigationController?.viewControllers.first == self)
|| tabBarController?.presentingViewController is UITabBarController
}
fileprivate var recurrenceRule: RecurrenceRule?
fileprivate var selectedIndexPath = IndexPath(row: 0, section: 0)

Expand All @@ -41,33 +46,20 @@ open class RecurrencePicker: UITableViewController {
open override func didMove(toParentViewController parent: UIViewController?) {
if parent == nil {
// navigation is popped
if let rule = recurrenceRule {
switch rule.frequency {
case .daily:
recurrenceRule?.byweekday.removeAll()
recurrenceRule?.bymonthday.removeAll()
recurrenceRule?.bymonth.removeAll()
case .weekly:
recurrenceRule?.byweekday = rule.byweekday.sorted(by: <)
recurrenceRule?.bymonthday.removeAll()
recurrenceRule?.bymonth.removeAll()
case .monthly:
recurrenceRule?.byweekday.removeAll()
recurrenceRule?.bymonthday = rule.bymonthday.sorted(by: <)
recurrenceRule?.bymonth.removeAll()
case .yearly:
recurrenceRule?.byweekday.removeAll()
recurrenceRule?.bymonthday.removeAll()
recurrenceRule?.bymonth = rule.bymonth.sorted(by: <)
default:
break
}
}
recurrenceRule?.startDate = Date()
recurrencePickerDidPickRecurrence()
}
}

delegate?.recurrencePicker(self, didPickRecurrence: recurrenceRule)
// MARK: - Actions
func doneButtonTapped(_ sender: UIBarButtonItem) {
dismiss(animated: true) {
self.recurrencePickerDidPickRecurrence()
}
}

func closeButtonTapped(_ sender: UIBarButtonItem) {
dismiss(animated: true, completion: nil)
}
}

extension RecurrencePicker {
Expand Down Expand Up @@ -129,7 +121,9 @@ extension RecurrencePicker {
if indexPath.section == 0 {
updateRecurrenceRule(withSelectedIndexPath: indexPath)
updateRecurrenceRuleText()
let _ = navigationController?.popViewController(animated: true)
if !isModal {
let _ = navigationController?.popViewController(animated: true)
}
} else {
let customRecurrenceViewController = CustomRecurrenceViewController(style: .grouped)
customRecurrenceViewController.occurrenceDate = occurrenceDate
Expand Down Expand Up @@ -165,6 +159,10 @@ extension RecurrencePicker {
// MARK: - Helper
fileprivate func commonInit() {
navigationItem.title = LocalizedString("RecurrencePicker.navigation.title")
if isModal {
navigationItem.rightBarButtonItem = UIBarButtonItem(title: LocalizedString("Done"), style: .done, target: self, action: #selector(doneButtonTapped(_:)))
navigationItem.leftBarButtonItem = UIBarButtonItem(title: LocalizedString("Close"), style: .done, target: self, action: #selector(closeButtonTapped(_:)))
}
navigationController?.navigationBar.tintColor = tintColor
tableView.tintColor = tintColor
if let backgroundColor = backgroundColor {
Expand Down Expand Up @@ -239,9 +237,38 @@ extension RecurrencePicker {
tableView.endUpdates()
footerView?.setNeedsLayout()
}

fileprivate func recurrencePickerDidPickRecurrence() {
if let rule = recurrenceRule {
switch rule.frequency {
case .daily:
recurrenceRule?.byweekday.removeAll()
recurrenceRule?.bymonthday.removeAll()
recurrenceRule?.bymonth.removeAll()
case .weekly:
recurrenceRule?.byweekday = rule.byweekday.sorted(by: <)
recurrenceRule?.bymonthday.removeAll()
recurrenceRule?.bymonth.removeAll()
case .monthly:
recurrenceRule?.byweekday.removeAll()
recurrenceRule?.bymonthday = rule.bymonthday.sorted(by: <)
recurrenceRule?.bymonth.removeAll()
case .yearly:
recurrenceRule?.byweekday.removeAll()
recurrenceRule?.bymonthday.removeAll()
recurrenceRule?.bymonth = rule.bymonth.sorted(by: <)
default:
break
}
}
recurrenceRule?.startDate = Date()

delegate?.recurrencePicker(self, didPickRecurrence: recurrenceRule)
}
}

extension RecurrencePicker: CustomRecurrenceViewControllerDelegate {
// MARK: - CustomRecurrenceViewController delegate
func customRecurrenceViewController(_ controller: CustomRecurrenceViewController, didPickRecurrence recurrenceRule: RecurrenceRule) {
self.recurrenceRule = recurrenceRule
updateRecurrenceRuleText()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
Copyright © 2016年 Teambition. All rights reserved.
*/

"Done" = "Done";
"Close" = "Close";

"frequency.daily" = "Daily";
"frequency.weekly" = "Weekly";
"frequency.monthly" = "Monthly";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
Copyright © 2016年 Teambition. All rights reserved.
*/

"Done" = "完成";
"Close" = "閉鎖";

"frequency.daily" = "毎日";
"frequency.weekly" = "毎週";
"frequency.monthly" = "毎月";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
Copyright © 2016年 Teambition. All rights reserved.
*/

"Done" = "완성하다";
"Close" = "닫기";

"frequency.daily" = "매일";
"frequency.weekly" = "매주";
"frequency.monthly" = "매월";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
Copyright © 2016年 Teambition. All rights reserved.
*/

"Done" = "完成";
"Close" = "关闭";

"frequency.daily" = "每天";
"frequency.weekly" = "每周";
"frequency.monthly" = "每月";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
Copyright © 2016年 Teambition. All rights reserved.
*/

"Done" = "完成";
"Close" = "關閉";

"frequency.daily" = "每天";
"frequency.weekly" = "每週";
"frequency.monthly" = "每月";
Expand Down
2 changes: 1 addition & 1 deletion RecurrencePicker/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.5</string>
<string>0.0.6</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
1 change: 1 addition & 0 deletions RecurrencePickerExample/ExampleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class ExampleViewController: UIViewController {
recurrencePicker.separatorColor = UIColor(white: 221 / 255, alpha: 1)
recurrencePicker.delegate = self
navigationController?.pushViewController(recurrencePicker, animated: true)
// present(UINavigationController(rootViewController: recurrencePicker), animated: true, completion: nil)
}

@IBAction func datePickerPicked(_ sender: UIDatePicker) {
Expand Down
4 changes: 2 additions & 2 deletions RecurrencePickerExample/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.5</string>
<string>0.0.6</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>5</string>
<string>6</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
Expand Down

0 comments on commit 14e898e

Please sign in to comment.