VLCMenuTableViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 "VLCAppDelegate.h"
  22. #import "VLCServerListViewController.h"
  23. #import "VLCOpenNetworkStreamViewController.h"
  24. #import "VLCSettingsController.h"
  25. #import "VLCAboutViewController.h"
  26. #import "VLCLibraryViewController.h"
  27. #import "VLCBugreporter.h"
  28. #import "VLCCloudServicesTableViewController.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>
  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. }
  50. @end
  51. @implementation VLCMenuTableViewController
  52. - (void)viewDidLoad
  53. {
  54. [super viewDidLoad];
  55. _sectionHeaderTexts = @[@"SECTION_HEADER_LIBRARY", @"SECTION_HEADER_NETWORK", @"Settings"];
  56. _menuItemsSectionOne = @[@"LIBRARY_ALL_FILES", @"LIBRARY_MUSIC", @"LIBRARY_SERIES"];
  57. _menuItemsSectionTwo = @[@"LOCAL_NETWORK", @"NETWORK_TITLE", @"DOWNLOAD_FROM_HTTP", @"WEBINTF_TITLE", @"CLOUD_SERVICES"];
  58. _menuItemsSectionThree = @[@"Settings", @"ABOUT_APP"];
  59. _menuTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  60. _menuTableView.delegate = self;
  61. _menuTableView.dataSource = self;
  62. _menuTableView.backgroundColor = [UIColor VLCMenuBackgroundColor];
  63. _menuTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  64. _menuTableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  65. _menuTableView.rowHeight = UITableViewAutomaticDimension;
  66. _menuTableView.sectionHeaderHeight = UITableViewAutomaticDimension;
  67. _menuTableView.estimatedSectionHeaderHeight = HEADER_HEIGHT;
  68. _menuTableView.estimatedRowHeight = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? IPAD_ROW_HEIGHT : ROW_HEIGHT;
  69. _menuTableView.scrollsToTop = NO;
  70. _menuTableView.translatesAutoresizingMaskIntoConstraints = NO;
  71. _menuTableView.showsHorizontalScrollIndicator = NO;
  72. _menuTableView.showsVerticalScrollIndicator = NO;
  73. [_menuTableView registerClass:[VLCWiFiUploadTableViewCell class] forCellReuseIdentifier:WiFiCellIdentifier];
  74. [_menuTableView registerClass:[VLCSidebarViewCell class] forCellReuseIdentifier:CellIdentifier];
  75. [self.view addSubview:_menuTableView];
  76. NSDictionary *dict;
  77. dict = NSDictionaryOfVariableBindings(_menuTableView);
  78. NSDictionary *metrics = @{@"TopPadding": @(TOP_PADDING),
  79. @"Standard": @(STANDARD_PADDING),
  80. @"menuWidth" : @(MENU_WIDTH)
  81. };
  82. // 20 to avoid seeing the tableview above the first sectionheader
  83. [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|->=TopPadding-[_menuTableView]->=Standard-|" options:0 metrics:metrics views:dict]];
  84. _heightConstraint = [NSLayoutConstraint constraintWithItem:_menuTableView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0 constant:0];
  85. _heightConstraint.priority = UILayoutPriorityRequired -1;
  86. [self.view addConstraint:_heightConstraint];
  87. [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_menuTableView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
  88. _leftTableConstraint = [NSLayoutConstraint constraintWithItem:_menuTableView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0];
  89. [self.view addConstraint:_leftTableConstraint];
  90. [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|->=0-[_menuTableView(==menuWidth)]" options:0 metrics:metrics views:dict]];
  91. [_menuTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
  92. }
  93. - (BOOL)shouldAutorotate
  94. {
  95. UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
  96. return (orientation == UIInterfaceOrientationPortraitUpsideDown) ? (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) : YES;
  97. }
  98. - (BOOL)canBecomeFirstResponder
  99. {
  100. return YES;
  101. }
  102. - (void)viewDidLayoutSubviews
  103. {
  104. [super viewDidLayoutSubviews];
  105. _heightConstraint.constant = MIN(_menuTableView.contentSize.height, self.view.frame.size.height-TOP_PADDING-STANDARD_PADDING);
  106. _leftTableConstraint.constant = MAX((self.view.frame.size.width*2 /3.0 - _menuTableView.frame.size.width)/2.0, STANDARD_PADDING);
  107. }
  108. #pragma mark - table view data source
  109. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  110. {
  111. return 3;
  112. }
  113. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  114. {
  115. if (section == 0) // media
  116. return _menuItemsSectionOne.count;
  117. else if (section == 1) // network
  118. return _menuItemsSectionTwo.count;
  119. else if (section == 2) // settings & co
  120. return _menuItemsSectionThree.count;
  121. else
  122. return 0;
  123. }
  124. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  125. {
  126. NSString *rawTitle;
  127. NSUInteger section = indexPath.section;
  128. if (section == 0)
  129. rawTitle = _menuItemsSectionOne[indexPath.row];
  130. else if(section == 1)
  131. rawTitle = _menuItemsSectionTwo[indexPath.row];
  132. else if(section == 2)
  133. rawTitle = _menuItemsSectionThree[indexPath.row];
  134. UITableViewCell *cell;
  135. if ([rawTitle isEqualToString:@"WEBINTF_TITLE"]) {
  136. cell = (VLCWiFiUploadTableViewCell *)[tableView dequeueReusableCellWithIdentifier:WiFiCellIdentifier];
  137. } else {
  138. cell = (VLCSidebarViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  139. }
  140. if (section == 0) {
  141. if ([rawTitle isEqualToString:@"LIBRARY_ALL_FILES"])
  142. cell.imageView.image = [UIImage imageNamed:@"AllFiles"];
  143. else if ([rawTitle isEqualToString:@"LIBRARY_MUSIC"])
  144. cell.imageView.image = [UIImage imageNamed:@"MusicAlbums"];
  145. else if ([rawTitle isEqualToString:@"LIBRARY_SERIES"])
  146. cell.imageView.image = [UIImage imageNamed:@"TVShowsIcon"];
  147. } else if (section == 1) {
  148. if ([rawTitle isEqualToString:@"LOCAL_NETWORK"])
  149. cell.imageView.image = [UIImage imageNamed:@"Local"];
  150. else if ([rawTitle isEqualToString:@"NETWORK_TITLE"])
  151. cell.imageView.image = [UIImage imageNamed:@"OpenNetStream"];
  152. else if ([rawTitle isEqualToString:@"DOWNLOAD_FROM_HTTP"])
  153. cell.imageView.image = [UIImage imageNamed:@"Downloads"];
  154. else if ([rawTitle isEqualToString:@"CLOUD_SERVICES"])
  155. cell.imageView.image = [UIImage imageNamed:@"iCloudIcon"];
  156. } else if (section == 2) {
  157. if ([rawTitle isEqualToString:@"Settings"])
  158. cell.imageView.image = [UIImage imageNamed:@"Settings"];
  159. else
  160. cell.imageView.image = [UIImage imageNamed:@"menuCone"];
  161. }
  162. if (![rawTitle isEqualToString:@"WEBINTF_TITLE"])
  163. cell.textLabel.text = NSLocalizedString(rawTitle, nil);
  164. return cell;
  165. }
  166. #pragma mark - table view delegation
  167. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  168. {
  169. NSString *headerText = NSLocalizedString(_sectionHeaderTexts[section], nil);
  170. UIView *headerView = nil;
  171. if (headerText) {
  172. headerView = [[UIView alloc] initWithFrame:CGRectZero];
  173. headerView.backgroundColor = [UIColor VLCMenuBackgroundColor];
  174. UILabel *textLabel = [UILabel new];
  175. textLabel.text = headerText;
  176. textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:[UIFont systemFontSize]];
  177. textLabel.textColor = [UIColor whiteColor];
  178. textLabel.backgroundColor = [UIColor VLCMenuBackgroundColor];
  179. [textLabel sizeToFit];
  180. textLabel.translatesAutoresizingMaskIntoConstraints = NO;
  181. [headerView addSubview:textLabel];
  182. UIView *bottomLine = [UIView new];
  183. bottomLine.backgroundColor = [UIColor whiteColor];
  184. bottomLine.translatesAutoresizingMaskIntoConstraints = NO;
  185. [headerView addSubview:bottomLine];
  186. NSDictionary *dict = NSDictionaryOfVariableBindings(textLabel,bottomLine);
  187. [headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[bottomLine]|" options:0 metrics:0 views:dict]];
  188. [headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(12)-[textLabel]" options:0 metrics:0 views:dict]];
  189. [headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|->=0-[textLabel(==22)]->=0-[bottomLine(0.5)]|" options:0 metrics:0 views:dict]];
  190. }
  191. return headerView;
  192. }
  193. #pragma mark - menu implementation
  194. - (VLCSettingsController *)settingsController
  195. {
  196. if (!_settingsController){
  197. _settingsController = [[VLCSettingsController alloc] initWithStyle:UITableViewStyleGrouped];
  198. }
  199. return _settingsController;
  200. }
  201. - (void)_revealItem:(NSUInteger)itemIndex inSection:(NSUInteger)sectionNumber
  202. {
  203. UIViewController *viewController;
  204. VLCAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
  205. if (sectionNumber == 1) {
  206. if (itemIndex == 0)
  207. viewController = [[VLCServerListViewController alloc] init];
  208. else if (itemIndex == 1) {
  209. viewController = [[VLCOpenNetworkStreamViewController alloc] initWithNibName:@"VLCOpenNetworkStreamViewController" bundle:nil];
  210. } else if (itemIndex == 2)
  211. viewController = [VLCDownloadViewController sharedInstance];
  212. else if (itemIndex == 3)
  213. [((VLCWiFiUploadTableViewCell *)[_menuTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:itemIndex inSection:sectionNumber]]) toggleHTTPServer];
  214. else if (itemIndex == 4)
  215. viewController = [[VLCCloudServicesTableViewController alloc] initWithNibName:@"VLCCloudServicesTableViewController" bundle:nil];
  216. } else if (sectionNumber == 2) {
  217. if (itemIndex == 0) {
  218. viewController = self.settingsController;
  219. } else if (itemIndex == 1)
  220. viewController = [[VLCAboutViewController alloc] init];
  221. } else {
  222. viewController = appDelegate.libraryViewController;
  223. [(VLCLibraryViewController *)viewController setLibraryMode:(int)itemIndex];
  224. }
  225. if (!viewController) {
  226. APLog(@"no view controller found for menu item");
  227. return;
  228. }
  229. VLCSidebarController *sidebarController = [VLCSidebarController sharedInstance];
  230. if ([sidebarController.contentViewController isKindOfClass:[UINavigationController class]]) {
  231. UINavigationController *navCon = (UINavigationController*)sidebarController.contentViewController;
  232. navCon.viewControllers = @[viewController];
  233. } else
  234. sidebarController.contentViewController = [[UINavigationController alloc] initWithRootViewController:viewController];
  235. [sidebarController hideSidebar];
  236. }
  237. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  238. {
  239. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  240. [self _revealItem:indexPath.row inSection:indexPath.section];
  241. }
  242. #pragma mark Public Methods
  243. - (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
  244. {
  245. [_menuTableView selectRowAtIndexPath:indexPath animated:animated scrollPosition:scrollPosition];
  246. if (scrollPosition == UITableViewScrollPositionNone)
  247. [_menuTableView scrollToRowAtIndexPath:indexPath atScrollPosition:scrollPosition animated:animated];
  248. [self _revealItem:indexPath.row inSection:indexPath.section];
  249. }
  250. #pragma mark - shake for support
  251. - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
  252. {
  253. if (motion == UIEventSubtypeMotionShake)
  254. [[VLCBugreporter sharedInstance] handleBugreportRequest];
  255. }
  256. @end