Skip to content

Instantly share code, notes, and snippets.

View wdchris's full-sized avatar

Chris Harding wdchris

View GitHub Profile
lazy var collectionViewDataSource: CalendarCollectionViewDataSource = {
let range = CalendarManager.getCalendarRange(withStartDate: startDate, withEndDate: endDate)
let collectionView = CalendarCollectionViewDataSource(calendarRange: range)
return collectionView
}()
collectionView.dataSource = self.collectionViewDataSource
func numberOfSections(in collectionView: UICollectionView) -> Int {
return calendarRange.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return calendarRange[section].daysInMonth
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
struct CalendarRange {
let month: Int
let year: Int
let daysInMonth: Int
}
static func getCalendarRange(withStartDate startDate: Date, withEndDate endDate: Date) -> [CalendarRange] {
let months = Calendar.current.dateComponents([.month], from: startDate, to: endDate).month ?? 1
CalendarCell.register(with: collectionView)
CalendarHeader.register(with: collectionView)
class CalendarHeader: UICollectionReusableView {
@IBOutlet weak var monthLabel: UILabel!
static let identifier = "calendarHeader"
static func register(with collectionView: UICollectionView) {
collectionView.register(UINib.init(nibName: "CalendarHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader , withReuseIdentifier: identifier)
}
class CalendarCell: UICollectionViewCell {
@IBOutlet weak var dateLabel: UILabel!
static let identifier = "calendarCell"
static func register(with collectionView: UICollectionView) {
collectionView.register(UINib.init(nibName: "CalendarCell", bundle: nil), forCellWithReuseIdentifier: identifier)
}
lazy var collectionViewFlowLayout : CalendarCollectionViewFlowLayout = {
let layout = CalendarCollectionViewFlowLayout()
return layout
}()
collectionView.delegate = self.collectionViewFlowLayout
var itemsPerRow: CGFloat = 3
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
guard let flow = collectionViewLayout as? UICollectionViewFlowLayout else {
fatalError("only flow layout is supported")
}
let paddingSpace = flow.sectionInset.left + flow.sectionInset.right + (minimumInteritemSpacing * itemsPerRow)
let availableWidth = collectionView.frame.width - paddingSpace
let widthPerItem = availableWidth / itemsPerRow
override func viewDidLoad() {
super.viewDidLoad()
contentView.addSubview(collectionView)
collectionView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 0).isActive = true
collectionView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: 0).isActive = true
collectionView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 0).isActive = true
collectionView.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: 0).isActive = true
}
import React, { useState } from "react"
const formEncode = data => {
return Object.keys(data)
.map(key => encodeURIComponent(key) + "=" + encodeURIComponent(data[key]))
.join("&")
}
const RegistrationForm = () => {
const [email, setEmail] = useState("")