VLCMenuTableViewController.m 12 KB

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