-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBandAddEventViewController.m
More file actions
163 lines (134 loc) · 5.82 KB
/
Copy pathBandAddEventViewController.m
File metadata and controls
163 lines (134 loc) · 5.82 KB
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
//
// BandAddEventViewController.m
// Discover Concerts
//
// Created by Laborator iOS on 4/17/13.
// Copyright (c) 2013 Loredana Albulescu. All rights reserved.
//
#import "BandAddEventViewController.h"
#import <FacebookSDK/FacebookSDK.h>
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define kBandsURL [NSURL URLWithString:@"http://discover-concerts.herokuapp.com/bands"]
@interface BandAddEventViewController ()
@property(nonatomic,retain) NSMutableArray *listOfUsersFacebookId;
@end
@implementation BandAddEventViewController
@synthesize nameTextField = _nameTextField;
@synthesize cityTextField = _cityTextField;
@synthesize locationTextField = _locationTextField;
@synthesize priceTextField = _priceTextField;
@synthesize dateTimeTextField = _dateTimeTextField;
@synthesize descriptionTextField = _descriptionTextField;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
self.title= @"Add event";
[self GetAllUserFromFavorites];
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)AddEvent:(id)sender
{
//de adaugat in baza de date
[self addToDatabaseEvent];
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Added event"
message:nil
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
}
- (void) addToDatabaseEvent
{
if (FBSession.activeSession.isOpen) {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *user,
NSError *error) {
if (!error) {
//get data from date picker
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *date = self.datePicker.date;
NSString *dateAsString = [dateFormatter stringFromDate:date];
NSLog(@"%@", [date description]);
NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:
_nameTextField.text,@"name",
_cityTextField.text,@"city",
_locationTextField.text,@"location",
_priceTextField.text,@"price",
dateAsString,@"date_time",
_descriptionTextField.text,@"description",
[user objectForKey:@"id"],@"band_id",nil];
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:info
options:NSJSONWritingPrettyPrinted
error:&error];
NSLog(@"json :%@", info);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://discover-concerts.herokuapp.com/concerts"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:jsonData];
[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
}
}];
}
}
//Get all bands from db
- (void)GetAllUserFromFavorites
{
if (FBSession.activeSession.isOpen) {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *user,
NSError *error) {
if (!error) {
NSString *urlForAllUsers= [NSString stringWithFormat:@"http://discover-concerts.herokuapp.com/favorites/%@%@",user.id,@"/show_favorites_of_band_id.json"];
NSLog(@"user favorites: %@", urlForAllUsers);
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlForAllUsers]];
[self performSelectorOnMainThread:@selector(fetchedDataFavoritesUsers:)
withObject:data waitUntilDone:YES];
});
}
}];
}
}
//parse out the json genres data and add the favorites bands
-(void) fetchedDataFavoritesUsers:(NSData*) responseData
{
_listOfUsersFacebookId = [[NSMutableArray alloc] init];
NSError* error;
NSArray* json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
for (NSDictionary* object in json) {
NSString* userFacebookId = [object objectForKey:@"user_id"];
[_listOfUsersFacebookId addObject:userFacebookId];
NSLog(@"fetchedDataFavoritesBands: %@", userFacebookId);
}
//send a Notification to _listOfUsersFacebookId
}
#pragma mark -
#pragma mark AlerView delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[self.navigationController popViewControllerAnimated:YES];
}
@end