VLCMenuTableViewController.m 14 KB

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