VLCMenuTableViewController.m 18 KB

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