VLCMenuTableViewController.m 15 KB

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