Skip to content

Commit

Permalink
Merge pull request #375 from rakuyoMo/feature/CalendarData-years
Browse files Browse the repository at this point in the history
Allows more flexibility in setting year ranges
  • Loading branch information
kvyatkovskys authored Sep 12, 2024
2 parents 758e97b + 4030d52 commit 85e42f1
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 13 deletions.
4 changes: 4 additions & 0 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 33 additions & 4 deletions Sources/KVKCalendar/CalendarData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,43 @@ struct CalendarData {
var yearsCount = [Int]()

init(date: Date, years: Int, style: Style) {
self.date = date
self.style = style

// count years for calendar
let indexesYear = [Int](repeating: 0, count: years).split(half: years / 2)

self.init(date: date, style: style, indexesYear: indexesYear)
}

init<R: YearRange>(date: Date, style: Style, yearRange: R) where R.Bound == Int {
self.init(date: date, style: style, startYear: yearRange.lowerBound, endYear: yearRange.upperBound)
}

init(date: Date, style: Style, startYear: Int, endYear: Int) {
let currentYear = Date().kvkYear

// 2024 - 2024 -> (left: 1, right: 0)
// 2024 - 2026 -> (left: 1, right: 2)
// 2023 - 2026 -> (left: 2, right: 2)
// 2022 - 2026 -> (left: 3, right: 2)

// +1 means adding the current year to the data
let leftCount = currentYear - min(startYear, currentYear) + 1
let rightCount = max(endYear, currentYear) - currentYear

let left = [Int](repeating: 0, count: leftCount)
let right = [Int](repeating: 0, count: rightCount)

self.init(date: date, style: style, indexesYear: (left, right))
}

private init(date: Date, style: Style, indexesYear: (left: [Int], right: [Int])) {
self.date = date
self.style = style

let lastYear = indexesYear.left
let nextYear = indexesYear.right


let years = lastYear.count + nextYear.count

// last years
for lastIdx in lastYear.indices.reversed() where years > 1 {
yearsCount.append(-lastIdx)
Expand Down
35 changes: 26 additions & 9 deletions Sources/KVKCalendar/KVKCalendarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,24 @@ public final class KVKCalendarView: UIView {
private(set) var yearView: YearView
private(set) var listView: ListView

public init(frame: CGRect, date: Date? = nil, style: Style = Style(), years: Int = 4) {
public convenience init(frame: CGRect, date: Date? = nil, style: Style = Style(), years: Int = 4) {
let calendarData = CalendarData(date: date ?? Date(), years: years, style: style.adaptiveStyle)
self.init(frame: frame, date: date, style: style, calendarData: calendarData)
}

public convenience init<R: YearRange>(frame: CGRect, date: Date? = nil, style: Style = Style(), yearRange: R) where R.Bound == Int {
self.init(frame: frame, date: date, style: style, startYear: yearRange.lowerBound, endYear: yearRange.upperBound)
}

public convenience init(frame: CGRect, date: Date? = nil, style: Style = Style(), startYear: Int, endYear: Int) {
let calendarData = CalendarData(date: date ?? Date(), style: style.adaptiveStyle, startYear: startYear, endYear: endYear)
self.init(frame: frame, date: date, style: style, calendarData: calendarData)
}

private init(frame: CGRect, date: Date?, style: Style, calendarData: CalendarData) {
let adaptiveStyle = style.adaptiveStyle
self.parameters = .init(type: style.defaultType ?? .day, style: adaptiveStyle)
self.calendarData = CalendarData(date: date ?? Date(), years: years, style: adaptiveStyle)
self.calendarData = calendarData

// day view
self.dayData = DayData(data: calendarData, startDay: adaptiveStyle.startWeekDay)
Expand Down Expand Up @@ -81,6 +95,14 @@ public final class KVKCalendarView: UIView {

super.init(frame: frame)

setup(with: date)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

private func setup(with date: Date?) {
dayView.scrollableWeekView.dataSource = self
dayView.dataSource = self
dayView.delegate = self
Expand All @@ -103,17 +125,12 @@ public final class KVKCalendarView: UIView {

viewCaches = [.day: dayView, .week: weekView, .month: monthView, .year: yearView, .list: listView]

if let defaultType = adaptiveStyle.defaultType {
if let defaultType = style.adaptiveStyle.defaultType {
parameters.type = defaultType
}
set(type: parameters.type, date: date)
reloadAllStyles(adaptiveStyle, force: true)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
reloadAllStyles(style.adaptiveStyle, force: true)
}

}

#endif
22 changes: 22 additions & 0 deletions Sources/KVKCalendar/YearRange.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// YearRange.swift
// KVKCalendar
//
// Created by Rakuyo on 9.9.2024.
//

import Foundation

public protocol YearRange {
associatedtype Bound: Comparable

/// The range's lower bound.
var lowerBound: Bound { get }

/// The range's upper bound.
var upperBound: Bound { get }
}

extension Range: YearRange { }

extension ClosedRange: YearRange { }

0 comments on commit 85e42f1

Please sign in to comment.