PageRenderTime 29ms CodeModel.GetById 8ms RepoModel.GetById 8ms app.codeStats 0ms

/LibraryApp/CheckDetailsView.m

https://bitbucket.org/glennsayers/rkyve
Objective C | 139 lines | 109 code | 16 blank | 14 comment | 12 complexity | cb7725a312fbb00eaad815d04c0e3617 MD5 | raw file
  1. //
  2. // CheckOutDetailsView.m
  3. // LibraryApp
  4. //
  5. // Created by Glenn Sayers on 17/08/2012.
  6. // Copyright (c) 2012 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "CheckDetailsView.h"
  9. #import <QuartzCore/QuartzCore.h>
  10. #import "MBProgressHUD.h"
  11. @implementation CheckDetailsView
  12. @synthesize nameField,locationField,tap,book;
  13. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  14. {
  15. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  16. if (self) {
  17. // Custom initialization
  18. }
  19. return self;
  20. }
  21. - (void)didReceiveMemoryWarning
  22. {
  23. // Releases the view if it doesn't have a superview.
  24. [super didReceiveMemoryWarning];
  25. // Release any cached data, images, etc that aren't in use.
  26. }
  27. -(void)cancel {
  28. [self.navigationController popViewControllerAnimated:YES];
  29. }
  30. -(void) checkOut {
  31. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  32. [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://rkyve-api.herokuapp.com/books/%@.json",[self.book.identity stringValue]]]];
  33. [request setHTTPMethod:@"PUT"];
  34. [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  35. NSString* str = [NSString stringWithFormat:@"{\"id\":\"%@\",\"location\":\"%@\",\"borrower\":\"%@\"}",[self.book.identity stringValue],self.locationField.text,self.nameField.text];
  36. NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
  37. [request setHTTPBody:data];
  38. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
  39. if (!connection) {
  40. NSLog(@"Connection Failed");
  41. UIAlertView *alert =[[UIAlertView alloc] initWithTitle:nil message:@"There was a problem checking out the book" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
  42. [alert show];
  43. } else {
  44. NSLog(@"connection success.");
  45. [self.navigationController popToRootViewControllerAnimated:YES];
  46. }
  47. }
  48. -(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
  49. }
  50. -(void) checkOutValidation {
  51. if(self.nameField.text.length ==0 && self.locationField.text ==0) {
  52. 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];
  53. [alert show];
  54. }
  55. else if(self.nameField.text.length ==0) {
  56. UIAlertView *alert =[[UIAlertView alloc] initWithTitle:nil message:@"You need to enter your name" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
  57. [alert show];
  58. }
  59. else if(self.locationField.text.length ==0) {
  60. UIAlertView *alert =[[UIAlertView alloc] initWithTitle:nil message:@"You need to enter the books new location" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
  61. [alert show];
  62. }
  63. else {
  64. MBProgressHUD *hud =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
  65. [self.nameField resignFirstResponder];
  66. [self.locationField resignFirstResponder];
  67. hud.labelText =@"Checking out";
  68. [hud show:YES];
  69. [self checkOut];
  70. [hud hide:YES];
  71. }
  72. }
  73. #pragma mark - View lifecycle
  74. -(void) setUpUI {
  75. self.nameField =[[UITextField alloc] initWithFrame:CGRectMake(50, 40, 200, 30)];
  76. self.nameField.backgroundColor=[UIColor whiteColor];
  77. self.nameField.layer.cornerRadius=8;
  78. self.nameField.delegate=self;
  79. self.nameField.placeholder =@"Your name";
  80. [self.view addSubview:self.nameField];
  81. self.locationField =[[UITextField alloc] initWithFrame:CGRectMake(50, 100, 200, 30)];
  82. self.locationField.backgroundColor =[UIColor whiteColor];
  83. self.locationField.layer.cornerRadius=8;
  84. self.locationField.delegate=self;
  85. self.locationField.placeholder =@"New location";
  86. [self.view addSubview:self.locationField];
  87. self.view.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.jpg"]];
  88. UIBarButtonItem *cancel =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];
  89. UIBarButtonItem *save =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(checkOutValidation)];
  90. self.navigationItem.leftBarButtonItem =cancel;
  91. self.navigationItem.rightBarButtonItem =save;
  92. }
  93. - (void)viewDidLoad
  94. {
  95. [super viewDidLoad];
  96. self.navigationItem.hidesBackButton = YES;
  97. self.title= @"Details";
  98. [self setUpUI];
  99. // Do any additional setup after loading the view from its nib.
  100. }
  101. -(void) removeKeypad {
  102. [self.view removeGestureRecognizer:self.tap];
  103. [self.nameField resignFirstResponder];
  104. [self.locationField resignFirstResponder];
  105. }
  106. -(void) textFieldDidBeginEditing:(UITextField *)textField {
  107. if(!self.tap) self.tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeKeypad)];
  108. [self.view addGestureRecognizer:self.tap];
  109. [self.nameField removeGestureRecognizer:self.tap];
  110. [self.locationField removeGestureRecognizer:self.tap];
  111. }
  112. - (void)viewDidUnload
  113. {
  114. [super viewDidUnload];
  115. // Release any retained subviews of the main view.
  116. // e.g. self.myOutlet = nil;
  117. }
  118. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  119. {
  120. // Return YES for supported orientations
  121. return (interfaceOrientation == UIInterfaceOrientationPortrait);
  122. }
  123. @end