VLCMenuTableViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 "VLCHTTPDownloadViewController.h"
  24. #import "VLCSettingsController.h"
  25. #import "UINavigationController+Theme.h"
  26. #import "UIBarButtonItem+Theme.h"
  27. #import "VLCAboutViewController.h"
  28. #import "VLCPlaylistViewController.h"
  29. #import "VLCBugreporter.h"
  30. @interface VLCMenuTableViewController () <UITableViewDataSource, UITableViewDelegate>
  31. {
  32. NSArray *_sectionHeaderTexts;
  33. NSArray *_menuItemsSectionOne;
  34. NSArray *_menuItemsSectionTwo;
  35. NSArray *_menuItemsSectionThree;
  36. UILabel *_uploadLocationLabel;
  37. UISwitch *_uploadSwitch;
  38. Reachability *_reachability;
  39. }
  40. @property (nonatomic) VLCHTTPUploaderController *uploadController;
  41. @property (nonatomic) VLCAppDelegate *appDelegate;
  42. @property (nonatomic) GHRevealViewController *revealController;
  43. @end
  44. @implementation VLCMenuTableViewController
  45. - (void)dealloc
  46. {
  47. [_reachability stopNotifier];
  48. [[NSNotificationCenter defaultCenter] removeObserver:self];
  49. }
  50. - (void)viewDidLoad
  51. {
  52. [super viewDidLoad];
  53. self.view.frame = CGRectMake(0.0f, 0.0f, kGHRevealSidebarWidth, CGRectGetHeight(self.view.bounds));
  54. self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
  55. _sectionHeaderTexts = @[@"SECTION_HEADER_LIBRARY", @"SECTION_HEADER_NETWORK", @"Settings"];
  56. _menuItemsSectionOne = @[@"LIBRARY_ALL_FILES"];
  57. _menuItemsSectionTwo = @[@"LOCAL_NETWORK", @"OPEN_NETWORK", @"DOWNLOAD_FROM_HTTP", @"WiFi Upload", @"Dropbox"];
  58. _menuItemsSectionThree = @[@"Settings", @"ABOUT_APP"];
  59. _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 44.0f, kGHRevealSidebarWidth, CGRectGetHeight(self.view.bounds) - 44.0f)
  60. style:UITableViewStylePlain];
  61. _tableView.delegate = self;
  62. _tableView.dataSource = self;
  63. _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
  64. _tableView.backgroundColor = [UIColor clearColor];
  65. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  66. _tableView.rowHeight = [VLCWiFiUploadTableViewCell heightOfCell];
  67. self.view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kGHRevealSidebarWidth, CGRectGetHeight(self.view.bounds))];
  68. [self.view addSubview:_tableView];
  69. UIImageView *brandingBackgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kGHRevealSidebarWidth, 44.0f)];
  70. brandingBackgroundImageView.contentMode = UIViewContentModeScaleToFill;
  71. brandingBackgroundImageView.image = [UIImage imageNamed:@"searchBarBG"];
  72. [self.view addSubview:brandingBackgroundImageView];
  73. UIImageView *brandingImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kGHRevealSidebarWidth, 44.0f)];
  74. brandingImageView.contentMode = UIViewContentModeCenter;
  75. brandingImageView.image = [UIImage imageNamed:@"title"];
  76. [self.view addSubview:brandingImageView];
  77. [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
  78. _reachability = [Reachability reachabilityForLocalWiFi];
  79. [_reachability startNotifier];
  80. [self netReachabilityChanged:nil];
  81. self.appDelegate = [[UIApplication sharedApplication] delegate];
  82. self.uploadController = self.appDelegate.uploadController;
  83. self.revealController = self.appDelegate.revealController;
  84. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netReachabilityChanged:) name:kReachabilityChangedNotification object:nil];
  85. }
  86. - (void)viewWillAppear:(BOOL)animated {
  87. self.view.frame = CGRectMake(0.0f, 0.0f,kGHRevealSidebarWidth, CGRectGetHeight(self.view.bounds));
  88. }
  89. - (void)netReachabilityChanged:(NSNotification *)notification
  90. {
  91. if (_reachability.currentReachabilityStatus == ReachableViaWiFi) {
  92. _uploadSwitch.enabled = YES;
  93. _uploadLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_SERVER_OFF", @"");
  94. } else {
  95. _uploadSwitch.enabled = NO;
  96. _uploadSwitch.on = NO;
  97. _uploadLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_NO_CONNECTIVITY", @"");
  98. }
  99. }
  100. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
  101. return (orientation == UIInterfaceOrientationPortraitUpsideDown)
  102. ? (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  103. : YES;
  104. }
  105. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  106. return 3;
  107. }
  108. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  109. if (section == 0) // media
  110. return 1;
  111. else if (section == 1) // network
  112. return 5;
  113. else if (section == 2) // settings & co
  114. return 2;
  115. else
  116. return 0;
  117. }
  118. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  119. static NSString *CellIdentifier = @"VLCMenuCell";
  120. static NSString *WiFiCellIdentifier = @"VLCMenuWiFiCell";
  121. NSString *title;
  122. if (indexPath.section == 0)
  123. title = NSLocalizedString(_menuItemsSectionOne[indexPath.row], @"");
  124. else if(indexPath.section == 1)
  125. title = NSLocalizedString(_menuItemsSectionTwo[indexPath.row], @"");
  126. else if(indexPath.section == 2)
  127. title = NSLocalizedString(_menuItemsSectionThree[indexPath.row], @"");
  128. UITableViewCell *cell;
  129. if ([title isEqualToString:@"WiFi Upload"]) {
  130. cell = (VLCWiFiUploadTableViewCell *)[tableView dequeueReusableCellWithIdentifier:WiFiCellIdentifier];
  131. if (cell == nil)
  132. cell = [VLCWiFiUploadTableViewCell cellWithReuseIdentifier:WiFiCellIdentifier];
  133. } else {
  134. cell = (GHMenuCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  135. if (cell == nil)
  136. cell = [[GHMenuCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  137. }
  138. if ([title isEqualToString:@"Dropbox"]) {
  139. [[(GHMenuCell*)cell titleImageView] setImage: [UIImage imageNamed:@"dropboxLabel"]];
  140. cell.textLabel.text = @"";
  141. } else if ([title isEqualToString:@"WiFi Upload"]) {
  142. _uploadLocationLabel = [(VLCWiFiUploadTableViewCell*)cell uploadAddressLabel];
  143. _uploadSwitch = [(VLCWiFiUploadTableViewCell*)cell serverOnSwitch];
  144. [_uploadSwitch addTarget:self action:@selector(toggleHTTPServer:) forControlEvents:UIControlEventTouchUpInside];
  145. BOOL isHTTPServerOn = [[NSUserDefaults standardUserDefaults] boolForKey:kVLCSettingSaveHTTPUploadServerStatus];
  146. [_uploadSwitch setOn:isHTTPServerOn];
  147. [self updateHTTPServerAddress];
  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. } else
  197. _uploadLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_SERVER_OFF", @"");
  198. }
  199. - (IBAction)toggleHTTPServer:(UISwitch *)sender
  200. {
  201. [[NSUserDefaults standardUserDefaults] setBool:sender.on forKey:kVLCSettingSaveHTTPUploadServerStatus];
  202. [self.uploadController changeHTTPServerState:sender.on];
  203. [self updateHTTPServerAddress];
  204. [[NSUserDefaults standardUserDefaults] synchronize];
  205. }
  206. - (void)_revealItem:(NSUInteger)itemIndex inSection:(NSUInteger)sectionNumber
  207. {
  208. UIViewController *viewController;
  209. if (sectionNumber == 1) {
  210. if (itemIndex == 0)
  211. viewController = [[VLCLocalServerListViewController alloc] init];
  212. else if (itemIndex == 1)
  213. viewController = [[VLCOpenNetworkStreamViewController alloc] init];
  214. else if (itemIndex == 2)
  215. viewController = [[VLCHTTPDownloadViewController alloc] init];
  216. else if (itemIndex == 4)
  217. viewController = self.appDelegate.dropboxTableViewController;
  218. } else if (sectionNumber == 2) {
  219. if (itemIndex == 0) {
  220. if (!self.settingsController)
  221. self.settingsController = [[VLCSettingsController alloc] init];
  222. if (!self.settingsViewController) {
  223. self.settingsViewController = [[IASKAppSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
  224. self.settingsController.viewController = self.settingsViewController;
  225. self.settingsViewController.navigationItem.leftBarButtonItem = [UIBarButtonItem themedRevealMenuButtonWithTarget:self.settingsController.viewController andSelector:@selector(dismiss:)];
  226. }
  227. self.settingsViewController.modalPresentationStyle = UIModalPresentationFormSheet;
  228. self.settingsViewController.delegate = self.settingsController;
  229. self.settingsViewController.showDoneButton = NO;
  230. self.settingsViewController.showCreditsFooter = NO;
  231. viewController = self.settingsController.viewController;
  232. } else if (itemIndex == 1)
  233. viewController = [[VLCAboutViewController alloc] init];
  234. } else
  235. viewController = self.appDelegate.playlistViewController;
  236. if (!viewController)
  237. return;
  238. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:viewController];
  239. [navCon loadTheme];
  240. _revealController.contentViewController = navCon;
  241. [_revealController toggleSidebar:NO duration:kGHRevealSidebarDefaultAnimationDuration];
  242. }
  243. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  244. [self _revealItem:indexPath.row inSection:indexPath.section];
  245. }
  246. #pragma mark Public Methods
  247. - (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition {
  248. [self.tableView selectRowAtIndexPath:indexPath animated:animated scrollPosition:scrollPosition];
  249. if (scrollPosition == UITableViewScrollPositionNone)
  250. [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:scrollPosition animated:animated];
  251. [self _revealItem:indexPath.row inSection:indexPath.section];
  252. }
  253. #pragma mark - shake to support
  254. - (BOOL)canBecomeFirstResponder {
  255. return YES;
  256. }
  257. - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
  258. if (motion == UIEventSubtypeMotionShake)
  259. [[VLCBugreporter sharedInstance] handleBugreportRequest];
  260. }
  261. @end