Advent Calendar Sample App

![advent_calender_6.png](https://qiita-image-store.s3.amazonaws.com/0/66857/f506fab1-b235-1755-2770-e84975794d5c.png)


この記事はGoodpatchのエンジニアがお送りするGoodpatch Advent Calendar 2015の6日目の記事です！カレンダーの記事自体の言語はほとんど日本語ですが、今日は英語で書きます！

Hello! This is the 6rd installment for Goodpatch Advent Calendar 2015, a calendar put together by the development team at UI design agency, Goodpatch. Although a majority of the articles for this calendar are written in Japanese (we are based in Tokyo with a studio in Berlin, as well!), there will be a few articles written in English.


When asked to write something for the advent calendar, my brain defaulted to the most obvious connection -- making an advent calendar app. This is a simple advent Calendar app that anyone (particularly beginners of iOS) can download and customize to distribute to their loved ones. Of course, by the time you read this, we will already be 6 days into the Advent season but I'm sure anyone you give this to will understand the slight delay. And they'll have 6 things to open right away!

##Github link

You can find the [project on Github](https://github.com/eedawa/ELAdventCalendar/tree/master/ELAdventCalendar).

The calendar cells of the advent calendar are sourced information from the ELCalendarDataSource class. Replace the filler data with your own messages or make further changes on your own to include images or other content.

##NSDateComponents

Because setting date-based limitations is the key feature of an advent calendar, I needed date checking for both the local notifications and the calendar day object.

Local notifications are set up to alert a user once a day during the advent season to check the app. I set up these notifications by creating a category class for UIApplication.

```lang:UIApplication-NotificationSettings.m
- (void)setupLocalNotifications{
    
    [self cancelAllLocalNotifications];
    
    NSDate *currentDate = [NSDate date];
    // Extract the current year;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy"];
    NSString *currentYear = [formatter stringFromDate:currentDate];
    // Create a notification to fire for each day of December until the 25th of the current year.
    [formatter setDateFormat:@"dd"];
    NSString *currentDay = [formatter stringFromDate:currentDate];
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    [dateComponents setMonth:12];
    [dateComponents setYear:[currentYear integerValue]];
    [dateComponents setHour:21];
    for (NSInteger i=[currentDay integerValue]; i<26; i++) {
        [dateComponents setDay:i]; //set this based on the calendar day for the cell
        NSDate *fireDate = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
        NSInteger daysLeftToChristmas = 25-i;
        UILocalNotification* localNotification = [[UILocalNotification alloc] init];
        localNotification.fireDate = fireDate;
        NSString *messageString = @"It's Christmas!!! Check your advent calendar!";
        if (daysLeftToChristmas > 0){
            messageString =[NSString stringWithFormat:@"%ld days left to Christmas! Check your advent calendar!", (long)daysLeftToChristmas];
        }
        localNotification.alertBody = messageString;
        localNotification.alertAction = @"Hohoho";
        localNotification.timeZone = [NSTimeZone localTimeZone];
        localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
        // Schedule the notification
        [self scheduleLocalNotification:localNotification];

    }
}
```
I also needed to compare the current date to the advent date to decide whether the message for that day could be accessed or not.

```lang:ELCalendarDay.m
- (BOOL)isAvailable{
    
    BOOL isAvailable = NO;
    
    NSDate *currentDate = [NSDate date];
    
    // Extract the current year;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy"];
    NSString *currentYear = [formatter stringFromDate:currentDate];
    // Specify the advent calendar day for the cell with the current year.
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    [dateComponents setDay:[self.number integerValue]]; //set this based on the calendar day for the cell
    [dateComponents setMonth:12];
    [dateComponents setYear:[currentYear integerValue]];
    NSDate *calendarDay = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
    
    // Compare the current date to the cell's advent day.
    NSComparisonResult comparisonResult = [calendarDay compare:currentDate];
    if (comparisonResult == NSOrderedSame || comparisonResult == NSOrderedAscending){
        // Advent day is either equal to or before the current date. Therefore, we can show the hidden contents.
        isAvailable = YES;
    }
    return isAvailable;
}
```

Like I said, it's a very simple app from a few hours of coding so you are free to expand and improve!! Have a wonderful holiday season and please keep following our Advent Calendar! Next up is @subuta_nico!