-
Notifications
You must be signed in to change notification settings - Fork 895
/
Copy pathCalendarMonthController.swift
462 lines (385 loc) · 16.9 KB
/
CalendarMonthController.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//
// CalendarMonthController.swift
// TelegramMac
//
// Created by keepcoder on 17/01/2017.
// Copyright © 2017 Telegram. All rights reserved.
//
import Cocoa
import TGUIKit
import CalendarUtils
struct CalendarMonthInteractions {
let lowYear: Int
let canBeNoYear: Bool
let selectAction:(Date)->Void
let backAction:((Date)->Void)?
let nextAction:((Date)->Void)?
let changeYear: (Int32, Date)->Void
init(lowYear: Int, canBeNoYear: Bool, selectAction:@escaping (Date)->Void, backAction:((Date)->Void)? = nil, nextAction:((Date)->Void)? = nil, changeYear: @escaping(Int32, Date)->Void) {
self.selectAction = selectAction
self.backAction = backAction
self.nextAction = nextAction
self.changeYear = changeYear
self.lowYear = lowYear
self.canBeNoYear = canBeNoYear
}
}
final class CalendarMonthStruct {
let month:Date
let prevMonth:Date
let nextMonth:Date
let lastDayOfMonth:Int
let lastDayOfPrevMonth:Int
let lastDayOfNextMonth:Int
let currentStartDay:Int
var selectedDay:Int?
let components:DateComponents
let dayHandler:(Int)->Void
let onlyFuture: Bool
let limitedBy: Date?
let dayPreview:(Int)->NSView?
var linesCount: Int {
switch mode {
case .media:
var count: Int = 0
for i in 0 ..< 7 * 6 {
if i + 1 < currentStartDay {
count += 1
} else if (i + 2) - currentStartDay > lastDayOfMonth {
} else {
count += 1
}
}
let result = Int(ceil(Float(count) / 7))
return result
case .normal:
return 6
}
}
enum Mode {
case media
case normal
}
let mode: Mode
init(month:Date, mode: Mode = .normal, selectDayAnyway: Bool, onlyFuture: Bool, limitedBy: Date?, dayHandler:@escaping (Int)->Void, dayPreview:@escaping(Int)->NSView? = { _ in return nil }) {
self.month = month
self.onlyFuture = onlyFuture
self.limitedBy = limitedBy
self.dayHandler = dayHandler
self.prevMonth = CalendarUtils.stepMonth(-1, date: month)
self.nextMonth = CalendarUtils.stepMonth(1, date: month)
self.lastDayOfMonth = CalendarUtils.lastDay(ofTheMonth: month)
self.lastDayOfPrevMonth = CalendarUtils.lastDay(ofTheMonth: month)
self.lastDayOfNextMonth = CalendarUtils.lastDay(ofTheMonth: month)
self.mode = mode
self.dayPreview = dayPreview
var calendar = NSCalendar.current
// calendar.timeZone = TimeZone(abbreviation: "UTC")!
let components = calendar.dateComponents([.year, .month, .day], from: month)
self.currentStartDay = CalendarUtils.weekDay(Date(timeIntervalSince1970: month.timeIntervalSince1970 - TimeInterval(components.day! * 24*60*60)))
if selectDayAnyway {
selectedDay = components.day!
} else {
selectedDay = nil
}
self.components = components
}
}
class CalendarMonthView : View {
private var month:CalendarMonthStruct?
private var dayPreviews: [Int : NSView] = [:]
private let dayPreviewsViews = View()
private let dayViews = View()
required init(frame frameRect: NSRect) {
super.init(frame: frameRect)
addSubview(dayPreviewsViews)
addSubview(dayViews)
backgroundColor = theme.colors.background
}
override func scrollWheel(with event: NSEvent) {
if let month = month {
switch month.mode {
case .media:
super.scrollWheel(with: event)
default:
break
}
}
}
func layout(for month:CalendarMonthStruct) {
dayPreviewsViews.removeAllSubviews()
dayViews.removeAllSubviews()
if self.month?.month != month.month {
self.dayPreviews.removeAll()
}
self.month = month
for i in 0 ..< 7 * 6 {
let day = TextButton()
day.set(font: .normal(.text), for: .Normal)
day.set(background: theme.colors.background, for: .Normal)
day.scaleOnClick = true
let hideExcess: Bool
switch month.mode {
case .media:
hideExcess = true
case .normal:
hideExcess = false
}
let current:Int
if i + 1 < month.currentStartDay {
current = (month.lastDayOfPrevMonth - month.currentStartDay) + i + 2
day.set(color: theme.colors.grayText, for: .Normal)
day.isHidden = hideExcess
} else if (i + 2) - month.currentStartDay > month.lastDayOfMonth {
current = (i + 2) - (month.currentStartDay + month.lastDayOfMonth)
day.set(color: theme.colors.grayText, for: .Normal)
day.isHidden = hideExcess
} else {
current = (i + 1) - month.currentStartDay + 1
var skipDay: Bool = false
let calendar = NSCalendar.current
// calendar.timeZone = TimeZone(abbreviation: "UTC")!
let components = calendar.dateComponents([.day, .year, .month], from: Date())
if month.onlyFuture, CalendarUtils.isSameDate(month.month, date: Date(), checkDay: false) {
if current < components.day! {
skipDay = true
}
} else if month.onlyFuture, components.year! + 1 == month.components.year! && components.month! == month.components.month! {
if current > components.day! {
skipDay = true
}
} else if CalendarUtils.isSameDate(month.month, date: Date(), checkDay: false), current > components.day! {
skipDay = true
}
let dayTimeinterval = CalendarUtils.monthDay(current, date: month.month).timeIntervalSince1970
let dayPreview = self.dayPreviews[current] ?? month.dayPreview(Int(dayTimeinterval))
if let dayPreview = dayPreview {
self.dayPreviews[current] = dayPreview
self.dayPreviewsViews.addSubview(dayPreview)
}
day.contextObject = current
if let limitedBy = month.limitedBy {
let limited = calendar.dateComponents([.year, .month, .day], from: limitedBy)
if limited.year! < month.components.year! || limited.month! < month.components.month! || limited.day! < current {
skipDay = true
}
}
if !skipDay {
day.set(color: theme.colors.underSelectedColor, for: .Highlight)
if (i + 1) % 7 == 0 || (i + 2) % 7 == 0 {
day.set(color: theme.colors.redUI, for: .Normal)
} else {
day.set(color: theme.colors.text, for: .Normal)
}
if dayPreview != nil {
day.set(background: .clear, for: .Normal)
day.set(background: .clear, for: .Highlight)
day.set(color: .white, for: .Normal)
day.set(font: .medium(.text), for: .Normal)
} else {
switch month.mode {
case .media:
day.set(background: theme.colors.background, for: .Normal)
day.set(background: theme.colors.background, for: .Highlight)
day.set(color: theme.colors.text, for: .Highlight)
case .normal:
if let selectedDay = month.selectedDay, current == selectedDay {
// day.isSelected = true
day.set(color: theme.colors.underSelectedColor, for: .Normal)
day.set(background: theme.colors.accent, for: .Normal)
day.set(background: theme.colors.accent, for: .Highlight)
} else {
day.set(background: theme.colors.background, for: .Normal)
day.set(background: theme.colors.accent, for: .Highlight)
}
}
}
day.set(handler: { [weak self] (control) in
if month.mode != .media {
month.selectedDay = current
}
month.dayHandler(current)
self?.layout(for: month)
}, for: .Click)
} else {
day.set(color: theme.colors.grayText, for: .Normal)
}
}
day.set(text: "\(current)", for: .Normal)
dayViews.addSubview(day)
}
self.needsLayout = true
}
override func layout() {
super.layout()
dayViews.frame = bounds
dayPreviewsViews.frame = bounds
guard let month = self.month else {
return
}
let oneSize:NSSize
var inset:NSPoint
switch month.mode {
case .normal:
oneSize = NSMakeSize(floorToScreenPixels(backingScaleFactor, (frame.width - 20) / 7), floorToScreenPixels(backingScaleFactor, (frame.height - 20) / CGFloat(month.linesCount)))
inset = NSMakePoint(10, 10)
case .media:
oneSize = NSMakeSize(floorToScreenPixels(backingScaleFactor, (frame.width - 20) / 7), floorToScreenPixels(backingScaleFactor, frame.height / CGFloat(month.linesCount)))
inset = NSMakePoint(10, 0)
}
for i in 0 ..< dayViews.subviews.count {
if let view = dayViews.subviews[i] as? TextButton {
view.frame = NSMakeRect(inset.x, inset.y, oneSize.width, oneSize.height)
view.layer?.cornerRadius = view.frame.height / 2
if let currentDay = view.contextObject as? Int {
if let preview = self.dayPreviews[currentDay] {
preview.setFrameOrigin(inset.x + (view.frame.width - preview.frame.width) / 2, inset.y + (view.frame.height - preview.frame.height) / 2)
}
}
inset.x += oneSize.width
if (i + 1) % 7 == 0 {
inset.x = 10
inset.y += oneSize.height
}
}
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class CalendarMonthController: GenericViewController<CalendarMonthView> {
let interactions:CalendarMonthInteractions
let month:CalendarMonthStruct
let onlyFuture: Bool
let limitedBy: Date?
let lowYear: Int
init(_ month:Date, onlyFuture: Bool, limitedBy: Date?, selectDayAnyway: Bool, interactions:CalendarMonthInteractions, lowYear: Int = 2013) {
self.onlyFuture = onlyFuture
self.limitedBy = limitedBy
self.lowYear = lowYear
self.month = CalendarMonthStruct(month: month, selectDayAnyway: selectDayAnyway, onlyFuture: self.onlyFuture, limitedBy: self.limitedBy, dayHandler: { day in
interactions.selectAction(CalendarUtils.monthDay(day, date: month))
})
self.interactions = interactions
super.init()
self.bar = NavigationBarStyle(height: 40)
}
override func getCenterBarViewOnce() -> TitledBarView {
let formatter:DateFormatter = DateFormatter()
formatter.locale = Locale(identifier: appAppearance.language.languageCode)
formatter.dateFormat = "LLLL"
let monthString:String = formatter.string(from: month.month)
formatter.dateFormat = "yyyy"
let yearString:String
if month.components.year == 1 {
yearString = "—"
} else {
yearString = formatter.string(from: month.month)
}
let barView = TitledBarView(controller: self, .initialize(string: monthString, color: theme.colors.text, font:.medium(.text)), .initialize(string:yearString, color: theme.colors.grayText, font:.normal(.small)))
barView.removeAllHandlers()
let lowYear = self.interactions.lowYear
let canBeNoYear = self.interactions.canBeNoYear
barView.contextMenu = { [weak self] in
guard let `self` = self else {
return nil
}
let nowTimestamp = Int32(CFAbsoluteTimeGetCurrent() + NSTimeIntervalSince1970)
var now: time_t = time_t(nowTimestamp)
var timeinfoNow: tm = tm()
localtime_r(&now, &timeinfoNow)
var items:[ContextMenuItem] = []
if canBeNoYear {
items.append(.init("—", handler: { [weak self] in
guard let `self` = self else {
return
}
self.interactions.changeYear(1, self.month.month)
}))
}
for i in stride(from: 1900 + timeinfoNow.tm_year, to: Int32(lowYear) - 1, by: -1) {
items.append(.init("\(i)", handler: { [weak self] in
guard let `self` = self else {
return
}
self.interactions.changeYear(i, self.month.month)
}))
}
if !items.isEmpty && !self.onlyFuture {
let menu = ContextMenu(betterInside: true)
for item in items {
menu.addItem(item)
}
return menu
}
return nil
}
return barView
}
var isNextEnabled:Bool {
if self.onlyFuture {
var calendar = NSCalendar.current
// calendar.timeZone = TimeZone(abbreviation: "UTC")!
let components = calendar.dateComponents([.year, .month, .day], from: Date())
if month.components.year! == components.year! {
return true
} else if components.year! + 1 == month.components.year! {
return month.components.month! < components.month!
}
return true
}
if month.components.year! == 1, month.components.month! == 12 {
return false
}
return !CalendarUtils.isSameDate(month.month, date: Date(), checkDay: false)
}
var isPrevEnabled:Bool {
if self.onlyFuture {
return !CalendarUtils.isSameDate(month.month, date: Date(), checkDay: false)
}
return month.components.year! > lowYear || ((month.components.year == lowYear || month.components.year! == 1) && month.components.month! > 1)
}
override func getLeftBarViewOnce() -> BarView {
let bar = ImageBarView(controller: self, theme.icons.calendarBack)
bar.button.isEnabled = isPrevEnabled
if isPrevEnabled {
bar.button.set(handler: { [weak self] (control) in
if let backAction = self?.interactions.backAction, let month = self?.month.month {
backAction(month)
}
}, for: .Click)
}
bar.set(image: bar.button.isEnabled ? theme.icons.calendarBack : theme.icons.calendarBackDisabled)
bar.setFrameSize(40,bar.frame.height)
bar.set(background: theme.colors.background, for: .Normal)
return bar
}
override func getRightBarViewOnce() -> BarView {
let bar = ImageBarView(controller: self, theme.icons.calendarNext)
bar.button.isEnabled = isNextEnabled
if isNextEnabled {
bar.button.set(handler: { [weak self] (control) in
if let nextAction = self?.interactions.nextAction, let month = self?.month.month {
nextAction(month)
}
}, for: .Click)
}
bar.set(image: bar.button.isEnabled ? theme.icons.calendarNext : theme.icons.calendarNextDisabled)
bar.setFrameSize(40,bar.frame.height)
bar.set(background: theme.colors.background, for: .Normal)
return bar
}
override var removeAfterDisapper: Bool {
return true
}
override func viewDidLoad() {
super.viewDidLoad()
genericView.layout(for: month)
readyOnce()
}
deinit {
var bp:Int = 0
bp += 1
}
}