-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoviesViewController.m
151 lines (110 loc) · 4.23 KB
/
MoviesViewController.m
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// MoviesViewController.m
// tomatoes
//
// Created by Vinit Patwa on 1/20/14.
// Copyright (c) 2014 Vinit Patwa. All rights reserved.
//
#import "MoviesViewController.h"
#import "MovieCell.h"
#import "DetailViewController.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "Movie.h"
#import "SVProgressHUD.h"
#import "YRDropdownView.h"
@interface MoviesViewController ()
@property (nonatomic, strong) NSArray *movies;
- (void)reload;
- (void)prepareForSegue;
@end
@implementation MoviesViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self reload];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if(self) {
[self reload];
}
return self;
}
- (void)reload {
NSLog(@"In reload");
[SVProgressHUD showWithStatus:@"Updating" maskType:SVProgressHUDMaskTypeBlack];
// 2) Get a concurrent queue form the system
dispatch_queue_t concurrentQueue =
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 3) Call data from rotten APIs in background
dispatch_async(concurrentQueue, ^{
//[NSThread sleepForTimeInterval:10.0];
NSLog(@"I M in SOmewhere");
NSString *strUrl = @"http://api.rottentomatoes.com/api/public/v1.0/lists/dvds/top_rentals.json?apikey=j26mp33uc2p8ds9cdkfp64tg";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:strUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
self.movies = [responseObject objectForKey:@"movies"];
[self.tableView reloadData];
dispatch_async(dispatch_get_main_queue(), ^{
[SVProgressHUD dismiss];
});
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
[YRDropdownView showDropdownInView:self.view
title:@"Error Retrieving Titles"
detail:@"No Internet Connection."];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"#############");
[SVProgressHUD dismiss];
});
}];
});
}
-(int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.movies.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MovieCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MovieCell"];
NSDictionary *movie = self.movies[indexPath.row];
Movie *mov = [Movie alloc];
mov = [mov initWithDictionary:movie];
cell.moviesTitleLabel.text = mov->title;
cell.synopsisLabel.text = mov->synopsis;
cell.castLabel.text = mov->cast;
cell.posterUrl = mov->profilePoster;
[cell.poster setImageWithURL:[NSURL URLWithString:cell.posterUrl]];
return cell;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"];
[refresh addTarget:self action:@selector(refreshList)
forControlEvents:UIControlEventValueChanged];
self.refreshControl = refresh;
}
- (void)refreshList
{
[self reload];
[self.refreshControl performSelector:@selector(endRefreshing)];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([[segue identifier] isEqualToString:@"DetailViewControllerSegue"]){
DetailViewController *cvc = (DetailViewController *)[segue destinationViewController];
MovieCell *curCell = sender;
cvc.movie = [[Movie alloc] initWithTitle:curCell.moviesTitleLabel.text synopsis:curCell.synopsisLabel.text profilePoster:curCell.posterUrl cast:curCell.castLabel.text];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end