VLCNetworkLoginDataSourceLogin.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. UITextAutocapitalizationType autocapitalizationType = UITextAutocapitalizationTypeSentences;
  58. switch (row) {
  59. case VLCNetworkServerLoginIndexServer:
  60. keyboardType = UIKeyboardTypeURL;
  61. labelString = NSLocalizedString(@"SERVER", nil);
  62. valueString = self.loginInformation.address;
  63. break;
  64. case VLCNetworkServerLoginIndexPort:
  65. keyboardType = UIKeyboardTypeNumberPad;
  66. labelString = NSLocalizedString(@"SERVER_PORT", nil);
  67. valueString = self.loginInformation.port.stringValue;
  68. break;
  69. case VLCNetworkServerLoginIndexUsername:
  70. labelString = NSLocalizedString(@"USER_LABEL", nil);
  71. autocapitalizationType = UITextAutocapitalizationTypeNone;
  72. valueString = self.loginInformation.username;
  73. break;
  74. case VLCNetworkServerLoginIndexPassword:
  75. labelString = NSLocalizedString(@"PASSWORD_LABEL", nil);
  76. valueString = self.loginInformation.password;
  77. secureTextEntry = YES;
  78. if (self.loginInformation.additionalFields.count == 0) {
  79. returnKeyType = UIReturnKeyDone;
  80. }
  81. break;
  82. default: {
  83. NSUInteger additionalFieldRow = row-VLCNetworkServerLoginIndexFieldCount;
  84. NSArray <VLCNetworkServerLoginInformationField *> *additionalFields = self.loginInformation.additionalFields;
  85. VLCNetworkServerLoginInformationField *field = additionalFields[additionalFieldRow];
  86. if (field.type == VLCNetworkServerLoginInformationFieldTypeNumber) {
  87. keyboardType = UIKeyboardTypeNumberPad;
  88. }
  89. valueString = field.textValue;
  90. labelString = field.localizedLabel;
  91. returnKeyType = additionalFieldRow == additionalFields.count-1 ? UIReturnKeyDone : UIReturnKeyNext;
  92. }
  93. break;
  94. }
  95. fieldCell.placeholderString = labelString;
  96. UITextField *textField = fieldCell.textField;
  97. textField.autocorrectionType = UITextAutocorrectionTypeNo;
  98. textField.autocapitalizationType = autocapitalizationType;
  99. textField.text = valueString;
  100. textField.keyboardType = keyboardType;
  101. textField.secureTextEntry = secureTextEntry;
  102. textField.returnKeyType = returnKeyType;
  103. textField.tag = row;
  104. fieldCell.delegate = self;
  105. }
  106. - (void)updatedStringValue:(NSString *)string forRow:(NSUInteger)row
  107. {
  108. switch (row) {
  109. case VLCNetworkServerLoginIndexServer:
  110. self.loginInformation.address = string;
  111. break;
  112. case VLCNetworkServerLoginIndexPort:
  113. self.loginInformation.port = string.length > 0 ? @(string.integerValue) : nil;
  114. break;
  115. case VLCNetworkServerLoginIndexUsername:
  116. self.loginInformation.username = string;
  117. break;
  118. case VLCNetworkServerLoginIndexPassword:
  119. self.loginInformation.password = string;
  120. break;
  121. default: {
  122. NSUInteger additionalFieldRow = row-VLCNetworkServerLoginIndexFieldCount;
  123. NSArray <VLCNetworkServerLoginInformationField *> *additionalFields = self.loginInformation.additionalFields;
  124. VLCNetworkServerLoginInformationField *field = additionalFields[additionalFieldRow];
  125. field.textValue = string;
  126. }
  127. break;
  128. }
  129. }
  130. - (void)makeCellFirstResponder:(UITableViewCell *)cell
  131. {
  132. if ([cell isKindOfClass:[VLCNetworkLoginViewFieldCell class]]) {
  133. [[(VLCNetworkLoginViewFieldCell *)cell textField] becomeFirstResponder];
  134. }
  135. }
  136. #pragma mark - VLCNetworkLoginDataSourceSection
  137. - (void)configureWithTableView:(UITableView *)tableView
  138. {
  139. [self registerCellsInTableView:tableView];
  140. self.tableView = tableView;
  141. }
  142. - (NSUInteger)numberOfRowsInTableView:(UITableView *)tableView
  143. {
  144. return VLCNetworkServerLoginIndexCount + self.loginInformation.additionalFields.count;
  145. }
  146. - (NSString *)cellIdentifierForRow:(NSUInteger)row
  147. {
  148. switch (row) {
  149. case VLCNetworkServerLoginIndexServer:
  150. case VLCNetworkServerLoginIndexPort:
  151. case VLCNetworkServerLoginIndexUsername:
  152. case VLCNetworkServerLoginIndexPassword:
  153. return kVLCNetworkLoginViewFieldCellIdentifier;
  154. default:
  155. break;
  156. }
  157. NSUInteger additionalFieldsCount = self.loginInformation.additionalFields.count;
  158. NSUInteger buttonRowIndex = row-additionalFieldsCount;
  159. if (buttonRowIndex == VLCNetworkServerLoginIndexSave) {
  160. return kVLCNetworkLoginViewButtonCellIdentifier;
  161. } else {
  162. return kVLCNetworkLoginViewFieldCellIdentifier;
  163. }
  164. }
  165. - (void)configureCell:(UITableViewCell *)cell forRow:(NSUInteger)row
  166. {
  167. if ([cell isKindOfClass:[VLCNetworkLoginViewFieldCell class]]) {
  168. [self configureFieldCell:(id)cell forRow:row];
  169. } else if ([cell isKindOfClass:[VLCNetworkLoginViewButtonCell class]]) {
  170. [self configureButtonCell:(id)cell forRow:row];
  171. } else {
  172. NSLog(@"%s can't configure cell: %@", __PRETTY_FUNCTION__, cell);
  173. }
  174. }
  175. - (NSUInteger)willSelectRow:(NSUInteger)row
  176. {
  177. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:self.sectionIndex]];
  178. if ([cell isKindOfClass:[VLCNetworkLoginViewFieldCell class]]) {
  179. [self makeCellFirstResponder:cell];
  180. return NSNotFound;
  181. } else {
  182. return row;
  183. }
  184. }
  185. - (void)didSelectRow:(NSUInteger)row
  186. {
  187. NSUInteger additionalFieldsCount = self.loginInformation.additionalFields.count;
  188. NSUInteger buttonRowIndex = row-additionalFieldsCount;
  189. if (buttonRowIndex == VLCNetworkServerLoginIndexSave) {
  190. [self.delegate saveLoginDataSource:self];
  191. }
  192. }
  193. #pragma mark - VLCNetworkLoginViewFieldCellDelegate
  194. - (BOOL)loginViewFieldCellShouldReturn:(VLCNetworkLoginViewFieldCell *)cell
  195. {
  196. if (cell.textField.returnKeyType == UIReturnKeyNext) {
  197. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  198. NSIndexPath *nextIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
  199. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:nextIndexPath];
  200. [self makeCellFirstResponder:cell];
  201. [self.tableView scrollToRowAtIndexPath:nextIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
  202. return NO;
  203. } else {
  204. return YES;
  205. }
  206. }
  207. - (void)loginViewFieldCellDidEndEditing:(VLCNetworkLoginViewFieldCell *)cell
  208. {
  209. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  210. [self updatedStringValue:cell.textField.text forRow:indexPath.row];
  211. }
  212. @end