VLCWiFiUploadTableViewCell.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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_iOS-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 setupCell];
  27. [self updateHTTPServerAddress];
  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)setupCell
  42. {
  43. self.reachability = [Reachability reachabilityForLocalWiFi];
  44. [self.reachability startNotifier];
  45. self.selectionStyle = UITableViewCellSelectionStyleNone;
  46. self.textLabel.text = NSLocalizedString(@"WEBINTF_TITLE", nil);
  47. self.detailTextLabel.text = NSLocalizedString(@"HTTP_UPLOAD_SERVER_OFF", nil);
  48. self.detailTextLabel.numberOfLines = 0;
  49. self.serverToggle = [[UISwitch alloc] init];
  50. [self.serverToggle addTarget:self action:@selector(toggleHTTPServer) forControlEvents:UIControlEventTouchUpInside];
  51. self.accessoryView = self.serverToggle;
  52. self.imageView.image = [UIImage imageNamed:@"WifiIcon"];
  53. [self updateTheme];
  54. }
  55. - (void)updateTheme
  56. {
  57. self.textLabel.textColor = PresentationTheme.current.colors.cellTextColor;
  58. self.detailTextLabel.textColor = PresentationTheme.current.colors.cellDetailTextColor;
  59. self.backgroundColor = PresentationTheme.current.colors.background;
  60. }
  61. - (void)netReachabilityChanged
  62. {
  63. [self updateHTTPServerAddress];
  64. }
  65. - (void)updateHTTPServerAddress
  66. {
  67. BOOL connectedViaWifi = [[VLCHTTPUploaderController sharedInstance] isReachable];
  68. self.serverToggle.enabled = connectedViaWifi;
  69. NSString *uploadText = connectedViaWifi ? [[VLCHTTPUploaderController sharedInstance] httpStatus] : NSLocalizedString(@"HTTP_UPLOAD_NO_CONNECTIVITY", nil);
  70. self.detailTextLabel.text = uploadText;
  71. self.serverToggle.on = connectedViaWifi && [VLCHTTPUploaderController sharedInstance].isServerRunning;
  72. }
  73. - (void)toggleHTTPServer
  74. {
  75. BOOL futureHTTPServerState = ![VLCHTTPUploaderController sharedInstance].isServerRunning ;
  76. [[NSUserDefaults standardUserDefaults] setBool:futureHTTPServerState forKey:kVLCSettingSaveHTTPUploadServerStatus];
  77. [[VLCHTTPUploaderController sharedInstance] changeHTTPServerState:futureHTTPServerState];
  78. [self updateHTTPServerAddress];
  79. [[NSUserDefaults standardUserDefaults] synchronize];
  80. }
  81. @end