-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCRViewController.m
145 lines (114 loc) · 3.6 KB
/
CRViewController.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
//
// CRViewController.m
//
// Created by Callum Ryan on 28/10/2013.
//
//
#import "CRViewController.h"
#import "CRStatsProvider.h"
#import "FMDB.h"
@interface CRViewController () {
NSString *_name;
NSString *_guid;
BOOL _gotData;
}
@property (nonatomic, strong) NSArray *data;
- (void)loadStats;
@end
@implementation CRViewController
@synthesize data = _data;
- (id)initWithName:(NSString *)passedName guid:(NSString *)passedGuid
{
if(self = [super init]){
_name = passedName;
_guid = passedGuid;
UITableView *table = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
table.delegate = self;
table.dataSource = self;
[self.view addSubview:table];
self.tableView = table;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Stats";
//Start loading spinner
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[spinner startAnimating];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:spinner];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidLoad];
[self.tableView reloadData];
[self loadStats];
}
- (void)loadStats
{
dispatch_queue_t sqlPersonQueue = dispatch_queue_create("SQL Queue", NULL);
dispatch_async(sqlPersonQueue, ^{
self.data = [CRStatsProvider statsForGuid:_guid];
dispatch_async(dispatch_get_main_queue(), ^{
_gotData = 1;
[self.tableView reloadData];
self.navigationItem.rightBarButtonItem = nil;
});
});
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
switch (indexPath.row) {
case 0:
cell.textLabel.text = @"Total";
break;
case 1:
cell.textLabel.text = @"Sent";
break;
case 2:
cell.textLabel.text = @"Received";
break;
}
if (_gotData) {
cell.detailTextLabel.text = [(NSNumber *)[self.data objectAtIndex:indexPath.row]stringValue];
} else {
cell.detailTextLabel.text = @"";
}
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return _name;
}
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
if (action == @selector(copy:)) { return YES; } //allow copying
return NO;
}
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
if (action == @selector(copy:)) {
//copy cell detail to pasteboard
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
[[UIPasteboard generalPasteboard] setString:cell.detailTextLabel.text];
}
}
@end