Mikhail Kuznetsov avatar Mikhail Kuznetsov committed adf10d6

loading show info;

Comments (0)

Files changed (10)

MyShows.xcodeproj/project.pbxproj

 		18660901144B150A0050B8E1 /* Reachability.m in Sources */ = {isa = PBXBuildFile; fileRef = 186608F8144B150A0050B8E1 /* Reachability.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
 		186C58CC145C39F000B0E53E /* TvShow.m in Sources */ = {isa = PBXBuildFile; fileRef = 186C58CB145C39F000B0E53E /* TvShow.m */; };
 		186C58CF145C464500B0E53E /* tv_show_placeholder.png in Resources */ = {isa = PBXBuildFile; fileRef = 186C58CE145C464500B0E53E /* tv_show_placeholder.png */; };
+		189812A41466A47C00D49096 /* ShowDetailViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 189812A31466A47C00D49096 /* ShowDetailViewController.m */; };
 		18C1DFBB1453136F00687C0B /* LoginViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 18C1DFBA1453136F00687C0B /* LoginViewController.m */; };
 		18C1DFC1145315B500687C0B /* ShowsListTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 18C1DFC0145315B500687C0B /* ShowsListTableViewController.m */; };
 		18EE6BAC1452D55F00A4D065 /* NSString+Hash.m in Sources */ = {isa = PBXBuildFile; fileRef = 18EE6BAB1452D55F00A4D065 /* NSString+Hash.m */; };
 		186C58CA145C39F000B0E53E /* TvShow.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TvShow.h; path = Models/TvShow.h; sourceTree = "<group>"; };
 		186C58CB145C39F000B0E53E /* TvShow.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TvShow.m; path = Models/TvShow.m; sourceTree = "<group>"; };
 		186C58CE145C464500B0E53E /* tv_show_placeholder.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = tv_show_placeholder.png; sourceTree = "<group>"; };
+		189812A21466A47C00D49096 /* ShowDetailViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ShowDetailViewController.h; sourceTree = "<group>"; };
+		189812A31466A47C00D49096 /* ShowDetailViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ShowDetailViewController.m; sourceTree = "<group>"; };
 		18C1DFB91453136F00687C0B /* LoginViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LoginViewController.h; sourceTree = "<group>"; };
 		18C1DFBA1453136F00687C0B /* LoginViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LoginViewController.m; sourceTree = "<group>"; };
 		18C1DFBF145315B500687C0B /* ShowsListTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ShowsListTableViewController.h; sourceTree = "<group>"; };
 				18C1DFBA1453136F00687C0B /* LoginViewController.m */,
 				18C1DFBF145315B500687C0B /* ShowsListTableViewController.h */,
 				18C1DFC0145315B500687C0B /* ShowsListTableViewController.m */,
+				189812A21466A47C00D49096 /* ShowDetailViewController.h */,
+				189812A31466A47C00D49096 /* ShowDetailViewController.m */,
 			);
 			path = Controllers;
 			sourceTree = "<group>";
 				1829F1931454720F00B98707 /* SVProgressHUD.m in Sources */,
 				186C58CC145C39F000B0E53E /* TvShow.m in Sources */,
 				184043E8145D9A6300DE0A94 /* TvShowCell.m in Sources */,
