VLCMenuTableViewController.m 13 KB

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