From ca2b418b00840ab4ff73286a80135b809965988e Mon Sep 17 00:00:00 2001 From: Casey Perkins Date: Mon, 16 Apr 2018 09:32:07 -0500 Subject: [PATCH] Implemented requirements for Vault programming test --- VaultCodeTest/Base.lproj/Main.storyboard | 58 ++++++++++++++++++------ VaultCodeTest/Info.plist | 6 +++ VaultCodeTest/ViewController.h | 9 ++++ VaultCodeTest/ViewController.m | 38 ++++++++++++++++ VaultCodeTestTests/VaultCodeTestTests.m | 5 +- 5 files changed, 100 insertions(+), 16 deletions(-) diff --git a/VaultCodeTest/Base.lproj/Main.storyboard b/VaultCodeTest/Base.lproj/Main.storyboard index 1709965..9915463 100644 --- a/VaultCodeTest/Base.lproj/Main.storyboard +++ b/VaultCodeTest/Base.lproj/Main.storyboard @@ -18,32 +18,41 @@ - - - + + + + + - - - + + - - - - + + - + + + + + + + + + + + + + + + + + + @@ -65,3 +92,4 @@ + diff --git a/VaultCodeTest/Info.plist b/VaultCodeTest/Info.plist index 16be3b6..43971f1 100644 --- a/VaultCodeTest/Info.plist +++ b/VaultCodeTest/Info.plist @@ -41,5 +41,11 @@ UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight + NSAppTransportSecurity + + NSAllowsArbitraryLoads + + + diff --git a/VaultCodeTest/ViewController.h b/VaultCodeTest/ViewController.h index f06a99d..4896dcb 100644 --- a/VaultCodeTest/ViewController.h +++ b/VaultCodeTest/ViewController.h @@ -12,9 +12,18 @@ - (IBAction)processString:(id)sender; +- (IBAction)getCat; +- (NSString *)reverseString:(NSString *)string; + @property (weak, nonatomic) IBOutlet UITextField *textSrc; @property (weak, nonatomic) IBOutlet UITextField *textDest; @property (weak, nonatomic) IBOutlet UIImageView *imageView; +@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageViewBottomConstraint; +@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageViewLeadingConstraint; +@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageViewTrailingConstraint; + + @end + diff --git a/VaultCodeTest/ViewController.m b/VaultCodeTest/ViewController.m index e2fc0be..948d53b 100644 --- a/VaultCodeTest/ViewController.m +++ b/VaultCodeTest/ViewController.m @@ -7,12 +7,15 @@ // #import "ViewController.h" +#import +#import "AFImageDownloader.h" @interface ViewController () @end @implementation ViewController +@synthesize textSrc, textDest, imageView; - (void)viewDidLoad { [super viewDidLoad]; @@ -27,5 +30,40 @@ - (void)didReceiveMemoryWarning { - (IBAction)processString:(id)sender { // Reverse string in textSrc and assign to textDest here + textDest.text = [self reverseString:textSrc.text]; } + +- (IBAction)getCat { + NSDate *now = [NSDate date]; + NSString *catImageStr = [NSString stringWithFormat:@"http://thecatapi.com/api/images/get?format=src&type=gif&timeindex=%f", now.timeIntervalSince1970]; // providing a time index ensures we get a new kitty each time the button is pressed, not one from a cached request. + + AFImageDownloader *downloader = [AFImageDownloader defaultInstance]; + NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: catImageStr]]; + + [downloader downloadImageForURLRequest:request success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull responseObject) { + [imageView setImage:responseObject]; + + } failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) { + [self alertForCatDownloadError]; + }]; +} + +- (NSString *)reverseString:(NSString *)string { + NSMutableString *reversedText = [[NSMutableString alloc] initWithCapacity:string.length]; + for (NSInteger i = string.length - 1; i > -1; i--) { + [reversedText appendString:[string substringWithRange:NSMakeRange(i, 1)]]; + } + return reversedText; +} + +- (void)alertForCatDownloadError { + UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"Problem getting kitty" message:@"Sorry, we were unable to retrieve a kitty from thecatapi.com site. Please try again in a moment." preferredStyle:UIAlertControllerStyleAlert]; + [actionSheet addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { + [self dismissViewControllerAnimated:YES completion:^{ + }]; + }]]; + [self presentViewController:actionSheet animated:YES completion:nil]; +} + @end + diff --git a/VaultCodeTestTests/VaultCodeTestTests.m b/VaultCodeTestTests/VaultCodeTestTests.m index 2dcd33b..7b769e6 100644 --- a/VaultCodeTestTests/VaultCodeTestTests.m +++ b/VaultCodeTestTests/VaultCodeTestTests.m @@ -27,9 +27,12 @@ - (void)testReverseString { NSString *src = @"abc123"; NSString *result = @"321cba"; - NSString *test = @""; // Assign result of string reversal function here + ViewController *controller = [ViewController new]; + NSString *test = [controller reverseString:src]; // Assign result of string reversal function here + XCTAssertTrue([result isEqualToString:test]); } @end +