VLCMenuTableViewController.m 14 KB

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