forked from WenchaoD/FSCalendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FullScreenExampleViewController.m
276 lines (214 loc) · 9.26 KB
/
FullScreenExampleViewController.m
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
//
// FullScreenExample.m
// FSCalendar
//
// Created by Wenchao Ding on 9/16/15.
// Copyright (c) 2015 Wenchao Ding. All rights reserved.
//
#import "FullScreenExampleViewController.h"
#import <EventKit/EventKit.h>
#import "LunarFormatter.h"
#import "FSCalendar.h"
NS_ASSUME_NONNULL_BEGIN
@interface FullScreenExampleViewController()<FSCalendarDataSource,FSCalendarDelegate,FSCalendarDelegateAppearance>
@property (weak, nonatomic) FSCalendar *calendar;
@property (assign, nonatomic) BOOL showsLunar;
@property (assign, nonatomic) BOOL showsEvents;
@property (strong, nonatomic) NSCache *cache;
- (void)todayItemClicked:(id)sender;
- (void)lunarItemClicked:(id)sender;
- (void)eventItemClicked:(id)sender;
@property (strong, nonatomic) NSCalendar *gregorian;
@property (strong, nonatomic) NSDateFormatter *dateFormatter;
@property (strong, nonatomic) NSDate *minimumDate;
@property (strong, nonatomic) NSDate *maximumDate;
@property (strong, nonatomic) LunarFormatter *lunarFormatter;
@property (strong, nonatomic) NSArray<EKEvent *> *events;
- (void)loadCalendarEvents;
- (nullable NSArray<EKEvent *> *)eventsForDate:(NSDate *)date;
@end
NS_ASSUME_NONNULL_END
@implementation FullScreenExampleViewController
#pragma mark - Life cycle
- (instancetype)init
{
self = [super init];
if (self) {
self.title = @"FSCalendar";
_lunarFormatter = [[LunarFormatter alloc] init];
}
return self;
}
- (void)loadView
{
UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
view.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.0];
self.view = view;
#define FULL_SCREEN 1
#if FULL_SCREEN
FSCalendar *calendar = [[FSCalendar alloc] initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height, self.view.bounds.size.width, self.view.bounds.size.height-self.navigationController.navigationBar.frame.size.height)];
calendar.backgroundColor = [UIColor whiteColor];
calendar.dataSource = self;
calendar.delegate = self;
calendar.pagingEnabled = NO; // important
calendar.allowsMultipleSelection = YES;
calendar.firstWeekday = 2;
calendar.placeholderType = FSCalendarPlaceholderTypeFillHeadTail;
calendar.appearance.caseOptions = FSCalendarCaseOptionsWeekdayUsesSingleUpperCase|FSCalendarCaseOptionsHeaderUsesUpperCase;
[self.view addSubview:calendar];
self.calendar = calendar;
#else
FSCalendar *calendar = [[FSCalendar alloc] initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height, self.view.bounds.size.width, 300)];
calendar.backgroundColor = [UIColor whiteColor];
calendar.dataSource = self;
calendar.delegate = self;
calendar.allowsMultipleSelection = YES;
calendar.firstWeekday = 2;
calendar.appearance.caseOptions = FSCalendarCaseOptionsWeekdayUsesSingleUpperCase|FSCalendarCaseOptionsHeaderUsesUpperCase;
[self.view addSubview:calendar];
self.calendar = calendar;
#endif
UIBarButtonItem *todayItem = [[UIBarButtonItem alloc] initWithTitle:@"Today" style:UIBarButtonItemStylePlain target:self action:@selector(todayItemClicked:)];
UIBarButtonItem *lunarItem = [[UIBarButtonItem alloc] initWithTitle:@"Lunar" style:UIBarButtonItemStylePlain target:self action:@selector(lunarItemClicked:)];
[lunarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor magentaColor]} forState:UIControlStateNormal];
UIBarButtonItem *eventItem = [[UIBarButtonItem alloc] initWithTitle:@"Event" style:UIBarButtonItemStylePlain target:self action:@selector(eventItemClicked:)];
[eventItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor purpleColor]} forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItems = @[eventItem ,lunarItem, todayItem];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
self.dateFormatter = [[NSDateFormatter alloc] init];
self.dateFormatter.dateFormat = @"yyyy-MM-dd";
self.minimumDate = [self.dateFormatter dateFromString:@"2020-02-03"];
self.maximumDate = [self.dateFormatter dateFromString:@"2023-04-10"];
self.calendar.accessibilityIdentifier = @"calendar";
[self loadCalendarEvents];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
[self.cache removeAllObjects];
}
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
#if FULL_SCREEN
self.calendar.frame = CGRectMake(0, CGRectGetMaxY(self.navigationController.navigationBar.frame), self.view.bounds.size.width, self.view.bounds.size.height-CGRectGetMaxY(self.navigationController.navigationBar.frame));
#else
self.calendar.frame = CGRectMake(0, CGRectGetMaxY(self.navigationController.navigationBar.frame), self.view.bounds.size.width, 300);
#endif
}
- (void)dealloc
{
NSLog(@"%s",__FUNCTION__);
}
#pragma mark - Target actions
- (void)todayItemClicked:(id)sender
{
[self.calendar setCurrentPage:[NSDate date] animated:YES];
}
- (void)lunarItemClicked:(UIBarButtonItem *)item
{
self.showsLunar = !self.showsLunar;
[self.calendar reloadData];
}
- (void)eventItemClicked:(id)sender
{
self.showsEvents = !self.showsEvents;
[self.calendar reloadData];
}
#pragma mark - FSCalendarDataSource
- (NSDate *)minimumDateForCalendar:(FSCalendar *)calendar
{
return self.minimumDate;
}
- (NSDate *)maximumDateForCalendar:(FSCalendar *)calendar
{
return self.maximumDate;
}
- (NSString *)calendar:(FSCalendar *)calendar subtitleForDate:(NSDate *)date
{
if (self.showsEvents) {
EKEvent *event = [self eventsForDate:date].firstObject;
if (event) {
return event.title;
}
}
if (self.showsLunar) {
return [self.lunarFormatter stringFromDate:date];
}
return nil;
}
#pragma mark - FSCalendarDelegate
- (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
{
NSLog(@"did select %@",[self.dateFormatter stringFromDate:date]);
}
- (void)calendarCurrentPageDidChange:(FSCalendar *)calendar
{
NSLog(@"did change page %@",[self.dateFormatter stringFromDate:calendar.currentPage]);
}
- (NSInteger)calendar:(FSCalendar *)calendar numberOfEventsForDate:(NSDate *)date
{
if (!self.showsEvents) return 0;
if (!self.events) return 0;
NSArray<EKEvent *> *events = [self eventsForDate:date];
return events.count;
}
- (NSArray<UIColor *> *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance eventDefaultColorsForDate:(NSDate *)date
{
if (!self.showsEvents) return nil;
if (!self.events) return nil;
NSArray<EKEvent *> *events = [self eventsForDate:date];
NSMutableArray<UIColor *> *colors = [NSMutableArray arrayWithCapacity:events.count];
[events enumerateObjectsUsingBlock:^(EKEvent * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[colors addObject:[UIColor colorWithCGColor:obj.calendar.CGColor]];
}];
return colors.copy;
}
#pragma mark - Private methods
- (void)loadCalendarEvents
{
__weak typeof(self) weakSelf = self;
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if(granted) {
NSDate *startDate = self.minimumDate;
NSDate *endDate = self.maximumDate;
NSPredicate *fetchCalendarEvents = [store predicateForEventsWithStartDate:startDate endDate:endDate calendars:nil];
NSArray<EKEvent *> *eventList = [store eventsMatchingPredicate:fetchCalendarEvents];
NSArray<EKEvent *> *events = [eventList filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(EKEvent * _Nullable event, NSDictionary<NSString *,id> * _Nullable bindings) {
return event.calendar.subscribed;
}]];
dispatch_async(dispatch_get_main_queue(), ^{
if (!weakSelf) return;
weakSelf.events = events;
[weakSelf.calendar reloadData];
});
} else {
// Alert
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Permission Error" message:@"Permission of calendar is required for fetching events." preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alertController animated:YES completion:nil];
}
}];
}
- (NSArray<EKEvent *> *)eventsForDate:(NSDate *)date
{
NSArray<EKEvent *> *events = [self.cache objectForKey:date];
if ([events isKindOfClass:[NSNull class]]) {
return nil;
}
NSArray<EKEvent *> *filteredEvents = [self.events filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(EKEvent * _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
return [evaluatedObject.occurrenceDate isEqualToDate:date];
}]];
if (filteredEvents.count) {
[self.cache setObject:filteredEvents forKey:date];
} else {
[self.cache setObject:[NSNull null] forKey:date];
}
return filteredEvents;
}
@end