/
CommandesViewController.m
245 lines (199 loc) · 8.55 KB
/
CommandesViewController.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
#import "CommandesViewController.h"
@interface CommandesViewController ()
@property (strong, nonatomic) IBOutlet UIButton *buttonLoginLogout;
@property (strong, nonatomic) IBOutlet UIButton *buttonPublish;
@property(nonatomic, retain) UITableView *tableView;
@end
@implementation CommandesViewController
@synthesize arrayOfWineToOrder = _arrayOfWineToOrder;
@synthesize postParams = _postParams;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)publishStory
{
int i=0;
for (int y=0; y<_arrayOfWineToOrder.count; y++) {
Wine *wine =[_arrayOfWineToOrder objectAtIndex:y];
i = i + wine.nombre;
}
NSString *texte = [NSString stringWithFormat:
@"J'ai commandé %d bouteilles",
i];
self.postParams =
[[NSMutableDictionary alloc] initWithObjectsAndKeys:
@"https://www.lepetitballon.com/", @"link",
@"http://www.lepetitballon.com/img/logo.png", @"picture",
@"Lepetitballon", @"name",
texte, @"caption",
nil];
[FBRequestConnection
startWithGraphPath:@"me/feed"
parameters:self.postParams
HTTPMethod:@"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
NSString *alertText;
if (error) {
alertText = [NSString stringWithFormat:
@"error: domain = %@, code = %d",
error.domain, error.code];
} else {
alertText = [NSString stringWithFormat:
@"Posted action, id: %@",
[result objectForKey:@"id"]];
}
// Show the result in an alert
[[[UIAlertView alloc] initWithTitle:@"Result"
message:alertText
delegate:self
cancelButtonTitle:@"OK!"
otherButtonTitles:nil]
show];
}];
}
- (void)viewDidLoad
{
if (_arrayOfWineToOrder == nil) {
_arrayOfWineToOrder = [NSMutableArray arrayWithObjects:nil];
}
AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(12, 50, 280, 45)];
self.tableView.tableFooterView =footer;
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(12, 50, 280, 45)];
self.tableView.tableHeaderView =header;
[super viewDidLoad];
_buttonPublish = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[_buttonPublish setFrame:CGRectMake(12, 8, 280, 30)];
[_buttonPublish setTitle:@"Publier" forState:UIControlStateNormal];
[_buttonPublish addTarget:self action:@selector(shareButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[_buttonPublish setHidden:TRUE];
[footer addSubview:_buttonPublish];
_buttonLoginLogout = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[_buttonLoginLogout setFrame:CGRectMake(18, 8, 280, 30)];
[_buttonLoginLogout setTitle:@"Facebook" forState:UIControlStateNormal];
[_buttonLoginLogout addTarget:self action:@selector(buttonClickHandler:) forControlEvents:UIControlEventTouchUpInside];
[header addSubview:_buttonLoginLogout];
[self updateView];
if (!appDelegate.session.isOpen) {
appDelegate.session = [[FBSession alloc] init];
if (appDelegate.session.state == FBSessionStateCreatedTokenLoaded) {
// even though we had a cached token, we need to login to make the session usable
[appDelegate.session openWithCompletionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
// we recurse here, in order to update buttons and labels
[self updateView];
}];
}
}
}
- (void)updateView {
// get the app delegate, so that we can reference the session property
AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
if (appDelegate.session.isOpen) {
[FBSession setActiveSession:appDelegate.session];
// valid account UI is shown whenever the session is open
[self.buttonLoginLogout setTitle:@"Log out" forState:UIControlStateNormal];
[_buttonPublish setHidden:FALSE];
} else {
// login-needed account UI is shown whenever the session is closed
[self.buttonLoginLogout setTitle:@"Log in" forState:UIControlStateNormal];
[_buttonPublish setHidden:TRUE];
// [self.textNoteOrLink setText:@"Login to create a link to fetch account data"];
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// NSLog([NSString stringWithFormat:@"count : %d",[_arrayOfWineToOrder count]],@"ee");
return [_arrayOfWineToOrder count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"WineCell";
WineCell *cell = (WineCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[WineCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UIImage *image = [UIImage imageNamed:[_dataToDraw objectAtIndex:[indexPath row]]];
// cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.imageView.image = image;
Wine *wine =[_arrayOfWineToOrder objectAtIndex:[indexPath row]];
cell.myLabel.text = wine.name;
cell.nombre.text = [NSString stringWithFormat:@"%d ",wine.nombre];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
#pragma mark - FBSession logic
// handler for button click, logs sessions in or out
- (IBAction)buttonClickHandler:(id)sender {
AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
// this button's job is to flip-flop the session from open to closed
if (appDelegate.session.isOpen) {
[appDelegate.session closeAndClearTokenInformation];
} else {
if (appDelegate.session.state != FBSessionStateCreated) {
// Create a new, logged out session.
appDelegate.session = [[FBSession alloc] init];
}
// if the session isn't open, let's open it now and present the login UX to the user
[appDelegate.session openWithCompletionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
// and here we make sure to update our UX according to the new session state
[self updateView];
}];
}
}
- (IBAction)shareButtonAction:(id)sender {
AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
if (appDelegate.session.isOpen) {
// Ask for publish_actions permissions in context
if ([appDelegate.session.permissions
indexOfObject:@"publish_actions"] == NSNotFound) {
// No permissions found in session, ask for it
[appDelegate.session reauthorizeWithPublishPermissions:[NSArray arrayWithObject:@"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
// If permissions granted, publish the story
[self publishStory];
}
}];
} else {
// If permissions present, publish the story
[self publishStory];
}
}else{
NSLog(@"eeze");
}
}
@end