VLCWiFiUploadTableViewCell.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*****************************************************************************
  2. * VLCWiFiUploadTableViewCell.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. #import "VLCWiFiUploadTableViewCell.h"
  13. #import "Reachability.h"
  14. #import "VLCHTTPUploaderController.h"
  15. @interface VLCWiFiUploadTableViewCell()
  16. @property (nonatomic, strong) UILabel *titleLabel;
  17. @property (nonatomic, strong) UILabel *uploadAddressLabel;
  18. @property (nonatomic, strong) UIButton *serverOnButton;
  19. @property (nonatomic, strong) Reachability *reachability;
  20. @end
  21. @implementation VLCWiFiUploadTableViewCell
  22. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  23. {
  24. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  25. if (self) {
  26. [self setupCell];
  27. [self setupConstraints];
  28. [self updateHTTPServerAddress];
  29. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netReachabilityChanged:) name:kReachabilityChangedNotification object:nil];
  30. }
  31. return self;
  32. }
  33. - (void)dealloc
  34. {
  35. [self.reachability stopNotifier];
  36. [[NSNotificationCenter defaultCenter] removeObserver:self];
  37. }
  38. - (void)setupCell
  39. {
  40. self.reachability = [Reachability reachabilityForLocalWiFi];
  41. [self.reachability startNotifier];
  42. self.titleLabel = [UILabel new];
  43. self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
  44. [self.contentView addSubview:self.titleLabel];
  45. self.titleLabel.text = NSLocalizedString(@"WEBINTF_TITLE", nil);
  46. self.titleLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
  47. self.titleLabel.shadowColor = [UIColor VLCDarkTextShadowColor];
  48. self.titleLabel.textColor = [UIColor whiteColor];
  49. self.titleLabel.font = [UIFont systemFontOfSize:16.0];
  50. self.titleLabel.superview.backgroundColor = [UIColor colorWithRed:(43.0f/255.0f) green:(43.0f/255.0f) blue:(43.0f/255.0f) alpha:1.0f];
  51. [self.titleLabel sizeToFit];
  52. self.clipsToBounds = YES;
  53. self.backgroundColor = [UIColor clearColor];
  54. self.selectionStyle = UITableViewCellSelectionStyleNone;
  55. self.uploadAddressLabel = [UILabel new];
  56. self.uploadAddressLabel.translatesAutoresizingMaskIntoConstraints = NO;
  57. self.uploadAddressLabel.numberOfLines = 0;
  58. [self.contentView addSubview:self.uploadAddressLabel];
  59. self.uploadAddressLabel.text = NSLocalizedString(@"HTTP_UPLOAD_SERVER_OFF", nil);
  60. self.uploadAddressLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
  61. self.uploadAddressLabel.shadowColor = [UIColor VLCDarkTextShadowColor];
  62. self.uploadAddressLabel.textColor = [UIColor whiteColor];
  63. self.uploadAddressLabel.font = [UIFont systemFontOfSize:12.0];
  64. [self.uploadAddressLabel sizeToFit];
  65. self.serverOnButton = [UIButton buttonWithType:UIButtonTypeCustom];
  66. self.serverOnButton.translatesAutoresizingMaskIntoConstraints = NO;
  67. [self.serverOnButton addTarget:self action:@selector(toggleHTTPServer) forControlEvents:UIControlEventTouchUpInside];
  68. [self.serverOnButton setImage:[UIImage imageNamed:@"WiFiUp"] forState:UIControlStateDisabled];
  69. [self.contentView addSubview:self.serverOnButton];
  70. UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  71. topLine.backgroundColor = [UIColor colorWithRed:(16.0f/255.0f) green:(16.0f/255.0f) blue:(16.0f/255.0f) alpha:1.0f];
  72. [self.contentView addSubview:topLine];
  73. UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 50.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  74. bottomLine.backgroundColor = [UIColor colorWithRed:(23.0f/255.0f) green:(23.0f/255.0f) blue:(23.0f/255.0f) alpha:1.0f];
  75. [self.contentView addSubview:bottomLine];
  76. }
  77. - (void)setupConstraints
  78. {
  79. NSDictionary *dict = NSDictionaryOfVariableBindings(_titleLabel, _uploadAddressLabel, _serverOnButton);
  80. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_serverOnButton(50)][_titleLabel]" options:0 metrics:0 views:dict]];
  81. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_serverOnButton(50)][_uploadAddressLabel]" options:0 metrics:0 views:dict]];
  82. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_serverOnButton(50)]|" options:0 metrics:0 views:dict]];
  83. [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
  84. [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:_uploadAddressLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_titleLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
  85. }
  86. - (void)netReachabilityChanged
  87. {
  88. [self updateHTTPServerAddress];
  89. }
  90. - (void)updateHTTPServerAddress
  91. {
  92. [self.serverOnButton setImage:[UIImage imageNamed:@"WifiUp"] forState:UIControlStateNormal];
  93. BOOL connectedViaWifi = self.reachability.currentReachabilityStatus == ReachableViaWiFi;
  94. self.serverOnButton.enabled = connectedViaWifi;
  95. NSString *uploadText = connectedViaWifi ? [[VLCHTTPUploaderController sharedInstance] httpStatus] : NSLocalizedString(@"HTTP_UPLOAD_NO_CONNECTIVITY", nil);
  96. self.uploadAddressLabel.text = uploadText;
  97. if (connectedViaWifi && [VLCHTTPUploaderController sharedInstance].isServerRunning) {
  98. [self.serverOnButton setImage:[UIImage imageNamed:@"WifiUpOn"] forState:UIControlStateNormal];
  99. }
  100. }
  101. - (void)toggleHTTPServer
  102. {
  103. BOOL futureHTTPServerState = ![VLCHTTPUploaderController sharedInstance].isServerRunning ;
  104. [[NSUserDefaults standardUserDefaults] setBool:futureHTTPServerState forKey:kVLCSettingSaveHTTPUploadServerStatus];
  105. [[VLCHTTPUploaderController sharedInstance] changeHTTPServerState:futureHTTPServerState];
  106. [self updateHTTPServerAddress];
  107. [[NSUserDefaults standardUserDefaults] synchronize];
  108. }
  109. @end