From d61ff4e42698052fb600a578d908175c066b4ff5 Mon Sep 17 00:00:00 2001 From: Ljuka Date: Thu, 27 Sep 2018 09:08:48 +0200 Subject: [PATCH 1/6] Added selectedStartDate and selectedEndDate listeners --- .../CalendarDateRangePickerViewController.swift | 6 ++++++ .../ViewController.swift | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerViewController.swift b/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerViewController.swift index 3626fdc..9c38d92 100644 --- a/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerViewController.swift +++ b/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerViewController.swift @@ -11,6 +11,8 @@ import UIKit public protocol CalendarDateRangePickerViewControllerDelegate { func didCancelPickingDateRange() func didPickDateRange(startDate: Date!, endDate: Date!) + func didSelectStartDate(startDate: Date!) + func didSelectEndDate(endDate: Date!) } public class CalendarDateRangePickerViewController: UICollectionViewController { @@ -155,16 +157,20 @@ extension CalendarDateRangePickerViewController : UICollectionViewDelegateFlowLa } if selectedStartDate == nil { selectedStartDate = cell.date + delegate.didSelectStartDate(startDate: selectedStartDate) } else if selectedEndDate == nil { if isBefore(dateA: selectedStartDate!, dateB: cell.date!) { selectedEndDate = cell.date + delegate.didSelectEndDate(endDate: selectedEndDate) self.navigationItem.rightBarButtonItem?.isEnabled = true } else { // If a cell before the currently selected start date is selected then just set it as the new start date selectedStartDate = cell.date + delegate.didSelectStartDate(startDate: selectedStartDate) } } else { selectedStartDate = cell.date + delegate.didSelectStartDate(startDate: selectedStartDate) selectedEndDate = nil } collectionView.reloadData() diff --git a/Example/CalendarDateRangePickerViewController/ViewController.swift b/Example/CalendarDateRangePickerViewController/ViewController.swift index b2b3933..b97083b 100644 --- a/Example/CalendarDateRangePickerViewController/ViewController.swift +++ b/Example/CalendarDateRangePickerViewController/ViewController.swift @@ -41,4 +41,17 @@ extension ViewController : CalendarDateRangePickerViewControllerDelegate { self.navigationController?.dismiss(animated: true, completion: nil) } + @objc func didSelectStartDate(startDate: Date!){ +// Do something when start date is selected... + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "EEEE, MMM d, yyyy" + print(dateFormatter.string(from: startDate)) + } + + @objc func didSelectEndDate(endDate: Date!){ +// Do something when end date is selected... + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "EEEE, MMM d, yyyy" + print(dateFormatter.string(from: endDate)) + } } From 901238a3589ef387ac5660601c19ba23e6ab3341 Mon Sep 17 00:00:00 2001 From: Ljuka Date: Fri, 28 Sep 2018 09:48:37 +0200 Subject: [PATCH 2/6] Enabling some fonts and colors to be customized --- .../Classes/CalendarDateRangePickerCell.swift | 10 +++++++--- .../Classes/CalendarDateRangePickerHeaderView.swift | 8 +++++++- .../CalendarDateRangePickerViewController.swift | 11 +++++++++++ .../project.pbxproj | 10 +++++----- .../xcshareddata/IDEWorkspaceChecks.plist | 8 ++++++++ 5 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 Example/CalendarDateRangePickerViewController.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist diff --git a/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerCell.swift b/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerCell.swift index b0b64cd..6ed29de 100644 --- a/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerCell.swift +++ b/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerCell.swift @@ -11,9 +11,13 @@ import UIKit class CalendarDateRangePickerCell: UICollectionViewCell { private let defaultTextColor = UIColor.darkGray - private let highlightedColor = UIColor(white: 0.9, alpha: 1.0) + var highlightedColor = UIColor(white: 0.9, alpha: 1.0) private let disabledColor = UIColor.lightGray - + var font = UIFont(name: "HelveticaNeue", size: CalendarDateRangePickerViewController.defaultCellFontSize) { + didSet { + label?.font = font + } + } var selectedColor: UIColor! var date: Date? @@ -36,7 +40,7 @@ class CalendarDateRangePickerCell: UICollectionViewCell { func initLabel() { label = UILabel(frame: frame) label.center = CGPoint(x: frame.size.width / 2, y: frame.size.height / 2) - label.font = UIFont(name: "HelveticaNeue", size: 15.0) + label.font = font label.textColor = UIColor.darkGray label.textAlignment = NSTextAlignment.center self.addSubview(label) diff --git a/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerHeaderView.swift b/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerHeaderView.swift index c5d3b4e..3729dcf 100644 --- a/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerHeaderView.swift +++ b/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerHeaderView.swift @@ -11,6 +11,11 @@ import UIKit class CalendarDateRangePickerHeaderView: UICollectionReusableView { var label: UILabel! + var font = UIFont(name: "HelveticaNeue-Light", size: CalendarDateRangePickerViewController.defaultHeaderFontSize) { + didSet { + label?.font = font + } + } override init(frame: CGRect) { super.init(frame: frame) @@ -25,10 +30,11 @@ class CalendarDateRangePickerHeaderView: UICollectionReusableView { func initLabel() { label = UILabel(frame: frame) label.center = CGPoint(x: frame.size.width / 2, y: frame.size.height / 2) - label.font = UIFont(name: "HelveticaNeue-Light", size: 17.0) + label.font = font label.textColor = UIColor.darkGray label.textAlignment = NSTextAlignment.center self.addSubview(label) } } + diff --git a/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerViewController.swift b/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerViewController.swift index 9c38d92..57f9552 100644 --- a/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerViewController.swift +++ b/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerViewController.swift @@ -31,6 +31,13 @@ public class CalendarDateRangePickerViewController: UICollectionViewController { public var selectedStartDate: Date? public var selectedEndDate: Date? + + public var cellHighlightedColor = UIColor(white: 0.9, alpha: 1.0) + public static let defaultCellFontSize:CGFloat = 15.0 + public static let defaultHeaderFontSize:CGFloat = 17.0 + public var cellFont:UIFont = UIFont(name: "HelveticaNeue", size: CalendarDateRangePickerViewController.defaultCellFontSize)! + public var headerFont:UIFont = UIFont(name: "HelveticaNeue-Light", size: CalendarDateRangePickerViewController.defaultHeaderFontSize)! + public var selectedColor = UIColor(red: 66/255.0, green: 150/255.0, blue: 240/255.0, alpha: 1.0) public var titleText = "Select Dates" @@ -92,7 +99,10 @@ extension CalendarDateRangePickerViewController { override public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellReuseIdentifier, for: indexPath) as! CalendarDateRangePickerCell + + cell.highlightedColor = self.cellHighlightedColor cell.selectedColor = self.selectedColor + cell.font = self.cellFont cell.reset() let blankItems = getWeekday(date: getFirstDateForSection(section: indexPath.section)) - 1 if indexPath.item < 7 { @@ -137,6 +147,7 @@ extension CalendarDateRangePickerViewController { case UICollectionElementKindSectionHeader: let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerReuseIdentifier, for: indexPath) as! CalendarDateRangePickerHeaderView headerView.label.text = getMonthLabel(date: getFirstDateForSection(section: indexPath.section)) + headerView.font = headerFont return headerView default: fatalError("Unexpected element kind") diff --git a/Example/CalendarDateRangePickerViewController.xcodeproj/project.pbxproj b/Example/CalendarDateRangePickerViewController.xcodeproj/project.pbxproj index 2525d1c..66e557a 100644 --- a/Example/CalendarDateRangePickerViewController.xcodeproj/project.pbxproj +++ b/Example/CalendarDateRangePickerViewController.xcodeproj/project.pbxproj @@ -215,7 +215,7 @@ TargetAttributes = { 607FACCF1AFB9204008FA782 = { CreatedOnToolsVersion = 6.3.1; - DevelopmentTeam = 3EKA2B3YUL; + DevelopmentTeam = 3PFT3Q8CHG; LastSwiftMigration = 0900; }; 607FACE41AFB9204008FA782 = { @@ -511,11 +511,11 @@ baseConfigurationReference = 863F34A79DF14110025EE9E4 /* Pods-CalendarDateRangePickerViewController_Example.debug.xcconfig */; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; - DEVELOPMENT_TEAM = 3EKA2B3YUL; + DEVELOPMENT_TEAM = 3PFT3Q8CHG; INFOPLIST_FILE = CalendarDateRangePickerViewController/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; MODULE_NAME = ExampleApp; - PRODUCT_BUNDLE_IDENTIFIER = "com.miraan.CalendarDateRangePickerViewController-Example"; + PRODUCT_BUNDLE_IDENTIFIER = "com.miraan.CalendarDateRangePickerViewController-Example-Ljuka"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_SWIFT3_OBJC_INFERENCE = Default; SWIFT_VERSION = 3.0; @@ -527,11 +527,11 @@ baseConfigurationReference = C9304EBA1ACFB048B6759598 /* Pods-CalendarDateRangePickerViewController_Example.release.xcconfig */; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; - DEVELOPMENT_TEAM = 3EKA2B3YUL; + DEVELOPMENT_TEAM = 3PFT3Q8CHG; INFOPLIST_FILE = CalendarDateRangePickerViewController/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; MODULE_NAME = ExampleApp; - PRODUCT_BUNDLE_IDENTIFIER = "com.miraan.CalendarDateRangePickerViewController-Example"; + PRODUCT_BUNDLE_IDENTIFIER = "com.miraan.CalendarDateRangePickerViewController-Example-Ljuka"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_SWIFT3_OBJC_INFERENCE = Default; SWIFT_VERSION = 3.0; diff --git a/Example/CalendarDateRangePickerViewController.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/Example/CalendarDateRangePickerViewController.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/Example/CalendarDateRangePickerViewController.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + From 67334c3fe8589df0ea02faef39a3c8c3a23e982f Mon Sep 17 00:00:00 2001 From: Ljuka Date: Wed, 24 Oct 2018 14:14:28 +0200 Subject: [PATCH 3/6] Added variables for changing highlighted labels in date range --- .../Classes/CalendarDateRangePickerCell.swift | 12 +++++++++--- .../CalendarDateRangePickerViewController.swift | 4 ++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerCell.swift b/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerCell.swift index 6ed29de..95d51c9 100644 --- a/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerCell.swift +++ b/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerCell.swift @@ -18,7 +18,10 @@ class CalendarDateRangePickerCell: UICollectionViewCell { label?.font = font } } + var selectedColor: UIColor! + var selectedLabelColor: UIColor! + var highlightedLabelColor: UIColor! var date: Date? var selectedView: UIView? @@ -73,7 +76,7 @@ class CalendarDateRangePickerCell: UICollectionViewCell { self.addSubview(selectedView!) self.sendSubview(toBack: selectedView!) - label.textColor = UIColor.white + label.textColor = selectedLabelColor } func highlightRight() { @@ -84,7 +87,7 @@ class CalendarDateRangePickerCell: UICollectionViewCell { halfBackgroundView?.backgroundColor = highlightedColor self.addSubview(halfBackgroundView!) self.sendSubview(toBack: halfBackgroundView!) - + label.textColor = highlightedLabelColor addRoundHighlightView() } @@ -96,7 +99,8 @@ class CalendarDateRangePickerCell: UICollectionViewCell { halfBackgroundView?.backgroundColor = highlightedColor self.addSubview(halfBackgroundView!) self.sendSubview(toBack: halfBackgroundView!) - + label.textColor = highlightedLabelColor + addRoundHighlightView() } @@ -112,6 +116,8 @@ class CalendarDateRangePickerCell: UICollectionViewCell { func highlight() { self.backgroundColor = highlightedColor + label.textColor = highlightedLabelColor + } func disable() { diff --git a/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerViewController.swift b/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerViewController.swift index 57f9552..bf20a56 100644 --- a/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerViewController.swift +++ b/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerViewController.swift @@ -40,6 +40,8 @@ public class CalendarDateRangePickerViewController: UICollectionViewController { public var selectedColor = UIColor(red: 66/255.0, green: 150/255.0, blue: 240/255.0, alpha: 1.0) + public var selectedLabelColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0) + public var highlightedLabelColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0) public var titleText = "Select Dates" override public func viewDidLoad() { @@ -102,6 +104,8 @@ extension CalendarDateRangePickerViewController { cell.highlightedColor = self.cellHighlightedColor cell.selectedColor = self.selectedColor + cell.selectedLabelColor = self.selectedLabelColor + cell.highlightedLabelColor = self.highlightedLabelColor cell.font = self.cellFont cell.reset() let blankItems = getWeekday(date: getFirstDateForSection(section: indexPath.section)) - 1 From 4de6a3f234cf45e3525705519c44dcb96258fa01 Mon Sep 17 00:00:00 2001 From: Ljuka Date: Sat, 27 Oct 2018 12:22:11 +0200 Subject: [PATCH 4/6] Added optional parameter for disabled dates --- .../Classes/CalendarDateRangePickerCell.swift | 4 +- ...alendarDateRangePickerViewController.swift | 63 +++++++++++++++++-- .../ViewController.swift | 8 +++ 3 files changed, 70 insertions(+), 5 deletions(-) diff --git a/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerCell.swift b/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerCell.swift index 95d51c9..219ed2f 100644 --- a/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerCell.swift +++ b/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerCell.swift @@ -22,7 +22,8 @@ class CalendarDateRangePickerCell: UICollectionViewCell { var selectedColor: UIColor! var selectedLabelColor: UIColor! var highlightedLabelColor: UIColor! - + var disabledDates: [Date]! + var disabledTimestampDates: [Int]? var date: Date? var selectedView: UIView? var halfBackgroundView: UIView? @@ -122,6 +123,7 @@ class CalendarDateRangePickerCell: UICollectionViewCell { func disable() { label.textColor = disabledColor + } } diff --git a/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerViewController.swift b/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerViewController.swift index bf20a56..7ee31e6 100644 --- a/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerViewController.swift +++ b/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerViewController.swift @@ -31,7 +31,11 @@ public class CalendarDateRangePickerViewController: UICollectionViewController { public var selectedStartDate: Date? public var selectedEndDate: Date? - + var selectedStartCell: IndexPath? + var selectedEndCell: IndexPath? + + public var disabledDates: [Date]? + public var cellHighlightedColor = UIColor(white: 0.9, alpha: 1.0) public static let defaultCellFontSize:CGFloat = 15.0 public static let defaultHeaderFontSize:CGFloat = 17.0 @@ -118,7 +122,17 @@ extension CalendarDateRangePickerViewController { let date = getDate(dayOfMonth: dayOfMonth, section: indexPath.section) cell.date = date cell.label.text = "\(dayOfMonth)" - + + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "yyyy-MM-dd" + let datePreFormatted = dateFormatter.string(from: date) + let dateFormatted = dateFormatter.date(from: datePreFormatted) + + if disabledDates != nil{ + if (disabledDates?.contains(cell.date!))!{ + cell.disable() + } + } if isBefore(dateA: date, dateB: minimumDate) { cell.disable() } @@ -167,24 +181,34 @@ extension CalendarDateRangePickerViewController : UICollectionViewDelegateFlowLa if cell.date == nil { return } - if isBefore(dateA: cell.date!, dateB: minimumDate) { + if isBefore(dateA: cell.date!, dateB: minimumDate){ return } + + if disabledDates != nil{ + if (disabledDates?.contains(cell.date!))!{ + return + } + } + if selectedStartDate == nil { selectedStartDate = cell.date + selectedStartCell = indexPath delegate.didSelectStartDate(startDate: selectedStartDate) } else if selectedEndDate == nil { - if isBefore(dateA: selectedStartDate!, dateB: cell.date!) { + if isBefore(dateA: selectedStartDate!, dateB: cell.date!) && !isBetween(selectedStartCell!, and: indexPath){ selectedEndDate = cell.date delegate.didSelectEndDate(endDate: selectedEndDate) self.navigationItem.rightBarButtonItem?.isEnabled = true } else { // If a cell before the currently selected start date is selected then just set it as the new start date selectedStartDate = cell.date + selectedStartCell = indexPath delegate.didSelectStartDate(startDate: selectedStartDate) } } else { selectedStartDate = cell.date + selectedStartCell = indexPath delegate.didSelectStartDate(startDate: selectedStartDate) selectedEndDate = nil } @@ -269,4 +293,35 @@ extension CalendarDateRangePickerViewController { return Calendar.current.compare(dateA, to: dateB, toGranularity: .day) == ComparisonResult.orderedAscending } + func isBetween(_ startDateCellIndex: IndexPath, and endDateCellIndex: IndexPath) -> Bool { + + if disabledDates == nil{ + return false + } + + var index = startDateCellIndex.row + var section = startDateCellIndex.section + var currentIndexPath: IndexPath + var cell: CalendarDateRangePickerCell? + + while !(index == endDateCellIndex.row && section == endDateCellIndex.section){ + currentIndexPath = IndexPath(row: index, section: section) + cell = collectionView?.cellForItem(at: currentIndexPath) as! CalendarDateRangePickerCell + if cell == nil{ + section = section + 1 + let blankItems = getWeekday(date: getFirstDateForSection(section: section)) - 1 + index = blankItems + 1 + currentIndexPath = IndexPath(row: index, section: section) + cell = collectionView?.cellForItem(at: currentIndexPath) as! CalendarDateRangePickerCell + } + + if (disabledDates?.contains((cell?.date)!))!{ + return true + } + index = index + 1 + } + + return false + } + } diff --git a/Example/CalendarDateRangePickerViewController/ViewController.swift b/Example/CalendarDateRangePickerViewController/ViewController.swift index b97083b..730c4db 100644 --- a/Example/CalendarDateRangePickerViewController/ViewController.swift +++ b/Example/CalendarDateRangePickerViewController/ViewController.swift @@ -19,6 +19,14 @@ class ViewController: UIViewController { dateRangePickerViewController.minimumDate = Date() dateRangePickerViewController.maximumDate = Calendar.current.date(byAdding: .year, value: 2, to: Date()) dateRangePickerViewController.selectedStartDate = Date() +/* + Set disabled dates if you want. It's optional... + + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "yyyy-MM-dd" + + dateRangePickerViewController.disabledDates = [dateFormatter.date(from: "2018-11-13"), dateFormatter.date(from: "2018-11-21")] as? [Date] +*/ dateRangePickerViewController.selectedEndDate = Calendar.current.date(byAdding: .day, value: 10, to: Date()) dateRangePickerViewController.selectedColor = UIColor.red dateRangePickerViewController.titleText = "Select Date Range" From fb458d335fc1a7326b1df7e5d677511520840da9 Mon Sep 17 00:00:00 2001 From: Ljuka Date: Sat, 27 Oct 2018 12:28:51 +0200 Subject: [PATCH 5/6] Update to swift 4.2 --- .../project.pbxproj | 4 ++-- .../CalendarDateRangePickerViewController/AppDelegate.swift | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Example/CalendarDateRangePickerViewController.xcodeproj/project.pbxproj b/Example/CalendarDateRangePickerViewController.xcodeproj/project.pbxproj index 66e557a..e7e3ea5 100644 --- a/Example/CalendarDateRangePickerViewController.xcodeproj/project.pbxproj +++ b/Example/CalendarDateRangePickerViewController.xcodeproj/project.pbxproj @@ -518,7 +518,7 @@ PRODUCT_BUNDLE_IDENTIFIER = "com.miraan.CalendarDateRangePickerViewController-Example-Ljuka"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_SWIFT3_OBJC_INFERENCE = Default; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.2; }; name = Debug; }; @@ -534,7 +534,7 @@ PRODUCT_BUNDLE_IDENTIFIER = "com.miraan.CalendarDateRangePickerViewController-Example-Ljuka"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_SWIFT3_OBJC_INFERENCE = Default; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.2; }; name = Release; }; diff --git a/Example/CalendarDateRangePickerViewController/AppDelegate.swift b/Example/CalendarDateRangePickerViewController/AppDelegate.swift index 8f91c84..6e63624 100644 --- a/Example/CalendarDateRangePickerViewController/AppDelegate.swift +++ b/Example/CalendarDateRangePickerViewController/AppDelegate.swift @@ -14,7 +14,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. return true } From 30807b399f6ae7c973d06935903e7fd515737c39 Mon Sep 17 00:00:00 2001 From: Ljuka Date: Sat, 27 Oct 2018 12:40:12 +0200 Subject: [PATCH 6/6] Making code more objc friendly --- .../Classes/CalendarDateRangePickerCell.swift | 36 +++++----- .../CalendarDateRangePickerHeaderView.swift | 6 +- ...alendarDateRangePickerViewController.swift | 66 +++++++++---------- 3 files changed, 54 insertions(+), 54 deletions(-) diff --git a/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerCell.swift b/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerCell.swift index 219ed2f..c6147b4 100644 --- a/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerCell.swift +++ b/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerCell.swift @@ -19,17 +19,17 @@ class CalendarDateRangePickerCell: UICollectionViewCell { } } - var selectedColor: UIColor! - var selectedLabelColor: UIColor! - var highlightedLabelColor: UIColor! - var disabledDates: [Date]! - var disabledTimestampDates: [Int]? - var date: Date? - var selectedView: UIView? - var halfBackgroundView: UIView? - var roundHighlightView: UIView? + @objc var selectedColor: UIColor! + @objc var selectedLabelColor: UIColor! + @objc var highlightedLabelColor: UIColor! + @objc var disabledDates: [Date]! + @objc var disabledTimestampDates: [Int]? + @objc var date: Date? + @objc var selectedView: UIView? + @objc var halfBackgroundView: UIView? + @objc var roundHighlightView: UIView? - var label: UILabel! + @objc var label: UILabel! override init(frame: CGRect) { super.init(frame: frame) @@ -41,7 +41,7 @@ class CalendarDateRangePickerCell: UICollectionViewCell { initLabel() } - func initLabel() { + @objc func initLabel() { label = UILabel(frame: frame) label.center = CGPoint(x: frame.size.width / 2, y: frame.size.height / 2) label.font = font @@ -50,7 +50,7 @@ class CalendarDateRangePickerCell: UICollectionViewCell { self.addSubview(label) } - func reset() { + @objc func reset() { self.backgroundColor = UIColor.clear label.textColor = defaultTextColor label.backgroundColor = UIColor.clear @@ -68,7 +68,7 @@ class CalendarDateRangePickerCell: UICollectionViewCell { } } - func select() { + @objc func select() { let width = self.frame.size.width let height = self.frame.size.height selectedView = UIView(frame: CGRect(x: (width - height) / 2, y: 0, width: height, height: height)) @@ -80,7 +80,7 @@ class CalendarDateRangePickerCell: UICollectionViewCell { label.textColor = selectedLabelColor } - func highlightRight() { + @objc func highlightRight() { // This is used instead of highlight() when we need to highlight cell with a rounded edge on the left let width = self.frame.size.width let height = self.frame.size.height @@ -92,7 +92,7 @@ class CalendarDateRangePickerCell: UICollectionViewCell { addRoundHighlightView() } - func highlightLeft() { + @objc func highlightLeft() { // This is used instead of highlight() when we need to highlight the cell with a rounded edge on the right let width = self.frame.size.width let height = self.frame.size.height @@ -105,7 +105,7 @@ class CalendarDateRangePickerCell: UICollectionViewCell { addRoundHighlightView() } - func addRoundHighlightView() { + @objc func addRoundHighlightView() { let width = self.frame.size.width let height = self.frame.size.height roundHighlightView = UIView(frame: CGRect(x: (width - height) / 2, y: 0, width: height, height: height)) @@ -115,13 +115,13 @@ class CalendarDateRangePickerCell: UICollectionViewCell { self.sendSubview(toBack: roundHighlightView!) } - func highlight() { + @objc func highlight() { self.backgroundColor = highlightedColor label.textColor = highlightedLabelColor } - func disable() { + @objc func disable() { label.textColor = disabledColor } diff --git a/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerHeaderView.swift b/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerHeaderView.swift index 3729dcf..f14cd4d 100644 --- a/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerHeaderView.swift +++ b/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerHeaderView.swift @@ -10,8 +10,8 @@ import UIKit class CalendarDateRangePickerHeaderView: UICollectionReusableView { - var label: UILabel! - var font = UIFont(name: "HelveticaNeue-Light", size: CalendarDateRangePickerViewController.defaultHeaderFontSize) { + @objc var label: UILabel! + @objc var font = UIFont(name: "HelveticaNeue-Light", size: CalendarDateRangePickerViewController.defaultHeaderFontSize) { didSet { label?.font = font } @@ -27,7 +27,7 @@ class CalendarDateRangePickerHeaderView: UICollectionReusableView { initLabel() } - func initLabel() { + @objc func initLabel() { label = UILabel(frame: frame) label.center = CGPoint(x: frame.size.width / 2, y: frame.size.height / 2) label.font = font diff --git a/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerViewController.swift b/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerViewController.swift index 7ee31e6..51149c2 100644 --- a/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerViewController.swift +++ b/CalendarDateRangePickerViewController/Classes/CalendarDateRangePickerViewController.swift @@ -17,36 +17,36 @@ public protocol CalendarDateRangePickerViewControllerDelegate { public class CalendarDateRangePickerViewController: UICollectionViewController { - let cellReuseIdentifier = "CalendarDateRangePickerCell" - let headerReuseIdentifier = "CalendarDateRangePickerHeaderView" + @objc let cellReuseIdentifier = "CalendarDateRangePickerCell" + @objc let headerReuseIdentifier = "CalendarDateRangePickerHeaderView" public var delegate: CalendarDateRangePickerViewControllerDelegate! - let itemsPerRow = 7 - let itemHeight: CGFloat = 40 - let collectionViewInsets = UIEdgeInsets(top: 0, left: 25, bottom: 0, right: 25) + @objc let itemsPerRow = 7 + @objc let itemHeight: CGFloat = 40 + @objc let collectionViewInsets = UIEdgeInsets(top: 0, left: 25, bottom: 0, right: 25) - public var minimumDate: Date! - public var maximumDate: Date! + @objc public var minimumDate: Date! + @objc public var maximumDate: Date! - public var selectedStartDate: Date? - public var selectedEndDate: Date? - var selectedStartCell: IndexPath? - var selectedEndCell: IndexPath? + @objc public var selectedStartDate: Date? + @objc public var selectedEndDate: Date? + @objc var selectedStartCell: IndexPath? + @objc var selectedEndCell: IndexPath? - public var disabledDates: [Date]? + @objc public var disabledDates: [Date]? - public var cellHighlightedColor = UIColor(white: 0.9, alpha: 1.0) - public static let defaultCellFontSize:CGFloat = 15.0 - public static let defaultHeaderFontSize:CGFloat = 17.0 - public var cellFont:UIFont = UIFont(name: "HelveticaNeue", size: CalendarDateRangePickerViewController.defaultCellFontSize)! - public var headerFont:UIFont = UIFont(name: "HelveticaNeue-Light", size: CalendarDateRangePickerViewController.defaultHeaderFontSize)! + @objc public var cellHighlightedColor = UIColor(white: 0.9, alpha: 1.0) + @objc public static let defaultCellFontSize:CGFloat = 15.0 + @objc public static let defaultHeaderFontSize:CGFloat = 17.0 + @objc public var cellFont:UIFont = UIFont(name: "HelveticaNeue", size: CalendarDateRangePickerViewController.defaultCellFontSize)! + @objc public var headerFont:UIFont = UIFont(name: "HelveticaNeue-Light", size: CalendarDateRangePickerViewController.defaultHeaderFontSize)! - public var selectedColor = UIColor(red: 66/255.0, green: 150/255.0, blue: 240/255.0, alpha: 1.0) - public var selectedLabelColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0) - public var highlightedLabelColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0) - public var titleText = "Select Dates" + @objc public var selectedColor = UIColor(red: 66/255.0, green: 150/255.0, blue: 240/255.0, alpha: 1.0) + @objc public var selectedLabelColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0) + @objc public var highlightedLabelColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0) + @objc public var titleText = "Select Dates" override public func viewDidLoad() { super.viewDidLoad() @@ -73,11 +73,11 @@ public class CalendarDateRangePickerViewController: UICollectionViewController { self.navigationItem.rightBarButtonItem?.isEnabled = selectedStartDate != nil && selectedEndDate != nil } - func didTapCancel() { + @objc func didTapCancel() { delegate.didCancelPickingDateRange() } - func didTapDone() { + @objc func didTapDone() { if selectedStartDate == nil || selectedEndDate == nil { return } @@ -242,23 +242,23 @@ extension CalendarDateRangePickerViewController { // Helper functions - func getFirstDate() -> Date { + @objc func getFirstDate() -> Date { var components = Calendar.current.dateComponents([.month, .year], from: minimumDate) components.day = 1 return Calendar.current.date(from: components)! } - func getFirstDateForSection(section: Int) -> Date { + @objc func getFirstDateForSection(section: Int) -> Date { return Calendar.current.date(byAdding: .month, value: section, to: getFirstDate())! } - func getMonthLabel(date: Date) -> String { + @objc func getMonthLabel(date: Date) -> String { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "MMMM yyyy" return dateFormatter.string(from: date) } - func getWeekdayLabel(weekday: Int) -> String { + @objc func getWeekdayLabel(weekday: Int) -> String { var components = DateComponents() components.calendar = Calendar.current components.weekday = weekday @@ -271,29 +271,29 @@ extension CalendarDateRangePickerViewController { return dateFormatter.string(from: date!) } - func getWeekday(date: Date) -> Int { + @objc func getWeekday(date: Date) -> Int { return Calendar.current.dateComponents([.weekday], from: date).weekday! } - func getNumberOfDaysInMonth(date: Date) -> Int { + @objc func getNumberOfDaysInMonth(date: Date) -> Int { return Calendar.current.range(of: .day, in: .month, for: date)!.count } - func getDate(dayOfMonth: Int, section: Int) -> Date { + @objc func getDate(dayOfMonth: Int, section: Int) -> Date { var components = Calendar.current.dateComponents([.month, .year], from: getFirstDateForSection(section: section)) components.day = dayOfMonth return Calendar.current.date(from: components)! } - func areSameDay(dateA: Date, dateB: Date) -> Bool { + @objc func areSameDay(dateA: Date, dateB: Date) -> Bool { return Calendar.current.compare(dateA, to: dateB, toGranularity: .day) == ComparisonResult.orderedSame } - func isBefore(dateA: Date, dateB: Date) -> Bool { + @objc func isBefore(dateA: Date, dateB: Date) -> Bool { return Calendar.current.compare(dateA, to: dateB, toGranularity: .day) == ComparisonResult.orderedAscending } - func isBetween(_ startDateCellIndex: IndexPath, and endDateCellIndex: IndexPath) -> Bool { + @objc func isBetween(_ startDateCellIndex: IndexPath, and endDateCellIndex: IndexPath) -> Bool { if disabledDates == nil{ return false