VLCMenuTableViewController.m 18 KB

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