Skip to content

Commit fd919bd

Browse files
committed
Use websocket instead of polling (receive screen) - work in progress
1 parent c521c3d commit fd919bd

4 files changed

Lines changed: 58 additions & 34 deletions

File tree

BitStore.xcodeproj/project.pbxproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888
9A8D3C50195E248C00E88A1C /* ChangeCurrencyViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A8D3C4E195E248C00E88A1C /* ChangeCurrencyViewController.m */; };
8989
9A8D3C53195E258600E88A1C /* SettingsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A8D3C52195E258600E88A1C /* SettingsViewController.m */; };
9090
9A97A0BD19D058530023C9AD /* piwiktracker.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 9A97A0BB19D058530023C9AD /* piwiktracker.xcdatamodeld */; };
91+
9A97A0C319D05BBF0023C9AD /* libicucore.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 9A97A0C219D05BBF0023C9AD /* libicucore.dylib */; };
92+
9A97A0C519D05C820023C9AD /* CFNetwork.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9A97A0C419D05C820023C9AD /* CFNetwork.framework */; };
9193
9AA466BC1972D82A00A7B562 /* Color.m in Sources */ = {isa = PBXBuildFile; fileRef = 9AA466BB1972D82A00A7B562 /* Color.m */; };
9294
9AA6C44C1986FAFA0035A430 /* HTAutocompleteTextField.m in Sources */ = {isa = PBXBuildFile; fileRef = 9AA6C44B1986FAFA0035A430 /* HTAutocompleteTextField.m */; };
9395
9AB7EAF019903DEA00B9A1BA /* API.m in Sources */ = {isa = PBXBuildFile; fileRef = 9AB7EAEF19903DEA00B9A1BA /* API.m */; };
@@ -278,6 +280,8 @@
278280
9A8D3C51195E258600E88A1C /* SettingsViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SettingsViewController.h; sourceTree = "<group>"; };
279281
9A8D3C52195E258600E88A1C /* SettingsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SettingsViewController.m; sourceTree = "<group>"; };
280282
9A97A0BC19D058530023C9AD /* piwiktracker.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = piwiktracker.xcdatamodel; sourceTree = "<group>"; };
283+
9A97A0C219D05BBF0023C9AD /* libicucore.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libicucore.dylib; path = usr/lib/libicucore.dylib; sourceTree = SDKROOT; };
284+
9A97A0C419D05C820023C9AD /* CFNetwork.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CFNetwork.framework; path = System/Library/Frameworks/CFNetwork.framework; sourceTree = SDKROOT; };
281285
9AA466BA1972D82A00A7B562 /* Color.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Color.h; sourceTree = "<group>"; };
282286
9AA466BB1972D82A00A7B562 /* Color.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Color.m; sourceTree = "<group>"; };
283287
9AA6C44A1986FAFA0035A430 /* HTAutocompleteTextField.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HTAutocompleteTextField.h; sourceTree = "<group>"; };
@@ -323,6 +327,9 @@
323327
isa = PBXFrameworksBuildPhase;
324328
buildActionMask = 2147483647;
325329
files = (
330+
0358466B194F09A900521709 /* Foundation.framework in Frameworks */,
331+
9A97A0C519D05C820023C9AD /* CFNetwork.framework in Frameworks */,
332+
9A97A0C319D05BBF0023C9AD /* libicucore.dylib in Frameworks */,
326333
03101B57198FCE7700E7FC5A /* CoreTelephony.framework in Frameworks */,
327334
9ABC07311983191E00EC87A3 /* MobileCoreServices.framework in Frameworks */,
328335
03101B55198FCE6600E7FC5A /* Ringcaptcha.framework in Frameworks */,
@@ -331,7 +338,6 @@
331338
03F2E9B1194F0FD7007BE9A6 /* Security.framework in Frameworks */,
332339
0358466D194F09A900521709 /* CoreGraphics.framework in Frameworks */,
333340
0358466F194F09A900521709 /* UIKit.framework in Frameworks */,
334-
0358466B194F09A900521709 /* Foundation.framework in Frameworks */,
335341
A423E0B434094809ABF9EF1D /* libPods.a in Frameworks */,
336342
);
337343
runOnlyForDeploymentPostprocessing = 0;
@@ -382,6 +388,8 @@
382388
03584669194F09A900521709 /* Frameworks */ = {
383389
isa = PBXGroup;
384390
children = (
391+
9A97A0C419D05C820023C9AD /* CFNetwork.framework */,
392+
9A97A0C219D05BBF0023C9AD /* libicucore.dylib */,
385393
03AC8C801995130000E901E2 /* libPods-CoreBitcoin.a */,
386394
03101B56198FCE7700E7FC5A /* CoreTelephony.framework */,
387395
9ABC07301983191E00EC87A3 /* MobileCoreServices.framework */,

BitStore/Controller/Account/Receive/ReceiveViewController.m

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
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"
@@ -21,32 +20,31 @@
2120
#import "Unit.h"
2221
#import "ReceiveAmountViewController.h"
2322
#import "ReceiveAmountDelegate.h"
23+
#import <SocketRocket/SRWebSocket.h>
2424

2525
static 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

Podfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ pod 'MBProgressHUD'
88
pod 'NSDate+TimeAgo'
99
pod 'Stripe'
1010
pod 'PiwikTracker'
11-
pod 'DMPasscode'
11+
pod 'DMPasscode'
12+
pod 'SocketRocket'

Podfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ PODS:
1111
- PaymentKit (1.0.5)
1212
- PiwikTracker (2.5.1):
1313
- AFNetworking (= 1.3.2)
14+
- SocketRocket (0.3.1-beta2)
1415
- Stripe (1.1.4):
1516
- PaymentKit (~> 1.0.4)
1617

@@ -22,6 +23,7 @@ DEPENDENCIES:
2223
- MBProgressHUD
2324
- NSDate+TimeAgo
2425
- PiwikTracker
26+
- SocketRocket
2527
- Stripe
2628

2729
EXTERNAL SOURCES:
@@ -39,6 +41,7 @@ SPEC CHECKSUMS:
3941
OpenSSL-Universal: 9c112508615ed33bb35f730eed820e93175a9dec
4042
PaymentKit: 14b54835f57ae9e2bf5628140fcd67001740cba2
4143
PiwikTracker: fd93aedfb17707ed487f2e69fd6af91723e2374b
44+
SocketRocket: 79a68ae186063c65e1fe6f638b1ae2927aa5f304
4245
Stripe: a0ed1c0675a3c36f6e8d575820cfec1dc9335f53
4346

4447
COCOAPODS: 0.33.1

0 commit comments

Comments
 (0)