-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathTodayViewController.m
More file actions
262 lines (223 loc) · 10.3 KB
/
Copy pathTodayViewController.m
File metadata and controls
262 lines (223 loc) · 10.3 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//
// TodayViewController.m
// BitStoreToday
//
// Created by Dylan Marriott on 22/09/14.
// Copyright (c) 2014 Dylan Marriott. All rights reserved.
//
#import "TodayViewController.h"
#import <NotificationCenter/NotificationCenter.h>
#import "SharedUser.h"
#import "AccountActionButton.h"
#import "BEMSimpleLineGraphView.h"
#import "QRHelper.h"
@interface TodayViewController () <NCWidgetProviding, BEMSimpleLineGraphDataSource, BEMSimpleLineGraphDelegate>
@end
@implementation TodayViewController {
SharedUser* _user;
UILabel* _priceLabel;
UIImageView* _qrImageView;
BEMSimpleLineGraphView* _miniChart;
BEMSimpleLineGraphView* _bigChart;
UIView* _bigChartContainer;
BOOL _chartOpen;
AccountActionButton* _receiveButton;
}
- (void)viewDidLoad {
[super viewDidLoad];
_user = [[SharedUser alloc] init];
self.preferredContentSize = CGSizeMake(0, 64);
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
for (UIView* v in self.view.subviews) {
[v removeFromSuperview];
}
UILabel* description = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 200, 20)];
description.text = NSLocalizedString(@"current_price", nil);
description.font = [UIFont systemFontOfSize:13];
description.textColor = [UIColor colorWithWhite:0.5 alpha:1.0];
[self.view addSubview:description];
_priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 200, 20)];
_priceLabel.textColor = [UIColor whiteColor];
_priceLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:24];
[self.view addSubview:_priceLabel];
UIColor* buttonTintColor = [UIColor whiteColor];
UIColor* buttonSelectedColor = [UIColor colorWithWhite:0.8 alpha:1.0];
NSString* receiveTitle = NSLocalizedString(@"receive", nil);
_receiveButton = [[AccountActionButton alloc] initWithTitle:receiveTitle image:[UIImage imageNamed:@"receive"] selectedImage:[UIImage imageNamed:@"receive_selected"]];
_receiveButton.padding = 10;
[_receiveButton setTintColor:buttonTintColor];
[_receiveButton setSelectedTintColor:buttonSelectedColor];
CGSize receiveSize = [receiveTitle sizeWithAttributes:@{NSFontAttributeName:_receiveButton.titleLabel.font}];
_receiveButton.frame = CGRectMake(self.view.bounds.size.width - receiveSize.width, 8, receiveSize.width, 50);
[_receiveButton addTarget:self action:@selector(actionReceive:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_receiveButton];
UIImage* qrImage = [QRHelper qrcode:_user.address withDimension:self.view.bounds.size.width];
_qrImageView = [[UIImageView alloc] initWithImage:qrImage];
_qrImageView.frame = CGRectMake(0, 64, qrImage.size.width, qrImage.size.height);
_qrImageView.backgroundColor = [UIColor yellowColor];
_qrImageView.alpha = 0.0;
[self.view addSubview:_qrImageView];
_miniChart = [[BEMSimpleLineGraphView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width - receiveSize.width - 134, 0, 120, 50)];
_miniChart.dataSource = self;
_miniChart.delegate = self;
_miniChart.animationGraphStyle = BEMLineAnimationNone;
_miniChart.colorTop = [UIColor clearColor];
_miniChart.colorBottom = [UIColor colorWithWhite:1.0 alpha:0.15];
[_miniChart addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionGraph:)]];
[self.view addSubview:_miniChart];
_bigChartContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, 140)];
_bigChartContainer.alpha = 0.0;
_bigChart = [[BEMSimpleLineGraphView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 140)];
_bigChart.dataSource = self;
_bigChart.delegate = self;
_bigChart.animationGraphStyle = BEMLineAnimationNone;
_bigChart.colorTop = [UIColor clearColor];
_bigChart.enableReferenceYAxisLines = YES;
_bigChart.enableYAxisLabel = YES;
_bigChart.colorYaxisLabel = [UIColor whiteColor];
_bigChart.alphaBackgroundYaxis = 0.5;
[_bigChartContainer addSubview:_bigChart];
UILabel* chartLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 115, self.view.bounds.size.width - 20, 20)];
chartLabel.text = @"24h BTC / USD";
chartLabel.textAlignment = NSTextAlignmentCenter;
chartLabel.font = [UIFont systemFontOfSize:12];
chartLabel.textColor = [UIColor whiteColor];
chartLabel.alpha = 0.6;
[_bigChartContainer addSubview:chartLabel];
[self.view addSubview:_bigChartContainer];
[self updatePriceLabel];
[self updateChart];
}
- (void)actionReceive:(UIButton *)receiveButton {
if (_chartOpen) {
[self toggleGraph];
}
[self toggleReceive];
if (_receiveButton.selected) {
self.preferredContentSize = CGSizeMake(0, self.view.frame.size.width + 64);
} else {
self.preferredContentSize = CGSizeMake(0, 64);
}
}
- (void)toggleReceive {
BOOL selected = !_receiveButton.selected;
_receiveButton.selected = selected;
_receiveButton.highlighted = selected;
[UIView animateWithDuration:0.3 animations:^() {
if (selected) {
_qrImageView.alpha = 1.0;
} else {
_qrImageView.alpha = 0.0;
}
}];
}
- (void)actionGraph:(id)sender {
if (_receiveButton.selected) {
[self toggleReceive];
}
[self toggleGraph];
if (_chartOpen) {
self.preferredContentSize = CGSizeMake(0, 150 + 64);
} else {
self.preferredContentSize = CGSizeMake(0, 64);
}
}
- (void)toggleGraph {
_chartOpen = !_chartOpen;
if (_chartOpen) {
_miniChart.colorBottom = [UIColor colorWithWhite:1.0 alpha:0.45];
} else {
_miniChart.colorBottom = [UIColor colorWithWhite:1.0 alpha:0.15];
}
[UIView animateWithDuration:0.3 animations:^() {
if (_chartOpen) {
_bigChartContainer.alpha = 1.0;
} else {
_bigChartContainer.alpha = 0.0;
}
}];
}
- (void)fetchCurrent {
NSString* url = [NSString stringWithFormat:@"https://api.bitcoinaverage.com/ticker/global/%@", _user.todayCurrency];
NSURLRequest* request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error) {
if (!error && data) {
NSError* jsonError;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (!jsonError) {
_user.cachedPrice = [json objectForKey:@"last"];
[self updatePriceLabel];
} else {
}
}
}];
}
- (void)fetchHistory {
NSString* url = @"https://api.bitcoinaverage.com/history/USD/per_minute_24h_sliding_window.csv";
NSURLRequest* request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error) {
if (!error && data) {
NSString* result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSArray* rows = [result componentsSeparatedByString:@"\r\n"];
int i = 0;
NSMutableArray* history = [NSMutableArray array];
NSNumberFormatter* f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
for (NSString* row in rows) {
if (i > 0 && i % 20 == 0) {
NSArray* cols = [row componentsSeparatedByString:@","];
if (cols.count == 2) {
NSNumber* value = [f numberFromString:cols[1]];
if (value) {
[history addObject:value];
}
}
}
i++;
}
_user.cachedHistory = history;
[self updateChart];
}
}];
}
- (void)updatePriceLabel {
if (_user.cachedPrice) {
_priceLabel.text = [NSString stringWithFormat:@"%.0f %@", [_user.cachedPrice floatValue], _user.todayCurrency];
} else {
_priceLabel.text = NSLocalizedString(@"loading", nil);
}
}
- (void)updateChart {
[_miniChart reloadGraph];
[_bigChart reloadGraph];
}
#pragma mark - Widget
- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {
[self fetchCurrent];
[self fetchHistory];
completionHandler(NCUpdateResultNewData);
}
- (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets:(UIEdgeInsets)defaultMarginInsets {
UIEdgeInsets newInsets = UIEdgeInsetsMake(12, 18, 18, 18);
return newInsets;
}
#pragma mark - BEMSimpleLineGraphDataSource
- (NSInteger)numberOfPointsInLineGraph:(BEMSimpleLineGraphView *)graph {
return _user.cachedHistory.count;
}
- (CGFloat)lineGraph:(BEMSimpleLineGraphView *)graph valueForPointAtIndex:(NSInteger)index {
return [[_user.cachedHistory objectAtIndex:index] floatValue];
}
#pragma mark - BEMSimpleLineGraphDelegate
- (CGFloat)staticPaddingForLineGraph:(BEMSimpleLineGraphView *)graph {
return 10;
}
@end