Skip to content

Instantly share code, notes, and snippets.

@muhku
Created August 22, 2010 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muhku/543607 to your computer and use it in GitHub Desktop.
Save muhku/543607 to your computer and use it in GitHub Desktop.
diff --git a/src/TapkuLibrary/TKCalendarMonthTiles.m b/src/TapkuLibrary/TKCalendarMonthTiles.m
index e48642d..9a2e184 100755
--- a/src/TapkuLibrary/TKCalendarMonthTiles.m
+++ b/src/TapkuLibrary/TKCalendarMonthTiles.m
@@ -57,15 +57,24 @@
NSDate *d = [NSDate dateFromDateInformation:info];
info = [d dateInformation];
- if(info.weekday > 1){
+ if((sunday && info.weekday>1) || (!sunday && info.weekday!=2)){
+
TKDateInformation info2 = info;
-
info2.month--;
if(info2.month<1) { info2.month = 12; info2.year--; }
NSDate *previousMonth = [NSDate dateFromDateInformation:info2];
int preDayCnt = [previousMonth daysInMonth];
- info2.day = preDayCnt - info.weekday+2;
+ info2.day = preDayCnt - info.weekday;
+ if (sunday) {
+ info2.day += 2;
+ } else {
+ if (info.weekday == 1) {
+ info2.day -= 4;
+ } else {
+ info2.day += 3;
+ }
+ }
firstDate = [NSDate dateFromDateInformation:info2];
@@ -82,8 +91,13 @@
info.day = daysInMonth;
NSDate *lastInMonth = [NSDate dateFromDateInformation:info];
info = [lastInMonth dateInformation];
- if(info.weekday < 7){
- info.day = 7 - info.weekday;
+ if((sunday && info.weekday < 7) || (!sunday && info.weekday != 1)){
+ if (sunday) {
+ info.day = 7 - info.weekday;
+ }
+ else {
+ info.day = 7 - info.weekday + 1;
+ }
info.month++;
if(info.month>12){
info.month = 1;
@@ -103,30 +117,43 @@
marks = [markArray retain];
monthDate = [date retain];
startOnSunday = sunday;
-
+
TKDateInformation dateInfo = [monthDate dateInformation];
firstWeekday = dateInfo.weekday;
daysInMonth = [date daysInMonth];
- int row = (daysInMonth + dateInfo.weekday - 1);
- row = (row / 7) + ((row % 7 == 0) ? 0:1);
- float h = 44 * row;
+
TKDateInformation todayInfo = [[NSDate date] dateInformation];
today = dateInfo.month == todayInfo.month && dateInfo.year == todayInfo.year ? todayInfo.day : 0;
- if(firstWeekday>1){
+ if((sunday && firstWeekday>1) || (!sunday && firstWeekday!=2)){
dateInfo.month--;
if(dateInfo.month<1) {
dateInfo.month = 12;
dateInfo.year--;
}
NSDate *previousMonth = [NSDate dateFromDateInformation:dateInfo];
- int preDayCnt = [previousMonth daysInMonth];
- firstOfPrev = preDayCnt - firstWeekday+2;
+ int preDayCnt = [previousMonth daysInMonth];
+ firstOfPrev = preDayCnt - firstWeekday;
+ if (sunday) {
+ firstOfPrev += 2;
+ } else {
+ if (firstWeekday == 1) {
+ firstOfPrev -= 4;
+ } else {
+ firstOfPrev += 3;
+ }
+ }
lastOfPrev = preDayCnt;
}
+ int prevDays = (firstOfPrev == -1 ? 0 : lastOfPrev - firstOfPrev + 1);
+ int row = daysInMonth + prevDays;
+ row = (row / 7) + ((row % 7 == 0) ? 0:1);
+
+ float h = 44 * row;
+
if(![super initWithFrame:CGRectMake(0, 1, 320, h+1)]) return nil;
[self.selectedImageView addSubview:self.currentDay];
diff --git a/src/TapkuLibrary/TKCalendarMonthView.m b/src/TapkuLibrary/TKCalendarMonthView.m
index 4570d2c..0a02f70 100755
--- a/src/TapkuLibrary/TKCalendarMonthView.m
+++ b/src/TapkuLibrary/TKCalendarMonthView.m
@@ -103,14 +103,21 @@
- (id) init{
- return [self initWithSundayAsFirst:YES];
+ BOOL sundayAsFirst = YES;
+ CFCalendarRef currentCalendar = CFCalendarCopyCurrent();
+ if (CFCalendarGetFirstWeekday(currentCalendar) == 2) {
+ sundayAsFirst = NO;
+ }
+ CFRelease(currentCalendar);
+
+ return [self initWithSundayAsFirst:sundayAsFirst];
}
- (id) initWithSundayAsFirst:(BOOL)s{
sunday = s;
currentTile = [[[TKCalendarMonthTiles alloc] initWithMonth:[self firstOfMonthFromDate:[NSDate date]]
- marks:nil startDayOnSunday:YES] autorelease];
+ marks:nil startDayOnSunday:sunday] autorelease];
[currentTile setTarget:self action:@selector(tile:)];
[currentTile setTarget:self action:@selector(tile:)];
@@ -293,7 +300,7 @@
NSArray *dates = [TKCalendarMonthTiles rangeOfDatesInMonthGrid:[currentTile monthDate] startOnSunday:sunday];
NSArray *ar = [dataSource calendarMonthView:self marksFromDate:[dates objectAtIndex:0] toDate:[dates lastObject]];
- TKCalendarMonthTiles *refresh = [[[TKCalendarMonthTiles alloc] initWithMonth:[currentTile monthDate] marks:ar startDayOnSunday:YES] autorelease];
+ TKCalendarMonthTiles *refresh = [[[TKCalendarMonthTiles alloc] initWithMonth:[currentTile monthDate] marks:ar startDayOnSunday:sunday] autorelease];
[refresh setTarget:self action:@selector(tile:)];
[self.tileBox addSubview:refresh];
diff --git a/src/TapkuLibrary/TKCalendarMonthTiles.m b/src/TapkuLibrary/TKCalendarMonthTiles.m
index 9a2e184..8a13f7a 100755
--- a/src/TapkuLibrary/TKCalendarMonthTiles.m
+++ b/src/TapkuLibrary/TKCalendarMonthTiles.m
@@ -323,12 +323,13 @@
if(row == (int) (self.bounds.size.height / 44)) row --;
+ day = row * 7 + column - firstWeekday+(startOnSunday ? 2 : (firstWeekday == 1 ? -4 : 3));
- if(row==0 && column < firstWeekday-1){
+
+ if(row==0 && day < 1){
day = firstOfPrev + column;
}else{
portion = 1;
- day = row * 7 + column - firstWeekday+2;
}
if(portion > 0 && day > daysInMonth){
portion = 2;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment