VLCMenuTableViewController.m 17 KB

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