-
Notifications
You must be signed in to change notification settings - Fork 0
/
NotificationsDetailViewController.m
128 lines (91 loc) · 4.35 KB
/
NotificationsDetailViewController.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
//
// NotificationsDetailViewController.m
// Metasome
//
// Created by Omar Metwally on 9/15/13.
// Copyright (c) 2013 Logisome. All rights reserved.
//
#import "NotificationsDetailViewController.h"
#import "TextFormatter.h"
#import "GAI.h"
#import "GAIDictionaryBuilder.h"
@interface NotificationsDetailViewController ()
@end
NSString * const MetasomeNotificationPrefKey = @"MetasomeNotificationPrefKey";
@implementation NotificationsDetailViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[TextFormatter formatTitleLabel:titleLabel withTitle:@"Reminders"];
[[self navigationItem] setTitleView:titleLabel];
}
return self;
}
-(void)preferredContentSizeChanged:(NSNotification *)aNotification
{
[promptLabel setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]];
[promptLabel2 setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]];
[self.view setNeedsLayout];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// set up google analytics view
[self setScreenName:@"NotificationsDetailViewController"];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(preferredContentSizeChanged:) name:UIContentSizeCategoryDidChangeNotification object:nil];
[notificationSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
BOOL onState = [[NSUserDefaults standardUserDefaults] boolForKey:MetasomeNotificationPrefKey];
[notificationSwitch setOn:onState];
if ( [[NSUserDefaults standardUserDefaults] boolForKey:MetasomeNotificationPrefKey] ) {
bellImage.image = [UIImage imageNamed:@"bell.png"];
} else {
bellImage.image = [UIImage imageNamed:@"crossed_out_bell.png"];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)switchChanged:(id)sender
{
UISwitch *s = (UISwitch *)sender;
[[NSUserDefaults standardUserDefaults] setBool:s.on forKey:MetasomeNotificationPrefKey];
if ( [[NSUserDefaults standardUserDefaults] boolForKey:MetasomeNotificationPrefKey] ) {
bellImage.image = [UIImage imageNamed:@"bell.png"];
} else {
bellImage.image = [UIImage imageNamed:@"crossed_out_bell.png"];
}
}
/* save local notification only when going backward to previous screen */
-(void)viewWillDisappear:(BOOL)animated
{
if (!notificationSwitch.on)
return;
// Start by clearing all notifications
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
// set fire date to current date at the selected time on UIDatePicker
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components = [calendar components:(NSHourCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]];
NSDateComponents *pickerComponents = [[NSDateComponents alloc] init];
pickerComponents = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[timePicker date]];
[components setHour:pickerComponents.hour];
[components setMinute:pickerComponents.minute];
[calendar setTimeZone: [NSTimeZone defaultTimeZone]];
NSDate *dateToFire = [calendar dateFromComponents:components];
localNotification.fireDate = dateToFire;
localNotification.alertBody = @"Remember to log your PulseBeat data!";
localNotification.alertAction = @"OK";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[localNotification setRepeatInterval:NSDayCalendarUnit ];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
// send hit data to google analytics
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
[tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"ui_action" action:@"button_press" label:@"notifications set ON" value:nil] build]];
}
@end