Skip to content

Instantly share code, notes, and snippets.

@imrekel
Last active October 13, 2015 04:18
Show Gist options
  • Save imrekel/4138773 to your computer and use it in GitHub Desktop.
Save imrekel/4138773 to your computer and use it in GitHub Desktop.
bme-mobilszoftverek - MeetWorld
- (void)downloadUsers
{
NSURLSessionConfiguration* sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession* session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:nil delegateQueue:[NSOperationQueue mainQueue]];
NSURL* url = [NSURL URLWithString:
@"http://atleast.aut.bme.hu/mobilszoftverek/meetworld/users"];
[[session dataTaskWithURL: url
completionHandler:^(NSData *data, NSURLResponse *response,
NSError *error) {
NSLog(@"Data received:\n%@\n",
[[NSString alloc] initWithData: data
encoding: NSUTF8StringEncoding]);
}] resume];
for (id<UserManagerObserver> observer in self.observers)
{
[observer usersDidUpdate];
}
}
- (void)downloadUsers
{
NSDictionary* user1 = @{@"id": @"Sanyi",
@"status": @"Szétverem a Mac Mini-t"};
NSDictionary* user2 = @{@"id": @"Panni",
@"status": @"Okosodom"};
[self.users addObject:user1];
[self.users addObject:user2];
for (id<UserManagerObserver> observer in self.observers)
{
[observer usersDidUpdate];
}
}
- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateBegan)
{
UIStoryboard* sb =
[UIStoryboard storyboardWithName:@"MainStoryboard"
bundle:[NSBundle mainBundle]];
UINavigationController* navigationController =
[sb instantiateViewControllerWithIdentifier:
@"UpdateUserNavigationController"];
UpdateUserViewController* updateUserVC = navigationController.viewControllers[0];
CGPoint touchPoint = [recognizer locationInView:self.mapView];
updateUserVC.coordinate = [self.mapView convertPoint:touchPoint
toCoordinateFromView:self.mapView];
[self presentViewController:navigationController animated:YES completion:nil];
}
}
NSURLSessionConfiguration* sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession* session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:nil delegateQueue:[NSOperationQueue mainQueue]];
NSURL* url = [NSURL URLWithString:
@"http://atleast.aut.bme.hu/mobilszoftverek/meetworld/updateuser"];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
request.HTTPBody = json;
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (!error)
{
NSDictionary* result = [NSJSONSerialization
JSONObjectWithData:data options:0 error:nil];
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:
@"A szerver válasza" message:result[@"result"]
delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alertView show];
}
else
{
NSLog(@"Error: %@", [error description]);
}
[self dismissViewControllerAnimated:YES completion:nil];
}];
[postDataTask resume];
NSMutableDictionary* user = [NSMutableDictionary dictionary];
user[@"id"] = self.usernameTextField.text;
user[@"status"] = self.statusTextField.text;
user[@"lat"] = [NSString stringWithFormat:@"%f",
self.coordinate.latitude];
user[@"lon"] = [NSString stringWithFormat:@"%f",
self.coordinate.longitude];
NSData* json = [NSJSONSerialization dataWithJSONObject:user options:0
error:nil];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"UserStatusCell"];
NSArray* users = [UserManager sharedUserManager].users;
NSDictionary* user = users[indexPath.row];
cell.textLabel.text = user[@"id"];
cell.detailTextLabel.text = user[@"status"];
return cell;
}
- (void)usersDidUpdate
{
NSArray* users = [UserManager sharedUserManager].users;
[self.mapView removeAnnotations:self.mapView.annotations];
for (NSDictionary* user in users)
{
UserAnnotation* annotation = [[UserAnnotation alloc] init];
annotation.title = user[@"id"];
annotation.subtitle = user[@"status"];
CLLocationCoordinate2D coord;
coord.latitude = [user[@"lat"] doubleValue];
coord.longitude = [user[@"lon"] doubleValue];
annotation.coordinate = coord;
[self.mapView addAnnotation:annotation];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment