-
Notifications
You must be signed in to change notification settings - Fork 0
/
JTCalendar.m
executable file
·248 lines (197 loc) · 6.87 KB
/
JTCalendar.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
//
// JTCalendar.m
// JTCalendar
//
// Created by Jonathan Tribouharet
//
#import "JTCalendar.h"
#define NUMBER_PAGES_LOADED 5 // Must be the same in JTCalendarView, JTCalendarMenuView, JTCalendarContentView
@interface JTCalendar(){
BOOL cacheLastWeekMode;
NSUInteger cacheFirstWeekDay;
}
@end
@implementation JTCalendar
- (instancetype)init
{
self = [super init];
if(!self){
return nil;
}
self->_currentDate = [NSDate date];
self->_calendarAppearance = [JTCalendarAppearance new];
self->_dataCache = [JTCalendarDataCache new];
self.dataCache.calendarManager = self;
return self;
}
// Bug in iOS
- (void)dealloc
{
[self->_menuMonthsView setDelegate:nil];
[self->_contentView setDelegate:nil];
}
- (void)setMenuMonthsView:(JTCalendarMenuView *)menuMonthsView
{
[self->_menuMonthsView setDelegate:nil];
[self->_menuMonthsView setCalendarManager:nil];
self->_menuMonthsView = menuMonthsView;
[self->_menuMonthsView setDelegate:self];
[self->_menuMonthsView setCalendarManager:self];
cacheLastWeekMode = self.calendarAppearance.isWeekMode;
cacheFirstWeekDay = self.calendarAppearance.calendar.firstWeekday;
[self.menuMonthsView setCurrentDate:self.currentDate];
[self.menuMonthsView reloadAppearance];
}
- (void)setContentView:(JTCalendarContentView *)contentView
{
[self->_contentView setDelegate:nil];
[self->_contentView setCalendarManager:nil];
self->_contentView = contentView;
[self->_contentView setDelegate:self];
[self->_contentView setCalendarManager:self];
[self.contentView setCurrentDate:self.currentDate];
[self.contentView reloadAppearance];
}
- (void)reloadData
{
// Erase cache
[self.dataCache reloadData];
[self repositionViews];
[self.contentView reloadData];
}
- (void)reloadAppearance
{
[self.menuMonthsView reloadAppearance];
[self.contentView reloadAppearance];
if(cacheLastWeekMode != self.calendarAppearance.isWeekMode || cacheFirstWeekDay != self.calendarAppearance.calendar.firstWeekday){
cacheLastWeekMode = self.calendarAppearance.isWeekMode;
cacheFirstWeekDay = self.calendarAppearance.calendar.firstWeekday;
if(self.calendarAppearance.focusSelectedDayChangeMode && self.currentDateSelected){
[self setCurrentDate:self.currentDateSelected];
}
else{
[self setCurrentDate:self.currentDate];
}
}
}
- (void)setCurrentDate:(NSDate *)currentDate
{
NSAssert(currentDate, @"JTCalendar currentDate cannot be null");
self->_currentDate = currentDate;
[self.menuMonthsView setCurrentDate:currentDate];
[self.contentView setCurrentDate:currentDate];
[self repositionViews];
[self.contentView reloadData];
}
#pragma mark - UIScrollView delegate
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
if(self.calendarAppearance.isWeekMode){
return;
}
CGFloat ratio = CGRectGetWidth(self.contentView.frame) / CGRectGetWidth(self.menuMonthsView.frame);
if(isnan(ratio)){
ratio = 1.;
}
ratio *= self.calendarAppearance.ratioContentMenu;
if(sender == self.menuMonthsView && self.menuMonthsView.scrollEnabled){
self.contentView.contentOffset = CGPointMake(sender.contentOffset.x * ratio, self.contentView.contentOffset.y);
}
else if(sender == self.contentView && self.contentView.scrollEnabled){
self.menuMonthsView.contentOffset = CGPointMake(sender.contentOffset.x / ratio, self.menuMonthsView.contentOffset.y);
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
if(scrollView == self.contentView){
self.menuMonthsView.scrollEnabled = NO;
}
else if(scrollView == self.menuMonthsView){
self.contentView.scrollEnabled = NO;
}
}
// Use for scroll with scrollRectToVisible or setContentOffset
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
[self updatePage];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self updatePage];
}
- (void)updatePage
{
CGFloat pageWidth = CGRectGetWidth(self.contentView.frame);
CGFloat fractionalPage = self.contentView.contentOffset.x / pageWidth;
int currentPage = roundf(fractionalPage);
if (currentPage == (NUMBER_PAGES_LOADED / 2)){
if(!self.calendarAppearance.isWeekMode){
self.menuMonthsView.scrollEnabled = YES;
}
self.contentView.scrollEnabled = YES;
return;
}
NSCalendar *calendar = self.calendarAppearance.calendar;
NSDateComponents *dayComponent = [NSDateComponents new];
dayComponent.month = 0;
dayComponent.day = 0;
if(!self.calendarAppearance.isWeekMode){
dayComponent.month = currentPage - (NUMBER_PAGES_LOADED / 2);
}
else{
dayComponent.day = 7 * (currentPage - (NUMBER_PAGES_LOADED / 2));
}
if(self.calendarAppearance.readFromRightToLeft){
dayComponent.month *= -1;
dayComponent.day *= -1;
}
NSDate *currentDate = [calendar dateByAddingComponents:dayComponent toDate:self.currentDate options:0];
[self setCurrentDate:currentDate];
if(!self.calendarAppearance.isWeekMode){
self.menuMonthsView.scrollEnabled = YES;
}
self.contentView.scrollEnabled = YES;
if(currentPage < (NUMBER_PAGES_LOADED / 2)){
if([self.dataSource respondsToSelector:@selector(calendarDidLoadPreviousPage)]){
[self.dataSource calendarDidLoadPreviousPage];
}
}
else if(currentPage > (NUMBER_PAGES_LOADED / 2)){
if([self.dataSource respondsToSelector:@selector(calendarDidLoadNextPage)]){
[self.dataSource calendarDidLoadNextPage];
}
}
}
- (void)repositionViews
{
// Position to the middle page
CGFloat pageWidth = CGRectGetWidth(self.contentView.frame);
self.contentView.contentOffset = CGPointMake(pageWidth * ((NUMBER_PAGES_LOADED / 2)), self.contentView.contentOffset.y);
CGFloat menuPageWidth = CGRectGetWidth([self.menuMonthsView.subviews.firstObject frame]);
self.menuMonthsView.contentOffset = CGPointMake(menuPageWidth * ((NUMBER_PAGES_LOADED / 2)), self.menuMonthsView.contentOffset.y);
}
- (void)loadNextMonth
{
[self loadNextPage];
}
- (void)loadPreviousMonth
{
[self loadPreviousPage];
}
- (void)loadNextPage
{
self.menuMonthsView.scrollEnabled = NO;
CGRect frame = self.contentView.frame;
frame.origin.x = frame.size.width * ((NUMBER_PAGES_LOADED / 2) + 1);
frame.origin.y = 0;
[self.contentView scrollRectToVisible:frame animated:YES];
}
- (void)loadPreviousPage
{
self.menuMonthsView.scrollEnabled = NO;
CGRect frame = self.contentView.frame;
frame.origin.x = frame.size.width * ((NUMBER_PAGES_LOADED / 2) - 1);
frame.origin.y = 0;
[self.contentView scrollRectToVisible:frame animated:YES];
}
@end