-
Notifications
You must be signed in to change notification settings - Fork 895
/
Copy pathCalendarController.swift
113 lines (90 loc) · 4.08 KB
/
CalendarController.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// CalendarController.swift
// TelegramMac
//
// Created by keepcoder on 17/01/2017.
// Copyright © 2017 Telegram. All rights reserved.
//
import Cocoa
import TGUIKit
import CalendarUtils
class CalendarControllerView : View {
}
private final class CalendarNavigation : NavigationViewController {
}
class CalendarController: GenericViewController<CalendarControllerView> {
private var navigation:CalendarNavigation!
private var interactions:CalendarMonthInteractions!
private let onlyFuture: Bool
private let lowYear: Int
private let current: Date
private let limitedBy: Date?
override func viewDidLoad() {
super.viewDidLoad()
addSubview(navigation.view)
readyOnce()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.navigation.viewDidAppear(animated)
self.window?.set(handler: { [weak self] _ -> KeyHandlerResult in
if let current = self?.navigation.controller as? CalendarMonthController, current.isPrevEnabled, let backAction = self?.interactions.backAction {
backAction(current.month.month)
}
return .invoked
}, with: self, for: .LeftArrow, priority: .modal)
self.window?.set(handler: { [weak self] _ -> KeyHandlerResult in
if let current = self?.navigation.controller as? CalendarMonthController, current.isNextEnabled, let nextAction = self?.interactions.nextAction {
nextAction(current.month.month)
}
return .invoked
}, with: self, for: .RightArrow, priority: .modal)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigation.viewWillDisappear(animated)
self.window?.remove(object: self, for: .LeftArrow)
self.window?.remove(object: self, for: .RightArrow)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
self.navigation.viewDidDisappear(animated)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigation.viewWillAppear(animated)
}
init(_ frameRect:NSRect, _ window: Window, current: Date = Date(), onlyFuture: Bool = false, limitedBy: Date? = nil, lowYear: Int = 2013, canBeNoYear: Bool = false, selectHandler:@escaping (Date)->Void) {
self.onlyFuture = onlyFuture
self.current = current
self.limitedBy = limitedBy
self.lowYear = lowYear
super.init(frame: frameRect)
bar = .init(height: 0)
self.interactions = CalendarMonthInteractions(lowYear: lowYear, canBeNoYear: canBeNoYear, selectAction: { [weak self] (selected) in
self?.popover?.hide()
self?.navigationController?.modal?.close()
selectHandler(selected)
}, backAction: { [weak self] date in
if let strongSelf = self {
strongSelf.navigation.push(strongSelf.stepMonth(date: CalendarUtils.stepMonth(-1, date: date)), style: .pop)
}
}, nextAction: { [weak self] date in
if let strongSelf = self {
strongSelf.navigation.push(strongSelf.stepMonth(date: CalendarUtils.stepMonth(1, date: date)), style: .push)
}
}, changeYear: { [weak self] year, date in
if let strongSelf = self {
strongSelf.navigation.push(strongSelf.stepMonth(date: CalendarUtils.year(Int(year), date: date)), style: .push)
}
})
self.navigation = CalendarNavigation(stepMonth(date: current), window)
self.navigation._frameRect = frameRect
}
func stepMonth(date:Date) -> CalendarMonthController {
return CalendarMonthController(date, onlyFuture: self.onlyFuture, limitedBy: limitedBy, selectDayAnyway: CalendarUtils.isSameDate(current, date: date, checkDay: false), interactions: interactions, lowYear: self.lowYear)
}
override var isAutoclosePopover: Bool {
return false
}
}