-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComposeCommentVC.m
169 lines (141 loc) · 6.35 KB
/
ComposeCommentVC.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
//
// ComposeCommentVC.m
// iMines-1
//
// Created by François de la Taste on 28/08/11.
// Copyright 2011 Mines ParisTech. All rights reserved.
//
#import "ComposeCommentVC.h"
#import "ASIFormDataRequest.h"
#import <QuartzCore/QuartzCore.h>
@implementation ComposeCommentVC
@synthesize delegate, idVDM;
- (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.
keyboardIsShown = FALSE;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
- (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 ComposeCommentVC:self shouldDismiss: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;
}
- (void)addComment{
if(keyboardIsShown) {
[textField resignFirstResponder];
}
else
{
NSString *contenuComment = textField.text;
contenuComment = [contenuComment stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *settingsPath = [[NSBundle mainBundle] pathForResource:@"SETTINGS" ofType:@"plist"];
NSDictionary *settings = [[NSDictionary alloc] initWithContentsOfFile:settingsPath];
NSUserDefaults *userSettings = [NSUserDefaults standardUserDefaults];
NSString *poster = [userSettings stringForKey:@"login_ccsi"];
NSString *secretKey = [settings objectForKey:@"vdm-secretkey"];
// Construction de la requête
NSString *baseURLVDM = [[settings objectForKey:@"serveur-vdm"] stringByAppendingString:[settings objectForKey:@"vdm-writecomm"]];
//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:contenuComment forKey:@"body"];
[request setPostValue:poster forKey:@"poster"];
[request setPostValue:secretKey forKey:@"key"];
[request setPostValue:[NSString stringWithFormat:@"%d",idVDM] forKey:@"id"];
//[request setUsername:userLogin];
//[request setPassword:userPassword];
NSLog(@"contenu : %@",contenuComment);
NSLog(@"poster : %@",poster);
NSLog(@"ID : %@",[NSString stringWithFormat:@"%d",idVDM]);
NSLog(@"requete : %@",[request url]);
[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:@"Commentaire posté !" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
[self.delegate ComposeCommentVC:self shouldDismiss:YES];
}
else {
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Erreur :" message:@"Le commentaire n'a pas été posté" 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];
}
}
}
@end