-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComposeVDMVC.m
272 lines (216 loc) · 10.6 KB
/
ComposeVDMVC.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
//
// ComposeVDMVC.m
// iMines-1
//
// Created by François de la Taste on 03/08/11.
// Copyright 2011 Mines ParisTech. All rights reserved.
//
#import "ComposeVDMVC.h"
#import <QuartzCore/QuartzCore.h>
#import "ASIFormDataRequest.h"
#import "ASIHTTPRequest.h"
@implementation ComposeVDMVC
@synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
textField.layer.cornerRadius = 5.0f;
textField.layer.borderColor = [UIColor blackColor].CGColor;
textField.layer.borderWidth = 1.0f;
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
selectedCategoryNumber = -1;
categories = [[NSArray alloc] initWithObjects:
@"Absurde",
@"Amour",
@"Assoces",
@"Emploi/Stage",
@"Soirées",
@"Vacances/VP",
@"Inclassable",
nil];
keyboardIsShown = FALSE;
[textField.layer setBorderWidth:1.0f];
[textField.layer setBorderColor:[UIColor blackColor].CGColor];
[textField.layer setCornerRadius:5.0f];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
NSUserDefaults *defaultSettings = [NSUserDefaults standardUserDefaults];
NSString *userLogin = [defaultSettings stringForKey:@"login_ccsi"];
NSString *userPassword = [defaultSettings stringForKey:@"password_ccsi"];
if([userLogin isEqualToString:@""] || [userPassword isEqualToString:@""]){
// Les identifiants ne sont pas renseignés, on alerte et on revient en arrière (géré par alertView:clickedButtonAtIndex:)
NSString *errorString = @"Tu dois renseigner ton login pour poster des VDM";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"VieDeMineur" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
alert.tag = 999;
[alert show];
[alert release];
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
// On a cliqué sur un des boutons de l'alerte affichée si les identifiants ne sont pas renseignés :
if(buttonIndex==0 && alertView.tag == 999){ // buttonIndex devrait toujours valoir 0 ici,et le tag sert à reconnaître la bonne alerte (pour ne pas confondre avec l'oubli de sélection de catégorie)
[self.delegate ComposeVDMVC:self shouldDismiss:YES];
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)dismiss{
[self.delegate ComposeVDMVC:self shouldDismiss:YES];
}
- (IBAction)addVDM {
if(keyboardIsShown) {
[textField resignFirstResponder];
}
else {
if (selectedCategoryNumber == -1) {
// l'utilisateur n'a pas sélectionné de catégorie...
NSString *errorString = @"Sélectionne une catégorie !";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"VieDeMineur" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else if ([textField.text isEqualToString:@""] || [textField.text isEqualToString:@"Aujourd'hui..."]) {
// le champ "contenu" est vide...
NSString *errorString = @"Remplis ta VDM!";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"VieDeMineur" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else {
UIActionSheet *actionSheet = [UIActionSheet alloc];
[actionSheet initWithTitle:@"Confirmer l'envoi" delegate:self cancelButtonTitle:@"Annuler" destructiveButtonTitle:@"Poster ma VDM" otherButtonTitles:nil];
//[actionSheet showFromTabBar:self.tabBarController.tabBar];
[actionSheet showInView:self.view];
}
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"button index : %d",buttonIndex);
if(buttonIndex==0){
// On envoie la requête pour créer une VDM
NSString *contenuVDM = textField.text;
contenuVDM = [contenuVDM stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSUserDefaults *defaultSettings = [NSUserDefaults standardUserDefaults];
NSString *poster = [defaultSettings stringForKey:@"login_ccsi"];
NSString *promo = [poster stringByPaddingToLength:2 withString:@"" startingAtIndex:0];
promo = [@"P" stringByAppendingString:promo];
NSString *catNumber = [NSString stringWithFormat:@"%d",selectedCategoryNumber+1];
NSString *settingsPath = [[NSBundle mainBundle] pathForResource:@"SETTINGS" ofType:@"plist"];
NSDictionary *settings = [[NSDictionary alloc] initWithContentsOfFile:settingsPath];
NSString *secretKey = [settings objectForKey:@"vdm-secretkey"];
// Construction de la requête
NSString *baseURLVDM = [[settings objectForKey:@"serveur-vdm"] stringByAppendingString:[settings objectForKey:@"vdm-writenew"]];
//NSString *userLogin = [defaultSettings stringForKey:@"login_vdm"];
//NSString *userPassword = [defaultSettings stringForKey:@"password_vdm"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:baseURLVDM]];
[request setShouldAttemptPersistentConnection:YES];
[request setPersistentConnectionTimeoutSeconds:120];
[request setPostValue:contenuVDM forKey:@"body"];
[request setPostValue:poster forKey:@"poster"];
[request setPostValue:promo forKey:@"promo"];
[request setPostValue:catNumber forKey:@"category"];
[request setPostValue:secretKey forKey:@"key"];
NSLog(@"body : %@",contenuVDM);
NSLog(@"poster : %@", poster);
NSLog(@"promo : %@", promo);
NSLog(@"category : %@", catNumber);
NSLog(@"Key : %@", secretKey);
//[request setUsername:userLogin];
//[request setPassword:userPassword];
//[request setShouldPresentCredentialsBeforeChallenge:NO];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
// NSLog(@"Pas d'erreur !");
NSString *response = [request responseString];
NSLog(@"Reponse : %@", response);
if ([response rangeOfString:@"OK"].location != NSNotFound) {
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"VDM postée !" message:@"Ta VDM doit maintenant être validée pour apparaître sur le site" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
[self.delegate ComposeVDMVC:self shouldDismiss:YES];
}
else {
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Erreur :" message:@"La VDM n'a pas été postée" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
}
}
else {
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Erreur :" message:@"Impossible d'envoyer la requête" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
}
}
}
- (IBAction)selectCategory {
VDMCategorySelectionTVC *vdmcstvc = [[VDMCategorySelectionTVC alloc] initWithStyle:UITableViewStyleGrouped andArray:categories];
[vdmcstvc setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[vdmcstvc setDelegate:self];
[self presentModalViewController:vdmcstvc animated:YES];
}
#pragma mark - Keyboard management
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
keyboardIsShown = TRUE;
rightBarButtonItem.title = @"OK";
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, textField.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, textField.frame.origin.y-kbSize.height);
[scrollView setContentOffset:scrollPoint animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
keyboardIsShown = FALSE;
rightBarButtonItem.title = @"Envoyer";
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}
#pragma mark - VDM Category Selection delegate
- (IBAction)VDMCategorySelection:(VDMCategorySelectionTVC *)vdmcstvc selectedCategory:(int)categoryNumber {
selectedCategoryNumber = categoryNumber;
[self dismissModalViewControllerAnimated:YES];
[categoryButton setTitle:[categories objectAtIndex:categoryNumber] forState:UIControlStateNormal];
}
@end