Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 61ba842d06
Fetching contributors…

Cannot retrieve contributors at this time

file 130 lines (105 sloc) 4.341 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
//
// NewMessageViewController.m
// Messagee
//
// Created by Ernesto Vargas on 2/10/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "NewMessageViewController.h"

@implementation NewMessageViewController
@synthesize messageTextField;


- (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

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/

- (void)viewWillAppear:(BOOL)flag {
    [super viewWillAppear:flag];
    [messageTextField becomeFirstResponder];
}

- (void)viewDidUnload
{
    [self setMessageTextField:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}



- (IBAction)postMessage:(id)sender {
    if (![messageTextField.text isEqualToString:@""]) {
        NSMutableDictionary *rpcData = [[NSMutableDictionary alloc] init ];
        NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
        
        // User Params
        [params setObject:[[UGUser sharedInstance] username] forKey:@"displayName"];
        [params setObject:[[UGUser sharedInstance] email] forKey:@"email"];
        [params setObject:[[UGUser sharedInstance] picture] forKey:@"picture"];
        
        // Post message params
        [rpcData setValue:params forKey:@"actor"];
        [rpcData setObject:@"post" forKey:@"verb"];
        [rpcData setObject:[messageTextField text] forKey:@"content"];
        
        // Parsing rpcData to JSON!
        id<RKParser> parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:RKMIMETypeJSON];
        NSError *error = nil;
        NSString *json = [parser stringFromObject:rpcData error:&error];
        
        if (!error){
            // POST json message to: /user/<username>/activities/
            [[[RKClient sharedClient] post:[NSString stringWithFormat:@"user/%@/activities/", [[UGUser sharedInstance] username]]
                                    params:[RKRequestSerialization serializationWithData:[json dataUsingEncoding:NSUTF8StringEncoding] MIMEType:RKMIMETypeJSON]
                                  delegate:self] send];
        }
    } else {
        UIAlertView* alert = [[UIAlertView alloc]
                              initWithTitle:@"New message Failed"
                              message:@"No message content."
                              delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
}

- (IBAction)closeMessage:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
}


- (void)requestDidStartLoad:(RKRequest *)request {
    NSLog(@"Request did start load %@", request.params);
}

- (void)request:(RKRequest *)request didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite {
    NSLog(@"Request did send body data: %@", request.HTTPBody);
}

- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {
    if ((response.isOK)&&(response.isJSON)) {
        NSLog(@"json: %@", [response parsedBody:nil]);
        UIAlertView* alert = [[UIAlertView alloc]
                              initWithTitle:nil
                              message:@"You created a new message!!"
                              delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [self dismissModalViewControllerAnimated:YES];
    } else {
        RKLogCritical(@"Loading of RKRequest %@ completed with status code %d. Response body: %@", request, response.statusCode, [response bodyAsString]);
    }
}

- (void)request:(RKRequest *)request didFailLoadWithError:(NSError *)error {
    NSLog(@"Did Fail Load with error %@", error);
}

@end
Something went wrong with that request. Please try again.