Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. public async void OpenShareDialog(DownloadCell cell, LRFile[] files)
  2. {
  3. CGRect cellRect = new CGRect(100, 100, 100, 100);
  4. if (cell != null)
  5. {
  6. // Get the cell rect and adjust it to consider scroll offset
  7. NSIndexPath path = _downloadsTableView.IndexPathForCell(cell);
  8. cellRect = _downloadsTableView.RectForRowAtIndexPath(path);
  9. cellRect = new CGRect(cellRect.X - _downloadsTableView.ContentOffset.X, cellRect.Y - _downloadsTableView.ContentOffset.Y, cellRect.Width, cellRect.Height);
  10. }
  11. List<NSObject> shareFiles = new List<NSObject>();
  12.  
  13. long attachmentsSize = 0;
  14. foreach(LRFile file in files)
  15. {
  16. NSObject shareFile = null;
  17. // Handling the different Mimetypes
  18. // P D F and I M A G E
  19. if (file.MimeType == "application/pdf" || file.MimeType.StartsWith("image"))
  20. {
  21. try
  22. {
  23. LRFileEntry entry = await ContentDb.GetLRFileEntry(file.Id.ToString());
  24. if (entry != null && !String.IsNullOrEmpty(entry.LocalPath))
  25. {
  26. NSData data = NSFileManager.DefaultManager.Contents(entry.LocalPath);
  27. if (data.Length > DataConstants.MaximumAttachmentSize || attachmentsSize + (int)data.Length > DataConstants.MaximumAttachmentSize)
  28. {
  29. // if file size exceeds 8mb use link share
  30. shareFile = new NSString(file.URL);
  31. }
  32. else
  33. {
  34. attachmentsSize += (int) data.Length;
  35. NSUrl fileUrl = NSUrl.CreateFileUrl(new string[] { entry.LocalPath });
  36. var originalFileName = entry.GetFilename();
  37. if (string.IsNullOrEmpty(originalFileName))
  38. {
  39. shareFile = fileUrl;
  40. }
  41. else
  42. {
  43. NSUrl tmpFolder = NSFileManager.DefaultManager.GetTemporaryDirectory();
  44. var tmpFileUrl = tmpFolder.Append(originalFileName, false);
  45. // create a temporary file with the original filename instead of id.jpg (e.g. 3242.jpg -> pump_module.jpg)
  46. NSError copyError;
  47. NSFileManager.DefaultManager.Copy(fileUrl, tmpFileUrl, out copyError);
  48. // if there was no error copying or the file already existed before copy
  49. if (copyError == null || NSFileManager.DefaultManager.FileExists(tmpFileUrl.Path))
  50. {
  51. // the file in the tmp folder has the correct filename
  52. shareFile = tmpFileUrl;
  53. }
  54. else
  55. {
  56. // on error fall back to the former name
  57. shareFile = fileUrl;
  58.  
  59. }
  60. }
  61. }
  62. }
  63. else
  64. shareFile = new NSString(file.URL);
  65. }
  66. catch (Exception ex)
  67. {
  68. Log.Err("DownloadsViewController.OpenShareDialog", ex, 2);
  69. }
  70. }
  71. // V I D E O and every other mimetyp
  72. else
  73. shareFile = new NSString(file.URL);
  74. if (shareFile != null)
  75. shareFiles.Add(shareFile);
  76. }
  77.  
  78. NSObject[] activityItems = shareFiles.ToArray();
  79. UIActivityViewController activityViewController = new UIActivityViewController(activityItems, null);
  80. activityViewController.ExcludedActivityTypes = new NSString[] { };
  81. if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
  82. {
  83. activityViewController.PopoverPresentationController.PermittedArrowDirections = UIPopoverArrowDirection.Right;
  84. activityViewController.PopoverPresentationController.SourceView = cell != null ? (UIView) cell : (UIView) NavigationController.NavigationBar;
  85. activityViewController.PopoverPresentationController.SourceRect =
  86. new CGRect(UIScreen.MainScreen.Bounds.Width - DataConstants.DownloadRightPadding - DataConstants.DownloadButtonWidth * 2 - 20, (cellRect.Height / 2), 0, 0);
  87. }
  88. PresentViewController(activityViewController, true, null);
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement