VLCMenuTableViewController.m 12 KB

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