Skip to content

Commit

Permalink
Merge pull request #286 from kvyatkovskys/feature/ver-0.6.10
Browse files Browse the repository at this point in the history
Some fixes and improvements for Month view
  • Loading branch information
kvyatkovskys committed Oct 26, 2022
2 parents 67bc7f2 + 406eda9 commit 4990b07
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 28 deletions.
2 changes: 1 addition & 1 deletion KVKCalendar.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'KVKCalendar'
s.version = '0.6.9'
s.version = '0.6.10'
s.summary = 'A most fully customization calendar for Apple platforms.'

s.description = <<-DESC
Expand Down
71 changes: 46 additions & 25 deletions Sources/KVKCalendar/MonthView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,16 @@ extension MonthView: CalendarSettingProtocol {
if let customHeaderView = dataSource?.willDisplayHeaderSubview(date: parameters.monthData.date, frame: headerViewFrame, type: .month) {
headerViewFrame = customHeaderView.frame
addSubview(customHeaderView)
} else {
} else if !style.month.isHiddenTitleHeader {
weekHeaderView.reloadFrame(frame)
}

collectionFrame.origin.y = headerViewFrame.height
collectionFrame.size.height = collectionFrame.height - headerViewFrame.height
if style.month.isHiddenTitleHeader {
collectionFrame.origin.y = 0
} else {
collectionFrame.origin.y = headerViewFrame.height
collectionFrame.size.height = collectionFrame.height - headerViewFrame.height
}

collectionView?.removeFromSuperview()
collectionView = nil
Expand Down Expand Up @@ -244,15 +248,19 @@ extension MonthView: CalendarSettingProtocol {
type: .month) {
headerViewFrame = customHeaderView.frame
addSubview(customHeaderView)
} else {
} else if !style.month.isHiddenTitleHeader {
if reload {
weekHeaderView = setupWeekHeaderView(prepareFrame: headerViewFrame)
}
addSubview(weekHeaderView)
}

collectionFrame.origin.y = headerViewFrame.height
collectionFrame.size.height = collectionFrame.height - headerViewFrame.height
if style.month.isHiddenTitleHeader {
collectionFrame.origin.y = 0
} else {
collectionFrame.origin.y = headerViewFrame.height
collectionFrame.size.height = collectionFrame.height - headerViewFrame.height
}

let result = createCollectionView(frame: collectionFrame, style: style.month)
collectionView = result.view
Expand All @@ -266,7 +274,12 @@ extension MonthView: CalendarSettingProtocol {
private func setupConstraintsIfNedeed(view: UICollectionView, customView: Bool) {
if !customView {
view.translatesAutoresizingMaskIntoConstraints = false
let top = view.topAnchor.constraint(equalTo: topAnchor, constant: headerViewFrame.height)
let top: NSLayoutConstraint
if style.month.isHiddenTitleHeader {
top = view.topAnchor.constraint(equalTo: topAnchor)
} else {
top = view.topAnchor.constraint(equalTo: topAnchor, constant: headerViewFrame.height)
}
let bottom = view.bottomAnchor.constraint(equalTo: bottomAnchor)
let left = view.leftAnchor.constraint(equalTo: leftAnchor)
let right = view.rightAnchor.constraint(equalTo: rightAnchor)
Expand Down Expand Up @@ -383,25 +396,33 @@ extension MonthView: UICollectionViewDataSource, UICollectionViewDataSourcePrefe

extension MonthView: UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard let objectView = collectionView else { return }

let center = convert(objectView.center, to: objectView)
guard let index = objectView.indexPathForItem(at: center) else { return }

let month = parameters.monthData.data.months[index.section]
weekHeaderView.date = month.date
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if !style.month.isPagingEnabled, let visibleItems = collectionView?.indexPathsForVisibleItems.sorted(by: { $0.row < $1.row }) {
let middleIndex = visibleItems[visibleItems.count / 2]
let month = parameters.monthData.data.months[middleIndex.section]
weekHeaderView.date = month.date

guard style.month.autoSelectionDateWhenScrolling,
let newDate = month.days.first(where: { $0.date?.kvkYear == month.date.kvkYear
&& $0.date?.kvkMonth == month.date.kvkMonth
&& $0.date?.kvkDay == parameters.monthData.date.kvkDay })?.date,
parameters.monthData.date != newDate
else {
return
}

parameters.monthData.date = newDate
willSelectDate?(newDate)
reload()
}
guard !style.month.isPagingEnabled, let objectView = collectionView else { return }

let center = convert(objectView.center, to: objectView)
guard let index = objectView.indexPathForItem(at: center) else { return }

let month = parameters.monthData.data.months[index.section]
guard style.month.autoSelectionDateWhenScrolling,
let newDate = month.days.first(where: { $0.date?.kvkYear == month.date.kvkYear
&& $0.date?.kvkMonth == month.date.kvkMonth
&& $0.date?.kvkDay == parameters.monthData.date.kvkDay })?.date,
parameters.monthData.date != newDate
else { return }

parameters.monthData.date = newDate
willSelectDate?(newDate)
reload()
}

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
Expand Down
6 changes: 5 additions & 1 deletion Sources/KVKCalendar/Style.swift
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,11 @@ public struct YearStyle {
public var selectCalendarType: CalendarType = .month
public var isAnimateSelection: Bool = false
public var isPagingEnabled: Bool = true

@available(swift, deprecated: 0.6.9, obsoleted: 0.6.10, renamed: "autoSelectionDateWhenScrolling")
public var isAutoSelectDateScrolling: Bool = true
public var autoSelectionDateWhenScrolling: Bool = true

public var weekDayAlignment: NSTextAlignment = .center
public var titleDateAlignment: NSTextAlignment = .left
public var colorBackground: UIColor = .white
Expand Down Expand Up @@ -727,7 +731,7 @@ extension YearStyle: Equatable {
&& compare(\.selectCalendarType)
&& compare(\.isAnimateSelection)
&& compare(\.isPagingEnabled)
&& compare(\.isAutoSelectDateScrolling)
&& compare(\.autoSelectionDateWhenScrolling)
&& compare(\.weekDayAlignment)
&& compare(\.titleDateAlignment)
&& compare(\.colorBackground)
Expand Down
2 changes: 1 addition & 1 deletion Sources/KVKCalendar/YearView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ extension YearView: UICollectionViewDataSource {

extension YearView: UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
guard data.style.year.isAutoSelectDateScrolling else { return }
guard data.style.year.autoSelectionDateWhenScrolling else { return }

let cells = collectionView?.indexPathsForVisibleItems ?? []
let dates = cells.compactMap { data.sections[$0.section].months[$0.row].date }
Expand Down

0 comments on commit 4990b07

Please sign in to comment.