+				189812A41466A47C00D49096 /* ShowDetailViewController.m in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

MyShows/Classes/MyShowsApi.h

 
 @property(retain, nonatomic) id responder;
 
--(void) initUrlsMap;
--(NSString *) dictToQueryString:(NSDictionary *) params;
--(void) sendRequest:(NSString *) resource queryParams: (NSDictionary *) queryParams;
+- (void)initUrlsMap;
+- (NSString *)dictToQueryString:(NSDictionary *) params;
+- (NSString *)getUrlForResource:(NSString *)resource;
+- (void)sendRequest:(NSString *)resource queryParams:(NSDictionary *)queryParams;
+- (void)sendRequest:(NSURL *)url resource:(NSString *)resource queryParams:(NSDictionary *)queryParams;
+- (void)sendRequest:(NSString *)resource path:(NSString *)path queryParams:(NSDictionary *)queryParams;
 
 @end

MyShows/Classes/MyShowsApi.m

     urlsMap = [[NSDictionary alloc] initWithObjectsAndKeys:
                @"profile/login", @"login",
                @"profile/shows/", @"shows",
+               @"shows/%@", @"showDetails",
                nil];
 }
 
+- (NSString *) getUrlForResource:(NSString *)resource {
+    if ([urlsMap objectForKey:resource]) {
+        return (NSString *)[urlsMap objectForKey:resource];
+    }
+    return nil;
+}
+
 - (NSString *) dictToQueryString:(NSDictionary *) params 
 {
     NSString *result = @"";
     return [result stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 }
 
-- (void) sendRequest:(NSString *)resource queryParams: (NSDictionary *) queryParams {
-    NSLog(@"Sending %@ request with params %@", resource, queryParams);
-    
-    NSURL *url = [NSURL URLWithString:[[NSString alloc] initWithFormat:@"%@%@?%@",
-                                       baseUrl, 
-                                       [urlsMap objectForKey:resource], 
-                                       [self dictToQueryString:queryParams]
-                                       ]
-                  ];
+- (void)sendRequest:(NSURL *)url resource:(NSString *)resource queryParams:(NSDictionary *)queryParams {
     NSLog(@"Requesting url is %@", url);
     SEL requestSelector = NSSelectorFromString([[NSString alloc] initWithFormat:@"request%@Finished:", [resource capitalizedString]]);
     
     [request startSynchronous];
 }
 
+- (void)sendRequest:(NSString *)resource queryParams:(NSDictionary *)queryParams {
+    NSLog(@"Sending %@ request with params %@", resource, queryParams);
+        
+    NSURL *url = [NSURL URLWithString:[[NSString alloc] initWithFormat:@"%@%@?%@",
+            baseUrl,
+            [urlsMap objectForKey:resource], 
+            [self dictToQueryString:queryParams]
+        ]
+    ];
+    
+    [self sendRequest:url resource:resource queryParams:queryParams];
+}
+
+- (void)sendRequest:(NSString *)resource path:(NSString *)path queryParams:(NSDictionary *)queryParams {
+    NSLog(@"Sending %@ request with params %@", resource, queryParams);
+    
+    NSURL *url = [NSURL URLWithString:[[NSString alloc] initWithFormat:@"%@%@?%@",
+                                       baseUrl,
+                                       path, 
+                                       [self dictToQueryString:queryParams]
+                                       ]
+                  ];
+    
+    [self sendRequest:url resource:resource queryParams:queryParams];
+}
+
 - (void) requestFinished:(ASIHTTPRequest *)request {
     NSLog(@"Request finished with %d status code", [request responseStatusCode]);
     
         }
     }
 }
+
+- (void) requestShowdetailsFinished:(ASIHTTPRequest *)request {
+    [self requestFinished:request];
+    
+    if ([request responseStatusCode] == 200) 
+    {
+        NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
+                                     [[NSNumber alloc] initWithInt:[request responseStatusCode]], @"status",
+                                     nil];
+        
+        parser = [[SBJsonParser alloc] init];
+        id parsed = [parser objectWithString:responseString];
+        if (parsed) {
+            //NSLog(@"Shows loaded %@", parsed);
+            NSLog(@"Shows loaded");
+            [data setObject:[parsed copy] forKey:@"data"];
+        }
+        else {
+            NSLog(@"Can't parse data %@", parser.error);
+        }
+
+        NSLog(@"Checking for selector");    
+        if ([[self responder] respondsToSelector:@selector(didRequestSucceed:)]) {
+            NSLog(@"Responding to selector didRequestSucceed");
+            [[self responder] didRequestSucceed:data];
+        }
+    }
+}
 @end

MyShows/Controllers/ShowDetailViewController.h

+//
+//  ShowDetailViewController.h
+//  MyShows
+//
+//  Created by Mikhail Kuznetsov on 06.11.11.
+//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+#import "MyShowsApi.h"
+#import "ImageManager.h"
+
+@class TvShow;
+
+@interface ShowDetailViewController : UIViewController <ApiDelegate, AsyncImageDelegate> {
+    MyShowsApi *api;
+    TvShow *show;
+}
+
+@property (nonatomic, retain) NSNumber *showId;
+@property (retain, nonatomic) IBOutlet UILabel *enTitle;
+
+@end

MyShows/Controllers/ShowDetailViewController.m

+//
+//  ShowDetailViewController.m
+//  MyShows
+//
+//  Created by Mikhail Kuznetsov on 06.11.11.
+//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
+//
+
+#import "ShowDetailViewController.h"
+#import "SVProgressHUD.h"
+#import "TvShow.h"
+
+@implementation ShowDetailViewController
+
+@synthesize showId;
+@synthesize enTitle;
+
+- (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
+{
+}
+*/
+
+- (void)viewDidLoad
+{
+    [super viewDidLoad];
+    
+    api = [[MyShowsApi alloc] init];
+    [api setResponder:self];
+}
+
+- (void)viewDidUnload
+{
+    [super viewDidUnload];
+    // Release any retained subviews of the main view.
+    // e.g. self.myOutlet = nil;
+}
+
+- (void)viewDidAppear:(BOOL)animated {
+    [super viewDidAppear:animated];
+    
+    [SVProgressHUD showInView:self.view];
+
+    [api sendRequest:@"showDetails" 
+        path:[[NSString alloc] initWithFormat:[api getUrlForResource:@"showDetails"],
+                                          [self.showId stringValue]
+                                          ] 
+         queryParams:nil];
+}
+
+- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
+{
+    // Return YES for supported orientations
+    return (interfaceOrientation == UIInterfaceOrientationPortrait);
+}
+
+#pragma mark - ApiDelegate
+
+- (void)didRequestSucceed:(NSDictionary *)data {
+    NSLog(@"Returned to view after successful request");
+    int status = [(NSNumber *)[data objectForKey:@"status"] intValue];
+    
+    if (status == 200 ) {
+        [SVProgressHUD dismiss];
+        
+        NSDictionary *loaded_show = [data objectForKey:@"data"];
+                
+        if ([loaded_show isKindOfClass:[NSDictionary class]]){
+            show = [[TvShow alloc] initWithDictionary:loaded_show];
+            enTitle.text = show.enTitle;
+        }        
+    }
+    else {
+        if (status == 401) {
+            [SVProgressHUD dismissWithError:@"Log in, please"];
+        }
+        else {
+            [SVProgressHUD dismiss];
+        }
+    }
+}
+
+- (void)didRequestFailed:(NSDictionary *)data {
+    
+}
+
+@end

