The host views now auto refresh and removed unused method

This commit is contained in:
Diego Waxemberg
2015-01-02 23:58:59 -05:00
parent d134ff83d6
commit 57bf748877
2 changed files with 40 additions and 28 deletions
+1 -1
View File
@@ -21,5 +21,5 @@
- (id) initWithComputer:(Host*)host andCallback:(id<HostCallback>)callback; - (id) initWithComputer:(Host*)host andCallback:(id<HostCallback>)callback;
- (id) initForAddWithCallback:(id<HostCallback>)callback; - (id) initForAddWithCallback:(id<HostCallback>)callback;
- (NSString*) online;
@end @end
+39 -27
View File
@@ -17,7 +17,8 @@
id<HostCallback> _callback; id<HostCallback> _callback;
CGSize _labelSize; CGSize _labelSize;
} }
static int LABEL_DY = 20; static const float REFRESH_CYCLE = 2.0f;
static const int LABEL_DY = 20;
- (id) init { - (id) init {
self = [super init]; self = [super init];
@@ -40,25 +41,46 @@ static int LABEL_DY = 20;
self = [self init]; self = [self init];
_host = host; _host = host;
_callback = callback; _callback = callback;
[_hostLabel setText:_host.name];
[_hostLabel sizeToFit];
_hostLabel.textColor = [UIColor whiteColor];
UILongPressGestureRecognizer* longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(hostLongClicked)];
[_hostButton addGestureRecognizer:longPressRecognizer];
[_hostButton addTarget:self action:@selector(hostClicked) forControlEvents:UIControlEventTouchUpInside];
[self updateContentsForHost:host];
[self updateBounds];
[self addSubview:_hostButton];
[self addSubview:_hostLabel];
[self addSubview:_hostStatus];
[self addSubview:_hostPairState];
[self startUpdateLoop];
return self;
}
- (void) updateBounds {
float x = _hostButton.frame.origin.x < _hostLabel.frame.origin.x ? _hostButton.frame.origin.x : _hostLabel.frame.origin.x;
float y = _hostButton.frame.origin.y < _hostLabel.frame.origin.y ? _hostButton.frame.origin.y : _hostLabel.frame.origin.y;
self.bounds = CGRectMake(x , y, _hostButton.frame.size.width > _hostLabel.frame.size.width ? _hostButton.frame.size.width : _hostLabel.frame.size.width, _hostButton.frame.size.height + _hostLabel.frame.size.height + LABEL_DY / 2);
self.frame = CGRectMake(x , y, _hostButton.frame.size.width > _hostLabel.frame.size.width ? _hostButton.frame.size.width : _hostLabel.frame.size.width, _hostButton.frame.size.height + _hostLabel.frame.size.height + LABEL_DY / 2);
}
- (void) updateContentsForHost:(Host*)host {
_hostLabel.text = _host.name;
_hostLabel.textColor = [UIColor whiteColor];
[_hostLabel sizeToFit];
switch (host.pairState) { switch (host.pairState) {
case PairStateUnknown: case PairStateUnknown:
[_hostPairState setText:@"Pair State Unknown"]; _hostPairState.text = @"Pair State Unknown";
break; break;
case PairStateUnpaired: case PairStateUnpaired:
[_hostPairState setText:@"Not Paired"]; _hostPairState.text = @"Not Paired";
break; break;
case PairStatePaired: case PairStatePaired:
[_hostPairState setText:@"Paired"]; _hostPairState.text = @"Paired";
break; break;
} }
[_hostPairState sizeToFit];
_hostPairState.textColor = [UIColor whiteColor]; _hostPairState.textColor = [UIColor whiteColor];
[_hostPairState sizeToFit];
if (host.online) { if (host.online) {
_hostStatus.text = @"Online"; _hostStatus.text = @"Online";
@@ -69,31 +91,19 @@ static int LABEL_DY = 20;
} }
[_hostStatus sizeToFit]; [_hostStatus sizeToFit];
[_hostButton addTarget:self action:@selector(hostClicked) forControlEvents:UIControlEventTouchUpInside];
_hostLabel.center = CGPointMake(_hostButton.frame.origin.x + (_hostButton.frame.size.width / 2), _hostButton.frame.origin.y + _hostButton.frame.size.height + LABEL_DY); _hostLabel.center = CGPointMake(_hostButton.frame.origin.x + (_hostButton.frame.size.width / 2), _hostButton.frame.origin.y + _hostButton.frame.size.height + LABEL_DY);
_hostPairState.center = CGPointMake(_hostLabel.center.x, _hostLabel.center.y + LABEL_DY); _hostPairState.center = CGPointMake(_hostLabel.center.x, _hostLabel.center.y + LABEL_DY);
_hostStatus.center = CGPointMake(_hostPairState.center.x, _hostPairState.center.y + LABEL_DY); _hostStatus.center = CGPointMake(_hostPairState.center.x, _hostPairState.center.y + LABEL_DY);
UILongPressGestureRecognizer* longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(hostLongClicked)];
[_hostButton addGestureRecognizer:longPressRecognizer];
[self updateBounds];
[self addSubview:_hostButton];
[self addSubview:_hostLabel];
[self addSubview:_hostStatus];
[self addSubview:_hostPairState];
return self;
} }
- (NSString *)online { - (void) startUpdateLoop {
return _hostStatus.text; [self performSelector:@selector(updateLoop) withObject:self afterDelay:REFRESH_CYCLE];
} }
- (void) updateBounds { - (void) updateLoop {
float x = _hostButton.frame.origin.x < _hostLabel.frame.origin.x ? _hostButton.frame.origin.x : _hostLabel.frame.origin.x; [self updateContentsForHost:_host];
float y = _hostButton.frame.origin.y < _hostLabel.frame.origin.y ? _hostButton.frame.origin.y : _hostLabel.frame.origin.y; [self performSelector:@selector(updateLoop) withObject:self afterDelay:REFRESH_CYCLE];
self.bounds = CGRectMake(x , y, _hostButton.frame.size.width > _hostLabel.frame.size.width ? _hostButton.frame.size.width : _hostLabel.frame.size.width, _hostButton.frame.size.height + _hostLabel.frame.size.height + LABEL_DY / 2);
self.frame = CGRectMake(x , y, _hostButton.frame.size.width > _hostLabel.frame.size.width ? _hostButton.frame.size.width : _hostLabel.frame.size.width, _hostButton.frame.size.height + _hostLabel.frame.size.height + LABEL_DY / 2);
} }
- (id) initForAddWithCallback:(id<HostCallback>)callback { - (id) initForAddWithCallback:(id<HostCallback>)callback {
@@ -121,6 +131,8 @@ static int LABEL_DY = 20;
return self; return self;
} }
- (void) hostLongClicked { - (void) hostLongClicked {
[_callback hostLongClicked:_host]; [_callback hostLongClicked:_host];
} }