VLCDocumentPickerController.m 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. if (!@available(iOS 8, *))
  23. return;
  24. UIDocumentMenuViewController *importMenu = [[UIDocumentMenuViewController alloc] initWithDocumentTypes:@[(id)kUTTypeAudiovisualContent] inMode:UIDocumentPickerModeImport];
  25. importMenu.delegate = self;
  26. UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
  27. UIPopoverPresentationController *popoverPres = importMenu.popoverPresentationController;
  28. if (popoverPres) { // not-nil on iPad
  29. UIView *sourceView = nil;
  30. if ([sender isKindOfClass:[UIView class]]) {
  31. sourceView = sender;
  32. } else {
  33. sourceView = rootVC.view;
  34. }
  35. popoverPres.sourceView = sourceView;
  36. popoverPres.sourceRect = sourceView.bounds;
  37. popoverPres.permittedArrowDirections = UIPopoverArrowDirectionLeft;
  38. }
  39. [rootVC presentViewController:importMenu animated:YES completion:nil];
  40. }
  41. #pragma mark - UIDocumentMenuDelegate
  42. - (void)documentMenu:(UIDocumentMenuViewController *)documentMenu didPickDocumentPicker:(UIDocumentPickerViewController *)documentPicker
  43. {
  44. documentPicker.delegate = self;
  45. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { // on iPhone it's done in menu table vc
  46. [[VLCSidebarController sharedInstance] selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
  47. scrollPosition:UITableViewScrollPositionNone];
  48. }
  49. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:documentPicker animated:YES completion:nil];
  50. }
  51. #pragma mark - UIDocumentPickerDelegate
  52. - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url
  53. {
  54. NSFileManager *fileManager = [NSFileManager defaultManager];
  55. NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
  56. NSString *filePath = [documentsPath stringByAppendingPathComponent:[url lastPathComponent]];
  57. NSError *error = nil;
  58. BOOL success = [fileManager moveItemAtPath:[url path] toPath:filePath error:&error];
  59. if (!error) {
  60. [[VLCMediaFileDiscoverer sharedInstance] updateMediaList];
  61. } else {
  62. UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"GDRIVE_ERROR_DOWNLOADING_FILE_TITLE", nil) message:error.description preferredStyle:UIAlertControllerStyleAlert];
  63. UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
  64. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  65. [rootVC dismissViewControllerAnimated:true completion:nil];
  66. }];
  67. [alert addAction:okAction];
  68. [rootVC presentViewController:alert animated:true completion:nil];
  69. }
  70. }
  71. @end