-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyProfileViewController.m
410 lines (337 loc) · 13.4 KB
/
MyProfileViewController.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
//
// MyProfileViewController.m
// Kickoff South Bend
//
// Created by Christian Poellabauer on 6/26/13.
// Copyright (c) 2013 Christian Poellabauer. All rights reserved.
//
#import "MyProfileViewController.h"
@interface MyProfileViewController () <FDTakeDelegate,UITextFieldDelegate>
@property (nonatomic,strong) NSString *passwordText;
@end
@implementation MyProfileViewController
@synthesize firstName, lastName, email, gradYear, affiliation, imageButton, ndStudent, ndGrad, passwordField;
@synthesize myPFObject;
@synthesize profileImage;
@synthesize addPhotoLabel;
@synthesize passwordText;
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
[passwordField resignFirstResponder];
return YES;
}
- (void)dismissKeyboard {
[firstName resignFirstResponder];
[lastName resignFirstResponder];
[affiliation resignFirstResponder];
[email resignFirstResponder];
[gradYear resignFirstResponder];
[passwordField resignFirstResponder];
}
- (void)keyboardWillShow:(NSNotification *)notif
{
if ([firstName isFirstResponder]) {
//CGPoint screenOrigin = [firstName convertPoint:firstName.bounds.origin toView:nil];
//if (screenOrigin.y > 300.0f)
// offset = screenOrigin.y - 300.0f;
//else if (screenOrigin.y < 80.0)
// offset = 0.0f;
offset = 130.0f;
}
if ([lastName isFirstResponder]) {
offset = 130.0f;
}
if ([email isFirstResponder]) {
offset = 130.0f;
}
if ([affiliation isFirstResponder]) {
offset = 130.0f;
}
if ([gradYear isFirstResponder]) {
offset = 130.0f;
}
if ([passwordField isFirstResponder]) {
offset = 130.0f;
}
if(self.view.frame.origin.y >= 0)
[self setViewMovedUp:YES];
}
- (void)keyboardWillHide:(NSNotification *)notif
{
if(self.view.frame.origin.y < 0)
[self setViewMovedUp:NO];
}
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5]; // if you want to slide up the view
CGRect rect = self.view.frame;
if (movedUp)
{
// 1. move the view's origin up so that the text field that will be hidden come above the keyboard
// 2. increase the size of the view so that the area behind the keyboard is covered up.
rect.origin.y -= offset;
rect.size.height += offset;
}
else
{
// revert back to the normal state.
rect.origin.y += offset;
rect.size.height -= offset;
offset = 0.0f;
}
self.view.frame = rect;
[UIView commitAnimations];
}
- (void)cancel {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewDidAppear:(BOOL)animated
{
self.takeController = [[FDTakeController alloc] init];
self.takeController.delegate = self;
}
#pragma mark - FDTakeDelegate
- (void)takeController:(FDTakeController *)controller didCancelAfterAttempting:(BOOL)madeAttempt
{
//UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"CNVRS" message:@"Cancelled" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
//[alertView show];
}
- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize {
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
- (UIImage *)imageCrop:(UIImage *)imageToCrop newSize:(CGSize)newSize
{
UIGraphicsBeginImageContext(newSize);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGRect clippedRect = CGRectMake(0, 0, newSize.width, newSize.height);
CGContextClipToRect( currentContext, clippedRect);
CGRect drawRect = CGRectMake(0, 0, newSize.width, newSize.height);
CGContextDrawImage(currentContext, drawRect, imageToCrop.CGImage);
UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return cropped;
}
- (UIImage *)imageByScalingAndCroppingForSize:(UIImage*)image targetSize:(CGSize)targetSize {
UIImage *sourceImage = image;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO)
{
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor > heightFactor)
scaleFactor = widthFactor; // scale to fit height
else
scaleFactor = heightFactor; // scale to fit width
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor > heightFactor)
{
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}
else
if (widthFactor < heightFactor)
{
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
UIGraphicsBeginImageContext(targetSize); // this will crop
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil)
NSLog(@"could not scale image");
//pop the context to get back to the default
UIGraphicsEndImageContext();
return newImage;
}
- (void)takeController:(FDTakeController *)controller gotPhoto:(UIImage *)photo withInfo:(NSDictionary *)info
{
[[imageButton imageView] setContentMode: UIViewContentModeScaleAspectFit];
[imageButton setImage:photo forState:UIControlStateNormal];
addPhotoLabel.hidden = TRUE;
profileImage = photo;
CGSize newSizeI;
newSizeI.width = 160;
newSizeI.height = 160;
//UIImage *newImage = [self resizeImage:profileImage newSize:newSizeI];;
//UIImage *newImage = [self imageCrop:profileImage newSize:newSizeI];
UIImage *newImage = [self imageByScalingAndCroppingForSize:profileImage targetSize:newSizeI];
NSData *data = UIImageJPEGRepresentation(newImage, 0.0);
PFFile *file = [PFFile fileWithName:@"snapshot.jpg" data:data];
[file save];
[myPFObject setObject:file forKey:@"profileimage"];
[myPFObject save];
}
-(IBAction)saveProfile:(id)sender
{
userProfileData = [ProfileData sharedInstance];
[userProfileData setProfileUpdated:TRUE];
PFObject *object = myPFObject;
if (object == nil) {
return;
}
passwordText = passwordField.text;
if (![passwordText isEqualToString:@"****"]) {
[PFUser currentUser].password = passwordText;
[[PFUser currentUser] save];
}
NSString *myFirstname = firstName.text;
NSString *myLastname = lastName.text;
NSString *myEmailAddress = email.text;
NSString *myAffiliation = affiliation.text;
NSString *year = gradYear.text;
BOOL isndgrad = ndGrad.on;
BOOL isstudent = ndStudent.on;
if (myFirstname.length) {
[object setObject:firstName.text forKey:@"firstname"];
[object setObject:[firstName.text lowercaseString] forKey:@"firstname_lower"];
}
if (myLastname.length) {
[object setObject:lastName.text forKey:@"lastname"];
[object setObject:[lastName.text lowercaseString] forKey:@"lastname_lower"];
}
if (myEmailAddress.length) {
[object setObject:email.text forKey:@"emailAddress"];
}
if (myAffiliation.length) {
[object setObject:affiliation.text forKey:@"affiliation"];
}
[object setObject:year forKey:@"year"];
if (isndgrad) {
[object setObject:[NSNumber numberWithBool:TRUE] forKey:@"ndgrad"];
} else {
[object setObject:[NSNumber numberWithBool:FALSE] forKey:@"ndgrad"];
}
if (isstudent) {
[object setObject:[NSNumber numberWithBool:TRUE] forKey:@"ndstudent"];
} else {
[object setObject:[NSNumber numberWithBool:FALSE] forKey:@"ndstudent"];
}
/*
NSData *data = UIImageJPEGRepresentation(profileImage, 0.0);
PFFile *file = [PFFile fileWithName:@"snapshot.jpg" data:data];
[file save];
[object setObject:file forKey:@"profileimage"];
*/
[object save];
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"My Profile"
message:@"Profile information has been updated!"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
[self.navigationController popViewControllerAnimated:YES];
}
-(IBAction)chooseImage:(id)sender
{
picker = [[UIImagePickerController alloc] init];
picker.mediaTypes = @[(NSString *) kUTTypeImage];
picker.allowsEditing = NO;
self.takeController.imagePicker.allowsEditing = NO;
[self.takeController takePhotoOrChooseFromLibrary];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *pickedImage = [info objectForKey:UIImagePickerControllerEditedImage];
profileImage = pickedImage;
[imageButton setBackgroundImage:pickedImage forState:UIControlStateNormal];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
passwordText = @"****";
}
return self;
}
- (void)viewWillDisappear:(BOOL)animated
{
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)viewWillAppear:(BOOL)animated
{
offset = 0.0;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
userProfileData = [ProfileData sharedInstance];
passwordText = @"****";
PFQuery *query = [PFQuery queryWithClassName:@"Profile"];
[query whereKey:@"username" equalTo:[userProfileData getUserName]];
myPFObject = [query getFirstObject];
[userProfileData setOwnObject:myPFObject];
if ([myPFObject objectForKey:@"profileimage"] == nil)
{
[[imageButton imageView] setContentMode: UIViewContentModeScaleAspectFit];
[imageButton setImage:[UIImage imageNamed:@"profile_placeholder.png"] forState:UIControlStateNormal];
profileImage = [UIImage imageNamed:@"profile_placeholder.png"];
} else
{
PFFile *myImageFile = [[userProfileData getOwnObject] objectForKey:@"profileimage"];
NSData *imageData = [myImageFile getData];
[[imageButton imageView] setContentMode: UIViewContentModeScaleAspectFit];
[imageButton setImage:[UIImage imageWithData:imageData] forState:UIControlStateNormal];
addPhotoLabel.hidden = TRUE;
}
NSString *myFirstName = [[userProfileData getOwnObject] objectForKey:@"firstname"];
NSString *myLastName = [[userProfileData getOwnObject] objectForKey:@"lastname"];
NSString *myEmail = [[userProfileData getOwnObject] objectForKey:@"emailAddress"];
NSString *myAffiliation = [[userProfileData getOwnObject] objectForKey:@"affiliation"];
NSString *myYear = [[userProfileData getOwnObject] objectForKey:@"year"];
BOOL graduate = [[[userProfileData getOwnObject] objectForKey:@"ndgrad"] boolValue];
BOOL student = [[[userProfileData getOwnObject] objectForKey:@"ndstudent"] boolValue];
firstName.text = myFirstName;
lastName.text = myLastName;
email.text = myEmail;
affiliation.text = myAffiliation;
gradYear.text = myYear;
ndGrad.on = graduate;
ndStudent.on = student;
passwordField.text = passwordText;
[firstName setDelegate:self];
[lastName setDelegate:self];
[email setDelegate:self];
[affiliation setDelegate:self];
[gradYear setDelegate:self];
[passwordField setDelegate:self];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end