VLCMenuTableViewController.m 17 KB

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