VLCMenuTableViewController.m 19 KB

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