VLCNetworkLoginDataSourceProtocol.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*****************************************************************************
  2. * VLC for iOS
  3. *****************************************************************************
  4. * Copyright (c) 2016 VideoLAN. All rights reserved.
  5. * $Id$
  6. *
  7. * Authors: Vincent L. Cone <vincent.l.cone # tuta.io>
  8. *
  9. * Refer to the COPYING file of the official project for license.
  10. *****************************************************************************/
  11. #import "VLCNetworkLoginDataSourceProtocol.h"
  12. #import "VLC-Swift.h"
  13. static NSString *const VLCNetworkLoginDataSourceProtocolCellIdentifier = @"VLCNetworkLoginDataSourceProtocolCell";
  14. @interface VLCNetworkLoginDataSourceProtocolCell : UITableViewCell
  15. @property (nonatomic) UISegmentedControl *segmentedControl;
  16. @end
  17. @interface VLCNetworkLoginDataSourceProtocol ()
  18. @property (nonatomic, weak) UITableView *tableView;
  19. @end
  20. @implementation VLCNetworkLoginDataSourceProtocol
  21. @synthesize sectionIndex;
  22. - (void)segmentedControlChanged:(UISegmentedControl *)control
  23. {
  24. NSInteger selectedIndex = control.selectedSegmentIndex;
  25. if (selectedIndex < 0 || VLCServerProtocolUndefined < selectedIndex) {
  26. selectedIndex = VLCServerProtocolUndefined;
  27. }
  28. self.protocol = (VLCServerProtocol)selectedIndex;
  29. [self.delegate protocolDidChange:self];
  30. }
  31. - (void)setProtocol:(VLCServerProtocol)protocol
  32. {
  33. if (_protocol != protocol) {
  34. _protocol = protocol;
  35. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:self.sectionIndex]];
  36. [self configureCell:cell forRow:0];
  37. }
  38. }
  39. #pragma mark - VLCNetworkLoginDataSourceSection
  40. - (void)configureWithTableView:(UITableView *)tableView
  41. {
  42. [tableView registerClass:[VLCNetworkLoginDataSourceProtocolCell class] forCellReuseIdentifier:VLCNetworkLoginDataSourceProtocolCellIdentifier];
  43. self.tableView = tableView;
  44. }
  45. - (NSUInteger)numberOfRowsInTableView:(UITableView *)tableView
  46. {
  47. return 1;
  48. }
  49. - (NSString *)cellIdentifierForRow:(NSUInteger)row
  50. {
  51. return VLCNetworkLoginDataSourceProtocolCellIdentifier;
  52. }
  53. - (void)configureCell:(UITableViewCell *)cell forRow:(NSUInteger)row
  54. {
  55. NSInteger segmentIndex = self.protocol;
  56. if (segmentIndex == VLCServerProtocolUndefined) {
  57. segmentIndex = -1;
  58. }
  59. VLCNetworkLoginDataSourceProtocolCell *protocolCell = [cell isKindOfClass:[VLCNetworkLoginDataSourceProtocolCell class]] ? (id)cell : nil;
  60. protocolCell.segmentedControl.selectedSegmentIndex = segmentIndex;
  61. if (![[protocolCell.segmentedControl allTargets] containsObject:self]) {
  62. [protocolCell.segmentedControl addTarget:self action:@selector(segmentedControlChanged:) forControlEvents:UIControlEventValueChanged];
  63. }
  64. }
  65. @end
  66. @implementation VLCNetworkLoginDataSourceProtocolCell
  67. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  68. {
  69. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  70. if (self) {
  71. _segmentedControl = [[UISegmentedControl alloc] initWithItems:
  72. @[NSLocalizedString(@"SMB_CIFS_FILE_SERVERS_SHORT", nil),
  73. NSLocalizedString(@"FTP_SHORT", nil),
  74. NSLocalizedString(@"PLEX_SHORT", nil),
  75. ]];
  76. _segmentedControl.tintColor = PresentationTheme.current.colors.orangeUI;
  77. if (@available(iOS 13.0, *)) {
  78. [self.segmentedControl setTitleTextAttributes:@{NSForegroundColorAttributeName:
  79. PresentationTheme.current.colors.cellDetailTextColor}
  80. forState:UIControlStateNormal];
  81. // Always use black since the background is always white.
  82. [self.segmentedControl setTitleTextAttributes:@{NSForegroundColorAttributeName:
  83. UIColor.blackColor}
  84. forState:UIControlStateSelected];
  85. }
  86. [self.contentView addSubview:_segmentedControl];
  87. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeDidChange) name:kVLCThemeDidChangeNotification object:nil];
  88. [self themeDidChange];
  89. }
  90. return self;
  91. }
  92. - (void)layoutSubviews
  93. {
  94. [super layoutSubviews];
  95. self.segmentedControl.frame = CGRectInset(self.contentView.bounds, 20, 5);
  96. }
  97. - (void)themeDidChange
  98. {
  99. self.backgroundColor = PresentationTheme.current.colors.background;
  100. }
  101. @end