/LibraryApp/CheckDetailsView.m
Objective C | 139 lines | 109 code | 16 blank | 14 comment | 12 complexity | cb7725a312fbb00eaad815d04c0e3617 MD5 | raw file
- //
- // CheckOutDetailsView.m
- // LibraryApp
- //
- // Created by Glenn Sayers on 17/08/2012.
- // Copyright (c) 2012 __MyCompanyName__. All rights reserved.
- //
- #import "CheckDetailsView.h"
- #import <QuartzCore/QuartzCore.h>
- #import "MBProgressHUD.h"
- @implementation CheckDetailsView
- @synthesize nameField,locationField,tap,book;
- - (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.
- }
- -(void)cancel {
- [self.navigationController popViewControllerAnimated:YES];
- }
- -(void) checkOut {
-
- NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
- [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://rkyve-api.herokuapp.com/books/%@.json",[self.book.identity stringValue]]]];
- [request setHTTPMethod:@"PUT"];
- [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
- NSString* str = [NSString stringWithFormat:@"{\"id\":\"%@\",\"location\":\"%@\",\"borrower\":\"%@\"}",[self.book.identity stringValue],self.locationField.text,self.nameField.text];
- NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
- [request setHTTPBody:data];
- NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
- if (!connection) {
- NSLog(@"Connection Failed");
- UIAlertView *alert =[[UIAlertView alloc] initWithTitle:nil message:@"There was a problem checking out the book" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
- [alert show];
- } else {
- NSLog(@"connection success.");
- [self.navigationController popToRootViewControllerAnimated:YES];
- }
- }
- -(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
- }
- -(void) checkOutValidation {
- if(self.nameField.text.length ==0 && self.locationField.text ==0) {
- UIAlertView *alert =[[UIAlertView alloc] initWithTitle:nil message:@"You need to enter your name and the books new location" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
- [alert show];
- }
- else if(self.nameField.text.length ==0) {
- UIAlertView *alert =[[UIAlertView alloc] initWithTitle:nil message:@"You need to enter your name" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
- [alert show];
- }
- else if(self.locationField.text.length ==0) {
- UIAlertView *alert =[[UIAlertView alloc] initWithTitle:nil message:@"You need to enter the books new location" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
- [alert show];
- }
- else {
- MBProgressHUD *hud =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
- [self.nameField resignFirstResponder];
- [self.locationField resignFirstResponder];
- hud.labelText =@"Checking out";
- [hud show:YES];
- [self checkOut];
- [hud hide:YES];
- }
- }
- #pragma mark - View lifecycle
- -(void) setUpUI {
- self.nameField =[[UITextField alloc] initWithFrame:CGRectMake(50, 40, 200, 30)];
- self.nameField.backgroundColor=[UIColor whiteColor];
- self.nameField.layer.cornerRadius=8;
- self.nameField.delegate=self;
- self.nameField.placeholder =@"Your name";
- [self.view addSubview:self.nameField];
-
- self.locationField =[[UITextField alloc] initWithFrame:CGRectMake(50, 100, 200, 30)];
- self.locationField.backgroundColor =[UIColor whiteColor];
- self.locationField.layer.cornerRadius=8;
- self.locationField.delegate=self;
- self.locationField.placeholder =@"New location";
- [self.view addSubview:self.locationField];
- self.view.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.jpg"]];
-
- UIBarButtonItem *cancel =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];
- UIBarButtonItem *save =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(checkOutValidation)];
- self.navigationItem.leftBarButtonItem =cancel;
- self.navigationItem.rightBarButtonItem =save;
-
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.navigationItem.hidesBackButton = YES;
- self.title= @"Details";
- [self setUpUI];
- // Do any additional setup after loading the view from its nib.
- }
- -(void) removeKeypad {
- [self.view removeGestureRecognizer:self.tap];
- [self.nameField resignFirstResponder];
- [self.locationField resignFirstResponder];
- }
- -(void) textFieldDidBeginEditing:(UITextField *)textField {
- if(!self.tap) self.tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeKeypad)];
- [self.view addGestureRecognizer:self.tap];
- [self.nameField removeGestureRecognizer:self.tap];
- [self.locationField removeGestureRecognizer:self.tap];
- }
- - (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);
- }
- @end