From c6c01edc643edb7813d6ba0ff30a39d5bc3ab5a4 Mon Sep 17 00:00:00 2001 From: Abhishek Date: Sat, 26 Jun 2021 15:17:15 -0500 Subject: [PATCH] Modern slider date calculations. --- Clocker/Panel/PanelController.swift | 4 +- .../ParentPanelController+ModernSlider.swift | 70 +++++++++++++++---- Clocker/Panel/ParentPanelController.swift | 15 +++- Clocker/Panel/UI/HourMarkerViewItem.swift | 23 ++---- 4 files changed, 77 insertions(+), 35 deletions(-) diff --git a/Clocker/Panel/PanelController.swift b/Clocker/Panel/PanelController.swift index 927c4c3..944084f 100644 --- a/Clocker/Panel/PanelController.swift +++ b/Clocker/Panel/PanelController.swift @@ -95,7 +95,9 @@ class PanelController: ParentPanelController { // Reset future slider value to zero futureSlider.integerValue = 0 sliderDatePicker.dateValue = Date() - setModernSliderLabel(0) + modernSliderLabel.stringValue = modernSliderDataSource[modernSliderDataSource.count / 2] + let indexPaths: Set = Set([IndexPath(item: modernSliderDataSource.count / 2, section: 0)]) + modernSlider.scrollToItems(at: indexPaths, scrollPosition: .centeredHorizontally) setTimezoneDatasourceSlider(sliderValue: 0) diff --git a/Clocker/Panel/ParentPanelController+ModernSlider.swift b/Clocker/Panel/ParentPanelController+ModernSlider.swift index 4398a2d..215926d 100644 --- a/Clocker/Panel/ParentPanelController+ModernSlider.swift +++ b/Clocker/Panel/ParentPanelController+ModernSlider.swift @@ -5,12 +5,13 @@ import Foundation extension ParentPanelController: NSCollectionViewDataSource, NSCollectionViewDelegate { func collectionView(_: NSCollectionView, numberOfItemsInSection _: Int) -> Int { - return 96 + return modernSliderDataSource.count } func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem { let item = collectionView.makeItem(withIdentifier: HourMarkerViewItem.reuseIdentifier, for: indexPath) as! HourMarkerViewItem - item.setup(with: indexPath.item) + let dataSoureValue = modernSliderDataSource[indexPath.item] + item.setup(with: indexPath.item, value: dataSoureValue) return item } } @@ -22,8 +23,8 @@ extension ParentPanelController { let newPoint = NSPoint(x: changedOrigin.x + contentView.frame.width / 2, y: changedOrigin.y) let indexPath = modernSlider.indexPathForItem(at: newPoint) if let correctIndexPath = indexPath?.item, let item = modernSlider.item(at: correctIndexPath) as? HourMarkerViewItem { - setModernSliderLabel(item.indexTag) - setTimezoneDatasourceSlider(sliderValue: item.indexTag * 15) + modernSliderLabel.stringValue = item.timeRepresentation +// setTimezoneDatasourceSlider(sliderValue: item.indexTag * 15) item.setupLineColor() mainTableView.reloadData() @@ -36,16 +37,59 @@ extension ParentPanelController { } } - func setModernSliderLabel(_ index: Int) { - var dateComponents = DateComponents() - dateComponents.minute = index * 15 - if let newDate = Calendar.autoupdatingCurrent.date(byAdding: dateComponents, to: Date().nextHour) { - let dateFormatter = DateFormatterManager.dateFormatterWithFormat(with: .none, - format: "MMM d HH:mm", - timezoneIdentifier: TimeZone.current.identifier, - locale: Locale.autoupdatingCurrent) + private func minuteFromCalendar() -> (Date, Int) { + let currentDate = Date() + var minute = Calendar.current.component(.minute, from: currentDate) + if minute < 15 { + minute = 15 + } else if minute < 30 { + minute = 30 + } else if minute < 45 { + minute = 45 + } else { + minute = 0 + } + + return (currentDate, minute) + } + + public func forward15Minutes() -> [String] { + let defaultParameters = minuteFromCalendar() + let hourQuarterDate = Calendar.current.nextDate(after: defaultParameters.0, matching: DateComponents(minute: defaultParameters.1), matchingPolicy: .strict, repeatedTimePolicy: .first, direction: .forward)! + var backwards = hourQuarterDate + var forwards = hourQuarterDate + + var hourQuarters = [String]() + for _ in 1 ... 96 { + backwards = Calendar.current.date(byAdding: .minute, value: -15, to: backwards)! + hourQuarters.append(timezoneFormattedStringRepresentation(backwards)) + } - modernSliderLabel.stringValue = dateFormatter.string(from: newDate) + hourQuarters.append(timezoneFormattedStringRepresentation(forwards)) + for _ in 1 ... 96 { + forwards = Calendar.current.date(byAdding: .minute, value: 15, to: forwards)! + hourQuarters.append(timezoneFormattedStringRepresentation(forwards)) } + return hourQuarters + } + + public func backward15Minutes() -> [String] { + let defaultParameters = minuteFromCalendar() + var hourQuarterDate = Calendar.current.nextDate(after: defaultParameters.0, matching: DateComponents(minute: defaultParameters.1), matchingPolicy: .strict, repeatedTimePolicy: .first, direction: .forward)! + var hourQuarters = [String]() + for _ in 1 ... 96 { + hourQuarterDate = Calendar.current.date(byAdding: .minute, value: -15, to: hourQuarterDate)! + hourQuarters.append(timezoneFormattedStringRepresentation(hourQuarterDate)) + } + + return hourQuarters + } + + private func timezoneFormattedStringRepresentation(_ date: Date) -> String { + let dateFormatter = DateFormatterManager.dateFormatterWithFormat(with: .none, + format: "MMM d HH:mm", + timezoneIdentifier: TimeZone.current.identifier, + locale: Locale.autoupdatingCurrent) + return dateFormatter.string(from: date) } } diff --git a/Clocker/Panel/ParentPanelController.swift b/Clocker/Panel/ParentPanelController.swift index 31558e9..1f2cf85 100644 --- a/Clocker/Panel/ParentPanelController.swift +++ b/Clocker/Panel/ParentPanelController.swift @@ -86,6 +86,7 @@ class ParentPanelController: NSWindowController { @IBOutlet var roundedDateView: NSView! // Modern Slider + public var modernSliderDataSource: [String] = [] @IBOutlet var modernSlider: NSCollectionView! @IBOutlet var modernSliderLabel: NSTextField! @IBOutlet var modernContainerView: NSView! @@ -197,6 +198,12 @@ class ParentPanelController: NSWindowController { } if modernSlider != nil { +// var backwards = backward15Minutes() +// backwards.reverse() +// let forwards = forward15Minutes() + modernSliderDataSource = forward15Minutes() + print(modernSliderDataSource) + modernSlider.enclosingScrollView?.scrollerInsets = NSEdgeInsets(top: 0, left: 0, bottom: 100, right: 0) modernSlider.delegate = self modernSlider.postsBoundsChangedNotifications = true @@ -204,7 +211,9 @@ class ParentPanelController: NSWindowController { selector: #selector(collectionViewDidScroll(_:)), name: NSView.boundsDidChangeNotification, object: modernSlider.superview) - setModernSliderLabel(0) + modernSliderLabel.stringValue = modernSliderDataSource[modernSliderDataSource.count / 2] + let indexPaths: Set = Set([IndexPath(item: modernSliderDataSource.count / 2, section: 0)]) + modernSlider.scrollToItems(at: indexPaths, scrollPosition: .centeredHorizontally) } if roundedDateView != nil { @@ -587,7 +596,9 @@ class ParentPanelController: NSWindowController { if let futureSliderCell = futureSlider.cell as? CustomSliderCell, futureSliderCell.tracking == true { return } - + if modernSliderLabel.isHidden == false { + return + } let dataOperation = TimezoneDataOperations(with: model) cellView.time.stringValue = dataOperation.time(with: futureSliderValue) cellView.sunriseSetTime.stringValue = dataOperation.formattedSunriseTime(with: futureSliderValue) diff --git a/Clocker/Panel/UI/HourMarkerViewItem.swift b/Clocker/Panel/UI/HourMarkerViewItem.swift index b84c591..931a17d 100644 --- a/Clocker/Panel/UI/HourMarkerViewItem.swift +++ b/Clocker/Panel/UI/HourMarkerViewItem.swift @@ -4,19 +4,13 @@ import Cocoa class HourMarkerViewItem: NSCollectionViewItem { static let reuseIdentifier = NSUserInterfaceItemIdentifier("HourMarkerViewItem") + public var timeRepresentation: String = "-1" - @IBOutlet var constraintFromTop: NSLayoutConstraint! - @IBOutlet var verticalLine: NSBox! - - public var indexTag: Int = -1 - - func setup(with hour: Int) { - var dateComponents = DateComponents() - dateComponents.minute = hour * 15 - indexTag = hour + func setup(with index: Int, value: String) { + timeRepresentation = value for constraint in view.constraints where constraint.identifier == "constrainFromTop" { - if hour % 4 == 0 { + if index % 4 == 0 { constraint.constant = 0 } else { constraint.constant = 20 @@ -40,12 +34,3 @@ class HourMarkerViewItem: NSCollectionViewItem { return false } } - -extension Date { - public var nextHour: Date { - let calendar = Calendar.current - let minutes = calendar.component(.minute, from: self) - let components = DateComponents(hour: 1, minute: -minutes) - return calendar.date(byAdding: components, to: self) ?? self - } -}