MyShows/Controllers/ShowsListTableViewController.h

     IBOutlet UITableView *showsTable;
 }
 
+@property (nonatomic, retain)NSNumber *detailId;
+
 - (UIImage *)loadImage:(TvShow *)currentShow indexPath:(NSIndexPath *) indexPath;
 - (UIImage *)resizeImage:(UIImage *)image;
 - (void)loadImagesForOnscreenRows;

MyShows/Controllers/ShowsListTableViewController.m

 #import "SVProgressHUD.h"
 #import "TvShow.h"
 #import "TvShowCell.h"
+#import "ShowDetailViewController.h"
 
 
 @implementation ShowsListTableViewController
 
+@synthesize detailId;
+
 - (id)initWithStyle:(UITableViewStyle)style
 {
     self = [super initWithStyle:style];
     [self loadImagesForOnscreenRows];
 }
 
--(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
-    [self performSegueWithIdentifier:@"show_details" sender:nil];
+- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
+    self.detailId = [(TvShow *)[shows objectAtIndex:[indexPath row]] showId];
+    [self performSegueWithIdentifier:@"show_detail" sender:nil];
+}
+
+- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
+    if ([segue.identifier isEqualToString:@"show_detail"]) {
+        [(ShowDetailViewController *)segue.destinationViewController setShowId:detailId];
+    }
+    else {
+        [super prepareForSegue:segue sender:sender];
+    }
 }
 
 @end

