VLCMenuTableViewController.m 17 KB

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