-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlarmOverviewViewController.m
283 lines (222 loc) · 9.92 KB
/
AlarmOverviewViewController.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//
// AlarmOverviewViewControllerTableViewController.m
// AlarmPlusPlus
//
// Created by Justin Sane on 14/12/15.
// Copyright © 2015 Marc Neveling. All rights reserved.
//
#import "AlarmOverviewViewController.h"
#import "AlarmCellTableViewCell.h"
#import "Alarm.h"
#import "AlarmAddViewController.h"
#import "AppDelegate.h"
#import "EditorTabBarController.h"
#import "DateUtils.h"
@interface AlarmOverviewViewController ()
@end
@implementation AlarmOverviewViewController
NSString *helloMessage = nil;
#pragma mark - Initialization
- (void)viewDidLoad {
[super viewDidLoad];
AppDelegate* appDelegate = [[UIApplication sharedApplication] delegate];
self.alarms = [appDelegate getAlarmArray];
self.notficationsManager = [appDelegate getNotificationsManager];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
}
- (void) viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self.tableView reloadData];
// check to see if an alarm has been added and we need to display confirmation message
if(helloMessage != nil){
[self confirmMessage:helloMessage];
helloMessage = nil;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table Data Source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.alarms count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
AlarmCellTableViewCell *cell;
cell = (AlarmCellTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"alarmCell" forIndexPath:indexPath];
Alarm *alarm = [self.alarms objectAtIndex:indexPath.row];
cell.name.text = alarm.name;
NSString *dateString = [NSDateFormatter localizedStringFromDate:alarm.date
dateStyle:NSDateFormatterNoStyle
timeStyle:NSDateFormatterShortStyle];
cell.time.text = dateString;
if(alarm.repeat == true){
[cell.repeat setImage:[UIImage imageNamed:@"repeat_icon.png"]];
}
[cell.stateButton setImage:[UIImage imageNamed:@"alarm_icon.png"] forState:UIControlStateNormal];
NSArray *weekdayViews = cell.weekdays.arrangedSubviews;
//iterate over weekdays and check whether flag for that weekday is checked and set tint for active state
for(int i = 0; i < 7; i++){
int mask = 1 << i;
UILabel *label = (UILabel*)[weekdayViews objectAtIndex:i];
if((alarm.weekdaysFlag & mask) != 0){
[label setFont:[UIFont boldSystemFontOfSize:14]];
[label setTextColor:[UIColor greenColor]];
}else{
[label setTextColor:[UIColor blackColor]];
}
}
// Set tints for activated/deactivated Alarms
if(alarm.active == true){
[cell.time setTextColor:[UIColor blackColor]];
[cell.name setTextColor:[UIColor blackColor]];
[cell.stateButton setTintColor:[UIColor greenColor]];
[cell.repeat setTintColor:[UIColor blackColor]];
}else{
for (UIView *view in weekdayViews){
UILabel *label = (UILabel*) view;
[label setTextColor:[UIColor grayColor]];
}
[cell.time setTextColor:[UIColor grayColor]];
[cell.name setTextColor:[UIColor grayColor]];
[cell.stateButton setTintColor:[UIColor grayColor]];
[cell.repeat setTintColor:[UIColor grayColor]];
}
return cell;
}
/// Handling a delete request
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
Alarm *deletedAlarm = (Alarm*) [self.alarms objectAtIndex:indexPath.row];
[self.alarms removeObjectAtIndex:indexPath.row];
[self.tableView reloadData];
if(deletedAlarm.active){
[self.notficationsManager deactivateAlarm:deletedAlarm];
}
}
}
#pragma mark - User Interaction
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (IBAction)activeButtonPressed:(id)sender {
// find row that needs to be activated/deactivated
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touchPoint];
Alarm *alarm = [self.alarms objectAtIndex:indexPath.row];
if(!alarm.active){
// activate alarm, schedule notification, confirm activation to user
[alarm activate];
[self.notficationsManager scheduleLocalNotificationWithAlarm:alarm];
[self confirmMessage:[self confirmAlarmMessageForAlarm:alarm]];
}else{
// deactivate alarm, cancel notification
alarm.active = false;
[self.notficationsManager deactivateAlarm:alarm];
}
[self.tableView reloadData];
}
#pragma mark - Segue Handling
/// Return from alarmAddView
/// Initialize added alarm, schedule it and confirm to user
- (IBAction)unwindToPlayersViewControllerAdd:(UIStoryboardSegue *)unwindSegue{
AlarmAddViewController *vc = [unwindSegue sourceViewController];
Alarm *alarm = [Alarm new];
alarm.ringtone = vc.ringtoneLabel.text;
//Parse difficulty
for (int i = 0; i < DifficulyCount; ++i){
NSString *difficultyStr = [Alarm difficultyToString:(Difficulties) i];
if([difficultyStr isEqualToString:vc.difficultyLabel.text]){
alarm.difficulty = (Difficulties) i;
}
}
//parse selected Problem Type
for (int i = 0; i < ProblemTypeCount; ++i){
NSString *problemStr = [Alarm problemTypeToString:(ProblemTypes) i];
if([problemStr isEqualToString:vc.problemLabel.text]){
alarm.problem = (ProblemTypes) i;
}
}
alarm.volume = vc.volumeSlider.value;
alarm.repeat = vc.repeatLabel.isOn;
alarm.name = vc.nameLabel.text;
alarm.active = true;
NSDate *pickedTime = vc.datePicker.date;
NSIndexSet *selectedWeekdaysIndexSet = vc.weekdaysControl.selectedSegmentIndexes;
// let the alarm initialise the remaining variables himself
[alarm finishAlarmSetupWithTime:pickedTime AndWeekdays:selectedWeekdaysIndexSet];
[self.alarms addObject:alarm];
[self.tableView reloadData];
[self.notficationsManager scheduleLocalNotificationWithAlarm:alarm];
// set the flag, that an new alarm has been added and a confirmation needs to be displayed.
// we cannot display it here since we are still segueing and the view hasn't appeared yet
helloMessage = [self confirmAlarmMessageForAlarm:alarm];
}
/// alarm add canceled
- (IBAction)unwindToPlayersViewControllerCancel:(UIStoryboardSegue *)unwindSegue{
}
/// return from editor
- (IBAction)unwindToEditorDone:(UIStoryboardSegue *)unwindSegue{
[(EditorTabBarController*)unwindSegue.sourceViewController.tabBarController saveAllInputs];
}
/// return from statistics
- (IBAction)unwindToStatisticsBack:(UIStoryboardSegue *)unwindSegue{
}
#pragma mark - Alarm Confirmation
-(NSString*) confirmAlarmMessageForAlarm: (Alarm*) alarm{
NSTimeInterval timeTillAlarm = [alarm.date timeIntervalSinceNow];
NSDateComponentsFormatter* formatter = [[NSDateComponentsFormatter alloc] init];
formatter.unitsStyle = NSDateComponentsFormatterUnitsStyleFull;
formatter.allowedUnits = NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour | NSCalendarUnitDay | NSCalendarUnitYear;
// Use the configured formatter to generate the string.
NSString* outputString = [formatter stringFromTimeInterval:timeTillAlarm];
return [NSString stringWithFormat:@"Alarm rings in %@.", outputString];
}
- (void)confirmMessage: (NSString*) message{
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Alarm set!"
message: message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
#pragma mark - Action Sheet Handling
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
/// displays navigation actionsheet
- (IBAction)detailsButtonClicked:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Settings",@"Editor",@"Statistics",nil];
[actionSheet showFromBarButtonItem:self.detailsButton animated:YES];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
//settings
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
break;
case 1:
//editor
[self performSegueWithIdentifier:@"EditorSegue" sender:self];
break;
case 2:
//statistics
[self performSegueWithIdentifier:@"StatisticsSegue" sender:self];
break;
case 3:
//cancel
break;
}
}
#pragma GCC diagnostic pop
@end