VLCMenuTableViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*****************************************************************************
  2. * VLCMenuTableViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. * Gleb Pinigin <gpinigin # gmail.com>
  10. * Jean-Romain Prévost <jr # 3on.fr>
  11. * Carola Nitz <nitz.carola # googlemail.com>
  12. * Tamas Timar <ttimar.vlc # gmail.com>
  13. * Pierre Sagaspe <pierre.sagaspe # me.com>
  14. *
  15. * Refer to the COPYING file of the official project for license.
  16. *****************************************************************************/
  17. #import "VLCMenuTableViewController.h"
  18. #import "VLCSidebarViewCell.h"
  19. #import <QuartzCore/QuartzCore.h>
  20. #import "VLCWiFiUploadTableViewCell.h"
  21. #import "VLCDownloadViewController.h"
  22. #import "VLCServerListViewController.h"
  23. #import "VLCOpenNetworkStreamViewController.h"
  24. #import "VLCSettingsController.h"
  25. #import "VLCAboutViewController.h"
  26. #import "VLCBugreporter.h"
  27. #import "VLCCloudServicesTableViewController.h"
  28. #import "VLC_iOS-Swift.h"
  29. #define ROW_HEIGHT 50.
  30. #define IPAD_ROW_HEIGHT 65.
  31. #define HEADER_HEIGHT 22.
  32. #define MENU_WIDTH 320.
  33. #define TOP_PADDING 20.
  34. #define STANDARD_PADDING 8.
  35. #define MAX_LEFT_INSET 170.
  36. #define COMPACT_INSET 20.
  37. static NSString *CellIdentifier = @"VLCMenuCell";
  38. static NSString *WiFiCellIdentifier = @"VLCMenuWiFiCell";
  39. @interface VLCMenuTableViewController () <UITableViewDataSource, UITableViewDelegate, VLCMediaViewControllerDelegate>
  40. {
  41. NSArray *_sectionHeaderTexts;
  42. NSArray *_menuItemsSectionOne;
  43. NSArray *_menuItemsSectionTwo;
  44. NSArray *_menuItemsSectionThree;
  45. UITableView *_menuTableView;
  46. NSLayoutConstraint *_heightConstraint;
  47. NSLayoutConstraint *_leftTableConstraint;
  48. VLCSettingsController *_settingsController;
  49. VLCMediaViewController *_videoViewController;
  50. }
  51. @end
  52. @implementation VLCMenuTableViewController
  53. - (void)viewDidLoad
  54. {
  55. [super viewDidLoad];
  56. _sectionHeaderTexts = @[@"SECTION_HEADER_LIBRARY", @"SECTION_HEADER_NETWORK", @"Settings"];
  57. _menuItemsSectionOne = @[@"LIBRARY_ALL_FILES", @"LIBRARY_MUSIC", @"LIBRARY_SERIES"];
  58. _menuItemsSectionTwo = @[@"LOCAL_NETWORK", @"NETWORK_TITLE", @"DOWNLOAD_FROM_HTTP", @"WEBINTF_TITLE", @"CLOUD_SERVICES"];
  59. _menuItemsSectionThree = @[@"Settings", @"ABOUT_APP"];
  60. _menuTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  61. _menuTableView.delegate = self;
  62. _menuTableView.dataSource = self;
  63. _menuTableView.backgroundColor = [UIColor VLCMenuBackgroundColor];
  64. _menuTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  65. _menuTableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  66. _menuTableView.rowHeight = UITableViewAutomaticDimension;
  67. _menuTableView.sectionHeaderHeight = UITableViewAutomaticDimension;
  68. _menuTableView.estimatedSectionHeaderHeight = HEADER_HEIGHT;
  69. _menuTableView.estimatedRowHeight = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? IPAD_ROW_HEIGHT : ROW_HEIGHT;
  70. _menuTableView.scrollsToTop = NO;
  71. _menuTableView.translatesAutoresizingMaskIntoConstraints = NO;
  72. _menuTableView.showsHorizontalScrollIndicator = NO;
  73. _menuTableView.showsVerticalScrollIndicator = NO;
  74. [_menuTableView registerClass:[VLCWiFiUploadTableViewCell class] forCellReuseIdentifier:WiFiCellIdentifier];
  75. [_menuTableView registerClass:[VLCSidebarViewCell class] forCellReuseIdentifier:CellIdentifier];
  76. [self.view addSubview:_menuTableView];
  77. NSDictionary *dict;
  78. dict = NSDictionaryOfVariableBindings(_menuTableView);
  79. NSDictionary *metrics = @{@"TopPadding": @(TOP_PADDING),
  80. @"Standard": @(STANDARD_PADDING),
  81. @"menuWidth" : @(MENU_WIDTH)
  82. };
  83. // 20 to avoid seeing the tableview above the first sectionheader
  84. [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|->=TopPadding-[_menuTableView]->=Standard-|" options:0 metrics:metrics views:dict]];
  85. _heightConstraint = [NSLayoutConstraint constraintWithItem:_menuTableView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0 constant:0];
  86. _heightConstraint.priority = UILayoutPriorityRequired -1;
  87. [self.view addConstraint:_heightConstraint];
  88. [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_menuTableView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
  89. _leftTableConstraint = [NSLayoutConstraint constraintWithItem:_menuTableView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0];
  90. [self.view addConstraint:_leftTableConstraint];
  91. [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|->=0-[_menuTableView(==menuWidth)]" options:0 metrics:metrics views:dict]];
  92. [self selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
  93. }
  94. - (BOOL)shouldAutorotate
  95. {
  96. UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
  97. return (orientation == UIInterfaceOrientationPortraitUpsideDown) ? (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) : YES;
  98. }
  99. - (BOOL)canBecomeFirstResponder
  100. {
  101. return YES;
  102. }
  103. - (void)viewDidLayoutSubviews
  104. {
  105. [super viewDidLayoutSubviews];
  106. _heightConstraint.constant = MIN(_menuTableView.contentSize.height, self.view.frame.size.height-TOP_PADDING-STANDARD_PADDING);
  107. _leftTableConstraint.constant = MAX((self.view.frame.size.width*2 /3.0 - _menuTableView.frame.size.width)/2.0, STANDARD_PADDING);
  108. }
  109. #pragma mark - table view data source
  110. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  111. {
  112. return 3;
  113. }
  114. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  115. {
  116. if (section == 0) // media
  117. return _menuItemsSectionOne.count;
  118. else if (section == 1) // network
  119. return _menuItemsSectionTwo.count;
  120. else if (section == 2) // settings & co
  121. return _menuItemsSectionThree.count;
  122. else
  123. return 0;
  124. }
  125. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  126. {
  127. NSString *rawTitle;
  128. NSUInteger section = indexPath.section;
  129. if (section == 0)
  130. rawTitle = _menuItemsSectionOne[indexPath.row];
  131. else if(section == 1)
  132. rawTitle = _menuItemsSectionTwo[indexPath.row];
  133. else if(section == 2)
  134. rawTitle = _menuItemsSectionThree[indexPath.row];
  135. UITableViewCell *cell;
  136. if ([rawTitle isEqualToString:@"WEBINTF_TITLE"]) {
  137. cell = (VLCWiFiUploadTableViewCell *)[tableView dequeueReusableCellWithIdentifier:WiFiCellIdentifier];
  138. } else {
  139. cell = (VLCSidebarViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  140. }
  141. if (section == 0) {
  142. if ([rawTitle isEqualToString:@"LIBRARY_ALL_FILES"])
  143. cell.imageView.image = [UIImage imageNamed:@"AllFiles"];
  144. else if ([rawTitle isEqualToString:@"LIBRARY_MUSIC"])
  145. cell.imageView.image = [UIImage imageNamed:@"MusicAlbums"];
  146. else if ([rawTitle isEqualToString:@"LIBRARY_SERIES"])
  147. cell.imageView.image = [UIImage imageNamed:@"TVShowsIcon"];
  148. } else if (section == 1) {
  149. if ([rawTitle isEqualToString:@"LOCAL_NETWORK"])
  150. cell.imageView.image = [UIImage imageNamed:@"Local"];
  151. else if ([rawTitle isEqualToString:@"NETWORK_TITLE"])
  152. cell.imageView.image = [UIImage imageNamed:@"OpenNetStream"];
  153. else if ([rawTitle isEqualToString:@"DOWNLOAD_FROM_HTTP"])
  154. cell.imageView.image = [UIImage imageNamed:@"Downloads"];
  155. else if ([rawTitle isEqualToString:@"CLOUD_SERVICES"])
  156. cell.imageView.image = [UIImage imageNamed:@"iCloudIcon"];
  157. } else if (section == 2) {
  158. if ([rawTitle isEqualToString:@"Settings"])
  159. cell.imageView.image = [UIImage imageNamed:@"Settings"];
  160. else
  161. cell.imageView.image = [UIImage imageNamed:@"menuCone"];
  162. }
  163. if (![rawTitle isEqualToString:@"WEBINTF_TITLE"])
  164. cell.textLabel.text = NSLocalizedString(rawTitle, nil);
  165. return cell;
  166. }
  167. #pragma mark - table view delegation
  168. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  169. {
  170. NSString *headerText = NSLocalizedString(_sectionHeaderTexts[section], nil);
  171. UIView *headerView = nil;
  172. if (headerText) {
  173. headerView = [[UIView alloc] initWithFrame:CGRectZero];
  174. headerView.backgroundColor = [UIColor VLCMenuBackgroundColor];
  175. UILabel *textLabel = [UILabel new];
  176. textLabel.text = headerText;
  177. textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:[UIFont systemFontSize]];
  178. textLabel.textColor = [UIColor whiteColor];
  179. textLabel.backgroundColor = [UIColor VLCMenuBackgroundColor];
  180. [textLabel sizeToFit];
  181. textLabel.translatesAutoresizingMaskIntoConstraints = NO;
  182. [headerView addSubview:textLabel];
  183. UIView *bottomLine = [UIView new];
  184. bottomLine.backgroundColor = [UIColor whiteColor];
  185. bottomLine.translatesAutoresizingMaskIntoConstraints = NO;
  186. [headerView addSubview:bottomLine];
  187. NSDictionary *dict = NSDictionaryOfVariableBindings(textLabel,bottomLine);
  188. [headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[bottomLine]|" options:0 metrics:0 views:dict]];
  189. [headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(12)-[textLabel]" options:0 metrics:0 views:dict]];
  190. [headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|->=0-[textLabel(==22)]->=0-[bottomLine(0.5)]|" options:0 metrics:0 views:dict]];
  191. }
  192. return headerView;
  193. }
  194. #pragma mark - menu implementation
  195. - (VLCSettingsController *)settingsController
  196. {
  197. if (!_settingsController){
  198. _settingsController = [[VLCSettingsController alloc] initWithStyle:UITableViewStyleGrouped];
  199. }
  200. return _settingsController;
  201. }
  202. - (VLCMediaViewController *)videoViewController
  203. {
  204. if (!_videoViewController) {
  205. _videoViewController = [[VLCMediaViewController alloc] initWithCollectionViewLayout:[UICollectionViewFlowLayout new]];
  206. }
  207. return _videoViewController;
  208. }
  209. - (void)_revealItem:(NSUInteger)itemIndex inSection:(NSUInteger)sectionNumber
  210. {
  211. UIViewController *viewController;
  212. if (sectionNumber == 1) {
  213. if (itemIndex == 0)
  214. viewController = [[VLCServerListViewController alloc] init];
  215. else if (itemIndex == 1) {
  216. viewController = [[VLCOpenNetworkStreamViewController alloc] initWithNibName:@"VLCOpenNetworkStreamViewController" bundle:nil];
  217. } else if (itemIndex == 2)
  218. viewController = [VLCDownloadViewController sharedInstance];
  219. else if (itemIndex == 3)
  220. [((VLCWiFiUploadTableViewCell *)[_menuTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:itemIndex inSection:sectionNumber]]) toggleHTTPServer];
  221. else if (itemIndex == 4)
  222. viewController = [[VLCCloudServicesTableViewController alloc] initWithNibName:@"VLCCloudServicesTableViewController" bundle:nil];
  223. } else if (sectionNumber == 2) {
  224. if (itemIndex == 0) {
  225. viewController = self.settingsController;
  226. } else if (itemIndex == 1)
  227. viewController = [[VLCAboutViewController alloc] init];
  228. } else {
  229. viewController = self.videoViewController;
  230. }
  231. if (!viewController) {
  232. APLog(@"no view controller found for menu item");
  233. return;
  234. }
  235. VLCSidebarController *sidebarController = [VLCSidebarController sharedInstance];
  236. if ([sidebarController.contentViewController isKindOfClass:[UINavigationController class]]) {
  237. UINavigationController *navCon = (UINavigationController*)sidebarController.contentViewController;
  238. navCon.viewControllers = @[viewController];
  239. } else
  240. sidebarController.contentViewController = [[UINavigationController alloc] initWithRootViewController:viewController];
  241. [sidebarController hideSidebar];
  242. }
  243. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  244. {
  245. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  246. [self _revealItem:indexPath.row inSection:indexPath.section];
  247. }
  248. #pragma mark Public Methods
  249. - (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
  250. {
  251. [_menuTableView selectRowAtIndexPath:indexPath animated:animated scrollPosition:scrollPosition];
  252. if (scrollPosition == UITableViewScrollPositionNone)
  253. [_menuTableView scrollToRowAtIndexPath:indexPath atScrollPosition:scrollPosition animated:animated];
  254. [self _revealItem:indexPath.row inSection:indexPath.section];
  255. }
  256. #pragma mark - shake for support
  257. - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
  258. {
  259. if (motion == UIEventSubtypeMotionShake)
  260. [[VLCBugreporter sharedInstance] handleBugreportRequest];
  261. }
  262. #pragma mark - VLCMediaViewControllerDelegate
  263. - (void)videoViewControllerDidSelectMediaObjectWithVLCMediaViewController:(VLCMediaViewController *)VLCMediaViewController mediaObject:(NSManagedObject *)mediaObject
  264. {
  265. }
  266. - (void)videoViewControllerDidSelectBackbuttonWithVLCMediaViewController:(VLCMediaViewController *)VLCMediaViewController {
  267. [[VLCSidebarController sharedInstance] toggleSidebar];
  268. }
  269. @end