VLCNetworkLoginDataSourceLogin.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.text = valueString;
  96. textField.keyboardType = keyboardType;
  97. textField.secureTextEntry = secureTextEntry;
  98. textField.returnKeyType = returnKeyType;
  99. textField.tag = row;
  100. fieldCell.delegate = self;
  101. }
  102. - (void)updatedStringValue:(NSString *)string forRow:(NSUInteger)row
  103. {
  104. switch (row) {
  105. case VLCNetworkServerLoginIndexServer:
  106. self.loginInformation.address = string;
  107. break;
  108. case VLCNetworkServerLoginIndexPort:
  109. self.loginInformation.port = string.length > 0 ? @(string.integerValue) : nil;
  110. break;
  111. case VLCNetworkServerLoginIndexUsername:
  112. self.loginInformation.username = string;
  113. break;
  114. case VLCNetworkServerLoginIndexPassword:
  115. self.loginInformation.password = string;
  116. break;
  117. default: {
  118. NSUInteger additionalFieldRow = row-VLCNetworkServerLoginIndexFieldCount;
  119. NSArray <VLCNetworkServerLoginInformationField *> *additionalFields = self.loginInformation.additionalFields;
  120. VLCNetworkServerLoginInformationField *field = additionalFields[additionalFieldRow];
  121. field.textValue = string;
  122. }
  123. break;
  124. }
  125. }
  126. - (void)makeCellFirstResponder:(UITableViewCell *)cell
  127. {
  128. if ([cell isKindOfClass:[VLCNetworkLoginViewFieldCell class]]) {
  129. [[(VLCNetworkLoginViewFieldCell *)cell textField] becomeFirstResponder];
  130. }
  131. }
  132. #pragma mark - VLCNetworkLoginDataSourceSection
  133. - (void)configureWithTableView:(UITableView *)tableView
  134. {
  135. [self registerCellsInTableView:tableView];
  136. self.tableView = tableView;
  137. }
  138. - (NSUInteger)numberOfRowsInTableView:(UITableView *)tableView
  139. {
  140. return VLCNetworkServerLoginIndexCount + self.loginInformation.additionalFields.count;
  141. }
  142. - (NSString *)cellIdentifierForRow:(NSUInteger)row
  143. {
  144. switch (row) {
  145. case VLCNetworkServerLoginIndexServer:
  146. case VLCNetworkServerLoginIndexPort:
  147. case VLCNetworkServerLoginIndexUsername:
  148. case VLCNetworkServerLoginIndexPassword:
  149. return kVLCNetworkLoginViewFieldCellIdentifier;
  150. default:
  151. break;
  152. }
  153. NSUInteger additionalFieldsCount = self.loginInformation.additionalFields.count;
  154. NSUInteger buttonRowIndex = row-additionalFieldsCount;
  155. if (buttonRowIndex == VLCNetworkServerLoginIndexSave) {
  156. return kVLCNetworkLoginViewButtonCellIdentifier;
  157. } else {
  158. return kVLCNetworkLoginViewFieldCellIdentifier;
  159. }
  160. }
  161. - (void)configureCell:(UITableViewCell *)cell forRow:(NSUInteger)row
  162. {
  163. if ([cell isKindOfClass:[VLCNetworkLoginViewFieldCell class]]) {
  164. [self configureFieldCell:(id)cell forRow:row];
  165. } else if ([cell isKindOfClass:[VLCNetworkLoginViewButtonCell class]]) {
  166. [self configureButtonCell:(id)cell forRow:row];
  167. } else {
  168. NSLog(@"%s can't configure cell: %@", __PRETTY_FUNCTION__, cell);
  169. }
  170. }
  171. - (NSUInteger)willSelectRow:(NSUInteger)row
  172. {
  173. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:self.sectionIndex]];
  174. if ([cell isKindOfClass:[VLCNetworkLoginViewFieldCell class]]) {
  175. [self makeCellFirstResponder:cell];
  176. return NSNotFound;
  177. } else {
  178. return row;
  179. }
  180. }
  181. - (void)didSelectRow:(NSUInteger)row
  182. {
  183. NSUInteger additionalFieldsCount = self.loginInformation.additionalFields.count;
  184. NSUInteger buttonRowIndex = row-additionalFieldsCount;
  185. if (buttonRowIndex == VLCNetworkServerLoginIndexSave) {
  186. [self.delegate saveLoginDataSource:self];
  187. }
  188. }
  189. #pragma mark - VLCNetworkLoginViewFieldCellDelegate
  190. - (BOOL)loginViewFieldCellShouldReturn:(VLCNetworkLoginViewFieldCell *)cell
  191. {
  192. if (cell.textField.returnKeyType == UIReturnKeyNext) {
  193. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  194. NSIndexPath *nextIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
  195. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:nextIndexPath];
  196. [self makeCellFirstResponder:cell];
  197. [self.tableView scrollToRowAtIndexPath:nextIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
  198. return NO;
  199. } else {
  200. return YES;
  201. }
  202. }
  203. - (void)loginViewFieldCellDidEndEditing:(VLCNetworkLoginViewFieldCell *)cell
  204. {
  205. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  206. [self updatedStringValue:cell.textField.text forRow:indexPath.row];
  207. }
  208. @end