@@ -35,15 +35,17 @@ @implementation ReceiveViewController {
3535 UIButton* _addButton;
3636 UILabel* _amountLabel;
3737 UIImageView* _qrCode;
38+
39+ SRWebSocket* _ws;
3840}
3941
4042- (id )init {
4143 if (self = [super init ]) {
4244 self.title = l10n (@" receive" );
4345 _address = [AddressHelper instance ].defaultAddress ;
44- SRWebSocket* ws = [[SRWebSocket alloc ] initWithURL: [NSURL URLWithString: @" wss://ws.blockchain.info/inv" ]];
45- ws .delegate = self;
46- [ws open ];
46+ _ws = [[SRWebSocket alloc ] initWithURL: [NSURL URLWithString: @" wss://ws.blockchain.info/inv" ]];
47+ _ws .delegate = self;
48+ [_ws open ];
4749 [[ExchangeHelper instance ] addExchangeListener: self ];
4850 }
4951 return self;
@@ -105,28 +107,6 @@ - (void)close:(id)sender {
105107 [self dismissViewControllerAnimated: YES completion: nil ];
106108}
107109
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-
130110#pragma mark - ExchangeListener
131111- (void )exchangeChanged : (Exchange *)exchange {
132112 _exchange = exchange;
@@ -153,14 +133,31 @@ - (void)amountSelected:(BTCSatoshi)satoshi {
153133
154134#pragma mark - SRWebSocketDelegate
155135- (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\" }" ;
136+ NSString * cmd = [NSString stringWithFormat: @" {\" op\" :\" addr_sub\" , \" addr\" :\" %@ \" }" , _address.address];
159137 [webSocket send: cmd];
160138}
161139
162- - (void )webSocket : (SRWebSocket *)webSocket didReceiveMessage : (id )message {
163- NSLog (@" did receive data: %@ " , message);
140+ - (void )webSocket : (SRWebSocket *)webSocket didReceiveMessage : (NSString *)message {
141+ NSDictionary * json = [NSJSONSerialization JSONObjectWithData: [message dataUsingEncoding: NSUTF8StringEncoding] options: 0 error: nil ];
142+ NSArray * outputs = [[json objectForKey: @" x" ] objectForKey: @" out" ];
143+ BTCSatoshi total = 0 ;
144+ for (NSDictionary * output in outputs) {
145+ if ([[output objectForKey: @" addr" ] isEqualToString: _address.address]) {
146+ total += [[output objectForKey: @" value" ] longValue ];
147+ }
148+ }
149+ if (_satoshi == 0 || _satoshi == total) {
150+ NSString * amount = [_exchange.unit valueForSatoshi: total];
151+ UIBAlertView* successAlert = [[UIBAlertView alloc ] initWithTitle: l10n (@" success" ) message: [NSString stringWithFormat: l10n (@" push_message" ), amount] cancelButtonTitle: l10n (@" okay" ) otherButtonTitles: nil ];
152+ [successAlert showWithDismissHandler: ^(NSInteger selectedIndex, NSString *selectedTitle, BOOL didCancel) {
153+ [self close: self ];
154+ }];
155+ }
156+ }
157+
158+ - (void )dealloc {
159+ [_ws close ];
160+ _ws = nil ;
164161}
165162
166163@end
0 commit comments