Commit a0a95e20123b7b599558bf9bed2a9493179c2a9e
1 parent
77358f2e08
Exists in
master
and in
1 other branch
update param upload-image
Showing 2 changed files with 36 additions and 26 deletions Inline Diff
LifeLog/LifeLog/RegisterViewController.m
1 | // | 1 | // |
2 | // RegisterViewController.m | 2 | // RegisterViewController.m |
3 | // LifeLog | 3 | // LifeLog |
4 | // | 4 | // |
5 | // Created by Panasonic R&D Center Vietnam on 7/31/17. | 5 | // Created by Panasonic R&D Center Vietnam on 7/31/17. |
6 | // Copyright © 2017 PhongNV. All rights reserved. | 6 | // Copyright © 2017 PhongNV. All rights reserved. |
7 | // | 7 | // |
8 | 8 | ||
9 | #import "RegisterViewController.h" | 9 | #import "RegisterViewController.h" |
10 | #import "CustomTextField.h" | 10 | #import "CustomTextField.h" |
11 | #import "Utilities.h" | 11 | #import "Utilities.h" |
12 | #import "NSDate+helper.h" | 12 | #import "NSDate+helper.h" |
13 | #import "ServerAPI.h" | 13 | #import "ServerAPI.h" |
14 | #import "AppDelegate.h" | 14 | #import "AppDelegate.h" |
15 | static int const kPickerTagArea = 1; | 15 | static int const kPickerTagArea = 1; |
16 | static int const kPickerTagSex = 2; | 16 | static int const kPickerTagSex = 2; |
17 | 17 | ||
18 | @interface RegisterViewController ()<UIActionSheetDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate> | 18 | @interface RegisterViewController ()<UIActionSheetDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate> |
19 | { | 19 | { |
20 | NSInteger indexArea; | 20 | NSInteger indexArea; |
21 | UIPickerView *pickerArea; | 21 | UIPickerView *pickerArea; |
22 | 22 | ||
23 | UIPickerView *pickerSex; | 23 | UIPickerView *pickerSex; |
24 | UIDatePicker *pickerDate; | 24 | UIDatePicker *pickerDate; |
25 | NSDate *dateBirthDay; | 25 | NSDate *dateBirthDay; |
26 | NSString *profile_image; | 26 | NSString *profile_image; |
27 | } | 27 | } |
28 | @property (nonatomic, weak) IBOutlet UIImageView *avatar; | 28 | @property (nonatomic, weak) IBOutlet UIImageView *avatar; |
29 | 29 | ||
30 | @property (nonatomic, weak) IBOutlet UILabel *lblUsername; | 30 | @property (nonatomic, weak) IBOutlet UILabel *lblUsername; |
31 | @property (nonatomic, weak) IBOutlet UILabel *lblName; | 31 | @property (nonatomic, weak) IBOutlet UILabel *lblName; |
32 | @property (nonatomic, weak) IBOutlet UILabel *lblNickname; | 32 | @property (nonatomic, weak) IBOutlet UILabel *lblNickname; |
33 | @property (nonatomic, weak) IBOutlet UILabel *lblEmail; | 33 | @property (nonatomic, weak) IBOutlet UILabel *lblEmail; |
34 | @property (nonatomic, weak) IBOutlet UILabel *lblPassword; | 34 | @property (nonatomic, weak) IBOutlet UILabel *lblPassword; |
35 | @property (nonatomic, weak) IBOutlet UILabel *lblConfirmPassword; | 35 | @property (nonatomic, weak) IBOutlet UILabel *lblConfirmPassword; |
36 | @property (nonatomic, weak) IBOutlet UILabel *lblSex; | 36 | @property (nonatomic, weak) IBOutlet UILabel *lblSex; |
37 | @property (nonatomic, weak) IBOutlet UILabel *lblBirthday; | 37 | @property (nonatomic, weak) IBOutlet UILabel *lblBirthday; |
38 | @property (nonatomic, weak) IBOutlet UILabel *lblHeight; | 38 | @property (nonatomic, weak) IBOutlet UILabel *lblHeight; |
39 | @property (nonatomic, weak) IBOutlet UILabel *lblHeightUnit; | 39 | @property (nonatomic, weak) IBOutlet UILabel *lblHeightUnit; |
40 | @property (nonatomic, weak) IBOutlet UILabel *lblWeight; | 40 | @property (nonatomic, weak) IBOutlet UILabel *lblWeight; |
41 | @property (nonatomic, weak) IBOutlet UILabel *lblWeightUnit; | 41 | @property (nonatomic, weak) IBOutlet UILabel *lblWeightUnit; |
42 | @property (nonatomic, weak) IBOutlet UILabel *lblFatPercentage; | 42 | @property (nonatomic, weak) IBOutlet UILabel *lblFatPercentage; |
43 | @property (nonatomic, weak) IBOutlet UILabel *lblFatPercentageUnit; | 43 | @property (nonatomic, weak) IBOutlet UILabel *lblFatPercentageUnit; |
44 | @property (nonatomic, weak) IBOutlet UILabel *lblPhysicalActivity; | 44 | @property (nonatomic, weak) IBOutlet UILabel *lblPhysicalActivity; |
45 | @property (nonatomic, weak) IBOutlet UILabel *lblArea; | 45 | @property (nonatomic, weak) IBOutlet UILabel *lblArea; |
46 | @property (nonatomic, weak) IBOutlet UILabel *lblNote; | 46 | @property (nonatomic, weak) IBOutlet UILabel *lblNote; |
47 | 47 | ||
48 | @property (nonatomic, weak) IBOutlet UITextField *tfUsername; | 48 | @property (nonatomic, weak) IBOutlet UITextField *tfUsername; |
49 | @property (nonatomic, weak) IBOutlet UITextField *tfName; | 49 | @property (nonatomic, weak) IBOutlet UITextField *tfName; |
50 | @property (nonatomic, weak) IBOutlet UITextField *tfNickname; | 50 | @property (nonatomic, weak) IBOutlet UITextField *tfNickname; |
51 | @property (nonatomic, weak) IBOutlet UITextField *tfEmail; | 51 | @property (nonatomic, weak) IBOutlet UITextField *tfEmail; |
52 | @property (nonatomic, weak) IBOutlet UITextField *tfPassword; | 52 | @property (nonatomic, weak) IBOutlet UITextField *tfPassword; |
53 | @property (nonatomic, weak) IBOutlet UITextField *tfConfirmPassword; | 53 | @property (nonatomic, weak) IBOutlet UITextField *tfConfirmPassword; |
54 | @property (nonatomic, weak) IBOutlet UITextField *tflblSex; | 54 | @property (nonatomic, weak) IBOutlet UITextField *tflblSex; |
55 | @property (nonatomic, weak) IBOutlet UITextField *tfBirthday; | 55 | @property (nonatomic, weak) IBOutlet UITextField *tfBirthday; |
56 | @property (nonatomic, weak) IBOutlet UITextField *tfHeight; | 56 | @property (nonatomic, weak) IBOutlet UITextField *tfHeight; |
57 | @property (nonatomic, weak) IBOutlet UITextField *tfWeight; | 57 | @property (nonatomic, weak) IBOutlet UITextField *tfWeight; |
58 | @property (nonatomic, weak) IBOutlet UITextField *tfFatPercentage; | 58 | @property (nonatomic, weak) IBOutlet UITextField *tfFatPercentage; |
59 | @property (nonatomic, weak) IBOutlet UISegmentedControl *physicalActivity; | 59 | @property (nonatomic, weak) IBOutlet UISegmentedControl *physicalActivity; |
60 | @property (nonatomic, weak) IBOutlet CustomTextField *tfArea; | 60 | @property (nonatomic, weak) IBOutlet CustomTextField *tfArea; |
61 | @property (nonatomic, weak) IBOutlet UITextView *tvNote; | 61 | @property (nonatomic, weak) IBOutlet UITextView *tvNote; |
62 | 62 | ||
63 | @property (nonatomic, weak) IBOutlet UIToolbar *toolBar; | 63 | @property (nonatomic, weak) IBOutlet UIToolbar *toolBar; |
64 | @property (nonatomic, strong) NSArray *arrayArea; | 64 | @property (nonatomic, strong) NSArray *arrayArea; |
65 | @property (nonatomic, strong) NSArray *arraySex; | 65 | @property (nonatomic, strong) NSArray *arraySex; |
66 | 66 | ||
67 | @property (nonatomic) UIImagePickerController *imagePickerController; | 67 | @property (nonatomic) UIImagePickerController *imagePickerController; |
68 | 68 | ||
69 | @end | 69 | @end |
70 | 70 | ||
71 | @implementation RegisterViewController | 71 | @implementation RegisterViewController |
72 | 72 | ||
73 | - (void)viewDidLoad { | 73 | - (void)viewDidLoad { |
74 | [super viewDidLoad]; | 74 | [super viewDidLoad]; |
75 | [self setupUIRegister]; | 75 | [self setupUIRegister]; |
76 | 76 | ||
77 | _arrayArea = [[NSArray alloc] initWithObjects:@"北海道", @"青森県", @"岩手県", @"宮城県", @"秋田県", @"山形県", @"福島県", @"茨城県", @"栃木県", @"群馬県", @"埼玉県", @"千葉県", @"東京都", @"神奈川県", @"新潟県", @"富山県", @"石川県", @"福井県", @"山梨県", @"長野県", @"岐阜県", @"静岡県", @"愛知県", @"三重県", @"滋賀県", @"京都府", @"大阪府", @"兵庫県", @"奈良県", @"和歌山県", @"鳥取県", @"島根県", @"岡山県", @"広島県", @"山口県", @"徳島県", @"香川県", @"愛媛県", @"高知県", @"福岡県", @"佐賀県", @"長崎県", @"熊本県", @"大分県" , @"宮崎県", @"鹿児島県", @"沖縄県", nil]; | 77 | _arrayArea = [[NSArray alloc] initWithObjects:@"北海道", @"青森県", @"岩手県", @"宮城県", @"秋田県", @"山形県", @"福島県", @"茨城県", @"栃木県", @"群馬県", @"埼玉県", @"千葉県", @"東京都", @"神奈川県", @"新潟県", @"富山県", @"石川県", @"福井県", @"山梨県", @"長野県", @"岐阜県", @"静岡県", @"愛知県", @"三重県", @"滋賀県", @"京都府", @"大阪府", @"兵庫県", @"奈良県", @"和歌山県", @"鳥取県", @"島根県", @"岡山県", @"広島県", @"山口県", @"徳島県", @"香川県", @"愛媛県", @"高知県", @"福岡県", @"佐賀県", @"長崎県", @"熊本県", @"大分県" , @"宮崎県", @"鹿児島県", @"沖縄県", nil]; |
78 | _arraySex = [[NSArray alloc] initWithObjects:@"男", @"女", nil]; | 78 | _arraySex = [[NSArray alloc] initWithObjects:@"男", @"女", nil]; |
79 | 79 | ||
80 | pickerArea = [[UIPickerView alloc] init]; | 80 | pickerArea = [[UIPickerView alloc] init]; |
81 | pickerArea.dataSource = self; | 81 | pickerArea.dataSource = self; |
82 | pickerArea.delegate = self; | 82 | pickerArea.delegate = self; |
83 | pickerArea.showsSelectionIndicator = YES; | 83 | pickerArea.showsSelectionIndicator = YES; |
84 | pickerArea.tag = kPickerTagArea; | 84 | pickerArea.tag = kPickerTagArea; |
85 | pickerArea.backgroundColor = [Utilities convertHecToColor:0x618DB6]; | 85 | pickerArea.backgroundColor = [Utilities convertHecToColor:0x618DB6]; |
86 | 86 | ||
87 | _tfArea.inputAccessoryView = _toolBar; | 87 | _tfArea.inputAccessoryView = _toolBar; |
88 | _tfArea.inputView = pickerArea; | 88 | _tfArea.inputView = pickerArea; |
89 | //_tfArea.alpha = 0.5; | 89 | //_tfArea.alpha = 0.5; |
90 | _tfArea.delegate = self; | 90 | _tfArea.delegate = self; |
91 | 91 | ||
92 | pickerSex = [[UIPickerView alloc] init]; | 92 | pickerSex = [[UIPickerView alloc] init]; |
93 | pickerSex.dataSource = self; | 93 | pickerSex.dataSource = self; |
94 | pickerSex.delegate = self; | 94 | pickerSex.delegate = self; |
95 | pickerSex.showsSelectionIndicator = YES; | 95 | pickerSex.showsSelectionIndicator = YES; |
96 | pickerSex.tag = kPickerTagSex; | 96 | pickerSex.tag = kPickerTagSex; |
97 | pickerSex.backgroundColor = [Utilities convertHecToColor:0x618DB6]; | 97 | pickerSex.backgroundColor = [Utilities convertHecToColor:0x618DB6]; |
98 | 98 | ||
99 | _tflblSex.inputAccessoryView = _toolBar; | 99 | _tflblSex.inputAccessoryView = _toolBar; |
100 | _tflblSex.inputView = pickerSex; | 100 | _tflblSex.inputView = pickerSex; |
101 | //_tflblSex.alpha = 0.5; | 101 | //_tflblSex.alpha = 0.5; |
102 | _tflblSex.delegate = self; | 102 | _tflblSex.delegate = self; |
103 | 103 | ||
104 | pickerDate = [[UIDatePicker alloc] init]; | 104 | pickerDate = [[UIDatePicker alloc] init]; |
105 | pickerDate.backgroundColor = [Utilities convertHecToColor:0x618DB6]; | 105 | pickerDate.backgroundColor = [Utilities convertHecToColor:0x618DB6]; |
106 | pickerDate.datePickerMode = UIDatePickerModeDate; | 106 | pickerDate.datePickerMode = UIDatePickerModeDate; |
107 | _tfBirthday.inputAccessoryView = _toolBar; | 107 | _tfBirthday.inputAccessoryView = _toolBar; |
108 | _tfBirthday.inputView = pickerDate; | 108 | _tfBirthday.inputView = pickerDate; |
109 | //_tflblSex.alpha = 0.5; | 109 | //_tflblSex.alpha = 0.5; |
110 | _tflblSex.delegate = self; | 110 | _tflblSex.delegate = self; |
111 | profile_image = @""; | 111 | profile_image = @""; |
112 | } | 112 | } |
113 | 113 | ||
114 | - (void)didReceiveMemoryWarning { | 114 | - (void)didReceiveMemoryWarning { |
115 | [super didReceiveMemoryWarning]; | 115 | [super didReceiveMemoryWarning]; |
116 | // Dispose of any resources that can be recreated. | 116 | // Dispose of any resources that can be recreated. |
117 | } | 117 | } |
118 | 118 | ||
119 | - (void)setupUIRegister | 119 | - (void)setupUIRegister |
120 | { | 120 | { |
121 | self.avatar.backgroundColor = [UIColor whiteColor]; | 121 | self.avatar.backgroundColor = [UIColor whiteColor]; |
122 | self.avatar.layer.borderWidth = 2.0f; | 122 | self.avatar.layer.borderWidth = 2.0f; |
123 | self.avatar.layer.borderColor = [[UIColor whiteColor] CGColor]; | 123 | self.avatar.layer.borderColor = [[UIColor whiteColor] CGColor]; |
124 | self.avatar.layer.cornerRadius = self.avatar.frame.size.width/2.0f; | 124 | self.avatar.layer.cornerRadius = self.avatar.frame.size.width/2.0f; |
125 | self.avatar.layer.masksToBounds = YES; | 125 | self.avatar.layer.masksToBounds = YES; |
126 | self.title = NSLocalizedString(@"lifelog.register.title", nil); | 126 | self.title = NSLocalizedString(@"lifelog.register.title", nil); |
127 | self.lblUsername.text = NSLocalizedString(@"lifelog.register.username", nil); | 127 | self.lblUsername.text = NSLocalizedString(@"lifelog.register.username", nil); |
128 | self.lblName.text = NSLocalizedString(@"lifelog.register.name", nil); | 128 | self.lblName.text = NSLocalizedString(@"lifelog.register.name", nil); |
129 | self.lblNickname.text = NSLocalizedString(@"lifelog.register.nickname", nil); | 129 | self.lblNickname.text = NSLocalizedString(@"lifelog.register.nickname", nil); |
130 | self.lblEmail.text = NSLocalizedString(@"lifelog.common.email", nil); | 130 | self.lblEmail.text = NSLocalizedString(@"lifelog.common.email", nil); |
131 | self.lblPassword.text = NSLocalizedString(@"lifelog.common.password", nil); | 131 | self.lblPassword.text = NSLocalizedString(@"lifelog.common.password", nil); |
132 | self.lblConfirmPassword.text = NSLocalizedString(@"lifelog.register.confirmation.password", nil); | 132 | self.lblConfirmPassword.text = NSLocalizedString(@"lifelog.register.confirmation.password", nil); |
133 | self.lblSex.text = NSLocalizedString(@"lifelog.register.sex", nil); | 133 | self.lblSex.text = NSLocalizedString(@"lifelog.register.sex", nil); |
134 | self.lblBirthday.text = NSLocalizedString(@"lifelog.register.birthday", nil); | 134 | self.lblBirthday.text = NSLocalizedString(@"lifelog.register.birthday", nil); |
135 | self.lblHeight.text = NSLocalizedString(@"lifelog.register.height", nil); | 135 | self.lblHeight.text = NSLocalizedString(@"lifelog.register.height", nil); |
136 | self.lblHeightUnit.text = NSLocalizedString(@"lifelog.register.height.unit", nil); | 136 | self.lblHeightUnit.text = NSLocalizedString(@"lifelog.register.height.unit", nil); |
137 | self.lblWeight.text = NSLocalizedString(@"lifelog.register.weight", nil); | 137 | self.lblWeight.text = NSLocalizedString(@"lifelog.register.weight", nil); |
138 | self.lblWeightUnit.text = NSLocalizedString(@"lifelog.register.weight.unit", nil); | 138 | self.lblWeightUnit.text = NSLocalizedString(@"lifelog.register.weight.unit", nil); |
139 | self.lblFatPercentage.text = NSLocalizedString(@"lifelog.register.fat.percentage", nil); | 139 | self.lblFatPercentage.text = NSLocalizedString(@"lifelog.register.fat.percentage", nil); |
140 | self.lblFatPercentageUnit.text = NSLocalizedString(@"lifelog.register.fat.percentage.unit", nil); | 140 | self.lblFatPercentageUnit.text = NSLocalizedString(@"lifelog.register.fat.percentage.unit", nil); |
141 | self.lblPhysicalActivity.text = NSLocalizedString(@"lifelog.register.daily.physical.activity.level", nil); | 141 | self.lblPhysicalActivity.text = NSLocalizedString(@"lifelog.register.daily.physical.activity.level", nil); |
142 | self.lblArea.text = NSLocalizedString(@"lifelog.register.area", nil); | 142 | self.lblArea.text = NSLocalizedString(@"lifelog.register.area", nil); |
143 | self.lblNote.text = NSLocalizedString(@"lifelog.register.note", nil); | 143 | self.lblNote.text = NSLocalizedString(@"lifelog.register.note", nil); |
144 | 144 | ||
145 | UIImage *img1=[UIImage imageNamed:@"icon_right"]; | 145 | UIImage *img1=[UIImage imageNamed:@"icon_right"]; |
146 | CGRect frameimg1 = CGRectMake(0, 0, img1.size.width, img1.size.height); | 146 | CGRect frameimg1 = CGRectMake(0, 0, img1.size.width, img1.size.height); |
147 | UIButton *registButton=[[UIButton alloc]initWithFrame:frameimg1]; | 147 | UIButton *registButton=[[UIButton alloc]initWithFrame:frameimg1]; |
148 | [registButton setBackgroundImage:img1 forState:UIControlStateNormal]; | 148 | [registButton setBackgroundImage:img1 forState:UIControlStateNormal]; |
149 | [registButton addTarget:self action:@selector(registButtonPressed:) | 149 | [registButton addTarget:self action:@selector(registButtonPressed:) |
150 | forControlEvents:UIControlEventTouchUpInside]; | 150 | forControlEvents:UIControlEventTouchUpInside]; |
151 | [registButton setShowsTouchWhenHighlighted:YES]; | 151 | [registButton setShowsTouchWhenHighlighted:YES]; |
152 | UIBarButtonItem *barButton = [[UIBarButtonItem alloc]initWithCustomView:registButton]; | 152 | UIBarButtonItem *barButton = [[UIBarButtonItem alloc]initWithCustomView:registButton]; |
153 | self.navigationItem.rightBarButtonItem = barButton; | 153 | self.navigationItem.rightBarButtonItem = barButton; |
154 | } | 154 | } |
155 | 155 | ||
156 | #pragma mark - Actions | 156 | #pragma mark - Actions |
157 | - (IBAction)avatarTouchUpInside:(id)sender { | 157 | - (IBAction)avatarTouchUpInside:(id)sender { |
158 | UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"Avatar" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Select Camera", @"Select Photo", nil]; | 158 | UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"Avatar" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Select Camera", @"Select Photo", nil]; |
159 | [action showInView:self.view]; | 159 | [action showInView:self.view]; |
160 | } | 160 | } |
161 | 161 | ||
162 | - (IBAction)registButtonPressed:(id)sender { | 162 | - (IBAction)registButtonPressed:(id)sender { |
163 | NSLog(@"registButtonPressed"); | 163 | NSLog(@"registButtonPressed"); |
164 | [self registerUser]; | 164 | [self registerUser]; |
165 | } | 165 | } |
166 | 166 | ||
167 | #pragma mark - UIActionSheetDelegate | 167 | #pragma mark - UIActionSheetDelegate |
168 | - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { | 168 | - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { |
169 | switch (buttonIndex) { | 169 | switch (buttonIndex) { |
170 | case 0: | 170 | case 0: |
171 | [self showCamera]; | 171 | [self showCamera]; |
172 | break; | 172 | break; |
173 | case 1: | 173 | case 1: |
174 | [self showPhoto]; | 174 | [self showPhoto]; |
175 | break; | 175 | break; |
176 | default: | 176 | default: |
177 | break; | 177 | break; |
178 | } | 178 | } |
179 | } | 179 | } |
180 | 180 | ||
181 | #pragma mark - UIImagePickerControllerDelegate | 181 | #pragma mark - UIImagePickerControllerDelegate |
182 | 182 | ||
183 | // This method is called when an image has been chosen from the library or taken from the camera. | 183 | // This method is called when an image has been chosen from the library or taken from the camera. |
184 | - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info | 184 | - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info |
185 | { | 185 | { |
186 | UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage]; | 186 | UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage]; |
187 | self.avatar.image = image; | 187 | //NSData *imageData = UIImageJPEGRepresentation (image, 0.5); |
188 | NSData *dataImage = UIImagePNGRepresentation(image); | 188 | |
189 | // My image view is 90x90 | ||
190 | //UIImage *thumbImage = MyCreateThumbnailImageFromData(imageData, 90); | ||
191 | UIImage *thumbImage = [self imageWithImage:image scaledToFillSize:CGSizeMake(80, 80)]; | ||
192 | |||
193 | self.avatar.image = thumbImage; | ||
194 | NSData *dataImage = UIImagePNGRepresentation(thumbImage); | ||
189 | RegisterViewController __weak *weakSelf = self; | 195 | RegisterViewController __weak *weakSelf = self; |
190 | [[ServerAPI server] uploadImage:nil andImageData:dataImage CompletionHandler:^(NSString *linkImage, NSError *error) { | 196 | [[ServerAPI server] uploadImage:nil andImageData:dataImage CompletionHandler:^(NSString *linkImage, NSError *error) { |
191 | if (weakSelf == nil) { | 197 | if (weakSelf == nil) { |
192 | return ; | 198 | return ; |
193 | } | 199 | } |
194 | if (error == nil) { | 200 | if (error == nil) { |
195 | profile_image = linkImage; | 201 | profile_image = linkImage; |
196 | } | 202 | } |
197 | else { | 203 | else { |
198 | dispatch_async(dispatch_get_main_queue(), ^{ | 204 | dispatch_async(dispatch_get_main_queue(), ^{ |
199 | NSString *message = [error.userInfo objectForKey:@"message"]; | 205 | NSString *message = [error.userInfo objectForKey:@"message"]; |
200 | [weakSelf showErrorMessage:message]; | 206 | [weakSelf showErrorMessage:message]; |
201 | }); | 207 | }); |
202 | } | 208 | } |
203 | }]; | 209 | }]; |
204 | // Dismiss the image picker. | 210 | // Dismiss the image picker. |
205 | [self dismissViewControllerAnimated:YES completion:nil]; | 211 | [self dismissViewControllerAnimated:YES completion:nil]; |
206 | _imagePickerController = nil; | 212 | _imagePickerController = nil; |
207 | } | 213 | } |
208 | 214 | ||
209 | - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker | 215 | - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker |
210 | { | 216 | { |
211 | [self dismissViewControllerAnimated:YES completion:^{ | 217 | [self dismissViewControllerAnimated:YES completion:^{ |
212 | //.. done dismissing | 218 | //.. done dismissing |
213 | }]; | 219 | }]; |
214 | } | 220 | } |
215 | 221 | ||
222 | - (UIImage *)imageWithImage:(UIImage *)image scaledToFillSize:(CGSize)size | ||
223 | { | ||
224 | CGFloat scale = MAX(size.width/image.size.width, size.height/image.size.height); | ||
225 | CGFloat width = image.size.width * scale; | ||
226 | CGFloat height = image.size.height * scale; | ||
227 | CGRect imageRect = CGRectMake((size.width - width)/2.0f, | ||
228 | (size.height - height)/2.0f, | ||
229 | width, | ||
230 | height); | ||
231 | |||
232 | UIGraphicsBeginImageContextWithOptions(size, NO, 0); | ||
233 | [image drawInRect:imageRect]; | ||
234 | UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); | ||
235 | UIGraphicsEndImageContext(); | ||
236 | return newImage; | ||
237 | } | ||
238 | |||
216 | #pragma mark - Function Private | 239 | #pragma mark - Function Private |
217 | - (void)showCamera { | 240 | - (void)showCamera { |
218 | if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) | 241 | if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) |
219 | { | 242 | { |
220 | UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; | 243 | UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; |
221 | imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext; | 244 | imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext; |
222 | imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; | 245 | imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; |
223 | imagePickerController.delegate = self; | 246 | imagePickerController.delegate = self; |
224 | imagePickerController.modalPresentationStyle = UIModalPresentationFullScreen; | 247 | imagePickerController.modalPresentationStyle = UIModalPresentationFullScreen; |
225 | _imagePickerController = imagePickerController; // we need this for later | 248 | _imagePickerController = imagePickerController; // we need this for later |
226 | [self presentViewController:self.imagePickerController animated:YES completion:^{ | 249 | [self presentViewController:self.imagePickerController animated:YES completion:^{ |
227 | //.. done presenting | 250 | //.. done presenting |
228 | }]; | 251 | }]; |
229 | } | 252 | } |
230 | } | 253 | } |
231 | 254 | ||
232 | - (void)showPhoto { | 255 | - (void)showPhoto { |
233 | UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; | 256 | UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; |
234 | imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext; | 257 | imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext; |
235 | imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; | 258 | imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; |
236 | imagePickerController.delegate = self; | 259 | imagePickerController.delegate = self; |
237 | imagePickerController.modalPresentationStyle = UIModalPresentationPopover; | 260 | imagePickerController.modalPresentationStyle = UIModalPresentationPopover; |
238 | _imagePickerController = imagePickerController; // we need this for later | 261 | _imagePickerController = imagePickerController; // we need this for later |
239 | [self presentViewController:self.imagePickerController animated:YES completion:^{ | 262 | [self presentViewController:self.imagePickerController animated:YES completion:^{ |
240 | //.. done presenting | 263 | //.. done presenting |
241 | }]; | 264 | }]; |
242 | } | 265 | } |
243 | 266 | ||
244 | - (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType fromButton:(UIBarButtonItem *)button | 267 | - (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType fromButton:(UIBarButtonItem *)button |
245 | { | 268 | { |
246 | if (self.avatar.isAnimating) | 269 | if (self.avatar.isAnimating) |
247 | { | 270 | { |
248 | [self.avatar stopAnimating]; | 271 | [self.avatar stopAnimating]; |
249 | } | 272 | } |
250 | 273 | ||
251 | UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; | 274 | UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; |
252 | imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext; | 275 | imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext; |
253 | imagePickerController.sourceType = sourceType; | 276 | imagePickerController.sourceType = sourceType; |
254 | imagePickerController.delegate = self; | 277 | imagePickerController.delegate = self; |
255 | imagePickerController.modalPresentationStyle = | 278 | imagePickerController.modalPresentationStyle = |
256 | (sourceType == UIImagePickerControllerSourceTypeCamera) ? UIModalPresentationFullScreen : UIModalPresentationPopover; | 279 | (sourceType == UIImagePickerControllerSourceTypeCamera) ? UIModalPresentationFullScreen : UIModalPresentationPopover; |
257 | 280 | ||
258 | UIPopoverPresentationController *presentationController = imagePickerController.popoverPresentationController; | 281 | UIPopoverPresentationController *presentationController = imagePickerController.popoverPresentationController; |
259 | presentationController.barButtonItem = button; // display popover from the UIBarButtonItem as an anchor | 282 | presentationController.barButtonItem = button; // display popover from the UIBarButtonItem as an anchor |
260 | presentationController.permittedArrowDirections = UIPopoverArrowDirectionAny; | 283 | presentationController.permittedArrowDirections = UIPopoverArrowDirectionAny; |
261 | 284 | ||
262 | if (sourceType == UIImagePickerControllerSourceTypeCamera) | 285 | if (sourceType == UIImagePickerControllerSourceTypeCamera) |
263 | { | 286 | { |
264 | // The user wants to use the camera interface. Set up our custom overlay view for the camera. | 287 | // The user wants to use the camera interface. Set up our custom overlay view for the camera. |
265 | imagePickerController.showsCameraControls = NO; | 288 | imagePickerController.showsCameraControls = NO; |
266 | 289 | ||
267 | /* | 290 | /* |
268 | Load the overlay view from the OverlayView nib file. Self is the File's Owner for the nib file, so the overlayView outlet is set to the main view in the nib. Pass that view to the image picker controller to use as its overlay view, and set self's reference to the view to nil. | 291 | Load the overlay view from the OverlayView nib file. Self is the File's Owner for the nib file, so the overlayView outlet is set to the main view in the nib. Pass that view to the image picker controller to use as its overlay view, and set self's reference to the view to nil. |
269 | */ | 292 | */ |
270 | } | 293 | } |
271 | 294 | ||
272 | _imagePickerController = imagePickerController; // we need this for later | 295 | _imagePickerController = imagePickerController; // we need this for later |
273 | 296 | ||
274 | [self presentViewController:self.imagePickerController animated:YES completion:^{ | 297 | [self presentViewController:self.imagePickerController animated:YES completion:^{ |
275 | //.. done presenting | 298 | //.. done presenting |
276 | }]; | 299 | }]; |
277 | } | 300 | } |
278 | 301 | ||
279 | - (IBAction)donePicker:(id)sender | 302 | - (IBAction)donePicker:(id)sender |
280 | { | 303 | { |
281 | [_tfArea resignFirstResponder]; | 304 | [_tfArea resignFirstResponder]; |
282 | [_tflblSex resignFirstResponder]; | 305 | [_tflblSex resignFirstResponder]; |
283 | if ([_tfBirthday becomeFirstResponder]) { | 306 | if ([_tfBirthday becomeFirstResponder]) { |
284 | [_tfBirthday resignFirstResponder]; | 307 | [_tfBirthday resignFirstResponder]; |
285 | dateBirthDay = pickerDate.date; | 308 | dateBirthDay = pickerDate.date; |
286 | _tfBirthday.text = [NSString stringWithFormat:@"%ld%@%ld%@%ld%@", (long)[dateBirthDay getYear], NSLocalizedString(@"lifelog.common.year", nil), (long)[dateBirthDay getMonth], NSLocalizedString(@"lifelog.common.month", nil), (long)[dateBirthDay getDay], NSLocalizedString(@"lifelog.common.day", nil)]; | 309 | _tfBirthday.text = [NSString stringWithFormat:@"%ld%@%ld%@%ld%@", (long)[dateBirthDay getYear], NSLocalizedString(@"lifelog.common.year", nil), (long)[dateBirthDay getMonth], NSLocalizedString(@"lifelog.common.month", nil), (long)[dateBirthDay getDay], NSLocalizedString(@"lifelog.common.day", nil)]; |
287 | } | 310 | } |
288 | } | 311 | } |
289 | 312 | ||
290 | #pragma mark - UITextFieldDelegate | 313 | #pragma mark - UITextFieldDelegate |
291 | - (void)textFieldDidEndEditing:(UITextField *)textField { | 314 | - (void)textFieldDidEndEditing:(UITextField *)textField { |
292 | //if (textField == _tfArea) { | 315 | //if (textField == _tfArea) { |
293 | [textField resignFirstResponder]; | 316 | [textField resignFirstResponder]; |
294 | //} | 317 | //} |
295 | } | 318 | } |
296 | 319 | ||
297 | - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string | 320 | - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string |
298 | { | 321 | { |
299 | //if (textField == _tfArea) { | 322 | //if (textField == _tfArea) { |
300 | [textField resignFirstResponder]; | 323 | [textField resignFirstResponder]; |
301 | return NO; | 324 | return NO; |
302 | //} | 325 | //} |
303 | //else { | 326 | //else { |
304 | return YES; | 327 | return YES; |
305 | //} | 328 | //} |
306 | } | 329 | } |
307 | 330 | ||
308 | - (BOOL)textFieldShouldReturn:(UITextField *)textField | 331 | - (BOOL)textFieldShouldReturn:(UITextField *)textField |
309 | { | 332 | { |
310 | [textField resignFirstResponder]; | 333 | [textField resignFirstResponder]; |
311 | return YES; | 334 | return YES; |
312 | } | 335 | } |
313 | 336 | ||
314 | - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { | 337 | - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { |
315 | //if (textField == _tfArea) { | 338 | //if (textField == _tfArea) { |
316 | [textField resignFirstResponder]; | 339 | [textField resignFirstResponder]; |
317 | //} | 340 | //} |
318 | return YES; | 341 | return YES; |
319 | } | 342 | } |
320 | 343 | ||
321 | - (BOOL)textFieldShouldEndEditing:(UITextField *)textField { | 344 | - (BOOL)textFieldShouldEndEditing:(UITextField *)textField { |
322 | //if (textField == _tfArea) { | 345 | //if (textField == _tfArea) { |
323 | [textField resignFirstResponder]; | 346 | [textField resignFirstResponder]; |
324 | //} | 347 | //} |
325 | return YES; | 348 | return YES; |
326 | } | 349 | } |
327 | 350 | ||
328 | #pragma mark - UIPickerViewDataSource | 351 | #pragma mark - UIPickerViewDataSource |
329 | 352 | ||
330 | - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView | 353 | - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView |
331 | { | 354 | { |
332 | return 1; | 355 | return 1; |
333 | } | 356 | } |
334 | 357 | ||
335 | - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component | 358 | - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component |
336 | { | 359 | { |
337 | int tag = (int)pickerView.tag; | 360 | int tag = (int)pickerView.tag; |
338 | switch (tag) { | 361 | switch (tag) { |
339 | case kPickerTagArea: | 362 | case kPickerTagArea: |
340 | return _arrayArea.count; | 363 | return _arrayArea.count; |
341 | case kPickerTagSex: | 364 | case kPickerTagSex: |
342 | return _arraySex.count; | 365 | return _arraySex.count; |
343 | default: | 366 | default: |
344 | return 0; | 367 | return 0; |
345 | } | 368 | } |
346 | } | 369 | } |
347 | 370 | ||
348 | #pragma mark - UIPickerViewDelegate | 371 | #pragma mark - UIPickerViewDelegate |
349 | - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component | 372 | - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component |
350 | { | 373 | { |
351 | int tag = (int)pickerView.tag; | 374 | int tag = (int)pickerView.tag; |
352 | switch (tag) { | 375 | switch (tag) { |
353 | case kPickerTagArea: | 376 | case kPickerTagArea: |
354 | _tfArea.text = [_arrayArea objectAtIndex:row]; | 377 | _tfArea.text = [_arrayArea objectAtIndex:row]; |
355 | break; | 378 | break; |
356 | case kPickerTagSex: | 379 | case kPickerTagSex: |
357 | _tflblSex.text = [_arraySex objectAtIndex:row]; | 380 | _tflblSex.text = [_arraySex objectAtIndex:row]; |
358 | break; | 381 | break; |
359 | default: | 382 | default: |
360 | break; | 383 | break; |
361 | } | 384 | } |
362 | } | 385 | } |
363 | 386 | ||
364 | - (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component | 387 | - (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component |
365 | { | 388 | { |
366 | NSString *strTitle = @""; | 389 | NSString *strTitle = @""; |
367 | int tag = (int)pickerView.tag; | 390 | int tag = (int)pickerView.tag; |
368 | switch (tag) { | 391 | switch (tag) { |
369 | case kPickerTagArea: | 392 | case kPickerTagArea: |
370 | strTitle = [_arrayArea objectAtIndex:row]; | 393 | strTitle = [_arrayArea objectAtIndex:row]; |
371 | break; | 394 | break; |
372 | case kPickerTagSex: | 395 | case kPickerTagSex: |
373 | strTitle = [_arraySex objectAtIndex:row]; | 396 | strTitle = [_arraySex objectAtIndex:row]; |
374 | break; | 397 | break; |
375 | default: | 398 | default: |
376 | strTitle = @""; | 399 | strTitle = @""; |
377 | break; | 400 | break; |
378 | } | 401 | } |
379 | NSAttributedString *attString = [[NSAttributedString alloc] initWithString:strTitle attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}]; | 402 | NSAttributedString *attString = [[NSAttributedString alloc] initWithString:strTitle attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}]; |
380 | 403 | ||
381 | return attString; | 404 | return attString; |
382 | 405 | ||
383 | } | 406 | } |
384 | 407 | ||
385 | - (NSMutableDictionary *)addParam:(NSString *)param value:(NSString *)value withDict:(NSMutableDictionary *)dict | 408 | - (NSMutableDictionary *)addParam:(NSString *)param value:(NSString *)value withDict:(NSMutableDictionary *)dict |
386 | { | 409 | { |
387 | if (value.length > 0) { | 410 | if (value.length > 0) { |
388 | [dict setObject:value forKey:param]; | 411 | [dict setObject:value forKey:param]; |
389 | } | 412 | } |
390 | return dict; | 413 | return dict; |
391 | } | 414 | } |
392 | 415 | ||
393 | /* | ||
394 | API regist: | ||
395 | - Url: http://clover.timesfun.jp:9001/register | ||
396 | Params: | ||
397 | username: "sonle33", password: "laslas123", full_name: "son le", nickname: "las", birthday: "1990-11-11", height: 177, weight: 70, fat_rate: 12, gender: 1, address: "asdsad", description: "nothing", receive_notification: 0, phone: 0988999777, share_data: 1, profile_image: "upload/avt/2019525859598528205222222.png", delete_flag: 0, email: "sonle33@las.vn", physical_activity: 0, remember_me: 1 | ||
398 | |||
399 | - Url: http://clover.timesfun.jp:9001/forgetPass | ||
400 | param: email | ||
401 | */ | ||
402 | |||
403 | - (NSDictionary *)createParamsForRegisterUser { | 416 | - (NSDictionary *)createParamsForRegisterUser { |
404 | NSMutableDictionary *dictResutl = [[NSMutableDictionary alloc] init]; | 417 | NSMutableDictionary *dictResutl = [[NSMutableDictionary alloc] init]; |
405 | dictResutl = [self addParam:@"username" value:_tfUsername.text withDict:dictResutl]; | 418 | dictResutl = [self addParam:@"username" value:_tfUsername.text withDict:dictResutl]; |
406 | dictResutl = [self addParam:@"full_name" value:_tfName.text withDict:dictResutl]; | 419 | dictResutl = [self addParam:@"full_name" value:_tfName.text withDict:dictResutl]; |
407 | dictResutl = [self addParam:@"nickname" value:_tfNickname.text withDict:dictResutl]; | 420 | dictResutl = [self addParam:@"nickname" value:_tfNickname.text withDict:dictResutl]; |
408 | dictResutl = [self addParam:@"email" value:_tfEmail.text withDict:dictResutl]; | 421 | dictResutl = [self addParam:@"email" value:_tfEmail.text withDict:dictResutl]; |
409 | dictResutl = [self addParam:@"password" value:_tfPassword.text withDict:dictResutl]; | 422 | dictResutl = [self addParam:@"password" value:_tfPassword.text withDict:dictResutl]; |
410 | dictResutl = [self addParam:@"confirm_password" value:_tfConfirmPassword.text withDict:dictResutl]; | 423 | //dictResutl = [self addParam:@"confirm_password" value:_tfConfirmPassword.text withDict:dictResutl]; |
411 | NSString *valueSex = nil; // 0 - 男, 1 - 女 | 424 | NSString *valueSex = nil; // 0 - 男, 1 - 女 |
412 | if (_tflblSex.text.length > 0) { | 425 | if (_tflblSex.text.length > 0) { |
413 | if ([_tflblSex.text isEqualToString:@"男"]) { | 426 | if ([_tflblSex.text isEqualToString:@"男"]) { |
414 | valueSex = @"0"; | 427 | valueSex = @"0"; |
415 | } | 428 | } |
416 | else if ([_tflblSex.text isEqualToString:@"男"]) { | 429 | else if ([_tflblSex.text isEqualToString:@"女"]) { |
417 | valueSex = @"1"; | 430 | valueSex = @"1"; |
418 | } | 431 | } |
419 | } | 432 | } |
420 | dictResutl = [self addParam:@"gender" value:valueSex withDict:dictResutl]; | 433 | dictResutl = [self addParam:@"gender" value:valueSex withDict:dictResutl]; |
421 | NSString *valueBirthDay = nil; | 434 | NSString *valueBirthDay = nil; |
422 | if (_tfBirthday.text.length > 0) { | 435 | if (_tfBirthday.text.length > 0) { |
423 | valueBirthDay = [NSString stringWithFormat:@"%ld-%ld-%ld", (long)[dateBirthDay getYear], (long)[dateBirthDay getMonth], (long)[dateBirthDay getDay]]; | 436 | valueBirthDay = [NSString stringWithFormat:@"%ld-%ld-%ld", (long)[dateBirthDay getYear], (long)[dateBirthDay getMonth], (long)[dateBirthDay getDay]]; |
424 | } | 437 | } |
425 | dictResutl = [self addParam:@"height" value:_tfHeight.text withDict:dictResutl]; | 438 | dictResutl = [self addParam:@"height" value:_tfHeight.text withDict:dictResutl]; |
426 | dictResutl = [self addParam:@"weight" value:_tfWeight.text withDict:dictResutl]; | 439 | dictResutl = [self addParam:@"weight" value:_tfWeight.text withDict:dictResutl]; |
427 | dictResutl = [self addParam:@"fat_rate" value:_tfFatPercentage.text withDict:dictResutl]; | 440 | dictResutl = [self addParam:@"fat_rate" value:_tfFatPercentage.text withDict:dictResutl]; |
428 | NSString *physicalActivityValue = [NSString stringWithFormat:@"%ld", _physicalActivity.selectedSegmentIndex]; | 441 | NSString *physicalActivityValue = [NSString stringWithFormat:@"%ld", _physicalActivity.selectedSegmentIndex]; |
429 | dictResutl = [self addParam:@"physical_activity" value:physicalActivityValue withDict:dictResutl]; | 442 | dictResutl = [self addParam:@"physical_activity" value:physicalActivityValue withDict:dictResutl]; |
430 | dictResutl = [self addParam:@"address" value:_tfArea.text withDict:dictResutl]; | 443 | dictResutl = [self addParam:@"address" value:_tfArea.text withDict:dictResutl]; |
431 | dictResutl = [self addParam:@"description" value:_tvNote.text withDict:dictResutl]; | 444 | dictResutl = [self addParam:@"description" value:_tvNote.text withDict:dictResutl]; |
432 | if (![profile_image isEqualToString:@""]) { | 445 | if (![profile_image isEqualToString:@""]) { |
433 | dictResutl = [self addParam:@"profile_image" value:profile_image withDict:dictResutl]; | 446 | dictResutl = [self addParam:@"profile_image" value:profile_image withDict:dictResutl]; |
434 | } | 447 | } |
435 | return dictResutl; | 448 | return dictResutl; |
436 | } | 449 | } |
437 | 450 | ||
438 | - (void)registerUser { | 451 | - (void)registerUser { |
439 | NSDictionary *paramRegister = [self createParamsForRegisterUser]; | 452 | NSDictionary *paramRegister = [self createParamsForRegisterUser]; |
440 | RegisterViewController __weak *weakSelf = self; | 453 | RegisterViewController __weak *weakSelf = self; |
441 | [[ServerAPI server] registerUserWithParams:paramRegister CompletionHandler:^(NSError *error) { | 454 | [[ServerAPI server] registerUserWithParams:paramRegister CompletionHandler:^(NSError *error) { |
442 | if (weakSelf == nil) { | 455 | if (weakSelf == nil) { |
443 | return ; | 456 | return ; |
444 | } | 457 | } |
445 | if (error == nil) { | 458 | if (error == nil) { |
446 | dispatch_async(dispatch_get_main_queue(), ^{ | 459 | dispatch_async(dispatch_get_main_queue(), ^{ |
447 | [[AppDelegate sharedAppDelegate] gotoMainMenu]; | 460 | [[AppDelegate sharedAppDelegate] gotoMainMenu]; |
448 | }); | 461 | }); |
449 | } | 462 | } |
450 | else { | 463 | else { |
451 | dispatch_async(dispatch_get_main_queue(), ^{ | 464 | dispatch_async(dispatch_get_main_queue(), ^{ |
452 | NSString *message = [error.userInfo objectForKey:@"message"]; | 465 | NSString *message = [error.userInfo objectForKey:@"message"]; |
453 | [weakSelf showErrorMessage:message]; | 466 | [weakSelf showErrorMessage:message]; |
454 | }); | 467 | }); |
455 | } | 468 | } |
456 | }]; | 469 | }]; |
457 | } | 470 | } |
458 | 471 | ||
459 | - (void)showErrorMessage:(NSString *)message | 472 | - (void)showErrorMessage:(NSString *)message |
460 | { | 473 | { |
461 | if (message.length > 0) { | 474 | if (message.length > 0) { |
462 | UIAlertController * alert= [UIAlertController | 475 | UIAlertController * alert= [UIAlertController |
463 | alertControllerWithTitle:@"Error" | 476 | alertControllerWithTitle:@"Error" |
464 | message:message | 477 | message:message |
465 | preferredStyle:UIAlertControllerStyleAlert]; | 478 | preferredStyle:UIAlertControllerStyleAlert]; |
466 | 479 | ||
467 | UIAlertAction* ok = [UIAlertAction | 480 | UIAlertAction* ok = [UIAlertAction |
468 | actionWithTitle:@"OK" | 481 | actionWithTitle:@"OK" |
469 | style:UIAlertActionStyleDefault | 482 | style:UIAlertActionStyleDefault |
470 | handler:^(UIAlertAction * action) | 483 | handler:^(UIAlertAction * action) |
471 | { | 484 | { |
472 | [alert dismissViewControllerAnimated:YES completion:nil]; | 485 | [alert dismissViewControllerAnimated:YES completion:nil]; |
473 | 486 |
LifeLog/LifeLog/ServerAPI.m
1 | // | 1 | // |
2 | // ServerAPI.m | 2 | // ServerAPI.m |
3 | // LifeLog | 3 | // LifeLog |
4 | // | 4 | // |
5 | // Created by Nguyen Van Phong on 7/30/17. | 5 | // Created by Nguyen Van Phong on 7/30/17. |
6 | // Copyright © 2017 PhongNV. All rights reserved. | 6 | // Copyright © 2017 PhongNV. All rights reserved. |
7 | // | 7 | // |
8 | 8 | ||
9 | #import "ServerAPI.h" | 9 | #import "ServerAPI.h" |
10 | 10 | ||
11 | NSString *const kServerAddress = @"http://clover.timesfun.jp:9001/"; | 11 | NSString *const kServerAddress = @"http://clover.timesfun.jp:9001/"; |
12 | 12 | ||
13 | @implementation NSString (NSString_Extended) | 13 | @implementation NSString (NSString_Extended) |
14 | - (NSString *)urlencode { | 14 | - (NSString *)urlencode { |
15 | NSMutableString *output = [NSMutableString string]; | 15 | NSMutableString *output = [NSMutableString string]; |
16 | const unsigned char *source = (const unsigned char *)[self UTF8String]; | 16 | const unsigned char *source = (const unsigned char *)[self UTF8String]; |
17 | int sourceLen = (int)strlen((const char *)source); | 17 | int sourceLen = (int)strlen((const char *)source); |
18 | for (int i = 0; i < sourceLen; ++i) { | 18 | for (int i = 0; i < sourceLen; ++i) { |
19 | const unsigned char thisChar = source[i]; | 19 | const unsigned char thisChar = source[i]; |
20 | if (thisChar == ' '){ | 20 | if (thisChar == ' '){ |
21 | [output appendString:@"+"]; | 21 | [output appendString:@"+"]; |
22 | } else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' || | 22 | } else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' || |
23 | (thisChar >= 'a' && thisChar <= 'z') || | 23 | (thisChar >= 'a' && thisChar <= 'z') || |
24 | (thisChar >= 'A' && thisChar <= 'Z') || | 24 | (thisChar >= 'A' && thisChar <= 'Z') || |
25 | (thisChar >= '0' && thisChar <= '9')) { | 25 | (thisChar >= '0' && thisChar <= '9')) { |
26 | [output appendFormat:@"%c", thisChar]; | 26 | [output appendFormat:@"%c", thisChar]; |
27 | } else { | 27 | } else { |
28 | [output appendFormat:@"%%%02X", thisChar]; | 28 | [output appendFormat:@"%%%02X", thisChar]; |
29 | } | 29 | } |
30 | } | 30 | } |
31 | return output; | 31 | return output; |
32 | } | 32 | } |
33 | @end | 33 | @end |
34 | 34 | ||
35 | @implementation ServerAPI | 35 | @implementation ServerAPI |
36 | static ServerAPI *_server = nil; | 36 | static ServerAPI *_server = nil; |
37 | 37 | ||
38 | @synthesize timeOutInterval = _timeOutInterval; | 38 | @synthesize timeOutInterval = _timeOutInterval; |
39 | 39 | ||
40 | + (instancetype)server | 40 | + (instancetype)server |
41 | { | 41 | { |
42 | @synchronized(self) { | 42 | @synchronized(self) { |
43 | if (_server == nil) { | 43 | if (_server == nil) { |
44 | _server = [[ServerAPI alloc] init]; | 44 | _server = [[ServerAPI alloc] init]; |
45 | } | 45 | } |
46 | } | 46 | } |
47 | return _server; | 47 | return _server; |
48 | } | 48 | } |
49 | 49 | ||
50 | - (instancetype)init | 50 | - (instancetype)init |
51 | { | 51 | { |
52 | self = [super init]; | 52 | self = [super init]; |
53 | if (self != nil) { | 53 | if (self != nil) { |
54 | self.timeOutInterval = 150; | 54 | self.timeOutInterval = 150; |
55 | } | 55 | } |
56 | return self; | 56 | return self; |
57 | } | 57 | } |
58 | 58 | ||
59 | // Login | 59 | // Login |
60 | - (void)loginWithEmail:(NSString *)email Password:(NSString *)password CompletionHandler: (void (^)(User *, NSString *, NSError *)) completion | 60 | - (void)loginWithEmail:(NSString *)email Password:(NSString *)password CompletionHandler: (void (^)(User *, NSString *, NSError *)) completion |
61 | { | 61 | { |
62 | [self _request:[kServerAddress stringByAppendingFormat: @"login"] method:@"POST" paras:@{@"email":email, @"password": password} completion:^(NSData *data, NSError *error) { | 62 | [self _request:[kServerAddress stringByAppendingFormat: @"login"] method:@"POST" paras:@{@"email":email, @"password": password} completion:^(NSData *data, NSError *error) { |
63 | 63 | ||
64 | if (completion == NULL) { | 64 | if (completion == NULL) { |
65 | return ; | 65 | return ; |
66 | } | 66 | } |
67 | 67 | ||
68 | if (error == nil) | 68 | if (error == nil) |
69 | { | 69 | { |
70 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; | 70 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; |
71 | 71 | ||
72 | 72 | ||
73 | int status = [dataResult[@"status"] intValue]; | 73 | int status = [dataResult[@"status"] intValue]; |
74 | if (status == 1) { // status = 1 success | 74 | if (status == 1) { // status = 1 success |
75 | NSString *token = dataResult[@"result"][@"token"]; | 75 | NSString *token = dataResult[@"result"][@"token"]; |
76 | NSDictionary *dictUser = dataResult[@"result"][@"user"]; | 76 | NSDictionary *dictUser = dataResult[@"result"][@"user"]; |
77 | User *user = [[User alloc] init]; | 77 | User *user = [[User alloc] init]; |
78 | user.user_id = [NSString stringWithFormat:@"%@",dictUser[@"id"]]; | 78 | user.user_id = [NSString stringWithFormat:@"%@",dictUser[@"id"]]; |
79 | user.username = [NSString stringWithFormat:@"%@",dictUser[@"username"]]; | 79 | user.username = [NSString stringWithFormat:@"%@",dictUser[@"username"]]; |
80 | user.full_name = [NSString stringWithFormat:@"%@",dictUser[@"full_name"]]; | 80 | user.full_name = [NSString stringWithFormat:@"%@",dictUser[@"full_name"]]; |
81 | user.nickname = [NSString stringWithFormat:@"%@",dictUser[@"nickname"]]; | 81 | user.nickname = [NSString stringWithFormat:@"%@",dictUser[@"nickname"]]; |
82 | user.email = [NSString stringWithFormat:@"%@",dictUser[@"email"]]; | 82 | user.email = [NSString stringWithFormat:@"%@",dictUser[@"email"]]; |
83 | user.password = [NSString stringWithFormat:@"%@",dictUser[@"password"]]; | 83 | user.password = [NSString stringWithFormat:@"%@",dictUser[@"password"]]; |
84 | user.birthday = [NSString stringWithFormat:@"%@",dictUser[@"birthday"]]; | 84 | user.birthday = [NSString stringWithFormat:@"%@",dictUser[@"birthday"]]; |
85 | user.address = [NSString stringWithFormat:@"%@",dictUser[@"address"]]; | 85 | user.address = [NSString stringWithFormat:@"%@",dictUser[@"address"]]; |
86 | user.gender = [[NSString stringWithFormat:@"%@",dictUser[@"gender"]] intValue]; | 86 | user.gender = [[NSString stringWithFormat:@"%@",dictUser[@"gender"]] intValue]; |
87 | user.height = [[NSString stringWithFormat:@"%@",dictUser[@"height"]] floatValue]; | 87 | user.height = [[NSString stringWithFormat:@"%@",dictUser[@"height"]] floatValue]; |
88 | user.weight = [[NSString stringWithFormat:@"%@",dictUser[@"weight"]] floatValue]; | 88 | user.weight = [[NSString stringWithFormat:@"%@",dictUser[@"weight"]] floatValue]; |
89 | user.user_description = [NSString stringWithFormat:@"%@",dictUser[@"description"]]; | 89 | user.user_description = [NSString stringWithFormat:@"%@",dictUser[@"description"]]; |
90 | completion(user, token, nil); | 90 | completion(user, token, nil); |
91 | } | 91 | } |
92 | else { // status = 0 error | 92 | else { // status = 0 error |
93 | NSString *message = dataResult[@"message"]; | 93 | NSString *message = dataResult[@"message"]; |
94 | NSError *loginFaild = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; | 94 | NSError *loginFaild = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; |
95 | completion(nil, nil, loginFaild); | 95 | completion(nil, nil, loginFaild); |
96 | } | 96 | } |
97 | } | 97 | } |
98 | else | 98 | else |
99 | { | 99 | { |
100 | completion(nil, nil, error); | 100 | completion(nil, nil, error); |
101 | } | 101 | } |
102 | }]; | 102 | }]; |
103 | } | 103 | } |
104 | 104 | ||
105 | // Register | 105 | // Register |
106 | - (void)registerUserWithParams:(NSDictionary *)params CompletionHandler: (void (^)(NSError *)) completion { | 106 | - (void)registerUserWithParams:(NSDictionary *)params CompletionHandler: (void (^)(NSError *)) completion { |
107 | [self _request:[kServerAddress stringByAppendingFormat: @"register"] method:@"POST" paras:params completion:^(NSData *data, NSError *error) { | 107 | [self _request:[kServerAddress stringByAppendingFormat: @"register"] method:@"POST" paras:params completion:^(NSData *data, NSError *error) { |
108 | 108 | ||
109 | if (completion == NULL) { | 109 | if (completion == NULL) { |
110 | return ; | 110 | return ; |
111 | } | 111 | } |
112 | 112 | ||
113 | if (error == nil) | 113 | if (error == nil) |
114 | { | 114 | { |
115 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; | 115 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; |
116 | completion(nil); | ||
117 | /* | ||
118 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; | ||
119 | if (dataResult == nil) { | ||
120 | completion(nil); | ||
121 | } | ||
116 | 122 | ||
117 | |||
118 | int status = [dataResult[@"status"] intValue]; | 123 | int status = [dataResult[@"status"] intValue]; |
119 | if (status == 1) { // status = 1 success | 124 | if (status == 1) { // status = 1 success |
120 | completion(nil); | 125 | completion(nil); |
121 | } | 126 | } |
122 | else { // status = 0 error | 127 | else { // status = 0 error |
123 | NSString *message = dataResult[@"message"]; | 128 | NSString *message = dataResult[@"message"]; |
124 | NSError *registerFaild = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; | 129 | NSError *registerFaild = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; |
125 | completion(registerFaild); | 130 | completion(registerFaild); |
126 | } | 131 | } |
132 | */ | ||
127 | } | 133 | } |
128 | else | 134 | else |
129 | { | 135 | { |
130 | completion(error); | 136 | completion(error); |
131 | } | 137 | } |
132 | }]; | 138 | }]; |
133 | } | 139 | } |
134 | 140 | ||
135 | - (void)uploadImage:(NSString *)token andImageData:(NSData *)data CompletionHandler:(void (^)(NSString *, NSError *)) completion { | 141 | - (void)uploadImage:(NSString *)token andImageData:(NSData *)data CompletionHandler:(void (^)(NSString *, NSError *)) completion { |
136 | NSDictionary *dict = nil; | 142 | NSDictionary *dict = nil; |
137 | NSString *base64Encoded = [data base64EncodedStringWithOptions:0]; | 143 | NSString *base64Encoded = [data base64EncodedStringWithOptions:0]; |
138 | if (token != nil) { | 144 | if (token != nil) { |
139 | dict = @{@"token":token, @"img": base64Encoded}; | 145 | dict = @{@"token":token, @"img": base64Encoded}; |
140 | } | 146 | } |
141 | else { | 147 | else { |
142 | dict = @{@"img": base64Encoded}; | 148 | dict = @{@"img": base64Encoded}; |
143 | } | 149 | } |
144 | [self _request:[kServerAddress stringByAppendingFormat: @"upload-image"] method:@"POST" paras:dict completion:^(NSData *data, NSError *error) { | 150 | [self _request:[kServerAddress stringByAppendingFormat: @"upload-image"] method:@"POST" paras:dict completion:^(NSData *data, NSError *error) { |
145 | 151 | ||
146 | if (completion == NULL) { | 152 | if (completion == NULL) { |
147 | return ; | 153 | return ; |
148 | } | 154 | } |
149 | 155 | ||
150 | if (error == nil) | 156 | if (error == nil) |
151 | { | 157 | { |
152 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; | 158 | NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; |
153 | 159 | NSString *image_profile = [NSString stringWithFormat:@"%@", dataResult[@"message"]]; | |
154 | int status = [dataResult[@"status"] intValue]; | 160 | completion(image_profile, nil); |
155 | if (status == 1) { // status = 1 success | ||
156 | NSString *image_profile = dataResult[@"result"][@"image_profile"]; | ||
157 | completion(image_profile, nil); | ||
158 | } | ||
159 | else { // status = 0 error | ||
160 | NSString *message = dataResult[@"message"]; | ||
161 | NSError *uploadFaild = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; | ||
162 | completion(nil, uploadFaild); | ||
163 | } | ||
164 | } | 161 | } |
165 | else | 162 | else |
166 | { | 163 | { |
167 | completion(nil, error); | 164 | completion(nil, error); |
168 | } | 165 | } |
169 | }]; | 166 | }]; |
170 | } | 167 | } |
171 | 168 | ||
172 | #pragma mark - Private Function | 169 | #pragma mark - Private Function |
173 | - (NSData *) _encodeDictionary: (NSDictionary *) dictionary | 170 | - (NSData *) _encodeDictionary: (NSDictionary *) dictionary |
174 | { | 171 | { |
175 | NSMutableArray *parts = [[NSMutableArray alloc] init]; | 172 | NSMutableArray *parts = [[NSMutableArray alloc] init]; |
176 | for (id key in dictionary) | 173 | for (id key in dictionary) |
177 | { | 174 | { |
178 | NSString *encodedValue = [[dictionary[key] description] urlencode]; | 175 | NSString *encodedValue = [[dictionary[key] description] urlencode]; |
179 | NSString *encodedKey = [[key description] urlencode];//[[key description] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; | 176 | NSString *encodedKey = [[key description] urlencode];//[[key description] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; |
180 | NSString *part = [NSString stringWithFormat: @"%@=%@", encodedKey, encodedValue]; | 177 | NSString *part = [NSString stringWithFormat: @"%@=%@", encodedKey, encodedValue]; |
181 | [parts addObject:part]; | 178 | [parts addObject:part]; |
182 | } | 179 | } |
183 | NSString *encodedDictionary = [parts componentsJoinedByString:@"&"]; | 180 | NSString *encodedDictionary = [parts componentsJoinedByString:@"&"]; |
184 | return [encodedDictionary dataUsingEncoding: NSUTF8StringEncoding]; | 181 | return [encodedDictionary dataUsingEncoding: NSUTF8StringEncoding]; |
185 | } | 182 | } |
186 | 183 | ||
187 | - (void) _request:(NSString *)address method:(NSString *)method paras:(NSDictionary *)paras completion:(void (^)(NSData *data, NSError *error))completion | 184 | - (void) _request:(NSString *)address method:(NSString *)method paras:(NSDictionary *)paras completion:(void (^)(NSData *data, NSError *error))completion |
188 | { | 185 | { |
189 | NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:address]]; | 186 | NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:address]]; |
190 | request.HTTPMethod = method; | 187 | request.HTTPMethod = method; |
191 | [request setValue: @"application/json" forHTTPHeaderField: @"Accept"]; | 188 | [request setValue: @"application/json" forHTTPHeaderField: @"Accept"]; |
192 | [request setValue: @"application/json" forHTTPHeaderField: @"Content-Type"]; | 189 | [request setValue: @"application/json" forHTTPHeaderField: @"Content-Type"]; |
193 | [request setTimeoutInterval:self.timeOutInterval]; | 190 | [request setTimeoutInterval:self.timeOutInterval]; |
194 | 191 | ||
195 | if (paras != nil) | 192 | if (paras != nil) |
196 | { | 193 | { |
197 | NSData *encodedData = [self _encodeDictionary: paras]; | 194 | NSData *encodedData = [self _encodeDictionary: paras]; |
198 | [request setValue: [NSString stringWithFormat: @"%lu", (unsigned long) encodedData.length] forHTTPHeaderField: @"Content-Length"]; | 195 | [request setValue: [NSString stringWithFormat: @"%lu", (unsigned long) encodedData.length] forHTTPHeaderField: @"Content-Length"]; |
199 | [request setValue: @"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField: @"Content-Type"]; | 196 | [request setValue: @"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField: @"Content-Type"]; |
200 | [request setHTTPBody: encodedData]; | 197 | [request setHTTPBody: encodedData]; |
201 | } | 198 | } |
202 | 199 | ||
203 | NSURLSession *session = [NSURLSession sharedSession]; | 200 | NSURLSession *session = [NSURLSession sharedSession]; |
204 | NSURLSessionDataTask *task = [session dataTaskWithRequest:request | 201 | NSURLSessionDataTask *task = [session dataTaskWithRequest:request |
205 | completionHandler: | 202 | completionHandler: |
206 | ^(NSData *data, NSURLResponse *response, NSError *error) { | 203 | ^(NSData *data, NSURLResponse *response, NSError *error) { |
207 | if (completion == NULL) { | 204 | if (completion == NULL) { |
208 | return ; | 205 | return ; |
209 | } | 206 | } |
210 | if (error == nil) | 207 | if (error == nil) |
211 | { | 208 | { |
212 | completion(data, nil); | 209 | completion(data, nil); |
213 | } | 210 | } |
214 | else | 211 | else |
215 | { | 212 | { |
216 | completion(nil, error); | 213 | completion(nil, error); |
217 | } | 214 | } |