VLCWiFiUploadTableViewCell.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*****************************************************************************
  2. * VLCWiFiUploadTableViewCell.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2018 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. * Carola Nitz <caro # videolan.org>
  10. *
  11. * Refer to the COPYING file of the official project for license.
  12. *****************************************************************************/
  13. #import "VLCWiFiUploadTableViewCell.h"
  14. #import "Reachability.h"
  15. #import "VLCHTTPUploaderController.h"
  16. #import "VLC-Swift.h"
  17. @interface VLCWiFiUploadTableViewCell()
  18. @property (nonatomic, strong) UISwitch *serverToggle;
  19. @property (nonatomic, strong) Reachability *reachability;
  20. @end
  21. @implementation VLCWiFiUploadTableViewCell
  22. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  23. {
  24. self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
  25. if (self) {
  26. [self setupReachability];
  27. [self setupCell];
  28. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netReachabilityChanged) name:kReachabilityChangedNotification object:nil];
  29. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTheme) name:kVLCThemeDidChangeNotification object:nil];
  30. }
  31. return self;
  32. }
  33. - (void)dealloc
  34. {
  35. [self.reachability stopNotifier];
  36. }
  37. + (NSString *)cellIdentifier
  38. {
  39. return @"VLCWiFiUploadTableViewCell";
  40. }
  41. - (void)setupReachability
  42. {
  43. self.reachability = [Reachability reachabilityForLocalWiFi];
  44. [self.reachability startNotifier];
  45. }
  46. - (void)setupCell
  47. {
  48. self.selectionStyle = UITableViewCellSelectionStyleNone;
  49. self.textLabel.text = NSLocalizedString(@"WEBINTF_TITLE", nil);
  50. self.detailTextLabel.text = NSLocalizedString(@"HTTP_UPLOAD_SERVER_OFF", nil);
  51. self.detailTextLabel.numberOfLines = 0;
  52. self.serverToggle = [[UISwitch alloc] init];
  53. [self.serverToggle addTarget:self action:@selector(toggleHTTPServer) forControlEvents:UIControlEventTouchUpInside];
  54. self.accessoryView = self.serverToggle;
  55. self.imageView.image = [UIImage imageNamed:@"WifiIcon"];
  56. [self updateTheme];
  57. [self updateHTTPServerAddress];
  58. }
  59. - (void)updateTheme
  60. {
  61. self.textLabel.textColor = PresentationTheme.current.colors.cellTextColor;
  62. self.detailTextLabel.textColor = PresentationTheme.current.colors.cellDetailTextColor;
  63. self.backgroundColor = PresentationTheme.current.colors.background;
  64. }
  65. - (void)netReachabilityChanged
  66. {
  67. [self updateHTTPServerAddress];
  68. }
  69. - (void)updateHTTPServerAddress
  70. {
  71. BOOL connectedViaWifi = [[VLCHTTPUploaderController sharedInstance] isReachable];
  72. self.serverToggle.enabled = connectedViaWifi;
  73. NSString *uploadText = connectedViaWifi ? [[VLCHTTPUploaderController sharedInstance] httpStatus] : NSLocalizedString(@"HTTP_UPLOAD_NO_CONNECTIVITY", nil);
  74. self.detailTextLabel.text = uploadText;
  75. self.serverToggle.on = connectedViaWifi && [VLCHTTPUploaderController sharedInstance].isServerRunning;
  76. }
  77. - (void)toggleHTTPServer
  78. {
  79. BOOL futureHTTPServerState = ![VLCHTTPUploaderController sharedInstance].isServerRunning ;
  80. [[NSUserDefaults standardUserDefaults] setBool:futureHTTPServerState forKey:kVLCSettingSaveHTTPUploadServerStatus];
  81. [[VLCHTTPUploaderController sharedInstance] changeHTTPServerState:futureHTTPServerState];
  82. [self updateHTTPServerAddress];
  83. [[NSUserDefaults standardUserDefaults] synchronize];
  84. }
  85. - (void)prepareForReuse
  86. {
  87. [super prepareForReuse];
  88. [self setupCell];
  89. }
  90. @end