VLCMenuTableViewController.m 14 KB

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