Skip to content

Commit

Permalink
Add a basic example for month-view
Browse files Browse the repository at this point in the history
please note, that configure is invoked in two places for a reason, at
least according to the [docs](https://patchthecode.com/jtapplecalendar-home/calendar-from-scratch/):

> These 2 functions should contain the same code, therefore it is wise
> to have a shared function to reduce code duplication. The only
> difference between these two functions should be the first line of code
> (the dequeuing code). Reasons for the 2 functions having the same code
> are found [here under problem#1](patchthecode/JTAppleCalendar#553).
  • Loading branch information
zeitschlag committed Feb 7, 2020
1 parent 264fee6 commit c32f4ad
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CalendarPOC.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
B34582F223ED9B98003460AE /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B34582F123ED9B98003460AE /* Assets.xcassets */; };
B34582F523ED9B98003460AE /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B34582F323ED9B98003460AE /* LaunchScreen.storyboard */; };
B34582FF23ED9E51003460AE /* JTAppleCalendar in Frameworks */ = {isa = PBXBuildFile; productRef = B34582FE23ED9E51003460AE /* JTAppleCalendar */; };
B345830123EDA2CE003460AE /* DayCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = B345830023EDA2CE003460AE /* DayCell.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -22,6 +23,7 @@
B34582F423ED9B98003460AE /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
B34582F623ED9B98003460AE /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
B34582FC23ED9C83003460AE /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
B345830023EDA2CE003460AE /* DayCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DayCell.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -58,6 +60,7 @@
children = (
B34582E823ED9B96003460AE /* AppDelegate.swift */,
B34582EC23ED9B96003460AE /* ViewController.swift */,
B345830023EDA2CE003460AE /* DayCell.swift */,
B34582F123ED9B98003460AE /* Assets.xcassets */,
B34582F323ED9B98003460AE /* LaunchScreen.storyboard */,
B34582F623ED9B98003460AE /* Info.plist */,
Expand Down Expand Up @@ -142,6 +145,7 @@
buildActionMask = 2147483647;
files = (
B34582ED23ED9B96003460AE /* ViewController.swift in Sources */,
B345830123EDA2CE003460AE /* DayCell.swift in Sources */,
B34582E923ED9B96003460AE /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
37 changes: 37 additions & 0 deletions CalendarPOC/DayCell.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// DayCell.swift
// CalendarPOC
//
// Created by Nathan Mattes on 07.02.20.
// Copyright © 2020 Nathan Mattes. All rights reserved.
//

import UIKit
import JTAppleCalendar

class DayCell: JTACDayCell {
static let reuseIdentifier = "CalendarCell"

let dateLabel: UILabel

override init(frame: CGRect) {

dateLabel = UILabel(frame: .zero)
dateLabel.translatesAutoresizingMaskIntoConstraints = false

super.init(frame: frame)

contentView.addSubview(dateLabel)

let constraints = [
dateLabel.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
dateLabel.centerYAnchor.constraint(equalTo: contentView.centerYAnchor)
]

NSLayoutConstraint.activate(constraints)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
92 changes: 91 additions & 1 deletion CalendarPOC/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,26 @@
//

import UIKit
import JTAppleCalendar

class ViewController: UIViewController {

let calendarView: JTACMonthView

init() {

calendarView = JTACMonthView(frame: .zero)
calendarView.scrollDirection = .vertical
calendarView.scrollingMode = .stopAtEachCalendarFrame
calendarView.translatesAutoresizingMaskIntoConstraints = false
calendarView.backgroundColor = .lightGray

super.init(nibName: nil, bundle: nil)

calendarView.calendarDataSource = self
calendarView.calendarDelegate = self
calendarView.register(DayCell.self, forCellWithReuseIdentifier: DayCell.reuseIdentifier)

}

required init?(coder: NSCoder) {
Expand All @@ -20,10 +35,85 @@ class ViewController: UIViewController {

// MARK: - UIViewController Lifecycle

override func loadView() {
super.loadView()

view.addSubview(calendarView)

setupConstraints()
}

private func setupConstraints() {
let constraints = [
calendarView.topAnchor.constraint(equalToSystemSpacingBelow: view.safeAreaLayoutGuide.topAnchor, multiplier: 1.0),
calendarView.leadingAnchor.constraint(equalToSystemSpacingAfter: view.safeAreaLayoutGuide.leadingAnchor, multiplier: 1.0),
calendarView.heightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.heightAnchor, multiplier: 0.5),
view.safeAreaLayoutGuide.trailingAnchor.constraint(equalToSystemSpacingAfter: calendarView.trailingAnchor, multiplier: 1.0),
view.safeAreaLayoutGuide.bottomAnchor.constraint(greaterThanOrEqualToSystemSpacingBelow: calendarView.bottomAnchor, multiplier: 1.0)
]
NSLayoutConstraint.activate(constraints)
}

override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = .orange
view.backgroundColor = .white

navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Today", style: .done, target: self, action: #selector(scrollToToday(_:)))
}

//MARK: - Actions

@objc
func scrollToToday(_ sender: Any) {
calendarView.scrollToDate(Date())
}
}

extension ViewController: JTACMonthViewDataSource {
func configureCalendar(_ calendar: JTACMonthView) -> ConfigurationParameters {
let startDate = Date(timeIntervalSinceNow: -60*60*24*365)
let endDate = Date(timeIntervalSinceNow: 60*60*24*365)

return ConfigurationParameters(startDate: startDate, endDate: endDate)
}
}

extension ViewController: JTACMonthViewDelegate {
func calendar(_ calendar: JTACMonthView, willDisplay cell: JTACDayCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {
guard let cell = cell as? DayCell else {
NSLog("This should never happen")
assert(false)
}

configure(cell, withCellState: cellState)
}

func calendar(_ calendar: JTACMonthView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTACDayCell {
guard let cell = calendarView.dequeueReusableJTAppleCell(withReuseIdentifier: DayCell.reuseIdentifier, for: indexPath) as? DayCell else {
NSLog("This should never happen")
assert(false)
}

configure(cell, withCellState: cellState)
return cell
}

private func configure(_ cell: DayCell, withCellState cellState: CellState) {
cell.dateLabel.text = cellState.text

if cellState.dateBelongsTo == .thisMonth {
cell.isHidden = false
} else {
cell.isHidden = true
}
}

func calendar(_ calendar: JTACMonthView, shouldSelectDate date: Date, cell: JTACDayCell?, cellState: CellState, indexPath: IndexPath) -> Bool {
return true
}

func calendar(_ calendar: JTACMonthView, didSelectDate date: Date, cell: JTACDayCell?, cellState: CellState, indexPath: IndexPath) {
print(date)
}
}

0 comments on commit c32f4ad

Please sign in to comment.