VLCMenuTableViewController.m 19 KB

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