VLCMenuTableViewController.m 13 KB

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