OSDN Git Service

UIを調整
[kcd/KCD.git] / KCD / HMPeriodicNotifier.m
1 //
2 //  HMPeriodicNotifier.m
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2015/06/13.
6 //  Copyright (c) 2015年 Hori,Masaki. All rights reserved.
7 //
8
9 #import "HMPeriodicNotifier.h"
10
11
12 NSString *HMPeriodicNotification = @"HMPeriodicNotification";
13
14 @interface HMPeriodicNotifier ()
15 @property NSUInteger hour;
16 @property NSUInteger minutes;
17
18 @property (strong, nonatomic) NSDateComponents *currentDay;
19 @property (strong, nonatomic) NSTimer *timer;
20 @end
21
22 @implementation HMPeriodicNotifier
23
24 + (instancetype)periodicNotifierWithHour:(NSUInteger)hour minutes:(NSUInteger)minutes
25 {
26         return [[self alloc] initWithHour:hour minutes:minutes];
27 }
28 - (instancetype)initWithHour:(NSUInteger)hour minutes:(NSUInteger)minutes
29 {
30         self = [super init];
31         if(self) {
32                 _hour = hour;
33                 _minutes = minutes;
34                 [self registerNotifications];
35                 [self notifyIfNeeded:nil];
36         }
37         return self;
38 }
39
40 - (void)registerNotifications
41 {
42         NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
43         
44         [nc addObserver:self
45                    selector:@selector(notifyIfNeeded:)
46                            name:NSSystemTimeZoneDidChangeNotification
47                          object:nil];
48         [nc addObserver:self
49                    selector:@selector(notifyIfNeeded:)
50                            name:NSSystemClockDidChangeNotification
51                          object:nil];
52         [nc addObserver:self
53                    selector:@selector(notifyIfNeeded:)
54                            name:NSWorkspaceDidWakeNotification
55                          object:nil];
56 }
57
58 - (void)notifyIfNeeded:(id)timer
59 {
60         NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0];
61         NSCalendarUnit unit = NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
62         NSDateComponents *currentDay = [[NSCalendar currentCalendar] components:unit fromDate:now];
63         currentDay.hour = self.hour;
64         currentDay.minute = self.minutes;
65         NSDate *notifyDate = [[NSCalendar currentCalendar] dateFromComponents:currentDay];
66         
67         if([now compare:notifyDate] == NSOrderedDescending) {
68                 currentDay.day += 1;
69                 [[NSNotificationCenter defaultCenter] postNotificationName:HMPeriodicNotification
70                                                                                                                         object:self];
71         }
72         
73         if(self.timer.valid) {
74                 [self.timer invalidate];
75         }
76         
77         NSDate *nextNotifyDate = [[NSCalendar currentCalendar] dateFromComponents:currentDay];
78         NSTimeInterval nextNotifyTime = [nextNotifyDate timeIntervalSinceNow];
79         
80         self.timer = [NSTimer scheduledTimerWithTimeInterval:nextNotifyTime + 0.1
81                                                                                                   target:self
82                                                                                                 selector:_cmd
83                                                                                                 userInfo:nil
84                                                                                                  repeats:NO];
85 }
86
87 @end