VLCNetworkLoginDataSourceLogin.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 "VLCNetworkLoginDataSourceLogin.h"
  12. #import "VLCNetworkLoginViewFieldCell.h"
  13. #import "VLCNetworkLoginViewButtonCell.h"
  14. typedef NS_ENUM(NSUInteger, VLCNetworkServerLoginIndex) {
  15. VLCNetworkServerLoginIndexServer,
  16. VLCNetworkServerLoginIndexPort,
  17. VLCNetworkServerLoginIndexUsername,
  18. VLCNetworkServerLoginIndexPassword,
  19. VLCNetworkServerLoginIndexSave,
  20. VLCNetworkServerLoginIndexCount,
  21. VLCNetworkServerLoginIndexFieldCount = VLCNetworkServerLoginIndexSave
  22. };
  23. @interface VLCNetworkLoginDataSourceLogin () <VLCNetworkLoginViewFieldCellDelegate>
  24. @property (nonatomic, weak) UITableView *tableView;
  25. @end
  26. @implementation VLCNetworkLoginDataSourceLogin
  27. @synthesize sectionIndex = _sectionIndex;
  28. #pragma mark - API
  29. - (void)registerCellsInTableView:(UITableView *)tableView
  30. {
  31. [tableView registerClass:[VLCNetworkLoginViewButtonCell class] forCellReuseIdentifier:kVLCNetworkLoginViewButtonCellIdentifier];
  32. [tableView registerClass:[VLCNetworkLoginViewFieldCell class] forCellReuseIdentifier:kVLCNetworkLoginViewFieldCellIdentifier];
  33. }
  34. - (void)setLoginInformation:(VLCNetworkServerLoginInformation *)loginInformation
  35. {
  36. _loginInformation = loginInformation;
  37. [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:self.sectionIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
  38. }
  39. #pragma mark - helper
  40. - (void)configureButtonCell:(VLCNetworkLoginViewButtonCell *)buttonCell forRow:(NSUInteger)row
  41. {
  42. NSString *labelString = nil;
  43. NSUInteger additionalFieldsCount = self.loginInformation.additionalFields.count;
  44. NSUInteger buttonRowIndex = row-additionalFieldsCount;
  45. if (buttonRowIndex == VLCNetworkServerLoginIndexSave) {
  46. labelString = NSLocalizedString(@"BUTTON_SAVE", nil);
  47. }
  48. buttonCell.titleString = labelString;
  49. }
  50. - (void)configureFieldCell:(VLCNetworkLoginViewFieldCell *)fieldCell forRow:(NSUInteger)row
  51. {
  52. UIKeyboardType keyboardType = UIKeyboardTypeDefault;
  53. BOOL secureTextEntry = NO;
  54. NSString *labelString = nil;
  55. NSString *valueString = nil;
  56. UIReturnKeyType returnKeyType = UIReturnKeyNext;
  57. switch (row) {
  58. case VLCNetworkServerLoginIndexServer:
  59. keyboardType = UIKeyboardTypeURL;
  60. labelString = NSLocalizedString(@"SERVER", nil);
  61. valueString = self.loginInformation.address;
  62. break;
  63. case VLCNetworkServerLoginIndexPort:
  64. keyboardType = UIKeyboardTypeNumberPad;
  65. labelString = NSLocalizedString(@"SERVER_PORT", nil);
  66. valueString = self.loginInformation.port.stringValue;
  67. break;
  68. case VLCNetworkServerLoginIndexUsername:
  69. labelString = NSLocalizedString(@"USER_LABEL", nil);
  70. valueString = self.loginInformation.username;
  71. break;
  72. case VLCNetworkServerLoginIndexPassword:
  73. labelString = NSLocalizedString(@"PASSWORD_LABEL", nil);
  74. valueString = self.loginInformation.password;
  75. secureTextEntry = YES;
  76. if (self.loginInformation.additionalFields.count == 0) {
  77. returnKeyType = UIReturnKeyDone;
  78. }
  79. break;
  80. default: {
  81. NSUInteger additionalFieldRow = row-VLCNetworkServerLoginIndexFieldCount;
  82. NSArray <VLCNetworkServerLoginInformationField *> *additionalFields = self.loginInformation.additionalFields;
  83. VLCNetworkServerLoginInformationField *field = additionalFields[additionalFieldRow];
  84. if (field.type == VLCNetworkServerLoginInformationFieldTypeNumber) {
  85. keyboardType = UIKeyboardTypeNumberPad;
  86. }
  87. valueString = field.textValue;
  88. labelString = field.localizedLabel;
  89. returnKeyType = additionalFieldRow == additionalFields.count-1 ? UIReturnKeyDone : UIReturnKeyNext;
  90. }
  91. break;
  92. }
  93. fieldCell.placeholderString = labelString;
  94. UITextField *textField = fieldCell.textField;
  95. textField.autocorrectionType = UITextAutocorrectionTypeNo;
  96. textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
  97. textField.text = valueString;
  98. textField.keyboardType = keyboardType;
  99. textField.secureTextEntry = secureTextEntry;
  100. textField.returnKeyType = returnKeyType;
  101. textField.tag = row;
  102. fieldCell.delegate = self;
  103. }
  104. - (void)updatedStringValue:(NSString *)string forRow:(NSUInteger)row
  105. {
  106. switch (row) {
  107. case VLCNetworkServerLoginIndexServer:
  108. self.loginInformation.address = string;
  109. break;
  110. case VLCNetworkServerLoginIndexPort:
  111. self.loginInformation.port = string.length > 0 ? @(string.integerValue) : nil;
  112. break;
  113. case VLCNetworkServerLoginIndexUsername:
  114. self.loginInformation.username = string;
  115. break;
  116. case VLCNetworkServerLoginIndexPassword:
  117. self.loginInformation.password = string;
  118. break;
  119. default: {
  120. NSUInteger additionalFieldRow = row-VLCNetworkServerLoginIndexFieldCount;
  121. NSArray <VLCNetworkServerLoginInformationField *> *additionalFields = self.loginInformation.additionalFields;
  122. VLCNetworkServerLoginInformationField *field = additionalFields[additionalFieldRow];
  123. field.textValue = string;
  124. }
  125. break;
  126. }
  127. }
  128. - (void)makeCellFirstResponder:(UITableViewCell *)cell
  129. {
  130. if ([cell isKindOfClass:[VLCNetworkLoginViewFieldCell class]]) {
  131. [[(VLCNetworkLoginViewFieldCell *)cell textField] becomeFirstResponder];
  132. }
  133. }
  134. #pragma mark - VLCNetworkLoginDataSourceSection
  135. - (void)configureWithTableView:(UITableView *)tableView
  136. {
  137. [self registerCellsInTableView:tableView];
  138. self.tableView = tableView;
  139. }
  140. - (NSUInteger)numberOfRowsInTableView:(UITableView *)tableView
  141. {
  142. return VLCNetworkServerLoginIndexCount + self.loginInformation.additionalFields.count;
  143. }
  144. - (NSString *)cellIdentifierForRow:(NSUInteger)row
  145. {
  146. switch (row) {
  147. case VLCNetworkServerLoginIndexServer:
  148. case VLCNetworkServerLoginIndexPort:
  149. case VLCNetworkServerLoginIndexUsername:
  150. case VLCNetworkServerLoginIndexPassword:
  151. return kVLCNetworkLoginViewFieldCellIdentifier;
  152. default:
  153. break;
  154. }
  155. NSUInteger additionalFieldsCount = self.loginInformation.additionalFields.count;
  156. NSUInteger buttonRowIndex = row-additionalFieldsCount;
  157. if (buttonRowIndex == VLCNetworkServerLoginIndexSave) {
  158. return kVLCNetworkLoginViewButtonCellIdentifier;
  159. } else {
  160. return kVLCNetworkLoginViewFieldCellIdentifier;
  161. }
  162. }
  163. - (void)configureCell:(UITableViewCell *)cell forRow:(NSUInteger)row
  164. {
  165. if ([cell isKindOfClass:[VLCNetworkLoginViewFieldCell class]]) {
  166. [self configureFieldCell:(id)cell forRow:row];
  167. } else if ([cell isKindOfClass:[VLCNetworkLoginViewButtonCell class]]) {
  168. [self configureButtonCell:(id)cell forRow:row];
  169. } else {
  170. NSLog(@"%s can't configure cell: %@", __PRETTY_FUNCTION__, cell);
  171. }
  172. }
  173. - (NSUInteger)willSelectRow:(NSUInteger)row
  174. {
  175. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:self.sectionIndex]];
  176. if ([cell isKindOfClass:[VLCNetworkLoginViewFieldCell class]]) {
  177. [self makeCellFirstResponder:cell];
  178. return NSNotFound;
  179. } else {
  180. return row;
  181. }
  182. }
  183. - (void)didSelectRow:(NSUInteger)row
  184. {
  185. NSUInteger additionalFieldsCount = self.loginInformation.additionalFields.count;
  186. NSUInteger buttonRowIndex = row-additionalFieldsCount;
  187. if (buttonRowIndex == VLCNetworkServerLoginIndexSave) {
  188. [self.delegate saveLoginDataSource:self];
  189. }
  190. }
  191. #pragma mark - VLCNetworkLoginViewFieldCellDelegate
  192. - (BOOL)loginViewFieldCellShouldReturn:(VLCNetworkLoginViewFieldCell *)cell
  193. {
  194. if (cell.textField.returnKeyType == UIReturnKeyNext) {
  195. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  196. NSIndexPath *nextIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
  197. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:nextIndexPath];
  198. [self makeCellFirstResponder:cell];
  199. [self.tableView scrollToRowAtIndexPath:nextIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
  200. return NO;
  201. } else {
  202. return YES;
  203. }
  204. }
  205. - (void)loginViewFieldCellDidEndEditing:(VLCNetworkLoginViewFieldCell *)cell
  206. {
  207. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  208. [self updatedStringValue:cell.textField.text forRow:indexPath.row];
  209. }
  210. @end