Skip to content

Commit

Permalink
Merge pull request #3 from kvyatkovskys/feature/fix-bugs
Browse files Browse the repository at this point in the history
resize calendar when rotate device, fixed scroll to current hour and determining the current date
  • Loading branch information
kvyatkovskys authored Feb 10, 2019
2 parents 07297f6 + 9a01176 commit 92b0f68
Show file tree
Hide file tree
Showing 13 changed files with 291 additions and 92 deletions.
2 changes: 2 additions & 0 deletions Example/KVKCalendar/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>
</dict>
</plist>
25 changes: 14 additions & 11 deletions Example/KVKCalendar/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ import UIKit
import KVKCalendar

final class ViewController: UIViewController {
fileprivate var selectDate = Date()
fileprivate var events = [Event]()

fileprivate var selectDate: Date = {
let formatter = DateFormatter()
formatter.dateFormat = "dd.MM.yyyy"
return formatter.date(from: "14.12.2018") ?? Date()
}()

fileprivate lazy var todayButton: UIBarButtonItem = {
let button = UIBarButtonItem(title: "Today", style: .done, target: self, action: #selector(today))
button.tintColor = .red
Expand All @@ -34,7 +39,8 @@ final class ViewController: UIViewController {
style.timelineStyle.offsetTimeY = 80
style.timelineStyle.offsetEvent = 3
style.allDayStyle.isPinned = true
let calendar = CalendarView(frame: frame, style: style)

let calendar = CalendarView(frame: frame, date: selectDate, style: style)
calendar.delegate = self
calendar.dataSource = self
return calendar
Expand Down Expand Up @@ -66,10 +72,6 @@ final class ViewController: UIViewController {
navigationItem.titleView = segmentedControl
navigationItem.rightBarButtonItem = todayButton

let formatter = DateFormatter()
formatter.dateFormat = "dd.MM.yyyy"
let date = formatter.date(from: "14.12.2018")
calendarView.set(type: .day, date: date ?? Date())
calendarView.addEventViewToDay(view: eventViewer)

loadEvents { [unowned self] (events) in
Expand All @@ -78,6 +80,12 @@ final class ViewController: UIViewController {
}
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
var frame = calendarView.frame
frame.size = size
calendarView.reloadFrame(frame: frame)
}

@objc func today(sender: UIBarButtonItem) {
calendarView.scrollToDate(date: Date())
}
Expand All @@ -96,11 +104,6 @@ final class ViewController: UIViewController {
}
calendarView.reloadData()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

extension ViewController: CalendarDelegate {
Expand Down
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.1.1'
s.version = '0.1.3'
s.summary = 'A most fully customization calendar library for iOS.'

s.description = <<-DESC
Expand Down
23 changes: 21 additions & 2 deletions KVKCalendar/Classes/CalendarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

import UIKit

protocol CalendarFrame {
func reloadFrame(frame: CGRect)
}

protocol CalendarSelectDateDelegate: AnyObject {
func didSelectCalendarDate(_ date: Date?, type: CalendarType)
func didSelectCalendarEvents(_ events: [Event])
Expand All @@ -32,7 +36,7 @@ public extension CalendarDelegate {
func didSelectMore(_ date: Date, frame: CGRect?) {}
}

public final class CalendarView: UIView, CalendarSelectDateDelegate {
public final class CalendarView: UIView, CalendarSelectDateDelegate, CalendarFrame {
public weak var delegate: CalendarDelegate?
public weak var dataSource: CalendarDataSource?
public var selectedType: CalendarType {
Expand Down Expand Up @@ -77,6 +81,13 @@ public final class CalendarView: UIView, CalendarSelectDateDelegate {
self.weekData = WeekData(yearData: yearData, timeSystem: timeHourSystem)
self.monthData = MonthData(yearData: yearData)
super.init(frame: frame)

if let defaultType = style.defaultType {
type = defaultType
set(type: type, date: date)
} else {
set(type: type, date: date)
}
}

required init?(coder aDecoder: NSCoder) {
Expand Down Expand Up @@ -120,7 +131,7 @@ public final class CalendarView: UIView, CalendarSelectDateDelegate {
weekCalendar.setDate(date: date)
case .month:
monthCalendar.setDate(date: date)
default:
case .year:
yearCalendar.setDate(date: date)
}
}
Expand Down Expand Up @@ -151,6 +162,14 @@ public final class CalendarView: UIView, CalendarSelectDateDelegate {
}
}

public func reloadFrame(frame: CGRect) {
self.frame = frame
dayCalendar.reloadFrame(frame: frame)
weekCalendar.reloadFrame(frame: frame)
monthCalendar.reloadFrame(frame: frame)
yearCalendar.reloadFrame(frame: frame)
}

// MARK: delegate selected calendar
func didSelectCalendarDate(_ date: Date?, type: CalendarType) {
delegate?.didSelectDate(date: date, type: type)
Expand Down
24 changes: 23 additions & 1 deletion KVKCalendar/Classes/DayViewCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import UIKit

final class DayViewCalendar: UIView, ScrollDayHeaderProtocol, TimelineDelegate {
final class DayViewCalendar: UIView, ScrollDayHeaderProtocol, TimelineDelegate, CalendarFrame {
fileprivate let style: Style
weak var delegate: CalendarSelectDateDelegate?
fileprivate var data: DayData
Expand Down Expand Up @@ -68,12 +68,34 @@ final class DayViewCalendar: UIView, ScrollDayHeaderProtocol, TimelineDelegate {
fatalError("init(coder:) has not been implemented")
}

func reloadFrame(frame: CGRect) {
self.frame = frame
topBackgroundView.frame.size.width = frame.size.width
scrollHeaderDay.reloadFrame(frame: frame)

var timelineFrame = timelineView.frame
timelineFrame.size.height = frame.size.height - scrollHeaderDay.frame.height
if UIDevice.current.userInterfaceIdiom == .pad {
timelineFrame.size.width = frame.size.width - style.timelineStyle.widthEventViewer
if let idx = subviews.index(where: { $0.tag == -1 }) {
let eventView = subviews[idx]
var eventFrame = timelineFrame
eventFrame.origin.x = eventFrame.width
eventFrame.size.width = style.timelineStyle.widthEventViewer
eventView.frame = eventFrame
}
}
timelineView.reloadFrame(frame: timelineFrame)
timelineView.createTimelinePage(dates: [data.date], events: data.events, selectedDate: data.date)
}

func addEventView(view: UIView) {
guard UIDevice.current.userInterfaceIdiom == .pad else { return }
var timlineFrame = timelineView.frame
timlineFrame.origin.x = timlineFrame.width
timlineFrame.size.width = style.timelineStyle.widthEventViewer
view.frame = timlineFrame
view.tag = -1
addSubview(view)
}

Expand Down
74 changes: 57 additions & 17 deletions KVKCalendar/Classes/MonthViewCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

import UIKit

final class MonthViewCalendar: UIView, MonthCellDelegate {
final class MonthViewCalendar: UIView, MonthCellDelegate, CalendarFrame {
fileprivate var data: MonthData
fileprivate let style: Style
fileprivate var collectionView: UICollectionView!
fileprivate var animated: Bool = false

weak var delegate: CalendarSelectDateDelegate?

Expand All @@ -25,19 +27,12 @@ final class MonthViewCalendar: UIView, MonthCellDelegate {
return view
}()

fileprivate lazy var collectionView: UICollectionView = {
fileprivate lazy var layout: UICollectionViewLayout = {
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
layout.scrollDirection = style.monthStyle.scrollDirection
let collection = UICollectionView(frame: frame, collectionViewLayout: layout)
collection.backgroundColor = .clear
collection.isPagingEnabled = true
collection.dataSource = self
collection.delegate = self
collection.register(MonthCollectionViewCell.self,
forCellWithReuseIdentifier: MonthCollectionViewCell.cellIdentifier)
return collection
return layout
}()

init(data: MonthData, frame: CGRect, style: Style) {
Expand All @@ -46,19 +41,41 @@ final class MonthViewCalendar: UIView, MonthCellDelegate {
super.init(frame: frame)
addSubview(headerView)

collectionView = createCollectionView(frame: frame)
var collectionFrame = frame
collectionFrame.origin.y = headerView.frame.height
collectionFrame.size.height = collectionFrame.height - headerView.frame.height
collectionView.frame = collectionFrame
addSubview(collectionView)
}

func reloadFrame(frame: CGRect) {
self.frame = frame
headerView.reloadFrame(frame: frame)

collectionView.removeFromSuperview()
collectionView = createCollectionView(frame: self.frame)

var collectionFrame = frame
collectionFrame.origin.y = headerView.frame.height
collectionFrame.size.height = collectionFrame.height - headerView.frame.height
collectionView.frame = collectionFrame
addSubview(collectionView)

scrollToDate(date: data.moveDate)
if let idx = data.days.index(where: { $0.date?.month == data.moveDate.month && $0.date?.year == data.moveDate.year }) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
self.collectionView.scrollToItem(at: IndexPath(row: idx, section: 0),
at: .top,
animated: false)
}
}
collectionView.reloadData()
}

func setDate(date: Date) {
headerView.date = date
data.moveDate = date
scrollToDate(date: date)
scrollToDate(date: date, animated: animated)
collectionView.reloadData()
}

Expand All @@ -67,15 +84,29 @@ final class MonthViewCalendar: UIView, MonthCellDelegate {
collectionView.reloadData()
}

fileprivate func scrollToDate(date: Date) {
fileprivate func createCollectionView(frame: CGRect) -> UICollectionView {
let collection = UICollectionView(frame: frame, collectionViewLayout: layout)
collection.backgroundColor = .clear
collection.isPagingEnabled = true
collection.dataSource = self
collection.delegate = self
collection.register(MonthCollectionViewCell.self,
forCellWithReuseIdentifier: MonthCollectionViewCell.cellIdentifier)
return collection
}

fileprivate func scrollToDate(date: Date, animated: Bool) {
delegate?.didSelectCalendarDate(date, type: .month)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
if let idx = self.data.days.index(where: { $0.date?.month == date.month && $0.date?.year == date.year }) {
if let idx = data.days.index(where: { $0.date?.month == date.month && $0.date?.year == date.year }) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
self.collectionView.scrollToItem(at: IndexPath(row: idx, section: 0),
at: .top,
animated: true)
animated: animated)
}
}
if !self.animated {
self.animated = true
}
}

required init?(coder aDecoder: NSCoder) {
Expand Down Expand Up @@ -117,7 +148,16 @@ extension MonthViewCalendar: UICollectionViewDelegate, UICollectionViewDelegateF
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let cells = collectionView.visibleCells as? [MonthCollectionViewCell] ?? [MonthCollectionViewCell()]
let cellDays = cells.filter({ $0.day.type != .empty })
guard let newMoveDate = cellDays.filter({ $0.day.date?.day == data.moveDate.day }).first?.day.date else { return }
guard let newMoveDate = cellDays.filter({ $0.day.date?.day == data.moveDate.day }).first?.day.date else {
let sorted = cellDays.sorted(by: { ($0.day.date?.day ?? 0) < ($1.day.date?.day ?? 0) })
if let lastDate = sorted.last?.day.date, lastDate.day < data.moveDate.day {
data.moveDate = lastDate
headerView.date = lastDate
delegate?.didSelectCalendarDate(lastDate, type: .month)
collectionView.reloadData()
}
return
}
data.moveDate = newMoveDate
headerView.date = newMoveDate
delegate?.didSelectCalendarDate(newMoveDate, type: .month)
Expand Down
Loading

0 comments on commit 92b0f68

Please sign in to comment.