123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- /*****************************************************************************
- * VLCMenuTableViewController.m
- * VLC for iOS
- *****************************************************************************
- * Copyright (c) 2013 VideoLAN. All rights reserved.
- * $Id$
- *
- * Authors: Felix Paul Kühne <fkuehne # videolan.org>
- * Gleb Pinigin <gpinigin # gmail.com>
- * Jean-Romain Prévost <jr # 3on.fr>
- * Carola Nitz <nitz.carola # googlemail.com>
- * Tamas Timar <ttimar.vlc # gmail.com>
- * Pierre Sagaspe <pierre.sagaspe # me.com>
- *
- * Refer to the COPYING file of the official project for license.
- *****************************************************************************/
- #import "VLCMenuTableViewController.h"
- #import "GHRevealViewController.h"
- #import "VLCSidebarViewCell.h"
- #import "Reachability.h"
- #import <QuartzCore/QuartzCore.h>
- #import "VLCWiFiUploadTableViewCell.h"
- #import "VLCHTTPUploaderController.h"
- #import "VLCAppDelegate.h"
- #import "HTTPServer.h"
- #import "IASKAppSettingsViewController.h"
- #import "GHRevealViewController.h"
- #import "VLCLocalServerListViewController.h"
- #import "VLCOpenNetworkStreamViewController.h"
- #import "VLCSettingsController.h"
- #import "UINavigationController+Theme.h"
- #import "UIBarButtonItem+Theme.h"
- #import "VLCAboutViewController.h"
- #import "VLCPlaylistViewController.h"
- #import "VLCBugreporter.h"
- @interface VLCMenuTableViewController () <UITableViewDataSource, UITableViewDelegate>
- {
- NSArray *_sectionHeaderTexts;
- NSArray *_menuItemsSectionOne;
- NSArray *_menuItemsSectionTwo;
- NSArray *_menuItemsSectionThree;
- UILabel *_uploadLocationLabel;
- UIButton *_uploadButton;
- Reachability *_reachability;
- }
- @property (nonatomic) VLCHTTPUploaderController *uploadController;
- @property (nonatomic) VLCAppDelegate *appDelegate;
- @property (nonatomic) GHRevealViewController *revealController;
- @end
- @implementation VLCMenuTableViewController
- - (void)dealloc
- {
- [_reachability stopNotifier];
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.view.frame = CGRectMake(0.0f, 0.0f, kGHRevealSidebarWidth, CGRectGetHeight(self.view.bounds));
- self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
- _sectionHeaderTexts = @[@"SECTION_HEADER_LIBRARY", @"SECTION_HEADER_NETWORK", @"Settings"];
- _menuItemsSectionOne = @[@"LIBRARY_ALL_FILES", @"LIBRARY_MUSIC", @"LIBRARY_SERIES"];
- _menuItemsSectionTwo = @[@"LOCAL_NETWORK", @"OPEN_NETWORK", @"DOWNLOAD_FROM_HTTP", @"WEBINTF_TITLE", @"Dropbox", @"Google Drive", @"OneDrive"];
- if ([UIDocumentPickerViewController class]) { // on iOS 8+ add document picker option
- NSMutableArray* expanded = [_menuItemsSectionTwo mutableCopy];
- [expanded addObject:@"CLOUD_DRIVES"];
- _menuItemsSectionTwo = expanded;
- }
- _menuItemsSectionThree = @[@"Settings", @"ABOUT_APP"];
- _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 44.0f + 20.0f, kGHRevealSidebarWidth, CGRectGetHeight(self.view.bounds) - (44.0f + 20.0f)) style:UITableViewStylePlain];
- _tableView.delegate = self;
- _tableView.dataSource = self;
- _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
- _tableView.backgroundColor = [UIColor colorWithRed:(43.0f/255.0f) green:(43.0f/255.0f) blue:(43.0f/255.0f) alpha:1.0f];
- _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
- _tableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
- _tableView.rowHeight = [VLCWiFiUploadTableViewCell heightOfCell];
- _tableView.scrollsToTop = NO;
- self.view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kGHRevealSidebarWidth, CGRectGetHeight(self.view.bounds))];
- self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
- [self.view addSubview:_tableView];
- if (SYSTEM_RUNS_IOS7_OR_LATER) {
- UIView *brandingBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kGHRevealSidebarWidth, 64.0f)];
- brandingBackgroundView.backgroundColor = [UIColor colorWithRed:0.1608 green:0.1608 blue:0.1608 alpha:1.0000];
- [self.view addSubview:brandingBackgroundView];
- } else {
- UIImageView *brandingBackgroundImageView;
- brandingBackgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 20.0f, kGHRevealSidebarWidth, 44.0f)];
- brandingBackgroundImageView.contentMode = UIViewContentModeScaleToFill;
- brandingBackgroundImageView.image = [UIImage imageNamed:@"headerSidebar"];
- [self.view addSubview:brandingBackgroundImageView];
- }
- UIImageView *brandingImageView;
- brandingImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kGHRevealSidebarWidth, 44.0f + 40.0f)];
- brandingImageView.contentMode = UIViewContentModeCenter;
- brandingImageView.image = [UIImage imageNamed:@"title"];
- [self.view addSubview:brandingImageView];
- [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
- _reachability = [Reachability reachabilityForLocalWiFi];
- [_reachability startNotifier];
- [self netReachabilityChanged:nil];
- self.appDelegate = [[UIApplication sharedApplication] delegate];
- self.uploadController = self.appDelegate.uploadController;
- self.revealController = self.appDelegate.revealController;
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netReachabilityChanged:) name:kReachabilityChangedNotification object:nil];
- BOOL isHTTPServerOn = [[NSUserDefaults standardUserDefaults] boolForKey:kVLCSettingSaveHTTPUploadServerStatus];
- [self.uploadController changeHTTPServerState:isHTTPServerOn];
- [self updateHTTPServerAddress];
- }
- - (void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
- self.view.frame = CGRectMake(0.0f, 0.0f,kGHRevealSidebarWidth, CGRectGetHeight(self.view.bounds));
- [self netReachabilityChanged:nil];
- }
- - (void)netReachabilityChanged:(NSNotification *)notification
- {
- if (_reachability.currentReachabilityStatus == ReachableViaWiFi) {
- _uploadButton.enabled = YES;
- [self updateHTTPServerAddress];
- } else {
- [_uploadButton setImage:[UIImage imageNamed:@"WifiUp"] forState:UIControlStateNormal];
- _uploadButton.enabled = NO;
- [_uploadButton setImage:[UIImage imageNamed:@"WiFiUp"] forState:UIControlStateDisabled];
- _uploadLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_NO_CONNECTIVITY", nil);
- [self.uploadController changeHTTPServerState:NO];
- }
- }
- - (BOOL)shouldAutorotate
- {
- UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
- return (orientation == UIInterfaceOrientationPortraitUpsideDown) ? (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) : YES;
- }
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 3;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- if (section == 0) // media
- return _menuItemsSectionOne.count;
- else if (section == 1) // network
- return _menuItemsSectionTwo.count;
- else if (section == 2) // settings & co
- return _menuItemsSectionThree.count;
- else
- return 0;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"VLCMenuCell";
- static NSString *WiFiCellIdentifier = @"VLCMenuWiFiCell";
- NSString *rawTitle;
- NSUInteger section = indexPath.section;
- if (section == 0)
- rawTitle = _menuItemsSectionOne[indexPath.row];
- else if(section == 1)
- rawTitle = _menuItemsSectionTwo[indexPath.row];
- else if(section == 2)
- rawTitle = _menuItemsSectionThree[indexPath.row];
- UITableViewCell *cell;
- if ([rawTitle isEqualToString:@"WEBINTF_TITLE"]) {
- cell = (VLCWiFiUploadTableViewCell *)[tableView dequeueReusableCellWithIdentifier:WiFiCellIdentifier];
- if (cell == nil)
- cell = [VLCWiFiUploadTableViewCell cellWithReuseIdentifier:WiFiCellIdentifier];
- } else {
- cell = (VLCSidebarViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil)
- cell = [[VLCSidebarViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
- }
- if (section == 0) {
- if ([rawTitle isEqualToString:@"LIBRARY_ALL_FILES"])
- cell.imageView.image = [UIImage imageNamed:@"AllFiles"];
- else if ([rawTitle isEqualToString:@"LIBRARY_MUSIC"])
- cell.imageView.image = [UIImage imageNamed:@"MusicAlbums"];
- else if ([rawTitle isEqualToString:@"LIBRARY_SERIES"])
- cell.imageView.image = [UIImage imageNamed:@"TVShowsIcon"];
- } else if (section == 1) {
- if ([rawTitle isEqualToString:@"LOCAL_NETWORK"])
- cell.imageView.image = [UIImage imageNamed:@"Local"];
- else if ([rawTitle isEqualToString:@"OPEN_NETWORK"])
- cell.imageView.image = [UIImage imageNamed:@"OpenNetStream"];
- else if ([rawTitle isEqualToString:@"DOWNLOAD_FROM_HTTP"])
- cell.imageView.image = [UIImage imageNamed:@"Downloads"];
- else if ([rawTitle isEqualToString:@"Dropbox"]) {
- cell.textLabel.text = rawTitle;
- cell.imageView.image = [UIImage imageNamed:@"Dropbox"];
- } else if ([rawTitle isEqualToString:@"Google Drive"]) {
- cell.textLabel.text = rawTitle;
- cell.imageView.image = [UIImage imageNamed:@"Drive"];
- } else if ([rawTitle isEqualToString:@"CLOUD_DRIVES"])
- cell.imageView.image = [UIImage imageNamed:@"CloudDrives"];
- else if ([rawTitle isEqualToString:@"WEBINTF_TITLE"]) {
- _uploadLocationLabel = [(VLCWiFiUploadTableViewCell*)cell uploadAddressLabel];
- _uploadButton = [(VLCWiFiUploadTableViewCell*)cell serverOnButton];
- [_uploadButton addTarget:self action:@selector(toggleHTTPServer:) forControlEvents:UIControlEventTouchUpInside];
- }
- } else if (section == 2) {
- if ([rawTitle isEqualToString:@"Settings"])
- cell.imageView.image = [UIImage imageNamed:@"Settings"];
- else
- cell.imageView.image = [UIImage imageNamed:@"menuCone"];
- }
- if (!([rawTitle isEqualToString:@"Dropbox"] || [rawTitle isEqualToString:@"WEBINTF_TITLE"]))
- cell.textLabel.text = NSLocalizedString(rawTitle, nil);
- return cell;
- }
- #pragma mark - tv delegate
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- {
- if (section < 3)
- return 21.f;
- return 0.;
- }
- - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
- {
- NSObject *headerText = NSLocalizedString(_sectionHeaderTexts[section], nil);
- UIView *headerView = nil;
- if (headerText != [NSNull null]) {
- headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 21.0f)];
- if (!SYSTEM_RUNS_IOS7_OR_LATER) {
- CAGradientLayer *gradient = [CAGradientLayer layer];
- gradient.frame = headerView.bounds;
- gradient.colors = @[
- (id)[UIColor colorWithRed:(66.0f/255.0f) green:(66.0f/255.0f) blue:(66.0f/255.0f) alpha:1.0f].CGColor,
- (id)[UIColor colorWithRed:(56.0f/255.0f) green:(56.0f/255.0f) blue:(56.0f/255.0f) alpha:1.0f].CGColor,
- ];
- [headerView.layer insertSublayer:gradient atIndex:0];
- } else
- headerView.backgroundColor = [UIColor VLCDarkBackgroundColor];
- UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 12.0f, 5.0f)];
- textLabel.text = (NSString *) headerText;
- textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:([UIFont systemFontSize] * 0.8f)];
- textLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
- textLabel.shadowColor = [UIColor VLCDarkTextShadowColor];
- textLabel.textColor = [UIColor colorWithRed:(118.0f/255.0f) green:(118.0f/255.0f) blue:(118.0f/255.0f) alpha:1.0f];
- textLabel.backgroundColor = [UIColor clearColor];
- [headerView addSubview:textLabel];
- UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
- topLine.backgroundColor = [UIColor colorWithRed:(95.0f/255.0f) green:(95.0f/255.0f) blue:(95.0f/255.0f) alpha:1.0f];
- [headerView addSubview:topLine];
- UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 21.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
- bottomLine.backgroundColor = [UIColor colorWithRed:(16.0f/255.0f) green:(16.0f/255.0f) blue:(16.0f/255.0f) alpha:1.0f];
- [headerView addSubview:bottomLine];
- }
- return headerView;
- }
- #pragma mark - menu implementation
- - (void)updateHTTPServerAddress
- {
- HTTPServer *server = self.uploadController.httpServer;
- if (server.isRunning) {
- _uploadLocationLabel.numberOfLines = 0;
- if (server.listeningPort != 80)
- _uploadLocationLabel.text = [NSString stringWithFormat:@"http://%@:%i\nhttp://%@:%i", [self.uploadController currentIPAddress], server.listeningPort, [self.uploadController hostname], server.listeningPort];
- else
- _uploadLocationLabel.text = [NSString stringWithFormat:@"http://%@\nhttp://%@", [self.uploadController currentIPAddress], [self.uploadController hostname]];
- [_uploadButton setImage:[UIImage imageNamed:@"WifiUpOn"] forState:UIControlStateNormal];
- } else {
- _uploadLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_SERVER_OFF", nil);
- [_uploadButton setImage:[UIImage imageNamed:@"WifiUp"] forState:UIControlStateNormal];
- }
- }
- - (IBAction)toggleHTTPServer:(UIButton *)sender
- {
- if (_uploadButton.enabled) {
- BOOL futureHTTPServerState = !self.uploadController.httpServer.isRunning;
- [[NSUserDefaults standardUserDefaults] setBool:futureHTTPServerState forKey:kVLCSettingSaveHTTPUploadServerStatus];
- [self.uploadController changeHTTPServerState:futureHTTPServerState];
- [self updateHTTPServerAddress];
- [[NSUserDefaults standardUserDefaults] synchronize];
- }
- }
- - (void)_revealItem:(NSUInteger)itemIndex inSection:(NSUInteger)sectionNumber
- {
- UIViewController *viewController;
- if (sectionNumber == 1) {
- if (itemIndex == 0)
- viewController = [[VLCLocalServerListViewController alloc] init];
- else if (itemIndex == 1) {
- if (SYSTEM_RUNS_IOS7_OR_LATER)
- viewController = [[VLCOpenNetworkStreamViewController alloc] initWithNibName:@"VLCFutureOpenNetworkStreamViewController" bundle:nil];
- else
- viewController = [[VLCOpenNetworkStreamViewController alloc] initWithNibName:@"VLCOpenNetworkStreamViewController" bundle:nil];
- } else if (itemIndex == 2)
- viewController = self.appDelegate.downloadViewController;
- else if (itemIndex == 3)
- [self toggleHTTPServer:nil];
- else if (itemIndex == 4)
- viewController = self.appDelegate.dropboxTableViewController;
- else if (itemIndex == 5)
- viewController = self.appDelegate.googleDriveTableViewController;
- else if (itemIndex == 6)
- viewController = self.appDelegate.oneDriveTableViewController;
- } else if (sectionNumber == 2) {
- if (itemIndex == 0) {
- if (!self.settingsController)
- self.settingsController = [[VLCSettingsController alloc] init];
- if (!self.settingsViewController) {
- self.settingsViewController = [[IASKAppSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
- self.settingsController.viewController = self.settingsViewController;
- self.settingsViewController.navigationItem.leftBarButtonItem = [UIBarButtonItem themedRevealMenuButtonWithTarget:self.settingsController.viewController andSelector:@selector(dismiss:)];
- }
- self.settingsViewController.modalPresentationStyle = UIModalPresentationFormSheet;
- self.settingsViewController.delegate = self.settingsController;
- self.settingsViewController.showDoneButton = NO;
- self.settingsViewController.showCreditsFooter = NO;
- viewController = self.settingsController.viewController;
- [self.settingsController willShow];
- } else if (itemIndex == 1)
- viewController = [[VLCAboutViewController alloc] init];
- } else {
- viewController = self.appDelegate.playlistViewController;
- [self.appDelegate.playlistViewController setLibraryMode:(int)itemIndex];
- }
- if (!viewController)
- return;
- UINavigationController *navCon = nil;
- if ([_revealController.contentViewController isKindOfClass:[UINavigationController class]]) {
- navCon = (UINavigationController*)_revealController.contentViewController;
- for (UIViewController *vc in navCon.viewControllers) {
- //send viewWillDisappear while vc still has a navigationcontroller
- if ([vc isKindOfClass:[VLCCloudStorageTableViewController class]])
- [vc viewWillDisappear:NO];
- }
- navCon.viewControllers = @[viewController];
- } else {
- navCon = [[UINavigationController alloc] initWithRootViewController:viewController];
- [navCon loadTheme];
- _revealController.contentViewController = navCon;
- }
- [_revealController toggleSidebar:NO duration:kGHRevealSidebarDefaultAnimationDuration];
- }
- - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- NSUInteger idx = [_menuItemsSectionTwo indexOfObject:@"CLOUD_DRIVES"];
- // don't let select CLOUD_DRIVES menu item since there is no view controller to reveal
- if (indexPath.section == 1 && indexPath.row == idx) {
- if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
- [self.appDelegate.documentPickerController showDocumentMenuViewController:[self.tableView cellForRowAtIndexPath:indexPath]];
- } else {
- [self.appDelegate.documentPickerController showDocumentMenuViewController:nil];
- [self selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
- }
- return nil;
- } else
- return indexPath;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- [self _revealItem:indexPath.row inSection:indexPath.section];
- }
- #pragma mark Public Methods
- - (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
- {
- [self.tableView selectRowAtIndexPath:indexPath animated:animated scrollPosition:scrollPosition];
- if (scrollPosition == UITableViewScrollPositionNone)
- [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:scrollPosition animated:animated];
- [self _revealItem:indexPath.row inSection:indexPath.section];
- }
- #pragma mark - shake to support
- - (BOOL)canBecomeFirstResponder
- {
- return YES;
- }
- - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
- {
- if (motion == UIEventSubtypeMotionShake)
- [[VLCBugreporter sharedInstance] handleBugreportRequest];
- }
- @end
|