VLCWiFiUploadTableViewCell.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*****************************************************************************
  2. * VLCWiFiUploadTableViewCell.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2015 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 VLCMenuBackgroundColor];
  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.serverOnButton = [UIButton buttonWithType:UIButtonTypeCustom];
  65. self.serverOnButton.translatesAutoresizingMaskIntoConstraints = NO;
  66. [self.serverOnButton addTarget:self action:@selector(toggleHTTPServer) forControlEvents:UIControlEventTouchUpInside];
  67. [self.serverOnButton setImage:[UIImage imageNamed:@"WiFiUp"] forState:UIControlStateDisabled];
  68. [self.contentView addSubview:self.serverOnButton];
  69. }
  70. - (void)setupConstraints
  71. {
  72. UIView *spacer1 = [UIView new];
  73. UIView *spacer2 = [UIView new];
  74. UIView *spacer3 = [UIView new];
  75. UIView *spacer4 = [UIView new];
  76. spacer1.translatesAutoresizingMaskIntoConstraints = NO;
  77. spacer2.translatesAutoresizingMaskIntoConstraints = NO;
  78. spacer3.translatesAutoresizingMaskIntoConstraints = NO;
  79. spacer4.translatesAutoresizingMaskIntoConstraints = NO;
  80. [self.contentView addSubview:spacer1];
  81. [self.contentView addSubview:spacer2];
  82. [self.contentView addSubview:spacer3];
  83. [self.contentView addSubview:spacer4];
  84. NSDictionary *dict = NSDictionaryOfVariableBindings(_titleLabel, _uploadAddressLabel, _serverOnButton, spacer1, spacer2, spacer3, spacer4);
  85. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_serverOnButton(50)]-==8-[_titleLabel]" options:0 metrics:0 views:dict]];
  86. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_serverOnButton(50)]-==8-[_uploadAddressLabel]" options:0 metrics:0 views:dict]];
  87. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[spacer1][_titleLabel]-==0-[_uploadAddressLabel(>=0)][spacer2(==spacer1)]|" options:0 metrics:0 views:dict]];
  88. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[spacer3][_serverOnButton(50)][spacer4]|" options:0 metrics:0 views:dict]];
  89. }
  90. - (void)netReachabilityChanged
  91. {
  92. [self updateHTTPServerAddress];
  93. }
  94. - (void)updateHTTPServerAddress
  95. {
  96. [self.serverOnButton setImage:[UIImage imageNamed:@"WifiUp"] forState:UIControlStateNormal];
  97. BOOL connectedViaWifi = [[VLCHTTPUploaderController sharedInstance] isReachable];
  98. self.serverOnButton.enabled = connectedViaWifi;
  99. NSString *uploadText = connectedViaWifi ? [[VLCHTTPUploaderController sharedInstance] httpStatus] : NSLocalizedString(@"HTTP_UPLOAD_NO_CONNECTIVITY", nil);
  100. self.uploadAddressLabel.text = uploadText;
  101. if (connectedViaWifi && [VLCHTTPUploaderController sharedInstance].isServerRunning) {
  102. [self.serverOnButton setImage:[UIImage imageNamed:@"WifiUpOn"] forState:UIControlStateNormal];
  103. }
  104. }
  105. - (void)toggleHTTPServer
  106. {
  107. BOOL futureHTTPServerState = ![VLCHTTPUploaderController sharedInstance].isServerRunning ;
  108. [[NSUserDefaults standardUserDefaults] setBool:futureHTTPServerState forKey:kVLCSettingSaveHTTPUploadServerStatus];
  109. [[VLCHTTPUploaderController sharedInstance] changeHTTPServerState:futureHTTPServerState];
  110. [self updateHTTPServerAddress];
  111. [[NSUserDefaults standardUserDefaults] synchronize];
  112. }
  113. @end