-
Notifications
You must be signed in to change notification settings - Fork 133
/
MCTableViewController.m
104 lines (79 loc) · 3.21 KB
/
MCTableViewController.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
//
// MCTableViewController.m
// RealmJSONDemo
//
// Created by Matthew Cheok on 27/7/14.
// Copyright (c) 2014 Matthew Cheok. All rights reserved.
//
#import "MCTableViewController.h"
#import "MCEpisode.h"
#import <Realm/Realm.h>
#import <Realm+JSON/RLMObject+JSON.h>
#import <AFNetworking.h>
#import <UIImageView+AFNetworking.h>
@interface MCTableViewController ()
@property (nonatomic, strong) RLMResults *results;
@property (nonatomic, strong) RLMNotificationToken *token;
@end
@implementation MCTableViewController
#pragma mark - Methods
- (IBAction)reloadData {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"https://www.nsscreencast.com/api/episodes.json" parameters:nil success: ^(AFHTTPRequestOperation *operation, id responseObject) {
NSArray *array = responseObject[@"episodes"];
dispatch_async(dispatch_get_main_queue(), ^{
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
NSArray *result = [MCEpisode createOrUpdateInRealm:realm withJSONArray:array];
[realm commitWriteTransaction];
NSLog(@"result: %@", result);
});
} failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
- (void)refreshData {
self.results = [[MCEpisode allObjectsInRealm:[RLMRealm defaultRealm]] sortedResultsUsingProperty:@"publishedDate" ascending:NO];
[self.tableView reloadData];
}
#pragma mark - UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.token = [[RLMRealm defaultRealm] addNotificationBlock: ^(NSString *notification, RLMRealm *realm) {
[self refreshData];
}];
[self refreshData];
[self reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.results.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSDateFormatter *dateFormatter = nil;
if (!dateFormatter) {
dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = NSDateFormatterMediumStyle;
dateFormatter.timeStyle = NSDateFormatterShortStyle;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
MCEpisode *episode = self.results[indexPath.row];
cell.textLabel.text = episode.title;
cell.detailTextLabel.text = [dateFormatter stringFromDate:episode.publishedDate];
cell.imageView.image = nil;
cell.backgroundColor = episode.episodeType == MCEpisodeTypePaid ? [UIColor colorWithRed:0.996 green:0.839 blue:0.843 alpha:1]: nil;
__weak UITableViewCell *weakCell = cell;
[cell.imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:episode.thumbnailURL]] placeholderImage:nil success: ^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
weakCell.imageView.image = image;
[weakCell setNeedsLayout];
} failure:nil];
return cell;
}
@end