VLCMenuTableViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 "GHRevealViewController.h"
  19. #import "VLCSidebarViewCell.h"
  20. #import "Reachability.h"
  21. #import <QuartzCore/QuartzCore.h>
  22. #import "VLCWiFiUploadTableViewCell.h"
  23. #import "VLCHTTPUploaderController.h"
  24. #import "VLCAppDelegate.h"
  25. #import "HTTPServer.h"
  26. #import "IASKAppSettingsViewController.h"
  27. #import "GHRevealViewController.h"
  28. #import "VLCLocalServerListViewController.h"
  29. #import "VLCOpenNetworkStreamViewController.h"
  30. #import "VLCSettingsController.h"
  31. #import "VLCAboutViewController.h"
  32. #import "VLCPlaylistViewController.h"
  33. #import "VLCBugreporter.h"
  34. #import "VLCCloudServicesTableViewController.h"
  35. #import "VLCNavigationController.h"
  36. @interface VLCMenuTableViewController () <UITableViewDataSource, UITableViewDelegate>
  37. {
  38. NSArray *_sectionHeaderTexts;
  39. NSArray *_menuItemsSectionOne;
  40. NSArray *_menuItemsSectionTwo;
  41. NSArray *_menuItemsSectionThree;
  42. NSMutableSet *_hiddenSettingKeys;
  43. UILabel *_uploadLocationLabel;
  44. UIButton *_uploadButton;
  45. Reachability *_reachability;
  46. }
  47. @end
  48. @implementation VLCMenuTableViewController
  49. - (void)dealloc
  50. {
  51. [_reachability stopNotifier];
  52. [[NSNotificationCenter defaultCenter] removeObserver:self];
  53. }
  54. - (void)viewDidLoad
  55. {
  56. [super viewDidLoad];
  57. self.view.frame = CGRectMake(0.0f, 0.0f, kGHRevealSidebarWidth, CGRectGetHeight(self.view.bounds));
  58. self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
  59. _sectionHeaderTexts = @[@"SECTION_HEADER_LIBRARY", @"SECTION_HEADER_NETWORK", @"Settings"];
  60. _menuItemsSectionOne = @[@"LIBRARY_ALL_FILES", @"LIBRARY_MUSIC", @"LIBRARY_SERIES"];
  61. _menuItemsSectionTwo = @[@"LOCAL_NETWORK", @"OPEN_NETWORK", @"DOWNLOAD_FROM_HTTP", @"WEBINTF_TITLE", @"CLOUD_SERVICES"];
  62. _menuItemsSectionThree = @[@"Settings", @"ABOUT_APP"];
  63. _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 44.0f + 20.0f, kGHRevealSidebarWidth, CGRectGetHeight(self.view.bounds) - (44.0f + 20.0f)) style:UITableViewStylePlain];
  64. _tableView.delegate = self;
  65. _tableView.dataSource = self;
  66. _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
  67. _tableView.backgroundColor = [UIColor colorWithRed:(43.0f/255.0f) green:(43.0f/255.0f) blue:(43.0f/255.0f) alpha:1.0f];
  68. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  69. _tableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  70. _tableView.rowHeight = [VLCWiFiUploadTableViewCell heightOfCell];
  71. _tableView.scrollsToTop = NO;
  72. self.view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kGHRevealSidebarWidth, CGRectGetHeight(self.view.bounds))];
  73. self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
  74. [self.view addSubview:_tableView];
  75. UIView *brandingBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kGHRevealSidebarWidth, 64.0f)];
  76. brandingBackgroundView.backgroundColor = [UIColor colorWithRed:0.1608 green:0.1608 blue:0.1608 alpha:1.0000];
  77. [self.view addSubview:brandingBackgroundView];
  78. UIImageView *brandingImageView;
  79. brandingImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kGHRevealSidebarWidth, 44.0f + 40.0f)];
  80. brandingImageView.contentMode = UIViewContentModeCenter;
  81. brandingImageView.image = [UIImage imageNamed:@"title"];
  82. [self.view addSubview:brandingImageView];
  83. [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
  84. _reachability = [Reachability reachabilityForLocalWiFi];
  85. [_reachability startNotifier];
  86. [self netReachabilityChanged:nil];
  87. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netReachabilityChanged:) name:kReachabilityChangedNotification object:nil];
  88. BOOL isHTTPServerOn = [[NSUserDefaults standardUserDefaults] boolForKey:kVLCSettingSaveHTTPUploadServerStatus];
  89. [[VLCHTTPUploaderController sharedInstance] changeHTTPServerState:isHTTPServerOn];
  90. [self updateHTTPServerAddress];
  91. }
  92. - (void)viewWillAppear:(BOOL)animated
  93. {
  94. [super viewWillAppear:animated];
  95. self.view.frame = CGRectMake(0.0f, 0.0f,kGHRevealSidebarWidth, CGRectGetHeight(self.view.bounds));
  96. [self netReachabilityChanged:nil];
  97. }
  98. - (void)netReachabilityChanged:(NSNotification *)notification
  99. {
  100. if (_reachability.currentReachabilityStatus == ReachableViaWiFi) {
  101. _uploadButton.enabled = YES;
  102. [self updateHTTPServerAddress];
  103. } else {
  104. [_uploadButton setImage:[UIImage imageNamed:@"WifiUp"] forState:UIControlStateNormal];
  105. _uploadButton.enabled = NO;
  106. [_uploadButton setImage:[UIImage imageNamed:@"WiFiUp"] forState:UIControlStateDisabled];
  107. _uploadLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_NO_CONNECTIVITY", nil);
  108. [[VLCHTTPUploaderController sharedInstance] changeHTTPServerState:NO];
  109. }
  110. }
  111. - (BOOL)shouldAutorotate
  112. {
  113. UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
  114. return (orientation == UIInterfaceOrientationPortraitUpsideDown) ? (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) : YES;
  115. }
  116. - (BOOL)canBecomeFirstResponder
  117. {
  118. return YES;
  119. }
  120. #pragma mark - table view data source
  121. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  122. {
  123. return 3;
  124. }
  125. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  126. {
  127. if (section == 0) // media
  128. return _menuItemsSectionOne.count;
  129. else if (section == 1) // network
  130. return _menuItemsSectionTwo.count;
  131. else if (section == 2) // settings & co
  132. return _menuItemsSectionThree.count;
  133. else
  134. return 0;
  135. }
  136. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  137. {
  138. static NSString *CellIdentifier = @"VLCMenuCell";
  139. static NSString *WiFiCellIdentifier = @"VLCMenuWiFiCell";
  140. NSString *rawTitle;
  141. NSUInteger section = indexPath.section;
  142. if (section == 0)
  143. rawTitle = _menuItemsSectionOne[indexPath.row];
  144. else if(section == 1)
  145. rawTitle = _menuItemsSectionTwo[indexPath.row];
  146. else if(section == 2)
  147. rawTitle = _menuItemsSectionThree[indexPath.row];
  148. UITableViewCell *cell;
  149. if ([rawTitle isEqualToString:@"WEBINTF_TITLE"]) {
  150. cell = (VLCWiFiUploadTableViewCell *)[tableView dequeueReusableCellWithIdentifier:WiFiCellIdentifier];
  151. if (cell == nil)
  152. cell = [VLCWiFiUploadTableViewCell cellWithReuseIdentifier:WiFiCellIdentifier];
  153. } else {
  154. cell = (VLCSidebarViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  155. if (cell == nil)
  156. cell = [[VLCSidebarViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  157. }
  158. if (section == 0) {
  159. if ([rawTitle isEqualToString:@"LIBRARY_ALL_FILES"])
  160. cell.imageView.image = [UIImage imageNamed:@"AllFiles"];
  161. else if ([rawTitle isEqualToString:@"LIBRARY_MUSIC"])
  162. cell.imageView.image = [UIImage imageNamed:@"MusicAlbums"];
  163. else if ([rawTitle isEqualToString:@"LIBRARY_SERIES"])
  164. cell.imageView.image = [UIImage imageNamed:@"TVShowsIcon"];
  165. } else if (section == 1) {
  166. if ([rawTitle isEqualToString:@"LOCAL_NETWORK"])
  167. cell.imageView.image = [UIImage imageNamed:@"Local"];
  168. else if ([rawTitle isEqualToString:@"OPEN_NETWORK"])
  169. cell.imageView.image = [UIImage imageNamed:@"OpenNetStream"];
  170. else if ([rawTitle isEqualToString:@"DOWNLOAD_FROM_HTTP"])
  171. cell.imageView.image = [UIImage imageNamed:@"Downloads"];
  172. else if ([rawTitle isEqualToString:@"WEBINTF_TITLE"]) {
  173. _uploadLocationLabel = [(VLCWiFiUploadTableViewCell*)cell uploadAddressLabel];
  174. _uploadButton = [(VLCWiFiUploadTableViewCell*)cell serverOnButton];
  175. [_uploadButton addTarget:self action:@selector(toggleHTTPServer:) forControlEvents:UIControlEventTouchUpInside];
  176. } else if ([rawTitle isEqualToString:@"CLOUD_SERVICES"])
  177. cell.imageView.image = [UIImage imageNamed:@"iCloudIcon"];
  178. } else if (section == 2) {
  179. if ([rawTitle isEqualToString:@"Settings"])
  180. cell.imageView.image = [UIImage imageNamed:@"Settings"];
  181. else
  182. cell.imageView.image = [UIImage imageNamed:@"menuCone"];
  183. }
  184. if (![rawTitle isEqualToString:@"WEBINTF_TITLE"])
  185. cell.textLabel.text = NSLocalizedString(rawTitle, nil);
  186. return cell;
  187. }
  188. #pragma mark - table view delegation
  189. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  190. {
  191. if (section < 3)
  192. return 21.f;
  193. return 0.;
  194. }
  195. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  196. {
  197. NSObject *headerText = NSLocalizedString(_sectionHeaderTexts[section], nil);
  198. UIView *headerView = nil;
  199. if (headerText != [NSNull null]) {
  200. headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 21.0f)];
  201. headerView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  202. UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 12.0f, 5.0f)];
  203. textLabel.text = (NSString *) headerText;
  204. textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:([UIFont systemFontSize] * 0.8f)];
  205. textLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
  206. textLabel.shadowColor = [UIColor VLCDarkTextShadowColor];
  207. textLabel.textColor = [UIColor colorWithRed:(118.0f/255.0f) green:(118.0f/255.0f) blue:(118.0f/255.0f) alpha:1.0f];
  208. textLabel.backgroundColor = [UIColor clearColor];
  209. [headerView addSubview:textLabel];
  210. UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  211. topLine.backgroundColor = [UIColor colorWithRed:(95.0f/255.0f) green:(95.0f/255.0f) blue:(95.0f/255.0f) alpha:1.0f];
  212. [headerView addSubview:topLine];
  213. UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 21.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  214. bottomLine.backgroundColor = [UIColor colorWithRed:(16.0f/255.0f) green:(16.0f/255.0f) blue:(16.0f/255.0f) alpha:1.0f];
  215. [headerView addSubview:bottomLine];
  216. }
  217. return headerView;
  218. }
  219. #pragma mark - menu implementation
  220. - (void)updateHTTPServerAddress
  221. {
  222. VLCHTTPUploaderController *uploadController = [VLCHTTPUploaderController sharedInstance];
  223. HTTPServer *server = uploadController.httpServer;
  224. if (server.isRunning) {
  225. _uploadLocationLabel.numberOfLines = 0;
  226. if (server.listeningPort != 80)
  227. _uploadLocationLabel.text = [NSString stringWithFormat:@"http://%@:%i\nhttp://%@:%i", [uploadController currentIPAddress], server.listeningPort, [uploadController hostname], server.listeningPort];
  228. else
  229. _uploadLocationLabel.text = [NSString stringWithFormat:@"http://%@\nhttp://%@", [uploadController currentIPAddress], [uploadController hostname]];
  230. [_uploadButton setImage:[UIImage imageNamed:@"WifiUpOn"] forState:UIControlStateNormal];
  231. } else {
  232. _uploadLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_SERVER_OFF", nil);
  233. [_uploadButton setImage:[UIImage imageNamed:@"WifiUp"] forState:UIControlStateNormal];
  234. }
  235. }
  236. - (IBAction)toggleHTTPServer:(UIButton *)sender
  237. {
  238. if (_uploadButton.enabled) {
  239. VLCHTTPUploaderController *uploadController = [VLCHTTPUploaderController sharedInstance];
  240. BOOL futureHTTPServerState = !uploadController.httpServer.isRunning;
  241. [[NSUserDefaults standardUserDefaults] setBool:futureHTTPServerState forKey:kVLCSettingSaveHTTPUploadServerStatus];
  242. [uploadController changeHTTPServerState:futureHTTPServerState];
  243. [self updateHTTPServerAddress];
  244. [[NSUserDefaults standardUserDefaults] synchronize];
  245. }
  246. }
  247. - (void)_revealItem:(NSUInteger)itemIndex inSection:(NSUInteger)sectionNumber
  248. {
  249. UIViewController *viewController;
  250. VLCAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
  251. if (sectionNumber == 1) {
  252. if (itemIndex == 0)
  253. viewController = [[VLCLocalServerListViewController alloc] init];
  254. else if (itemIndex == 1) {
  255. viewController = [[VLCOpenNetworkStreamViewController alloc] initWithNibName:@"VLCOpenNetworkStreamViewController" bundle:nil];
  256. } else if (itemIndex == 2)
  257. viewController = appDelegate.downloadViewController;
  258. else if (itemIndex == 3)
  259. [self toggleHTTPServer:nil];
  260. else if (itemIndex == 4)
  261. viewController = [[VLCCloudServicesTableViewController alloc] initWithNibName:@"VLCCloudServicesTableViewController" bundle:nil];
  262. } else if (sectionNumber == 2) {
  263. if (itemIndex == 0) {
  264. if (!self.settingsController)
  265. self.settingsController = [[VLCSettingsController alloc] init];
  266. VLCSettingsController *settingsController = self.settingsController;
  267. if (!self.settingsViewController) {
  268. self.settingsViewController = [[IASKAppSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
  269. settingsController.viewController = self.settingsViewController;
  270. self.settingsViewController.navigationItem.leftBarButtonItem = [UIBarButtonItem themedRevealMenuButtonWithTarget:self.settingsController.viewController andSelector:@selector(dismiss:)];
  271. }
  272. _hiddenSettingKeys = [[NSMutableSet alloc] init];
  273. IASKAppSettingsViewController *settingsVC = self.settingsViewController;
  274. settingsVC.modalPresentationStyle = UIModalPresentationFormSheet;
  275. settingsVC.delegate = self.settingsController;
  276. settingsVC.showDoneButton = NO;
  277. settingsVC.showCreditsFooter = NO;
  278. if (![[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingPlaybackGestures] boolValue])
  279. [_hiddenSettingKeys addObject:@"EnableVariableJumpDuration"];
  280. [self.settingsController.viewController setHiddenKeys:_hiddenSettingKeys];
  281. viewController = settingsVC;
  282. } else if (itemIndex == 1)
  283. viewController = [[VLCAboutViewController alloc] init];
  284. } else {
  285. viewController = appDelegate.playlistViewController;
  286. [(VLCPlaylistViewController *)viewController setLibraryMode:(int)itemIndex];
  287. }
  288. if (!viewController)
  289. return;
  290. GHRevealViewController *revealController = appDelegate.revealController;
  291. if ([revealController.contentViewController isKindOfClass:[UINavigationController class]]) {
  292. UINavigationController *navCon = (UINavigationController*)revealController.contentViewController;
  293. navCon.viewControllers = @[viewController];
  294. } else
  295. revealController.contentViewController = [[VLCNavigationController alloc] initWithRootViewController:viewController];
  296. [revealController toggleSidebar:NO duration:kGHRevealSidebarDefaultAnimationDuration];
  297. }
  298. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  299. {
  300. [self _revealItem:indexPath.row inSection:indexPath.section];
  301. }
  302. #pragma mark Public Methods
  303. - (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
  304. {
  305. UITableView *tableView = self.tableView;
  306. [tableView selectRowAtIndexPath:indexPath animated:animated scrollPosition:scrollPosition];
  307. if (scrollPosition == UITableViewScrollPositionNone)
  308. [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:scrollPosition animated:animated];
  309. [self _revealItem:indexPath.row inSection:indexPath.section];
  310. }
  311. #pragma mark - shake for support
  312. - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
  313. {
  314. if (motion == UIEventSubtypeMotionShake)
  315. [[VLCBugreporter sharedInstance] handleBugreportRequest];
  316. }
  317. @end