VLCMenuTableViewController.m 14 KB

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