@@ -21,8 +21,10 @@ @implementation TodayViewController {
2121 SharedUser* _user;
2222 UILabel* _priceLabel;
2323 UIImageView* _qrImageView;
24- BEMSimpleLineGraphView* _chart;
25- NSArray * _data;
24+ BEMSimpleLineGraphView* _miniChart;
25+ BEMSimpleLineGraphView* _bigChart;
26+ UIView* _bigChartContainer;
27+ BOOL _chartOpen;
2628}
2729
2830- (void )viewDidLoad {
@@ -72,13 +74,35 @@ - (void)viewWillAppear:(BOOL)animated {
7274 [self .view addSubview: _qrImageView];
7375
7476
75- _chart = [[BEMSimpleLineGraphView alloc ] initWithFrame: CGRectMake (self .view.bounds.size.width - receiveSize.width - 134 , 0 , 120 , 50 )];
76- _chart.dataSource = self;
77- _chart.delegate = self;
78- _chart.animationGraphStyle = BEMLineAnimationNone;
79- _chart.colorTop = [UIColor clearColor ];
80- _chart.colorBottom = [UIColor colorWithWhite: 1.0 alpha: 0.15 ];
81- [self .view addSubview: _chart];
77+ _miniChart = [[BEMSimpleLineGraphView alloc ] initWithFrame: CGRectMake (self .view.bounds.size.width - receiveSize.width - 134 , 0 , 120 , 50 )];
78+ _miniChart.dataSource = self;
79+ _miniChart.delegate = self;
80+ _miniChart.animationGraphStyle = BEMLineAnimationNone;
81+ _miniChart.colorTop = [UIColor clearColor ];
82+ _miniChart.colorBottom = [UIColor colorWithWhite: 1.0 alpha: 0.15 ];
83+ [_miniChart addGestureRecognizer: [[UITapGestureRecognizer alloc ] initWithTarget: self action: @selector (actionGraph: )]];
84+ [self .view addSubview: _miniChart];
85+
86+ _bigChartContainer = [[UIView alloc ] initWithFrame: CGRectMake (0 , 64 , self .view.bounds.size.width, 140 )];
87+ _bigChartContainer.alpha = 0.0 ;
88+ _bigChart = [[BEMSimpleLineGraphView alloc ] initWithFrame: CGRectMake (0 , 0 , self .view.bounds.size.width, 140 )];
89+ _bigChart.dataSource = self;
90+ _bigChart.delegate = self;
91+ _bigChart.animationGraphStyle = BEMLineAnimationNone;
92+ _bigChart.colorTop = [UIColor clearColor ];
93+ _bigChart.enableReferenceYAxisLines = YES ;
94+ _bigChart.enableYAxisLabel = YES ;
95+ _bigChart.colorYaxisLabel = [UIColor whiteColor ];
96+ _bigChart.alphaBackgroundYaxis = 0.5 ;
97+ [_bigChartContainer addSubview: _bigChart];
98+ UILabel* chartLabel = [[UILabel alloc ] initWithFrame: CGRectMake (20 , 110 , self .view.bounds.size.width - 20 , 20 )];
99+ chartLabel.text = @" 24h BTC / USD" ;
100+ chartLabel.textAlignment = NSTextAlignmentCenter;
101+ chartLabel.font = [UIFont systemFontOfSize: 12 ];
102+ chartLabel.textColor = [UIColor whiteColor ];
103+ chartLabel.alpha = 0.6 ;
104+ [_bigChartContainer addSubview: chartLabel];
105+ [self .view addSubview: _bigChartContainer];
82106
83107 [self updatePriceLabel ];
84108 [self updateChart ];
@@ -104,21 +128,26 @@ - (void)actionReceive:(UIButton *)receiveButton {
104128 }
105129}
106130
107- - (void )updatePriceLabel {
108- if (_user.cachedPrice ) {
109- _priceLabel.text = [NSString stringWithFormat: @" %.0f %@ " , [_user.cachedPrice floatValue ], _user.todayCurrency];
131+ - (void )actionGraph : (id )sender {
132+ _chartOpen = !_chartOpen;
133+ if (_chartOpen) {
134+ _miniChart.colorBottom = [UIColor colorWithWhite: 1.0 alpha: 0.45 ];
135+ self.preferredContentSize = CGSizeMake (0 , 150 + 64 );
110136 } else {
111- _priceLabel.text = NSLocalizedString(@" loading" , nil );
137+ _miniChart.colorBottom = [UIColor colorWithWhite: 1.0 alpha: 0.15 ];
138+ self.preferredContentSize = CGSizeMake (0 , 64 );
112139 }
140+
141+ [UIView animateWithDuration: 0.3 animations: ^() {
142+ if (_chartOpen) {
143+ _bigChartContainer.alpha = 1.0 ;
144+ } else {
145+ _bigChartContainer.alpha = 0.0 ;
146+ }
147+ }];
113148}
114149
115- - (void )updateChart {
116- _data = @[@3.5 , @3.6 , @3.2 , @2.3 , @2.5 , @2.8 , @2.5 , @2.9 , @3.4 , @3.8 ];
117- [_chart reloadGraph ];
118- }
119-
120- #pragma mark - Widget
121- - (void )widgetPerformUpdateWithCompletionHandler : (void (^)(NCUpdateResult))completionHandler {
150+ - (void )fetchCurrent {
122151 NSString * url = [NSString stringWithFormat: @" https://api.bitcoinaverage.com/ticker/global/%@ " , _user.todayCurrency];
123152 NSURLRequest * request = [[NSURLRequest alloc ] initWithURL: [NSURL URLWithString: url]];
124153 [NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue ]
@@ -133,16 +162,64 @@ - (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))compl
133162 if (!jsonError) {
134163 _user.cachedPrice = [json objectForKey: @" last" ];
135164 [self updatePriceLabel ];
136- [self updateChart ];
137- completionHandler (NCUpdateResultNewData);
138165 } else {
139- completionHandler (NCUpdateResultFailed);
140166 }
141167 }
142- completionHandler (NCUpdateResultFailed);
143168 }];
144-
145-
169+ }
170+
171+ - (void )fetchHistory {
172+ NSString * url = @" https://api.bitcoinaverage.com/history/USD/per_minute_24h_sliding_window.csv" ;
173+ NSURLRequest * request = [[NSURLRequest alloc ] initWithURL: [NSURL URLWithString: url]];
174+ [NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue ]
175+ completionHandler: ^(NSURLResponse *response,
176+ NSData *data,
177+ NSError *error) {
178+
179+ if (!error && data) {
180+ NSString * result = [[NSString alloc ] initWithData: data encoding: NSUTF8StringEncoding];
181+ NSArray * rows = [result componentsSeparatedByString: @" \r\n " ];
182+ int i = 0 ;
183+ NSMutableArray * history = [NSMutableArray array ];
184+ NSNumberFormatter * f = [[NSNumberFormatter alloc ] init ];
185+ [f setNumberStyle: NSNumberFormatterDecimalStyle];
186+ for (NSString * row in rows) {
187+ if (i > 0 && i % 20 == 0 ) {
188+ NSArray * cols = [row componentsSeparatedByString: @" ," ];
189+ if (cols.count == 2 ) {
190+ NSNumber * value = [f numberFromString: cols[1 ]];
191+ if (value) {
192+ [history addObject: value];
193+ }
194+
195+ }
196+ }
197+ i++;
198+ }
199+ _user.cachedHistory = history;
200+ [self updateChart ];
201+ }
202+ }];
203+ }
204+
205+ - (void )updatePriceLabel {
206+ if (_user.cachedPrice ) {
207+ _priceLabel.text = [NSString stringWithFormat: @" %.0f %@ " , [_user.cachedPrice floatValue ], _user.todayCurrency];
208+ } else {
209+ _priceLabel.text = NSLocalizedString(@" loading" , nil );
210+ }
211+ }
212+
213+ - (void )updateChart {
214+ [_miniChart reloadGraph ];
215+ [_bigChart reloadGraph ];
216+ }
217+
218+ #pragma mark - Widget
219+ - (void )widgetPerformUpdateWithCompletionHandler : (void (^)(NCUpdateResult))completionHandler {
220+ [self fetchCurrent ];
221+ [self fetchHistory ];
222+ completionHandler (NCUpdateResultNewData);
146223}
147224
148225- (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets : (UIEdgeInsets)defaultMarginInsets {
@@ -152,14 +229,16 @@ - (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets:(UIEdgeInsets)defaultM
152229
153230#pragma mark - BEMSimpleLineGraphDataSource
154231- (NSInteger )numberOfPointsInLineGraph : (BEMSimpleLineGraphView *)graph {
155- return _data .count ;
232+ return _user. cachedHistory .count ;
156233}
157234
158235- (CGFloat)lineGraph : (BEMSimpleLineGraphView *)graph valueForPointAtIndex : (NSInteger )index {
159- return [[_data objectAtIndex: index] floatValue ];
236+ return [[_user.cachedHistory objectAtIndex: index] floatValue ];
160237}
161238
162239#pragma mark - BEMSimpleLineGraphDelegate
163-
240+ - (CGFloat)staticPaddingForLineGraph : (BEMSimpleLineGraphView *)graph {
241+ return 10 ;
242+ }
164243
165244@end
0 commit comments