VLCMenuTableViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 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. NSMutableSet *_hiddenSettingKeys;
  46. UITableView *_menuTableView;
  47. NSArray <NSLayoutConstraint *> *_leftTableConstraints;
  48. CGFloat _tableViewWidth;
  49. }
  50. @property (strong, nonatomic) IASKAppSettingsViewController *settingsViewController;
  51. @property (strong, nonatomic) VLCSettingsController *settingsController;
  52. @end
  53. @implementation VLCMenuTableViewController
  54. - (void)dealloc
  55. {
  56. [[NSNotificationCenter defaultCenter] removeObserver:self];
  57. }
  58. - (void)viewDidLoad
  59. {
  60. [super viewDidLoad];
  61. _sectionHeaderTexts = @[@"SECTION_HEADER_LIBRARY", @"SECTION_HEADER_NETWORK", @"Settings"];
  62. _menuItemsSectionOne = @[@"LIBRARY_ALL_FILES", @"LIBRARY_MUSIC", @"LIBRARY_SERIES"];
  63. _menuItemsSectionTwo = @[@"LOCAL_NETWORK", @"OPEN_NETWORK", @"DOWNLOAD_FROM_HTTP", @"WEBINTF_TITLE", @"CLOUD_SERVICES"];
  64. _menuItemsSectionThree = @[@"Settings", @"ABOUT_APP"];
  65. NSUInteger count = _menuItemsSectionOne.count + _menuItemsSectionTwo.count + _menuItemsSectionThree.count;
  66. CGRect screenDimensions = [UIScreen mainScreen].bounds;
  67. CGFloat rowHeight;
  68. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  69. rowHeight = IPAD_ROW_HEIGHT;
  70. else
  71. rowHeight = ROW_HEIGHT;
  72. CGFloat height = (count * rowHeight) + (3. * HEADER_HEIGHT);
  73. CGFloat top;
  74. if (height > screenDimensions.size.height - COMPACT_INSET) {
  75. height = screenDimensions.size.height - COMPACT_INSET;
  76. top = COMPACT_INSET;
  77. } else
  78. top = (screenDimensions.size.height - height) / 2.;
  79. CGFloat left;
  80. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  81. left = MAX_LEFT_INSET;
  82. else
  83. left = COMPACT_INSET;
  84. _tableViewWidth = MENU_WIDTH;
  85. if (screenDimensions.size.width <= left + _tableViewWidth)
  86. _tableViewWidth = _tableViewWidth - (left * 2.);
  87. _menuTableView = [[UITableView alloc] initWithFrame:CGRectMake(left, top, _tableViewWidth, height)
  88. style:UITableViewStylePlain];
  89. _menuTableView.delegate = self;
  90. _menuTableView.dataSource = self;
  91. _menuTableView.backgroundColor = [UIColor VLCMenuBackgroundColor];
  92. _menuTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  93. _menuTableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  94. _menuTableView.rowHeight = rowHeight;
  95. _menuTableView.scrollsToTop = NO;
  96. _menuTableView.translatesAutoresizingMaskIntoConstraints = NO;
  97. _menuTableView.showsHorizontalScrollIndicator = NO;
  98. _menuTableView.showsVerticalScrollIndicator = NO;
  99. [self.view addSubview:_menuTableView];
  100. NSDictionary *dict;
  101. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  102. UIView *spacer1 = [UIView new];
  103. UIView *spacer2 = [UIView new];
  104. spacer1.translatesAutoresizingMaskIntoConstraints = NO;
  105. spacer2.translatesAutoresizingMaskIntoConstraints = NO;
  106. [self.view addSubview:spacer1];
  107. [self.view addSubview:spacer2];
  108. dict = NSDictionaryOfVariableBindings(_menuTableView, spacer1, spacer2);
  109. [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|[spacer1][_menuTableView(==%0.2f)][spacer2(==spacer1)]|", height] options:0 metrics:0 views:dict]];
  110. } else {
  111. dict = NSDictionaryOfVariableBindings(_menuTableView);
  112. [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-==%0.2f-[_menuTableView(<=%0.2f)]-==%0.2f-|", top, height, top] options:0 metrics:0 views:dict]];
  113. }
  114. dict = NSDictionaryOfVariableBindings(_menuTableView);
  115. _leftTableConstraints = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-==%0.2f-[_menuTableView(%0.2f)]->=0-|", left, _tableViewWidth] options:0 metrics:0 views:dict];
  116. [self.view addConstraints:_leftTableConstraints];
  117. [_menuTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
  118. }
  119. - (BOOL)shouldAutorotate
  120. {
  121. UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
  122. return (orientation == UIInterfaceOrientationPortraitUpsideDown) ? (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) : YES;
  123. }
  124. - (BOOL)canBecomeFirstResponder
  125. {
  126. return YES;
  127. }
  128. - (void)viewWillLayoutSubviews
  129. {
  130. CGFloat viewWidth = self.view.frame.size.width;
  131. [self.view removeConstraints:_leftTableConstraints];
  132. CGFloat left;
  133. if (viewWidth >= _tableViewWidth) {
  134. left = (viewWidth - _tableViewWidth) / 3.;
  135. if (left > MAX_LEFT_INSET)
  136. left = MAX_LEFT_INSET;
  137. } else {
  138. _tableViewWidth = MENU_WIDTH;
  139. left = 0.;
  140. }
  141. NSDictionary *dict = NSDictionaryOfVariableBindings(_menuTableView);
  142. _leftTableConstraints = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-==%0.2f-[_menuTableView(%0.2f)]->=0-|", left, _tableViewWidth] options:0 metrics:0 views:dict];
  143. [self.view addConstraints:_leftTableConstraints];
  144. [super viewWillLayoutSubviews];
  145. [[VLCSidebarController sharedInstance] resizeContentView];
  146. }
  147. #pragma mark - table view data source
  148. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  149. {
  150. return 3;
  151. }
  152. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  153. {
  154. if (section == 0) // media
  155. return _menuItemsSectionOne.count;
  156. else if (section == 1) // network
  157. return _menuItemsSectionTwo.count;
  158. else if (section == 2) // settings & co
  159. return _menuItemsSectionThree.count;
  160. else
  161. return 0;
  162. }
  163. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  164. {
  165. NSString *rawTitle;
  166. NSUInteger section = indexPath.section;
  167. if (section == 0)
  168. rawTitle = _menuItemsSectionOne[indexPath.row];
  169. else if(section == 1)
  170. rawTitle = _menuItemsSectionTwo[indexPath.row];
  171. else if(section == 2)
  172. rawTitle = _menuItemsSectionThree[indexPath.row];
  173. UITableViewCell *cell;
  174. if ([rawTitle isEqualToString:@"WEBINTF_TITLE"]) {
  175. cell = (VLCWiFiUploadTableViewCell *)[tableView dequeueReusableCellWithIdentifier:WiFiCellIdentifier];
  176. if (cell == nil) {
  177. cell = [[VLCWiFiUploadTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:WiFiCellIdentifier];
  178. }
  179. } else {
  180. cell = (VLCSidebarViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  181. if (cell == nil)
  182. cell = [[VLCSidebarViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  183. }
  184. if (section == 0) {
  185. if ([rawTitle isEqualToString:@"LIBRARY_ALL_FILES"])
  186. cell.imageView.image = [UIImage imageNamed:@"AllFiles"];
  187. else if ([rawTitle isEqualToString:@"LIBRARY_MUSIC"])
  188. cell.imageView.image = [UIImage imageNamed:@"MusicAlbums"];
  189. else if ([rawTitle isEqualToString:@"LIBRARY_SERIES"])
  190. cell.imageView.image = [UIImage imageNamed:@"TVShowsIcon"];
  191. } else if (section == 1) {
  192. if ([rawTitle isEqualToString:@"LOCAL_NETWORK"])
  193. cell.imageView.image = [UIImage imageNamed:@"Local"];
  194. else if ([rawTitle isEqualToString:@"OPEN_NETWORK"])
  195. cell.imageView.image = [UIImage imageNamed:@"OpenNetStream"];
  196. else if ([rawTitle isEqualToString:@"DOWNLOAD_FROM_HTTP"])
  197. cell.imageView.image = [UIImage imageNamed:@"Downloads"];
  198. else if ([rawTitle isEqualToString:@"CLOUD_SERVICES"])
  199. cell.imageView.image = [UIImage imageNamed:@"iCloudIcon"];
  200. } else if (section == 2) {
  201. if ([rawTitle isEqualToString:@"Settings"])
  202. cell.imageView.image = [UIImage imageNamed:@"Settings"];
  203. else
  204. cell.imageView.image = [UIImage imageNamed:@"menuCone"];
  205. }
  206. if (![rawTitle isEqualToString:@"WEBINTF_TITLE"])
  207. cell.textLabel.text = NSLocalizedString(rawTitle, nil);
  208. return cell;
  209. }
  210. #pragma mark - table view delegation
  211. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  212. {
  213. if (section < 3)
  214. return 22.f;
  215. return 0.;
  216. }
  217. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  218. {
  219. NSString *headerText = NSLocalizedString(_sectionHeaderTexts[section], nil);
  220. UIView *headerView = nil;
  221. if (headerText) {
  222. headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, HEADER_HEIGHT)];
  223. headerView.backgroundColor = [UIColor VLCMenuBackgroundColor];
  224. UILabel *textLabel = [UILabel new];
  225. textLabel.text = headerText;
  226. textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:[UIFont systemFontSize]];
  227. textLabel.textColor = [UIColor whiteColor];
  228. textLabel.backgroundColor = [UIColor VLCMenuBackgroundColor];
  229. [textLabel sizeToFit];
  230. textLabel.translatesAutoresizingMaskIntoConstraints = NO;
  231. [headerView addSubview:textLabel];
  232. UIView *bottomLine = [UIView new];
  233. bottomLine.backgroundColor = [UIColor whiteColor];
  234. bottomLine.translatesAutoresizingMaskIntoConstraints = NO;
  235. [headerView addSubview:bottomLine];
  236. NSDictionary *dict = NSDictionaryOfVariableBindings(textLabel,bottomLine);
  237. [headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[bottomLine]|" options:0 metrics:0 views:dict]];
  238. [headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(12)-[textLabel]" options:0 metrics:0 views:dict]];
  239. [headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|->=0-[textLabel]->=0-[bottomLine(0.5)]|" options:0 metrics:0 views:dict]];
  240. }
  241. return headerView;
  242. }
  243. #pragma mark - menu implementation
  244. - (void)_revealItem:(NSUInteger)itemIndex inSection:(NSUInteger)sectionNumber
  245. {
  246. UIViewController *viewController;
  247. VLCAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
  248. if (sectionNumber == 1) {
  249. if (itemIndex == 0)
  250. viewController = [[VLCServerListViewController alloc] init];
  251. else if (itemIndex == 1) {
  252. viewController = [[VLCOpenNetworkStreamViewController alloc] initWithNibName:@"VLCOpenNetworkStreamViewController" bundle:nil];
  253. } else if (itemIndex == 2)
  254. viewController = [VLCDownloadViewController sharedInstance];
  255. else if (itemIndex == 3)
  256. [((VLCWiFiUploadTableViewCell *)[_menuTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:itemIndex inSection:sectionNumber]]) toggleHTTPServer];
  257. else if (itemIndex == 4)
  258. viewController = [[VLCCloudServicesTableViewController alloc] initWithNibName:@"VLCCloudServicesTableViewController" bundle:nil];
  259. } else if (sectionNumber == 2) {
  260. if (itemIndex == 0) {
  261. if (!self.settingsController)
  262. self.settingsController = [[VLCSettingsController alloc] init];
  263. VLCSettingsController *settingsController = self.settingsController;
  264. if (!self.settingsViewController) {
  265. self.settingsViewController = [[IASKAppSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
  266. settingsController.viewController = self.settingsViewController;
  267. self.settingsViewController.navigationItem.leftBarButtonItem = [UIBarButtonItem themedRevealMenuButtonWithTarget:self.settingsController.viewController andSelector:@selector(dismiss:)];
  268. }
  269. IASKAppSettingsViewController *settingsVC = self.settingsViewController;
  270. settingsVC.modalPresentationStyle = UIModalPresentationFormSheet;
  271. settingsVC.delegate = self.settingsController;
  272. settingsVC.showDoneButton = NO;
  273. settingsVC.showCreditsFooter = NO;
  274. viewController = settingsVC;
  275. [self.settingsController willShow];
  276. } else if (itemIndex == 1)
  277. viewController = [[VLCAboutViewController alloc] init];
  278. } else {
  279. viewController = appDelegate.libraryViewController;
  280. [(VLCLibraryViewController *)viewController setLibraryMode:(int)itemIndex];
  281. }
  282. if (!viewController)
  283. return;
  284. VLCSidebarController *sidebarController = [VLCSidebarController sharedInstance];
  285. if ([sidebarController.contentViewController isKindOfClass:[UINavigationController class]]) {
  286. UINavigationController *navCon = (UINavigationController*)sidebarController.contentViewController;
  287. navCon.viewControllers = @[viewController];
  288. } else
  289. sidebarController.contentViewController = [[VLCNavigationController alloc] initWithRootViewController:viewController];
  290. [sidebarController hideSidebar];
  291. }
  292. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  293. {
  294. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  295. [self _revealItem:indexPath.row inSection:indexPath.section];
  296. }
  297. #pragma mark Public Methods
  298. - (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
  299. {
  300. [_menuTableView selectRowAtIndexPath:indexPath animated:animated scrollPosition:scrollPosition];
  301. if (scrollPosition == UITableViewScrollPositionNone)
  302. [_menuTableView scrollToRowAtIndexPath:indexPath atScrollPosition:scrollPosition animated:animated];
  303. [self _revealItem:indexPath.row inSection:indexPath.section];
  304. }
  305. #pragma mark - shake for support
  306. - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
  307. {
  308. if (motion == UIEventSubtypeMotionShake)
  309. [[VLCBugreporter sharedInstance] handleBugreportRequest];
  310. }
  311. @end