-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRootViewController.m
More file actions
170 lines (135 loc) · 4.32 KB
/
RootViewController.m
File metadata and controls
170 lines (135 loc) · 4.32 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
//
// RootViewController.m
// iReader-RSS
//
// Created by Julien Sarazin on 11/09/12.
// Copyright (c) 2012 Julien Sarazin. All rights reserved.
//
#import "RootViewController.h"
#import "RssItemCell.h"
#import "DetailViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)fetchRssItems
{
if(self.rssLoader == nil)
{
self.rssLoader = [[RssLoader alloc] init];
self.rssLoader.delegate = self;
}
self.selector = [RSSChannelSelector sharedRSSChannel];
self.selector.delegate = self;
[self.selector setCurrentChannelFromString:Key_LeMondeTechnologies];
}
#pragma mark TableViewController life cycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = @"Articles";
self.rssItems = nil;
self.rssLoader = nil;
#warning TODO: Test if the internet connection is reachable.
self.connected = YES;
self.rssItems = [[NSMutableArray alloc] init];
[self performSelector:@selector(fetchRssItems) withObject:nil];
[self.tableView reloadData];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.rssLoader.loaded)
return self.rssItems.count;
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.rssLoader.loaded == NO)
{
NSLog(@"Prout");
return [self getLoadingTableCellWithTableView:tableView];
}
RssItemCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"RssItemCell" owner:self options:nil] objectAtIndex:0];
RssItem* item = [self.rssItems objectAtIndex: indexPath.row];
cell.title.text = item.title;
cell.description.text = item.description;
[cell.enclosureLoader startAnimating];
cell.enclosureLoader.hidden = NO;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue,
^{
UIImage *enclosure_rawImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:item.enclosure]]];
[cell.enclosure setImage:enclosure_rawImage];
[cell.enclosureLoader stopAnimating];
cell.enclosureLoader.hidden = YES;
});
return cell;
}
- (UITableViewCell *)getLoadingTableCellWithTableView:(UITableView *)tableView
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"LoadingCell" owner:self options:nil];
UITableViewCell *cell = [topLevelObjects lastObject];
cell.textLabel.text = @"Loading...";
UIActivityIndicatorView* activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[activity startAnimating];
[cell setAccessoryView: activity];
return cell;
}
#pragma mark - Table view orientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"RssItemDetail" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"RssItemDetail"])
{
DetailViewController *controller = [segue destinationViewController];
controller.rssItem = [self.rssItems objectAtIndex:self.tableView.indexPathForSelectedRow.row];
}
}
#pragma mark - RssLoader delegate
- (void)rssItemsUpdated:(NSMutableArray *)items
{
self.rssItems = items;
[self.tableView reloadData];
}
- (void)updateFailedWithError:(NSError *)error
{
[[[UIAlertView alloc] initWithTitle:@"Error" message:[error description] delegate:nil cancelButtonTitle:@"Ok"otherButtonTitles:nil] show];
}
#pragma mark - RSSChannelSelector delegate
- (void)currentChannelChanged
{
[self.rssLoader loadWithURL:[self.selector getCurrentChannel]];
}
@end