forked from klazuka/Kal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKalLogic.m
148 lines (118 loc) · 4.47 KB
/
KalLogic.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
/*
* Copyright (c) 2009 Keith Lazuka
* License: http://www.opensource.org/licenses/mit-license.html
*/
#import "KalLogic.h"
#import "KalDate.h"
#import "KalPrivate.h"
@interface KalLogic ()
- (void)moveToMonthForDate:(NSDate *)date;
- (void)recalculateVisibleDays;
- (NSUInteger)numberOfDaysInPreviousPartialWeek;
- (NSUInteger)numberOfDaysInFollowingPartialWeek;
@property (nonatomic, retain) NSDate *fromDate;
@property (nonatomic, retain) NSDate *toDate;
@property (nonatomic, retain) NSArray *daysInSelectedMonth;
@property (nonatomic, retain) NSArray *daysInFinalWeekOfPreviousMonth;
@property (nonatomic, retain) NSArray *daysInFirstWeekOfFollowingMonth;
@end
@implementation KalLogic
@synthesize baseDate, fromDate, toDate, daysInSelectedMonth, daysInFinalWeekOfPreviousMonth, daysInFirstWeekOfFollowingMonth;
+ (NSSet *)keyPathsForValuesAffectingSelectedMonthNameAndYear
{
return [NSSet setWithObjects:@"baseDate", nil];
}
- (id)initForDate:(NSDate *)date
{
if ((self = [super init])) {
monthAndYearFormatter = [[NSDateFormatter alloc] init];
[monthAndYearFormatter setDateFormat:@"LLLL yyyy"];
[self moveToMonthForDate:date];
}
return self;
}
- (id)init
{
return [self initForDate:[NSDate date]];
}
- (void)moveToMonthForDate:(NSDate *)date
{
self.baseDate = [date cc_dateByMovingToFirstDayOfTheMonth];
[self recalculateVisibleDays];
}
- (void)retreatToPreviousMonth
{
[self moveToMonthForDate:[self.baseDate cc_dateByMovingToFirstDayOfThePreviousMonth]];
}
- (void)advanceToFollowingMonth
{
[self moveToMonthForDate:[self.baseDate cc_dateByMovingToFirstDayOfTheFollowingMonth]];
}
- (NSString *)selectedMonthNameAndYear;
{
return [monthAndYearFormatter stringFromDate:self.baseDate];
}
#pragma mark Low-level implementation details
- (NSUInteger)numberOfDaysInPreviousPartialWeek
{
return [self.baseDate cc_weekday] - 1;
}
- (NSUInteger)numberOfDaysInFollowingPartialWeek
{
NSDateComponents *c = [self.baseDate cc_componentsForMonthDayAndYear];
c.day = [self.baseDate cc_numberOfDaysInMonth];
NSDate *lastDayOfTheMonth = [[NSCalendar currentCalendar] dateFromComponents:c];
return 7 - [lastDayOfTheMonth cc_weekday];
}
- (NSArray *)calculateDaysInFinalWeekOfPreviousMonth
{
NSMutableArray *days = [NSMutableArray array];
NSDate *beginningOfPreviousMonth = [self.baseDate cc_dateByMovingToFirstDayOfThePreviousMonth];
int n = [beginningOfPreviousMonth cc_numberOfDaysInMonth];
int numPartialDays = [self numberOfDaysInPreviousPartialWeek];
NSDateComponents *c = [beginningOfPreviousMonth cc_componentsForMonthDayAndYear];
for (int i = n - (numPartialDays - 1); i < n + 1; i++)
[days addObject:[KalDate dateForDay:i month:c.month year:c.year]];
return days;
}
- (NSArray *)calculateDaysInSelectedMonth
{
NSMutableArray *days = [NSMutableArray array];
NSUInteger numDays = [self.baseDate cc_numberOfDaysInMonth];
NSDateComponents *c = [self.baseDate cc_componentsForMonthDayAndYear];
for (int i = 1; i < numDays + 1; i++)
[days addObject:[KalDate dateForDay:i month:c.month year:c.year]];
return days;
}
- (NSArray *)calculateDaysInFirstWeekOfFollowingMonth
{
NSMutableArray *days = [NSMutableArray array];
NSDateComponents *c = [[self.baseDate cc_dateByMovingToFirstDayOfTheFollowingMonth] cc_componentsForMonthDayAndYear];
NSUInteger numPartialDays = [self numberOfDaysInFollowingPartialWeek];
for (int i = 1; i < numPartialDays + 1; i++)
[days addObject:[KalDate dateForDay:i month:c.month year:c.year]];
return days;
}
- (void)recalculateVisibleDays
{
self.daysInSelectedMonth = [self calculateDaysInSelectedMonth];
self.daysInFinalWeekOfPreviousMonth = [self calculateDaysInFinalWeekOfPreviousMonth];
self.daysInFirstWeekOfFollowingMonth = [self calculateDaysInFirstWeekOfFollowingMonth];
KalDate *from = [self.daysInFinalWeekOfPreviousMonth count] > 0 ? [self.daysInFinalWeekOfPreviousMonth objectAtIndex:0] : [self.daysInSelectedMonth objectAtIndex:0];
KalDate *to = [self.daysInFirstWeekOfFollowingMonth count] > 0 ? [self.daysInFirstWeekOfFollowingMonth lastObject] : [self.daysInSelectedMonth lastObject];
self.fromDate = [[from NSDate] cc_dateByMovingToBeginningOfDay];
self.toDate = [[to NSDate] cc_dateByMovingToEndOfDay];
}
#pragma mark -
- (void) dealloc
{
[monthAndYearFormatter release];
[baseDate release];
[fromDate release];
[toDate release];
[daysInSelectedMonth release];
[daysInFinalWeekOfPreviousMonth release];
[daysInFirstWeekOfFollowingMonth release];
[super dealloc];
}
@end