VLCDocumentPickerController.m 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*****************************************************************************
  2. * VLCDocumentPickerController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2014 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Tamas Timar <ttimar.vlc # gmail.com>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. #import "VLCDocumentPickerController.h"
  13. #import <MobileCoreServices/MobileCoreServices.h>
  14. #import "VLCMediaFileDiscoverer.h"
  15. #import "VLCLibraryViewController.h"
  16. @interface VLCDocumentPickerController () <UIDocumentMenuDelegate, UIDocumentPickerDelegate>
  17. @end
  18. @implementation VLCDocumentPickerController
  19. #pragma mark - Public Methods
  20. - (void)showDocumentMenuViewController:(id)sender
  21. {
  22. UIDocumentMenuViewController *importMenu = [[UIDocumentMenuViewController alloc] initWithDocumentTypes:@[(id)kUTTypeAudiovisualContent] inMode:UIDocumentPickerModeImport];
  23. importMenu.delegate = self;
  24. UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
  25. UIPopoverPresentationController *popoverPres = importMenu.popoverPresentationController;
  26. if (popoverPres) { // not-nil on iPad
  27. UIView *sourceView = nil;
  28. if ([sender isKindOfClass:[UIView class]]) {
  29. sourceView = sender;
  30. } else {
  31. sourceView = rootVC.view;
  32. }
  33. popoverPres.sourceView = sourceView;
  34. popoverPres.sourceRect = sourceView.bounds;
  35. popoverPres.permittedArrowDirections = UIPopoverArrowDirectionLeft;
  36. }
  37. [rootVC presentViewController:importMenu animated:YES completion:nil];
  38. }
  39. #pragma mark - UIDocumentMenuDelegate
  40. - (void)documentMenu:(UIDocumentMenuViewController *)documentMenu didPickDocumentPicker:(UIDocumentPickerViewController *)documentPicker
  41. {
  42. documentPicker.delegate = self;
  43. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { // on iPhone it's done in menu table vc
  44. [[VLCSidebarController sharedInstance] selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
  45. scrollPosition:UITableViewScrollPositionNone];
  46. }
  47. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:documentPicker animated:YES completion:nil];
  48. }
  49. #pragma mark - UIDocumentPickerDelegate
  50. - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url
  51. {
  52. NSFileManager *fileManager = [NSFileManager defaultManager];
  53. NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
  54. NSString *filePath = [documentsPath stringByAppendingPathComponent:[url lastPathComponent]];
  55. NSError *error = nil;
  56. [fileManager moveItemAtPath:[url path] toPath:filePath error:&error];
  57. if (!error) {
  58. [[VLCMediaFileDiscoverer sharedInstance] updateMediaList];
  59. } else {
  60. UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"GDRIVE_ERROR_DOWNLOADING_FILE_TITLE", nil) message:error.description preferredStyle:UIAlertControllerStyleAlert];
  61. UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
  62. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  63. [rootVC dismissViewControllerAnimated:true completion:nil];
  64. }];
  65. [alert addAction:okAction];
  66. [rootVC presentViewController:alert animated:true completion:nil];
  67. }
  68. }
  69. @end