/
SVSourcesViewController.m
136 lines (119 loc) · 4.89 KB
/
SVSourcesViewController.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
//
// SVSourcesViewController.m
// Scanvine
//
// Created by Jon Evans on 2013-09-22.
// Copyright (c) 2013 scanvine.com. All rights reserved.
//
#import "SVSourcesViewController.h"
#import "SVSourceTableViewCell.h"
#import "SVMasterViewController.h"
#import "SVHTTPClient.h"
#import "SVUtil.h"
#import "GAI.h"
#import "GAIFields.h"
#import "GAIDictionaryBuilder.h"
@interface SVSourcesViewController ()
@end
@implementation SVSourcesViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
[self.tableView registerClass:SVSourceTableViewCell.class forCellReuseIdentifier:@"Source"];
self.title = @"Sources";
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *file = [directory stringByAppendingPathComponent:@"latest_sources"];
self.json = [NSDictionary dictionaryWithContentsOfFile:file];
[self reload];
[self fetch];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
id tracker = [[GAI sharedInstance] defaultTracker];
[tracker set:kGAIScreenName value:@"Sources"];
[tracker send:[[GAIDictionaryBuilder createAppView] build]];
}
- (void)reload {
if (!self.json)
return;
self.sources = self.json[@"sources"];
[self.tableView reloadData];
}
- (void)fetch {
[self showLoading];
[[SVHTTPClient sharedHTTPClient] getPath:[SVUtil apiPathFor:@"sources"] parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject){
NSLog(@"API source fetch success");
[self stopLoading];
NSError* JSONError = nil;
self.json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:&JSONError];
if(JSONError || !self.json){
NSLog(@"Error: %@", JSONError);
[SVUtil showAlertWithTitle:@"Server error" andMessage:@"Unable to parse server source list. Sorry! Please try again later."];
return;
}
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *file = [directory stringByAppendingPathComponent:@"latest_sources"];
BOOL success = [self.json writeToFile:file atomically:YES];
if (success) {
NSURL *fileURL = [NSURL fileURLWithPath:file];
[fileURL setResourceValue:[NSNumber numberWithBool:YES] forKey: NSURLIsExcludedFromBackupKey error: nil];
[self reload];
}
else {
NSLog(@"Unable to write latest sources to file");
[SVUtil showAlertWithTitle:@"Cache error" andMessage:@"Unable to cache server source list for offline use."];
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"API fetch failed: %@",[error localizedDescription]);
[SVUtil showAlertWithTitle:@"Fetch error" andMessage:@"Unable to fetch source data from server. Using cached list, if any. Sorry! Please try again later."];
[self stopLoading];
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.sources ? self.sources.count+1 : 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Source";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (indexPath.row==0) {
cell.textLabel.text = @"Source";
cell.detailTextLabel.text = @"Average Score";
}
else {
NSUInteger row = indexPath.row-1;
cell.textLabel.text = [self.sources objectAtIndex:row][@"name"];
cell.detailTextLabel.text = [[self.sources objectAtIndex:row][@"average_score"] stringValue];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row==0) {
sortByName = !sortByName;
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:(sortByName ? @"name" : @"average_score")
ascending:(sortByName ? YES : NO)];
self.sources = [self.sources sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
[self.tableView reloadData];
}
else {
SVMasterViewController *mvc = [[SVMasterViewController alloc] initWithNibName:nil bundle:nil];
mvc.source = [self.sources objectAtIndex:indexPath.row-1];
[self.navigationController pushViewController:mvc animated:YES];
}
}
@end