-
Notifications
You must be signed in to change notification settings - Fork 121
/
LongPressWeekView.swift
60 lines (52 loc) · 2.64 KB
/
LongPressWeekView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//
// LongPressWeekView.swift
// JZCalendarWeekViewExample
//
// Created by Jeff Zhang on 30/4/18.
// Copyright © 2018 Jeff Zhang. All rights reserved.
//
import UIKit
import JZCalendarWeekView
/// All-Day & Long Press
class LongPressWeekView: JZLongPressWeekView {
override func registerViewClasses() {
super.registerViewClasses()
self.collectionView.register(UINib(nibName: LongPressEventCell.className, bundle: nil), forCellWithReuseIdentifier: LongPressEventCell.className)
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: LongPressEventCell.className, for: indexPath) as? LongPressEventCell,
let event = getCurrentEvent(with: indexPath) as? AllDayEvent {
cell.configureCell(event: event)
return cell
}
preconditionFailure("LongPressEventCell and AllDayEvent should be casted")
}
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if kind == JZSupplementaryViewKinds.allDayHeader {
guard let alldayHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: kind, for: indexPath) as? JZAllDayHeader else {
preconditionFailure("SupplementaryView should be JZAllDayHeader")
}
let date = flowLayout.dateForColumnHeader(at: indexPath)
let events = allDayEventsBySection[date]
let views = getAllDayHeaderViews(allDayEvents: events as? [AllDayEvent] ?? [])
alldayHeader.updateView(views: views)
return alldayHeader
}
return super.collectionView(collectionView, viewForSupplementaryElementOfKind: kind, at: indexPath)
}
private func getAllDayHeaderViews(allDayEvents: [AllDayEvent]) -> [UIView] {
var allDayViews = [UIView]()
for event in allDayEvents {
if let view = UINib(nibName: LongPressEventCell.className, bundle: nil).instantiate(withOwner: nil, options: nil)[0] as? LongPressEventCell {
view.configureCell(event: event, isAllDay: true)
allDayViews.append(view)
}
}
return allDayViews
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let selectedEvent = getCurrentEvent(with: indexPath) as? AllDayEvent {
ToastUtil.toastMessageInTheMiddle(message: selectedEvent.title)
}
}
}