-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewEventViewController.m
More file actions
297 lines (225 loc) · 10 KB
/
NewEventViewController.m
File metadata and controls
297 lines (225 loc) · 10 KB
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
//
// NewEventViewController.m
// Metasome
//
// Created by Omar Metwally on 8/26/13.
// Copyright (c) 2013 Logisome. All rights reserved.
//
#import "NewEventViewController.h"
#import "MetasomeEventStore.h"
#import "MetasomeEvent.h"
#import "DatePickerViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "TextFormatter.h"
#import "GAI.h"
#import "GAIDictionaryBuilder.h"
@implementation NewEventViewController
@synthesize titleTextField, descriptionTextView, visibilitySwitch, dateLabel, dateButton, visibilityLabel, descriptionLabel, dateSelected;
@synthesize titleLabel;
@synthesize originalPoint, toPoint;
@synthesize initialColor;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//self.title = @"New Event";
[self setEventSelected:nil];
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveEvent)];
[[self navigationItem] setRightBarButtonItem:saveButton];
hintText = @"";
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[TextFormatter formatTitleLabel:titleLabel withTitle:@"New Diary Entry"];
[[self navigationItem] setTitleView:titleLabel];
}
return self;
}
-(void)enterTextView:(NSNotification *)note
{
}
-(void)exitTextView:(NSNotification *)note
{
/*
if ( [[[self descriptionTextView] text] isEqualToString:@""] ) {
self.descriptionTextView.textColor = [UIColor grayColor];
[[self descriptionTextView] setText:hintText];
}
CABasicAnimation *down = [CABasicAnimation animationWithKeyPath:@"position"];
CAMediaTimingFunction *tf = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[down setFromValue:[NSValue valueWithCGPoint:self.toPoint]];
//update self.toPoint *after* setting fromValue
[self setToPoint:self.originalPoint];
[down setToValue:[NSValue valueWithCGPoint:self.originalPoint]];
[down setDuration:0.25];
[down setDelegate:self];
[down setTimingFunction:tf];
[[[self view] layer] addAnimation:down forKey:@"moveDown"];
self.view.center = self.originalPoint;
*/
}
- (IBAction)changeDate:(id)sender {
// [[self navigationController] navigationBar].hidden = YES;
//self.navigationController.navigationBar.backgroundColor = [UIColor darkGrayColor];
DatePickerViewController *dpvc = [[DatePickerViewController alloc] init];
[dpvc setParentDateController:self];
[[self navigationController] pushViewController:dpvc animated:YES];
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
//self.view.center = self.toPoint;
}
-(void)slideViewUp
{
}
-(void)switchChanged:(id)sender
{
[[self eventSelected] setVisible:self.visibilitySwitch.on];
[[MetasomeEventStore sharedStore] saveChanges];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (IBAction)backgroundTouched:(id)sender {
[[self view] endEditing:YES];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//IF the table view is asking to commit a delete command
if (editingStyle == UITableViewCellEditingStyleDelete) {
MetasomeEventStore *es = [MetasomeEventStore sharedStore];
NSArray *events = [es allEvents];
MetasomeEvent *event = [events objectAtIndex:[indexPath row]];
[es removeEvent:event];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
-(void)saveEvent
{
//Make sure a title has been entered before saving
if ([[[self titleTextField] text] length] > 0) {
//If opening a pre-existing event record, overwrite
//that record with the text field/view's contents
//[self eventSelected] points to an event in the sharedStore
if ([self eventSelected]) {
[[self eventSelected] setTitle:[[self titleTextField] text]];
if ([[[self descriptionTextView] text] length] == 0)
[[self eventSelected] setDetails:nil];
else
[[self eventSelected] setDetails:[[self descriptionTextView] text]];
[[self eventSelected] setDate:self.dateSelected];
[[self eventSelected] setVisible:visibilitySwitch.on];
}
//Otherwise, create a new event and save it in the sharedStore
else {
MetasomeEvent *newEvent = [[MetasomeEvent alloc] initWithEventTitle:[[self titleTextField] text] details:[[self descriptionTextView] text] date:self.dateSelected];
[[MetasomeEventStore sharedStore] addEvent:newEvent];
}
if (![[MetasomeEventStore sharedStore] saveChanges]) {
NSLog(@"Unable to save event!");
}
// sent hit data to google analytics
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
[tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"ui_action" action:@"button_press" label:@"New diary entry" value:nil] build]];
[[self navigationController] popViewControllerAnimated:YES];
}
//If no title is specified, show an alert
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please enter an event title" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil ];
[alert show];
}
}
-(void)preferredContentSizeChanged:(NSNotification *)aNotification {
titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
titleTextField.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
descriptionTextView.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
descriptionLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
//dateLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
dateButton.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
dateButton.titleLabel.adjustsFontSizeToFitWidth = YES;
visibilityLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
[self.view setNeedsLayout];
}
-(IBAction)buttonHighlight:(id)sender
{
UIColor *highlightColor = [UIColor greenColor];
UIButton *button = (UIButton *)sender;
button.backgroundColor = highlightColor;
}
-(IBAction)buttonNormal:(id)sender
{
UIButton *button = (UIButton *)sender;
button.backgroundColor = initialColor;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Initialize google analytics
[self setScreenName:@"NewEventViewController"];
initialColor = [[self dateButton] backgroundColor];
dateButton.layer.cornerRadius = 10.0;
[dateButton addTarget:self action:@selector(buttonHighlight:) forControlEvents:UIControlEventTouchDown];
[dateButton addTarget:self action:@selector(buttonNormal:) forControlEvents:UIControlEventTouchUpOutside];
[dateButton addTarget:self action:@selector(buttonNormal:) forControlEvents:UIControlEventTouchDragOutside];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(preferredContentSizeChanged:) name:UIContentSizeCategoryDidChangeNotification object:nil];
[[self visibilitySwitch] addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//[dateFormatter setDateFormat:@"dd MMM yyyy HH:mm"];
[dateFormatter setDateFormat:@"MMM dd yyyy"];
if ([[self eventSelected] date]) {
dateSelected = self.eventSelected.date;
self.dateLabel.text = [dateFormatter stringFromDate:[[self eventSelected] date]];
} else {
dateSelected = [NSDate date];
self.dateLabel.text = [dateFormatter stringFromDate:dateSelected];
}
self.dateLabel.textColor = [UIColor blueColor];
[dateButton sizeToFit];
//If eventSelected is set to point to a pre-existing event in MetasomeEventStore,
//populate text field/view and date picker
if ([self eventSelected]) {
self.titleTextField.text = [[self eventSelected] title];
self.descriptionTextView.text = [[self eventSelected] details];
//self.eventDatePicker.date = [[self eventSelected] date];
[visibilitySwitch setOn:[[self eventSelected] visible]];
}
/*
if ([[self eventSelected] details]) {
self.descriptionTextView.textColor = [UIColor blackColor];
[[self descriptionTextView] setText:[[self eventSelected] details]];
} else {
self.descriptionTextView.textColor = [UIColor grayColor];
[[self descriptionTextView] setText:hintText];
}
*/
// Make the description UITextView look nice
[self formatTextView:descriptionTextView];
[self formatLabel:descriptionLabel];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)formatTextView:(UITextView *)tv
{
//To make the border look very close to a UITextField
[tv.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[tv.layer setBorderWidth:2.0];
//The rounded corner part, where you specify your view's corner radius:
tv.layer.cornerRadius = 5;
tv.clipsToBounds = YES;
//[tv setTextContainerInset:UIEdgeInsetsMake(-60.0, 0.0, 0.0, 0.0)];
//[tv setTextColor:[UIColor blackColor]];
}
-(void)formatLabel:(UILabel *)label
{
label.layer.cornerRadius = 5;
label.clipsToBounds = YES;
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[dateButton setBackgroundColor:initialColor];
}
@end