MyShows/Models/TvShow.h

 
 @interface TvShow : NSObject {
     //JSON fields
+    NSNumber *_showId;
     NSString *_ruTitle;
     NSString *_enTitle;
     NSString *_imageUrl;
     UIImage *_rating;
 }
 
+@property (nonatomic, retain) NSNumber *showId;
 @property (nonatomic, retain) NSString *ruTitle;
 @property (nonatomic, retain) NSString *enTitle;
 @property (nonatomic, retain) NSString *imageUrl;

MyShows/Models/TvShow.m

 
 @implementation TvShow
 
+@synthesize showId = _showId;
 @synthesize ruTitle = _ruTitle;
 @synthesize enTitle = _enTitle;
 @synthesize imageUrl = _imageUrl;
 - (NSDictionary *)fieldMap {
     // object field - json field
     return [NSDictionary dictionaryWithObjectsAndKeys:
+            @"showId", @"showId",
             @"ruTitle", @"ruTitle",
             @"enTitle", @"title",
             @"imageUrl", @"image",

MyShows/en.lproj/MainStoryboard.storyboard

         <scene sceneID="ggw-Vt-6aG">
             <objects>
                 <placeholder placeholderIdentifier="IBFirstResponder" id="KYm-g1-kv8" userLabel="First Responder" sceneMemberID="firstResponder"/>
-                <viewController id="N1f-SA-PHD" sceneMemberID="viewController">
+                <viewController id="N1f-SA-PHD" customClass="ShowDetailViewController" sceneMemberID="viewController">
                     <view key="view" contentMode="scaleToFill" id="QJM-qk-s9q">
                         <rect key="frame" x="0.0" y="64" width="320" height="367"/>
                         <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                         <subviews>
-                            <label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" text="Label" lineBreakMode="tailTruncation" minimumFontSize="10" id="E92-Vp-Cv4">
-                                <rect key="frame" x="111" y="90" width="42" height="21"/>
-                                <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
-                                <fontDescription key="fontDescription" type="system" pointSize="17"/>
-                                <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
-                                <nil key="highlightedColor"/>
-                            </label>
+                            <scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" showsHorizontalScrollIndicator="NO" id="UnJ-C9-HXM">
+                                <rect key="frame" x="0.0" y="0.0" width="320" height="367"/>
+                                <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
+                                <subviews>
+                                    <label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" text="Label" lineBreakMode="tailTruncation" minimumFontSize="10" id="VuU-t8-fIY">
+                                        <rect key="frame" x="139" y="20" width="42" height="21"/>
+                                        <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
+                                        <fontDescription key="fontDescription" type="system" pointSize="17"/>
+                                        <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
+                                        <nil key="highlightedColor"/>
+                                    </label>
+                                </subviews>
+                            </scrollView>
                         </subviews>
                         <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
                     </view>
                     <navigationItem key="navigationItem" id="pIn-1H-XqX"/>
+                    <connections>
+                        <outlet property="enTitle" destination="VuU-t8-fIY" id="TI8-LX-il3"/>
+                    </connections>
                 </viewController>
             </objects>
             <point key="canvasLocation" x="548" y="796"/>
                 <relationship kind="outlet" name="password" candidateClass="UITextField"/>
             </relationships>
         </class>
+        <class className="ShowDetailViewController" superclassName="UIViewController">
+            <source key="sourceIdentifier" type="project" relativePath="./Classes/ShowDetailViewController.h"/>
+            <relationships>
+                <relationship kind="outlet" name="enTitle" candidateClass="UILabel"/>
+            </relationships>
+        </class>
         <class className="ShowsListTableViewController" superclassName="UITableViewController">
             <source key="sourceIdentifier" type="project" relativePath="./Classes/ShowsListTableViewController.h"/>
             <relationships>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.