-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableViewController.m
More file actions
197 lines (141 loc) · 6.31 KB
/
TableViewController.m
File metadata and controls
197 lines (141 loc) · 6.31 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//
// TableViewController.m
// Rotten Tomatoes
//
// Created by Jacob Cho on 2014-07-18.
// Copyright (c) 2014 Jacob Cho. All rights reserved.
//
#import "TableViewController.h"
#import "Movies.h"
#import "InTheatresCell.h"
#import "MovieDetailsViewController.h"
@interface TableViewController ()
@end
@implementation TableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self setNavBarColor];
NSString *URL = [RT_URL stringByAppendingString:API_KEY];
NSURL *rtURL = [NSURL URLWithString:URL];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data = [NSData dataWithContentsOfURL:rtURL];
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
self.movies = [NSMutableArray array];
NSArray *moviesArray = [dataDictionary objectForKeyedSubscript:@"movies"];
for (NSDictionary *moviesDictionary in moviesArray) {
Movies *movie = [Movies movieWithTitle:[moviesDictionary objectForKey:@"title"]];
// "Posters" is a JSON Object within "Title"
movie.posters = [moviesDictionary objectForKey:@"posters"];
// "Thumbnail is an Object within "Posters"
movie.thumbnail = [movie.posters objectForKey:@"thumbnail"];
movie.synopsis = [moviesDictionary objectForKey:@"synopsis"];
// "Release_dates is a JSON object within "Title"
movie.release_dates = [moviesDictionary objectForKey:@"release_dates"];
// "Theater" is an Object within "Release_dates"
movie.date = [movie.release_dates objectForKey:@"theater"];
// "Rating" is a JSON Object within "Title"
movie.rating = [moviesDictionary objectForKey:@"ratings"];
// Scores are JSON Objects within "Rating"
movie.critics_rating = [movie.rating objectForKey:@"critics_rating"];
movie.critics_score = [[movie.rating objectForKey:@"critics_score"]intValue];
movie.audience_score = [[movie.rating objectForKey:@"audience_score"]intValue];
[self.movies addObject:movie];
}
});
});
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.movies.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
InTheatresCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
// Configure the cell...
Movies *movie = [self.movies objectAtIndex:indexPath.row ];
// Set thumbnail image for cell
NSData *imageData = [NSData dataWithContentsOfURL:movie.thumbnailURL];
UIImage *image = [UIImage imageWithData:imageData];
cell.thumbnail.image = image;
// Set movie title for cell
cell.movieTitle.text = movie.title;
// Set critics score in detailTextLabel
cell.movieRating.text = [NSString stringWithFormat:@"%d",movie.critics_score];
if (movie.critics_score > 50) {
UIImage *freshImage = [UIImage imageNamed:@"flattomato"];
cell.freshImage.image = freshImage;
} else {
UIImage *freshImage = [UIImage imageNamed:@"flatrotten"];
cell.freshImage.image = freshImage;
}
return cell;
}
- (void)setNavBarColor
{
// Set navigation bar color to flat green
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:46.0/255.0 green:204.0/255.0 blue:113.0/255.0 alpha:1.0];
// Set navigation bar text color to white
NSMutableDictionary *textAttributes = [[NSMutableDictionary alloc] initWithDictionary:self.navigationController.navigationBar.titleTextAttributes];
[textAttributes setValue:[UIColor whiteColor] forKey:UITextAttributeTextColor];
self.navigationController.navigationBar.titleTextAttributes = textAttributes;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"movieDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
Movies *movie = [self.movies objectAtIndex:indexPath.row];
MovieDetailsViewController *details = (MovieDetailsViewController *)segue.destinationViewController;
details.movies = movie;
}
}
@end