2014-10-24 16:02:10 -04:00
|
|
|
//
|
|
|
|
|
// UIComputerView.m
|
2015-05-12 21:48:30 -04:00
|
|
|
// Moonlight
|
2014-10-24 16:02:10 -04:00
|
|
|
//
|
|
|
|
|
// Created by Diego Waxemberg on 10/22/14.
|
2015-05-12 21:48:30 -04:00
|
|
|
// Copyright (c) 2014 Moonlight Stream. All rights reserved.
|
2014-10-24 16:02:10 -04:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#import "UIComputerView.h"
|
|
|
|
|
|
|
|
|
|
@implementation UIComputerView {
|
2015-12-01 20:31:24 -08:00
|
|
|
TemporaryHost* _host;
|
2014-10-24 16:02:10 -04:00
|
|
|
UIButton* _hostButton;
|
|
|
|
|
UILabel* _hostLabel;
|
2015-01-01 22:30:03 -05:00
|
|
|
UILabel* _hostStatus;
|
|
|
|
|
UILabel* _hostPairState;
|
2014-10-26 02:15:53 -04:00
|
|
|
id<HostCallback> _callback;
|
|
|
|
|
CGSize _labelSize;
|
2014-10-24 16:02:10 -04:00
|
|
|
}
|
2015-01-02 23:58:59 -05:00
|
|
|
static const float REFRESH_CYCLE = 2.0f;
|
2018-08-26 16:43:45 -07:00
|
|
|
|
|
|
|
|
#if TARGET_OS_TV
|
|
|
|
|
static const int LABEL_DY = 40;
|
|
|
|
|
#else
|
2015-01-02 23:58:59 -05:00
|
|
|
static const int LABEL_DY = 20;
|
2018-08-26 16:43:45 -07:00
|
|
|
#endif
|
2014-10-24 16:02:10 -04:00
|
|
|
|
2014-10-26 02:15:53 -04:00
|
|
|
- (id) init {
|
2014-10-24 16:02:10 -04:00
|
|
|
self = [super init];
|
|
|
|
|
_hostButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
|
|
|
[_hostButton setContentEdgeInsets:UIEdgeInsetsMake(0, 4, 0, 4)];
|
2014-10-26 02:15:53 -04:00
|
|
|
[_hostButton setBackgroundImage:[UIImage imageNamed:@"Computer"] forState:UIControlStateNormal];
|
2014-10-24 16:02:10 -04:00
|
|
|
[_hostButton sizeToFit];
|
|
|
|
|
|
2014-10-26 02:15:53 -04:00
|
|
|
_hostButton.layer.shadowColor = [[UIColor blackColor] CGColor];
|
|
|
|
|
_hostButton.layer.shadowOffset = CGSizeMake(5,8);
|
|
|
|
|
_hostButton.layer.shadowOpacity = 0.7;
|
|
|
|
|
|
2014-10-24 16:02:10 -04:00
|
|
|
_hostLabel = [[UILabel alloc] init];
|
2015-01-01 22:30:03 -05:00
|
|
|
_hostStatus = [[UILabel alloc] init];
|
|
|
|
|
_hostPairState = [[UILabel alloc] init];
|
2018-08-26 00:15:56 -07:00
|
|
|
|
2014-10-26 02:15:53 -04:00
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-09 20:13:15 -05:00
|
|
|
- (id) initForAddWithCallback:(id<HostCallback>)callback {
|
|
|
|
|
self = [self init];
|
|
|
|
|
_callback = callback;
|
|
|
|
|
|
|
|
|
|
[_hostButton setBackgroundImage:[UIImage imageNamed:@"Computer"] forState:UIControlStateNormal];
|
|
|
|
|
[_hostButton setContentEdgeInsets:UIEdgeInsetsMake(0, 4, 0, 4)];
|
2018-08-26 00:15:56 -07:00
|
|
|
[_hostButton addTarget:self action:@selector(addClicked) forControlEvents:UIControlEventPrimaryActionTriggered];
|
2015-01-09 20:13:15 -05:00
|
|
|
[_hostButton sizeToFit];
|
|
|
|
|
|
|
|
|
|
[_hostLabel setText:@"Add Host"];
|
|
|
|
|
[_hostLabel sizeToFit];
|
|
|
|
|
_hostLabel.textColor = [UIColor whiteColor];
|
|
|
|
|
_hostLabel.center = CGPointMake(_hostButton.frame.origin.x + (_hostButton.frame.size.width / 2), _hostButton.frame.origin.y + _hostButton.frame.size.height + LABEL_DY);
|
|
|
|
|
|
|
|
|
|
UIImageView* addIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"AddComputerIcon"]];
|
|
|
|
|
[addIcon sizeToFit];
|
|
|
|
|
addIcon.center = CGPointMake(_hostButton.frame.origin.x + _hostButton.frame.size.width, _hostButton.frame.origin.y);
|
|
|
|
|
|
|
|
|
|
// This is required to ensure this button is the same size as the others
|
|
|
|
|
_hostPairState.text = @"None";
|
|
|
|
|
_hostStatus.text = @"None";
|
|
|
|
|
[_hostPairState sizeToFit];
|
|
|
|
|
[_hostStatus sizeToFit];
|
|
|
|
|
|
|
|
|
|
[self updateBounds];
|
|
|
|
|
[self addSubview:_hostButton];
|
|
|
|
|
[self addSubview:_hostLabel];
|
|
|
|
|
[self addSubview:addIcon];
|
|
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-01 20:31:24 -08:00
|
|
|
- (id) initWithComputer:(TemporaryHost*)host andCallback:(id<HostCallback>)callback {
|
2014-10-26 02:15:53 -04:00
|
|
|
self = [self init];
|
2015-01-01 22:30:03 -05:00
|
|
|
_host = host;
|
2014-10-26 02:15:53 -04:00
|
|
|
_callback = callback;
|
2015-01-02 23:58:59 -05:00
|
|
|
|
|
|
|
|
UILongPressGestureRecognizer* longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(hostLongClicked)];
|
|
|
|
|
[_hostButton addGestureRecognizer:longPressRecognizer];
|
2018-08-26 01:15:53 -07:00
|
|
|
[_hostButton addTarget:self action:@selector(hostClicked) forControlEvents:UIControlEventPrimaryActionTriggered];
|
2015-01-02 23:58:59 -05:00
|
|
|
|
|
|
|
|
[self updateContentsForHost:host];
|
|
|
|
|
[self updateBounds];
|
|
|
|
|
[self addSubview:_hostButton];
|
|
|
|
|
[self addSubview:_hostLabel];
|
|
|
|
|
[self addSubview:_hostStatus];
|
|
|
|
|
[self addSubview:_hostPairState];
|
|
|
|
|
[self startUpdateLoop];
|
2015-01-09 20:13:15 -05:00
|
|
|
|
2015-01-02 23:58:59 -05:00
|
|
|
return self;
|
|
|
|
|
}
|
2014-10-26 02:15:53 -04:00
|
|
|
|
2015-01-02 23:58:59 -05:00
|
|
|
- (void) updateBounds {
|
2015-01-09 01:12:59 -05:00
|
|
|
float x = FLT_MAX;
|
|
|
|
|
float y = FLT_MAX;
|
|
|
|
|
float width = 0;
|
|
|
|
|
float height;
|
|
|
|
|
|
|
|
|
|
x = MIN(x, _hostButton.frame.origin.x);
|
|
|
|
|
x = MIN(x, _hostLabel.frame.origin.x);
|
|
|
|
|
x = MIN(x, _hostStatus.frame.origin.x);
|
|
|
|
|
x = MIN(x, _hostPairState.frame.origin.x);
|
|
|
|
|
|
|
|
|
|
y = MIN(y, _hostButton.frame.origin.y);
|
|
|
|
|
y = MIN(y, _hostLabel.frame.origin.y);
|
|
|
|
|
y = MIN(y, _hostStatus.frame.origin.y);
|
|
|
|
|
y = MIN(y, _hostPairState.frame.origin.y);
|
|
|
|
|
|
|
|
|
|
width = MAX(width, _hostButton.frame.size.width);
|
|
|
|
|
width = MAX(width, _hostLabel.frame.size.width);
|
|
|
|
|
width = MAX(width, _hostStatus.frame.size.width);
|
|
|
|
|
width = MAX(width, _hostPairState.frame.size.width);
|
|
|
|
|
|
|
|
|
|
height = _hostButton.frame.size.height +
|
|
|
|
|
_hostLabel.frame.size.height +
|
|
|
|
|
_hostStatus.frame.size.height +
|
|
|
|
|
_hostPairState.frame.size.height +
|
|
|
|
|
LABEL_DY / 2;
|
2015-01-09 20:13:15 -05:00
|
|
|
|
2015-01-09 01:12:59 -05:00
|
|
|
self.bounds = CGRectMake(x, y, width, height);
|
|
|
|
|
self.frame = CGRectMake(x, y, width, height);
|
2015-01-02 23:58:59 -05:00
|
|
|
}
|
|
|
|
|
|
2015-12-01 20:31:24 -08:00
|
|
|
- (void) updateContentsForHost:(TemporaryHost*)host {
|
2015-01-02 23:58:59 -05:00
|
|
|
_hostLabel.text = _host.name;
|
2014-12-04 18:41:01 +09:00
|
|
|
_hostLabel.textColor = [UIColor whiteColor];
|
2015-01-02 23:58:59 -05:00
|
|
|
[_hostLabel sizeToFit];
|
2015-01-01 22:30:03 -05:00
|
|
|
|
|
|
|
|
switch (host.pairState) {
|
|
|
|
|
case PairStateUnknown:
|
2015-01-02 23:58:59 -05:00
|
|
|
_hostPairState.text = @"Pair State Unknown";
|
2015-01-01 22:30:03 -05:00
|
|
|
break;
|
|
|
|
|
case PairStateUnpaired:
|
2015-01-02 23:58:59 -05:00
|
|
|
_hostPairState.text = @"Not Paired";
|
2015-01-01 22:30:03 -05:00
|
|
|
break;
|
|
|
|
|
case PairStatePaired:
|
2015-01-02 23:58:59 -05:00
|
|
|
_hostPairState.text = @"Paired";
|
2015-01-01 22:30:03 -05:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
_hostPairState.textColor = [UIColor whiteColor];
|
2015-01-02 23:58:59 -05:00
|
|
|
[_hostPairState sizeToFit];
|
2015-01-01 22:30:03 -05:00
|
|
|
|
|
|
|
|
if (host.online) {
|
|
|
|
|
_hostStatus.text = @"Online";
|
|
|
|
|
_hostStatus.textColor = [UIColor greenColor];
|
|
|
|
|
} else {
|
|
|
|
|
_hostStatus.text = @"Offline";
|
|
|
|
|
_hostStatus.textColor = [UIColor grayColor];
|
|
|
|
|
}
|
|
|
|
|
[_hostStatus sizeToFit];
|
|
|
|
|
|
2015-01-09 20:13:15 -05:00
|
|
|
float x = _hostButton.frame.origin.x + _hostButton.frame.size.width / 2;
|
|
|
|
|
_hostLabel.center = CGPointMake(x, _hostButton.frame.origin.y + _hostButton.frame.size.height + LABEL_DY);
|
|
|
|
|
_hostPairState.center = CGPointMake(x, _hostLabel.center.y + LABEL_DY);
|
|
|
|
|
_hostStatus.center = CGPointMake(x, _hostPairState.center.y + LABEL_DY);
|
2014-10-26 02:15:53 -04:00
|
|
|
}
|
|
|
|
|
|
2015-01-02 23:58:59 -05:00
|
|
|
- (void) startUpdateLoop {
|
|
|
|
|
[self performSelector:@selector(updateLoop) withObject:self afterDelay:REFRESH_CYCLE];
|
2015-01-01 22:30:03 -05:00
|
|
|
}
|
|
|
|
|
|
2015-01-02 23:58:59 -05:00
|
|
|
- (void) updateLoop {
|
|
|
|
|
[self updateContentsForHost:_host];
|
|
|
|
|
[self performSelector:@selector(updateLoop) withObject:self afterDelay:REFRESH_CYCLE];
|
2014-10-26 02:15:53 -04:00
|
|
|
}
|
|
|
|
|
|
2015-01-01 23:25:09 -05:00
|
|
|
- (void) hostLongClicked {
|
2015-01-08 19:37:51 -05:00
|
|
|
[_callback hostLongClicked:_host view:self];
|
2015-01-01 23:25:09 -05:00
|
|
|
}
|
|
|
|
|
|
2014-10-26 02:15:53 -04:00
|
|
|
- (void) hostClicked {
|
2015-07-09 21:15:52 -07:00
|
|
|
[_callback hostClicked:_host view:self];
|
2014-10-26 02:15:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) addClicked {
|
|
|
|
|
[_callback addHostClicked];
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-24 16:02:10 -04:00
|
|
|
@end
|