88
99#import " ReceiveViewController.h"
1010#import " Address.h"
11- #import " AddressListener.h"
1211#import " QRHelper.h"
1312#import " Transaction.h"
1413#import " UIBAlertView.h"
2120#import " Unit.h"
2221#import " ReceiveAmountViewController.h"
2322#import " ReceiveAmountDelegate.h"
23+ #import < SocketRocket/SRWebSocket.h>
2424
2525static int QR_WIDTH = 240 ;
2626
27- @interface ReceiveViewController () <AddressListener, ExchangeListener, ReceiveAmountDelegate>
27+ @interface ReceiveViewController () <ExchangeListener, ReceiveAmountDelegate, SRWebSocketDelegate >
2828@end
2929
3030@implementation ReceiveViewController {
3131 Address* _address;
3232 Exchange* _exchange;
33- BTCSatoshi _lastBalance;
34- BOOL _secStop; // this is in case the stopUpdate call didn't work
3533 BTCSatoshi _satoshi;
3634
3735 UIButton* _addButton;
38- UILabel* _amountLabel;
36+ UILabel* _amountLabel;
3937 UIImageView* _qrCode;
4038}
4139
4240- (id )init {
4341 if (self = [super init ]) {
4442 self.title = l10n (@" receive" );
45- Address* a = [AddressHelper instance ].defaultAddress ;
46- _lastBalance = a. total ;
47- [a addAddressListener: self ] ;
48- [a startUpdate: 2 ]; // check every two seconds
49- [[ExchangeHelper instance ] addExchangeListener: self ];
43+ _address = [AddressHelper instance ].defaultAddress ;
44+ SRWebSocket* ws = [[SRWebSocket alloc ] initWithURL: [ NSURL URLWithString: @" wss://ws.blockchain.info/inv " ]] ;
45+ ws. delegate = self;
46+ [ws open ];
47+ [[ExchangeHelper instance ] addExchangeListener: self ];
5048 }
5149 return self;
5250}
@@ -103,32 +101,38 @@ - (void)actionAdd:(id)sender {
103101 [self .navigationController pushViewController: vc animated: YES ];
104102}
105103
106- - (void )addressChanged : (Address *)address {
107- _address = address;
108- if (_address.transactions .count > 0 ) {
109- if (_address.total != _lastBalance) {
110- [_address stopUpdate ];
111- NSString * amount = [_exchange.unit valueForSatoshi: (_address.total - _lastBalance)];
112-
113- if (!_secStop) {
114- if (_satoshi == 0 || _satoshi == (_address.total - _lastBalance)) {
115- UIBAlertView* successAlert = [[UIBAlertView alloc ] initWithTitle: l10n (@" success" ) message: [NSString stringWithFormat: l10n (@" push_message" ), amount] cancelButtonTitle: l10n (@" okay" ) otherButtonTitles: nil ];
116- [successAlert showWithDismissHandler: ^(NSInteger selectedIndex, NSString *selectedTitle, BOOL didCancel) {
117- [self close: self ];
118- }];
119- _secStop = YES ;
120- }
121- } else {
122- [_address stopUpdate ];
123- }
124- }
125- }
104+ - (void )close : (id )sender {
105+ [self dismissViewControllerAnimated: YES completion: nil ];
126106}
127107
108+ // - (void)addressChanged:(Address *)address {
109+ // _address = address;
110+ // if (_address.transactions.count > 0) {
111+ // if (_address.total != _lastBalance) {
112+ // [_address stopUpdate];
113+ // NSString* amount = [_exchange.unit valueForSatoshi:(_address.total - _lastBalance)];
114+ //
115+ // if (!_secStop) {
116+ // if (_satoshi == 0 || _satoshi == (_address.total - _lastBalance)) {
117+ // UIBAlertView* successAlert = [[UIBAlertView alloc] initWithTitle:l10n(@"success") message:[NSString stringWithFormat:l10n(@"push_message"), amount] cancelButtonTitle:l10n(@"okay") otherButtonTitles:nil];
118+ // [successAlert showWithDismissHandler:^(NSInteger selectedIndex, NSString *selectedTitle, BOOL didCancel) {
119+ // [self close:self];
120+ // }];
121+ // _secStop = YES;
122+ // }
123+ // } else {
124+ // [_address stopUpdate];
125+ // }
126+ // }
127+ // }
128+ // }
129+
130+ #pragma mark - ExchangeListener
128131- (void )exchangeChanged : (Exchange *)exchange {
129132 _exchange = exchange;
130133}
131134
135+ #pragma mark - ReceiveAmountDelegate
132136- (void )amountSelected : (BTCSatoshi)satoshi {
133137 _satoshi = satoshi;
134138 _addButton.hidden = _satoshi > 0 ;
@@ -147,8 +151,16 @@ - (void)amountSelected:(BTCSatoshi)satoshi {
147151 _qrCode.image = qrCodeImage;
148152}
149153
150- - (void )close : (id )sender {
151- [self dismissViewControllerAnimated: YES completion: nil ];
154+ #pragma mark - SRWebSocketDelegate
155+ - (void )webSocketDidOpen : (SRWebSocket *)webSocket {
156+ NSLog (@" websocket open" );
157+ // NSString* cmd = [NSString stringWithFormat:@"{\"op\":\"addr_sub\", \"addr\":\"%@\"}", _address.address];
158+ NSString * cmd = @" {\" op\" :\" unconfirmed_sub\" }" ;
159+ [webSocket send: cmd];
160+ }
161+
162+ - (void )webSocket : (SRWebSocket *)webSocket didReceiveMessage : (id )message {
163+ NSLog (@" did receive data: %@ " , message);
152164}
153165
154166@end
0 commit comments