VLCNetworkLoginDataSource.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "VLCNetworkLoginDataSource.h"
  12. #import "VLC-Swift.h"
  13. @implementation VLCNetworkLoginDataSource
  14. - (void)configureWithTableView:(UITableView *)tableView
  15. {
  16. tableView.dataSource = self;
  17. tableView.delegate = self;
  18. [self.dataSources enumerateObjectsUsingBlock:^(id<VLCNetworkLoginDataSourceSection> _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  19. [obj configureWithTableView:tableView];
  20. }];
  21. }
  22. - (void)setDataSources:(NSArray<id<VLCNetworkLoginDataSourceSection>> *)dataSources
  23. {
  24. _dataSources = [dataSources copy];
  25. [dataSources enumerateObjectsUsingBlock:^(id<VLCNetworkLoginDataSourceSection> _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  26. obj.sectionIndex = idx;
  27. }];
  28. }
  29. #pragma mark - UITableViewDataSource
  30. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  31. {
  32. return self.dataSources.count;
  33. }
  34. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  35. {
  36. return [self.dataSources[section] numberOfRowsInTableView:tableView];
  37. }
  38. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  39. {
  40. id<VLCNetworkLoginDataSourceSection> dataSource = self.dataSources[indexPath.section];
  41. NSUInteger row = indexPath.row;
  42. NSString *cellIdentifier = [dataSource cellIdentifierForRow:row];
  43. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
  44. [dataSource configureCell:cell forRow:row];
  45. if ([[dataSource cellIdentifierForRow:indexPath.row] isEqual:@"VLCNetworkLoginViewFieldCellIdentifier"]) {
  46. if (cell == nil) {
  47. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
  48. }
  49. tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
  50. } else {
  51. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  52. }
  53. return cell;
  54. }
  55. #pragma mark - UITableViewDelegate
  56. //- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  57. //{
  58. // id<VLCNetworkLoginDataSourceSection> dataSource = self.dataSources[indexPath.section];
  59. // if (![[dataSource cellIdentifierForRow:indexPath.row] isEqual:@"VLCNetworkLoginViewFieldCellIdentifier"]) {
  60. // cell.backgroundColor = (indexPath.row % 2 == 0)? PresentationTheme.current.colors.cellBackgroundA : PresentationTheme.current.colors.cellBackgroundB;
  61. // }
  62. //}
  63. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  64. {
  65. BOOL editable = YES;
  66. id<VLCNetworkLoginDataSourceSection> dataSource = self.dataSources[indexPath.section];
  67. if ([dataSource respondsToSelector:@selector(canEditRow:)]) {
  68. editable = [dataSource canEditRow:indexPath.row];
  69. }
  70. return editable;
  71. }
  72. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  73. {
  74. id<VLCNetworkLoginDataSourceSection> dataSource = self.dataSources[indexPath.section];
  75. if ([dataSource respondsToSelector:@selector(commitEditingStyle:forRow:)]) {
  76. [dataSource commitEditingStyle:editingStyle forRow:indexPath.row];
  77. }
  78. }
  79. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  80. {
  81. id<VLCNetworkLoginDataSourceSection> dataSource = self.dataSources[indexPath.section];
  82. if ([[dataSource cellIdentifierForRow:indexPath.row] isEqual:@"VLCNetworkLoginSavedLoginCell"]) {
  83. return UITableViewCellEditingStyleDelete;
  84. } else {
  85. return UITableViewCellEditingStyleNone;
  86. }
  87. }
  88. - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  89. NSIndexPath *targetIndexPath = indexPath;
  90. id<VLCNetworkLoginDataSourceSection> dataSource = self.dataSources[indexPath.section];
  91. if ([dataSource respondsToSelector:@selector(willSelectRow:)]) {
  92. NSUInteger targetRow = [dataSource willSelectRow:indexPath.row];
  93. if (targetRow == NSNotFound) {
  94. targetIndexPath = nil;
  95. } else if (targetRow != indexPath.row) {
  96. targetIndexPath = [NSIndexPath indexPathForRow:targetRow inSection:indexPath.section];
  97. }
  98. } else if (![dataSource respondsToSelector:@selector(didSelectRow:)]) {
  99. targetIndexPath = nil;
  100. }
  101. return targetIndexPath;
  102. }
  103. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  104. {
  105. id<VLCNetworkLoginDataSourceSection> dataSource = self.dataSources[indexPath.section];
  106. if ([dataSource respondsToSelector:@selector(didSelectRow:)]) {
  107. [dataSource didSelectRow:indexPath.row];
  108. }
  109. }
  110. @end