Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intergation custom table view to vc #4

Merged
merged 6 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/CalendarStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public struct TimelineStyle {
public var verticalDiff: CGFloat = 50
public var verticalInset: CGFloat = 10
public var leadingInset: CGFloat = 53
public var eventGap: CGFloat = 0
public var eventGap: CGFloat = 1
public init() {}
}

Expand Down
17 changes: 16 additions & 1 deletion Sources/DayView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public class DayView: UIView, TimelinePagerViewDelegate, DayHeaderViewDelegate {
}
}

public var minDate: Date? {
didSet {
timelinePagerView.minDate = minDate
}
}

public var horizontalSpacing: CGFloat = 0 {
didSet {
layoutTableView()
Expand All @@ -73,6 +79,15 @@ public class DayView: UIView, TimelinePagerViewDelegate, DayHeaderViewDelegate {
timelinePagerView.autoScrollToFirstEvent = value
}
}

public var timeIntervalBefore: Float {
get {
timelinePagerView.timeIntervalBefore
}
set (value) {
timelinePagerView.timeIntervalBefore = value
}
}

public let dayHeaderView: DayHeaderView
public let timelinePagerView: TimelinePagerView
Expand Down Expand Up @@ -220,7 +235,7 @@ public class DayView: UIView, TimelinePagerViewDelegate, DayHeaderViewDelegate {
}

public func scrollToCurrentTime(animated: Bool = true) {
timelinePagerView.scrollToCurrentTime(animated: animated)
timelinePagerView.scrollToCurrentTime(animated: animated)
}

public func reloadData() {
Expand Down
15 changes: 15 additions & 0 deletions Sources/Extensions/Date+DateOnly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,19 @@ extension Date {
func year() -> Int {
return Calendar.current.component(.year, from: self)
}

/* Returns current time in Float
- before point - hours
- after point - percent of minutes in hour
*/
func timeOnly() -> Float {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

честно сказать, так и не понял, что этот метод возвращает... Float... Time... В каких единицах измерения возвращается timeOnly?

let hours = Double(Calendar.current.component(.hour, from: Date()))
let minutes = Double(Calendar.current.component(.minute, from: Date()))
var time = hours
let minutesInPercent = minutes / 60.0
if minutesInPercent < 1 {
time = hours + minutesInPercent
}
return Float(time)
}
}
39 changes: 26 additions & 13 deletions Sources/Timeline/TimelineContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import UIKit
public final class TimelineContainer: UIScrollView {
public let timeline: TimelineView
unowned var parent: UIViewController?

var timeIntervalBefore: Float {
if let parent = parent?.parent as? CKPageViewController {
return parent.timeIntervalBefore
}
return .zero
}

public init(_ timeline: TimelineView) {
self.timeline = timeline
Expand Down Expand Up @@ -60,24 +67,30 @@ public final class TimelineContainer: UIScrollView {
}

public func scroollToCurrentTime(animated: Bool) {
let allDayViewHeight = timeline.allDayViewHeight
let padding = allDayViewHeight + 200
if let yToScroll = timeline.currentTimeYPosition {
setTimelineOffset(CGPoint(x: contentOffset.x, y: yToScroll - padding), animated: animated)
}
let timeToScroll = Date().timeOnly() - timeIntervalBefore
scrollTo(hour24: timeToScroll, animated: animated)
}

public func scrollTo(hour24: Float, animated: Bool = true) {
let percentToScroll = CGFloat(hour24 / 24)
let yToScroll = contentSize.height * percentToScroll
let padding: CGFloat = 8
setTimelineOffset(CGPoint(x: contentOffset.x, y: yToScroll - padding), animated: animated)
setTimelineOffset(CGPoint(x: contentOffset.x, y: yToScroll), animated: animated)
}

private func setTimelineOffset(_ offset: CGPoint, animated: Bool) {
let yToScroll = offset.y
let bottomOfScrollView = contentSize.height - bounds.size.height
let newContentY = (yToScroll < bottomOfScrollView) ? yToScroll : bottomOfScrollView
setContentOffset(CGPoint(x: offset.x, y: newContentY), animated: animated)
}
private func setTimelineOffset(_ offset: CGPoint, animated: Bool) {
let yToScroll = offset.y
let bottomOfScrollView = contentSize.height - bounds.size.height

var newContentY: CGFloat = yToScroll
if yToScroll < bottomOfScrollView {
newContentY = (yToScroll < frame.minY) ? frame.minY : yToScroll
} else {
newContentY = bottomOfScrollView
}

if let parent = parent?.parent as? CKPageViewController {
parent.commonOffset?.y = newContentY
}
setContentOffset(CGPoint(x: offset.x, y: newContentY), animated: animated)
}
}
7 changes: 7 additions & 0 deletions Sources/Timeline/TimelineContainerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ public final class TimelineContainerController: UIViewController {
public override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
container.contentSize = timeline.frame.size
if let parent = self.parent as? CKPageViewController {
if parent.isFirstLaunch {
container.scroollToCurrentTime(animated: true)
parent.isFirstLaunch = false
}
}

if let newOffset = self.pendingContentOffset {
// Apply new offset only once the size has been determined
if view.bounds != .zero {
Expand Down
16 changes: 15 additions & 1 deletion Sources/Timeline/TimelinePagerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public protocol TimelinePagerViewDelegate: AnyObject {

final class CKPageViewController: UIPageViewController {
var commonOffset: CGPoint?
var isFirstLaunch = true
var timeIntervalBefore: Float = .zero
}

public final class TimelinePagerView: UIView, UIGestureRecognizerDelegate, UIScrollViewDelegate, DayViewStateUpdating, UIPageViewControllerDataSource, UIPageViewControllerDelegate, TimelineViewDelegate {
Expand Down Expand Up @@ -43,6 +45,16 @@ public final class TimelinePagerView: UIView, UIGestureRecognizerDelegate, UIScr
}

public var autoScrollToFirstEvent = false

/// Min date in calendar for swipe
public var minDate: Date?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Добавить бы комментарий с описанием, что это за минимальная дата и для чего она используется


/// Time interval before current time
public var timeIntervalBefore: Float = .zero {
didSet {
pagingViewController.timeIntervalBefore = timeIntervalBefore
}
}

private var pagingViewController = CKPageViewController(transitionStyle: .scroll,
navigationOrientation: .horizontal,
Expand Down Expand Up @@ -455,7 +467,6 @@ public final class TimelinePagerView: UIView, UIGestureRecognizerDelegate, UIScr
completion: nil)

self.pagingViewController.viewControllers?.first?.view.setNeedsLayout()
self.scrollToFirstEventIfNeeded(animated: true)
self.delegate?.timelinePager(timelinePager: self, didMoveTo: newDate)
}
}
Expand Down Expand Up @@ -500,6 +511,9 @@ public final class TimelinePagerView: UIView, UIGestureRecognizerDelegate, UIScr
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let containerController = viewController as? TimelineContainerController else { return nil }
let previousDate = calendar.date(byAdding: .day, value: -1, to: containerController.timeline.date)!
if let minDate = minDate {
guard previousDate >= minDate else { return nil }
}
let timelineContainerController = configureTimelineController(date: previousDate)
let offset = containerController.container.contentOffset
timelineContainerController.pendingContentOffset = offset
Expand Down