Skip to content

Instantly share code, notes, and snippets.

View gmoraleda's full-sized avatar

Guillermo Moraleda gmoraleda

View GitHub Profile
func getOffsetForDate(date: Date) -> CGFloat {
// dayViewHeight being the height of the view that containts the hour label and the line separator
let components = Calendar.current.dateComponents([.minute, .hour], from: date)
let hourOffset = CGFloat(components.hour ?? 0) * (dayViewHeight + Sizes.gridLineSeparation)
let minuteOffset = ceilToPixelsAccuracy(CGFloat(components.minute ?? 0) / 60 * (dayViewHeight + Sizes.gridLineSeparation))
return hourOffset + minuteOffset + ceilToPixelsAccuracy(dayViewHeight / 2)
}
// Function to ceil according to the scaling factor
func getStartOffset(for event: CalendarEvent) -> CGFloat {
return gridView.getOffsetForDate(date: event.startDate)
}
func getHeight(for event: CalendarEvent) -> CGFloat {
let height = gridView.getOffsetForDate(date: event.endDate) - gridView.getOffsetForDate(date: event.startDate)
return height > Sizes.minEventHeight ? height : Sizes.minEventHeight
}
let myCollectionView: UICollectionView?
lazy var collectionView = setupCollectionView()
private func setupCollectionView() -> UICollectionView {
let layout = UICollectionViewFlowLayout()
layout.minimumInteritemSpacing = 0
layout.itemSize = CGSize(width: 50, height:50)
// Perform any other customizing to your layout/UICollectionView
return UICollectionView(frame: .zero, collectionViewLayout: layout)
let myCollectionView = UICollectionView()
// ... things go on until I have a frame and a layout...
myCollectionView = UICollectionView(frame: myFrame, collectionViewLayout: myCollectionViewLayout)
struct ApiResponse: Codable {
let errorCode: ErrorCode
let name: String
let age: Int
private enum CodingKeys: String, CodingKey {
case errorCode = "errorcode"
case name = "firstname"
case age = "userage"
}
struct ApiResponse: Codable {
let errorCode: ErrorCode
let name: String
let age: Int
}
enum ErrorCode: Int, Error, Codable {
case noError = 0
case noConnection = 401
case userNotFound = 404
// ...
var localizedDescription: String? {
switch self {
case .noError:
return nil
struct ApiResponse: Codable {
let errorCode: Int
let name: String
let age: Int
}
Alamofire.request(requestURL, method: .get, parameters: parameters, encoding: URLEncoding.default).responseData { response in
if let data = response.result.value {
let decoder = JSONDecoder()
do {
let apiResponse = try decoder.decode(ApiResponse.self, from: data)
// Do stuff
} catch {
NSLog("Error while decoding")
// Error handling
}