-
Notifications
You must be signed in to change notification settings - Fork 0
/
ControleDeNotasViewController.m
executable file
·441 lines (351 loc) · 16 KB
/
ControleDeNotasViewController.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//
// ControleDeNotasViewController.m
// iLuno
//
// Created by Gabriel Vincent on 16/06/12.
// Copyright (c) 2012 _A_Z. All rights reserved.
//
#define Opening 0
#define Closing 1
#define Database @"ControleDeNotas"
#define Add 0
#define Save 1
#define Edit 0
#define OK 1
#define None -1
#define Disabled 2
#define Enabled 3
#import "ControleDeNotasViewController.h"
#import "ControleDaMateriaViewController.h"
@interface ControleDeNotasViewController ()
@end
@implementation ControleDeNotasViewController
- (id)initWithStyle:(UITableViewStyle)style {
self = [super initWithStyle:style];
if (self) {
}
return self;
}
- (NSString *) simplifiedString:(NSString *)string {
string = [string lowercaseString];
string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
string = [string stringByReplacingOccurrencesOfString:@"á" withString:@"a"];
string = [string stringByReplacingOccurrencesOfString:@"é" withString:@"e"];
string = [string stringByReplacingOccurrencesOfString:@"í" withString:@"i"];
string = [string stringByReplacingOccurrencesOfString:@"ó" withString:@"o"];
string = [string stringByReplacingOccurrencesOfString:@"ú" withString:@"ú"];
string = [string stringByReplacingOccurrencesOfString:@"ç" withString:@"c"];
string = [string stringByReplacingOccurrencesOfString:@"ã" withString:@"a"];
string = [string stringByReplacingOccurrencesOfString:@"â" withString:@"a"];
string = [string stringByReplacingOccurrencesOfString:@"ê" withString:@"e"];
string = [string stringByReplacingOccurrencesOfString:@"ô" withString:@"o"];
string = [string stringByReplacingOccurrencesOfString:@"à" withString:@"a"];
string = [string stringByReplacingOccurrencesOfString:@"è" withString:@"e"];
string = [string stringByReplacingOccurrencesOfString:@"ì" withString:@"i"];
string = [string stringByReplacingOccurrencesOfString:@"ò" withString:@"ò"];
string = [string stringByReplacingOccurrencesOfString:@"ù" withString:@"u"];
string = [string stringByReplacingOccurrencesOfString:@"ñ" withString:@"n"];
return string;
}
- (void) setLeftBarButton:(NSInteger) button {
if (button == Edit) self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Editar" style:UIBarButtonItemStyleBordered target:self action:@selector(toggleTableViewEdditing)];
else if (button == OK) self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"OK" style:UIBarButtonItemStyleDone target:self action:@selector(toggleTableViewEdditing)];
else if (button == None) self.navigationItem.leftBarButtonItem = nil;
else if (button == Disabled) self.navigationItem.leftBarButtonItem.enabled = NO;
else if (button == Enabled) self.navigationItem.leftBarButtonItem.enabled = YES;
}
- (void) setRightBarButton:(NSInteger) button {
if (button == Add) self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addSubject)];
else if (button == Save) self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Salvar" style:UIBarButtonItemStyleDone target:self action:@selector(saveSubject)];
else if (button == None) self.navigationItem.rightBarButtonItem = nil;
else if (button == Disabled) self.navigationItem.rightBarButtonItem.enabled = NO;
else if (button == Enabled) self.navigationItem.rightBarButtonItem.enabled = YES;
}
- (void) toggleTableViewEdditing {
if ([self.tableView isEditing]) {
[self.tableView setEditing:NO animated:YES];
[self setRightBarButton:Add];
[self setLeftBarButton:Edit];
}
else {
[self.tableView setEditing:YES animated:YES];
[self setLeftBarButton:OK];
[self setRightBarButton:None];
}
}
- (IBAction)dynamicallyValidateSubject:(id)sender {
// Verifica se a string está vazia ou se é composta por epaços
if (materiaTextField.text.length == 0 || [materiaTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""].length == 0) {
[self setRightBarButton:Disabled];
}
else {
[self setRightBarButton:Enabled];
}
}
- (BOOL) subjectNameIsValid {
for (NSDictionary *dict in arrayMaterias) {
NSString *materia1 = [self simplifiedString:materiaTextField.text];
NSString *materia2 = [self simplifiedString:[dict objectForKey:@"Materia"]];
if ([materia1 isEqualToString:materia2]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Erro" message:[NSString stringWithFormat:@"Uma matéria com o nome \"%@\" já foi inserida. Por favor, escolha outro nome e tente novamente.", materiaTextField.text] delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
return NO;
}
}
return YES;
}
- (void) cancelAdding {
[self performGraphicalAdjustmentsFor:Closing];
}
- (void) performGraphicalAdjustmentsFor:(NSInteger) action {
if (action == Opening) {
self.tableView.scrollEnabled = NO;
[self.tableView setEditing:NO];
titleLabel.text = @"Nova Matéria";
if (darkView == nil) {
darkView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height)];
darkView.backgroundColor = [UIColor blackColor];
[self.navigationController.view addSubview:darkView];
}
darkView.alpha = 0.0;
if (cancelGesture == nil) {
cancelGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelAdding)];
[darkView addGestureRecognizer:cancelGesture];
}
if (adicionarMateriaView == nil) {
[[NSBundle mainBundle] loadNibNamed:@"AdicionarMateria" owner:self options:nil];
adicionarMateriaView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"darkest-background-full.png"]];
}
[self.navigationController.view addSubview:adicionarMateriaView];
[self.navigationController.view bringSubviewToFront:self.navigationController.navigationBar];
adicionarMateriaView.frame = CGRectMake(0, -44, 320, 44);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
self.navigationController.navigationBar.frame = CGRectMake(0, 44, 320, 44);
darkView.alpha = 0.6;
adicionarMateriaView.frame = CGRectMake(0, 0, 320, 44);
[UIView commitAnimations];
[self.tableView setContentOffset:CGPointMake(0, -44) animated:YES];
[self setRightBarButton:Save];
[self setRightBarButton:Disabled];
[self setLeftBarButton:None];
[materiaTextField becomeFirstResponder];
}
else if (action == Closing) {
[materiaTextField resignFirstResponder];
titleLabel.text = @"Controle de Notas";
// [self.navigationController.view addSubview:darkView];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
[self.tableView setContentOffset:CGPointMake(0, -44)];
self.navigationController.navigationBar.frame = CGRectMake(0, 0, 320, 44);
darkView.alpha = 0.0;
adicionarMateriaView.frame = CGRectMake(0, -44, 320, 44);
self.tableView.scrollEnabled = YES;
[UIView commitAnimations];
[self.tableView setContentOffset:CGPointMake(0, -44) animated:YES];
[adicionarMateriaView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.2];
[materiaTextField performSelector:@selector(setText:) withObject:@"" afterDelay:0.2];
[self setRightBarButton:Add];
[self setRightBarButton:Enabled];
[self setLeftBarButton:Edit];
}
}
- (void) saveSubject {
if ([self subjectNameIsValid]) {
NSString *materia = [materiaTextField text];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:materia forKey:@"Materia"];
[plistManager addNewEntry:dict ToDatabase:Database];
[arrayMaterias addObject:dict];
[self performGraphicalAdjustmentsFor:Closing];
UIView *backgroundView = [[UIView alloc] init];
backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"darkest-background-full.png"]];
self.tableView.backgroundView = backgroundView;
[self.tableView reloadData];
}
}
- (void) addSubject {
[self performGraphicalAdjustmentsFor:Opening];
}
- (void)viewDidLoad
{
[super viewDidLoad];
if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault];
}
self.navigationController.navigationBar.tintColor = UIColorFromRGB(0xFDDD5B);
self.view.frame = CGRectMake(0, 0, 320, 400);
self.navigationItem.title = @"Matérias";
titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.font = [UIFont boldSystemFontOfSize:20.0];
titleLabel.shadowColor = [UIColor colorWithRed:252.0/255.0 green:234.0/255.0 blue:162.0/255.0 alpha:0.9];
titleLabel.shadowOffset = CGSizeMake(0, 1);
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.75];
self.navigationItem.titleView = titleLabel;
titleLabel.text = @"Controle de Notas";
[titleLabel sizeToFit];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
plistManager = [[GVPlistPersistence alloc] init];
arrayMaterias = [NSMutableArray arrayWithArray:[plistManager databaseWithName:Database]];
[self setRightBarButton:Add];
[self setLeftBarButton:Edit];
if ([arrayMaterias count] == 0) {
[self setLeftBarButton:Disabled];
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"AdicioneUmaMateria.png"]];
}
else {
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"darkest-background-full.png"]];
}
if (![plistManager databaseAlreadyExistsWithName:Database]) [plistManager createNewDatabaseWithName:Database];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = bgColor;
}
- (NSInteger)realRowNumberForIndexPath:(NSIndexPath *)indexPath inTableView:(UITableView *)tableView
{
NSInteger retInt = 0;
if (!indexPath.section)
{
return indexPath.row;
}
for (int i = 0; i < indexPath.section; i++) {
retInt += [tableView numberOfRowsInSection:i];
}
return retInt + indexPath.row;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [arrayMaterias count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.textLabel.text = [[arrayMaterias objectAtIndex:indexPath.row] objectForKey:@"Materia"];
cell.textLabel.textColor = UIColorFromRGB(0xFFCC00);
cell.textLabel.shadowOffset = CGSizeMake(0, -1);
cell.textLabel.shadowColor = [UIColor blackColor];
NSInteger realRow = [self realRowNumberForIndexPath:indexPath inTableView:tableView];
if (realRow % 2 == 0) {
bgColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"darker-background-pattern.png"]];
}
else {
bgColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"darkest-background-full.png"]];
}
UIView* accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 50)];
UIImageView* accessoryViewImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow.png"]];
accessoryViewImage.center = CGPointMake(12, 25);
[accessoryView addSubview:accessoryViewImage];
[cell setAccessoryView:accessoryView];
// Adiciona o separador no topo da cell
if ([[cell subviews] count] < 2) {
UIImageView *separatorView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"separator.png"]];
separatorView.frame = CGRectMake(0, 0, 320, 9);
[cell addSubview:separatorView];
}
// Verifica se alguma row tem um separador a mais (acontece quando é inserida uma nova matéria];
if (cell.subviews.count == 3 && indexPath.row < [arrayMaterias count]-1) {
for (UIImageView *separator in cell.subviews) {
if (separator.frame.origin.y == 60) {
[separator removeFromSuperview];
break;
}
}
}
// Insere um segundo separador à última row da TableView
if (indexPath.row == [arrayMaterias count]-1) {
if ([[cell subviews] count] < 3) {
UIImageView *separatorView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"separator.png"]];
separatorView.frame = CGRectMake(0, 60, 320, 9);
[cell addSubview:separatorView];
}
}
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 60.0;
}
// When single cell will be eddited
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
[self setLeftBarButton:OK];
}
// When single cell did finish being eddited
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {
[self setLeftBarButton:Edit];
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[arrayMaterias removeObjectAtIndex:indexPath.row];
[plistManager removeEntryAtIndex:indexPath.row FromDatabase:Database];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
if ([arrayMaterias count] == 0) {
[self.tableView setEditing:NO];
[self setLeftBarButton:Edit];
[self setLeftBarButton:Disabled];
[self setRightBarButton:Add];
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"AdicioneUmaMateria.png"]];
}
[self.tableView performSelector:@selector(reloadData) withObject:nil afterDelay:0.3];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
NSMutableDictionary *temporarySubject = [arrayMaterias objectAtIndex:fromIndexPath.row];
[arrayMaterias removeObjectAtIndex:fromIndexPath.row];
[arrayMaterias insertObject:temporarySubject atIndex:toIndexPath.row];
[plistManager removeEntryAtIndex:fromIndexPath.row FromDatabase:Database];
[plistManager addEntry:temporarySubject atIndex:toIndexPath.row ToDatabase:Database];
[self.tableView performSelector:@selector(reloadData) withObject:nil afterDelay:0.25];
}
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ControleDaMateriaViewController *detailViewController = [[ControleDaMateriaViewController alloc] initWithNibName:@"ControleDaMateriaViewController" bundle:nil];
detailViewController.materia = [self.tableView cellForRowAtIndexPath:indexPath].textLabel.text;
[self.navigationController pushViewController:detailViewController animated:YES];
}
@end