VLCMenuTableViewController.m 18 KB

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