1010#import < NotificationCenter/NotificationCenter.h>
1111#import " SharedUser.h"
1212#import " AccountActionButton.h"
13+ #import " BEMSimpleLineGraphView.h"
1314
14- @interface TodayViewController () <NCWidgetProviding>
15+ @interface TodayViewController () <NCWidgetProviding, BEMSimpleLineGraphDataSource, BEMSimpleLineGraphDelegate >
1516
1617@end
1718
1819@implementation TodayViewController {
1920 SharedUser* _user;
2021 UILabel* _priceLabel;
22+ BEMSimpleLineGraphView* _chart;
23+ NSArray * _data;
2124}
2225
2326- (void )viewDidLoad {
2427 [super viewDidLoad ];
2528 _user = [[SharedUser alloc ] init ];
26- self.preferredContentSize = CGSizeMake (200 , 64 );
29+ self.preferredContentSize = CGSizeMake (0 , 64 );
2730}
2831
2932- (void )viewWillAppear : (BOOL )animated {
3033 [super viewWillAppear: animated];
3134
32- UILabel* description = [[UILabel alloc ] initWithFrame: CGRectMake (0 , 0 , 200 , 30 )];
35+ for (UIView* v in self.view .subviews ) {
36+ [v removeFromSuperview ];
37+ }
38+
39+ UILabel* description = [[UILabel alloc ] initWithFrame: CGRectMake (0 , 5 , 200 , 20 )];
3340 description.text = NSLocalizedString(@" current_price" , nil );
3441 description.font = [UIFont systemFontOfSize: 13 ];
3542 description.textColor = [UIColor colorWithWhite: 0.5 alpha: 1.0 ];
3643 [self .view addSubview: description];
3744
38- _priceLabel = [[UILabel alloc ] initWithFrame: CGRectMake (0 , 27 , 200 , 30 )];
45+ _priceLabel = [[UILabel alloc ] initWithFrame: CGRectMake (0 , 30 , 200 , 20 )];
3946 _priceLabel.textColor = [UIColor whiteColor ];
4047 _priceLabel.font = [UIFont fontWithName: @" HelveticaNeue-Light" size: 24 ];
41- [self updatePriceLabel ];
4248 [self .view addSubview: _priceLabel];
4349
4450
4551 UIColor* buttonTintColor = [UIColor whiteColor ];
4652 UIColor* buttonSelectedColor = [UIColor colorWithWhite: 0.8 alpha: 1.0 ];
4753
48- NSString * sendTitle = NSLocalizedString(@" send" , nil );
49- AccountActionButton* sendButton = [[AccountActionButton alloc ] initWithTitle: sendTitle image: [UIImage imageNamed: @" send" ] selectedImage: [UIImage imageNamed: @" send_selected" ]];
50- sendButton.padding = 5 ;
51- [sendButton setTintColor: buttonTintColor];
52- [sendButton setSelectedTintColor: buttonSelectedColor];
53- CGSize sendSize = [sendTitle sizeWithAttributes: @{NSFontAttributeName :sendButton.titleLabel .font }];
54- [sendButton addTarget: self action: @selector (actionSend: ) forControlEvents: UIControlEventTouchUpInside];
55- [self .view addSubview: sendButton];
56-
57-
5854 NSString * receiveTitle = NSLocalizedString(@" receive" , nil );
5955 AccountActionButton* receiveButton = [[AccountActionButton alloc ] initWithTitle: receiveTitle image: [UIImage imageNamed: @" receive" ] selectedImage: [UIImage imageNamed: @" receive_selected" ]];
6056 receiveButton.padding = 10 ;
6157 [receiveButton setTintColor: buttonTintColor];
6258 [receiveButton setSelectedTintColor: buttonSelectedColor];
6359 CGSize receiveSize = [receiveTitle sizeWithAttributes: @{NSFontAttributeName :receiveButton.titleLabel .font }];
60+ receiveButton.frame = CGRectMake (self.view .bounds .size .width - receiveSize.width , 8 , receiveSize.width , 50 );
6461 [receiveButton addTarget: self action: @selector (actionReceive: ) forControlEvents: UIControlEventTouchUpInside];
6562 [self .view addSubview: receiveButton];
6663
6764
68- CGFloat width = sendSize.width + receiveSize.width ;
69- CGFloat padding = (180 - width) / 3 ;
70- sendButton.frame = CGRectMake (self.view .bounds .size .width - sendSize.width - padding, 7 , sendSize.width , 50 );
71- receiveButton.frame = CGRectMake (self.view .bounds .size .width - receiveSize.width - sendSize.width - padding * 2 , 12 , receiveSize.width , 50 );
72- }
73-
74- - (void )actionSend : (id )sender {
75- NSURL * url = [[NSURL alloc ] initWithScheme: @" bitstore" host: nil path: @" /send" ];
76- [self .extensionContext openURL: url completionHandler: nil ];
65+ _chart = [[BEMSimpleLineGraphView alloc ] initWithFrame: CGRectMake (self .view.bounds.size.width - receiveSize.width - 134 , 0 , 120 , 50 )];
66+ _chart.dataSource = self;
67+ _chart.delegate = self;
68+ _chart.animationGraphStyle = BEMLineAnimationNone;
69+ _chart.colorTop = [UIColor clearColor ];
70+ _chart.colorBottom = [UIColor colorWithWhite: 1.0 alpha: 0.15 ];
71+ [self .view addSubview: _chart];
72+
73+ [self updatePriceLabel ];
74+ [self updateChart ];
7775}
7876
79- - (void )actionReceive : (id )sender {
80- NSURL * url = [[NSURL alloc ] initWithScheme: @" bitstore" host: nil path: @" /receive" ];
81- [self .extensionContext openURL: url completionHandler: nil ];
77+ - (void )actionReceive : (UIButton *)receiveButton {
78+ receiveButton.selected = !receiveButton.selected ;
79+ receiveButton.highlighted = !receiveButton.selected ;
80+ if (receiveButton.selected ) {
81+ self.preferredContentSize = CGSizeMake (0 , 400 );
82+ } else {
83+ self.preferredContentSize = CGSizeMake (0 , 64 );
84+ }
8285}
8386
8487- (void )updatePriceLabel {
@@ -89,6 +92,12 @@ - (void)updatePriceLabel {
8992 }
9093}
9194
95+ - (void )updateChart {
96+ _data = @[@3.5 , @3.6 , @3.2 , @2.3 , @2.5 , @2.8 , @2.5 , @2.9 , @3.4 , @3.8 ];
97+ [_chart reloadGraph ];
98+ }
99+
100+ #pragma mark - Widget
92101- (void )widgetPerformUpdateWithCompletionHandler : (void (^)(NCUpdateResult))completionHandler {
93102 NSString * url = [NSString stringWithFormat: @" https://api.bitcoinaverage.com/ticker/global/%@ " , _user.todayCurrency];
94103 NSURLRequest * request = [[NSURLRequest alloc ] initWithURL: [NSURL URLWithString: url]];
@@ -97,24 +106,40 @@ - (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))compl
97106 NSData *data,
98107 NSError *error) {
99108
109+
100110 if (!error && data) {
101111 NSError * jsonError;
102112 NSDictionary * json = [NSJSONSerialization JSONObjectWithData: data options: 0 error: &error];
103113 if (!jsonError) {
104114 _user.cachedPrice = [json objectForKey: @" last" ];
105115 [self updatePriceLabel ];
116+ [self updateChart ];
106117 completionHandler (NCUpdateResultNewData);
107118 } else {
108119 completionHandler (NCUpdateResultFailed);
109120 }
110121 }
111122 completionHandler (NCUpdateResultFailed);
112123 }];
124+
125+
113126}
114127
115128- (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets : (UIEdgeInsets)defaultMarginInsets {
116- UIEdgeInsets newInsets = UIEdgeInsetsMake (18 , defaultMarginInsets. left - 0 , 18 , defaultMarginInsets. right );
129+ UIEdgeInsets newInsets = UIEdgeInsetsMake (12 , 18 , 18 , 18 );
117130 return newInsets;
118131}
119132
133+ #pragma mark - BEMSimpleLineGraphDataSource
134+ - (NSInteger )numberOfPointsInLineGraph : (BEMSimpleLineGraphView *)graph {
135+ return _data.count ;
136+ }
137+
138+ - (CGFloat)lineGraph : (BEMSimpleLineGraphView *)graph valueForPointAtIndex : (NSInteger )index {
139+ return [[_data objectAtIndex: index] floatValue ];
140+ }
141+
142+ #pragma mark - BEMSimpleLineGraphDelegate
143+
144+
120145@end
0 commit comments