VLCMenuTableViewController.m 15 KB

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