Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public async void OpenShareDialog(DownloadCell cell, LRFile[] files)
- {
- CGRect cellRect = new CGRect(100, 100, 100, 100);
- if (cell != null)
- {
- // Get the cell rect and adjust it to consider scroll offset
- NSIndexPath path = _downloadsTableView.IndexPathForCell(cell);
- cellRect = _downloadsTableView.RectForRowAtIndexPath(path);
- cellRect = new CGRect(cellRect.X - _downloadsTableView.ContentOffset.X, cellRect.Y - _downloadsTableView.ContentOffset.Y, cellRect.Width, cellRect.Height);
- }
- List<NSObject> shareFiles = new List<NSObject>();
- long attachmentsSize = 0;
- foreach(LRFile file in files)
- {
- NSObject shareFile = null;
- // Handling the different Mimetypes
- // P D F and I M A G E
- if (file.MimeType == "application/pdf" || file.MimeType.StartsWith("image"))
- {
- try
- {
- LRFileEntry entry = await ContentDb.GetLRFileEntry(file.Id.ToString());
- if (entry != null && !String.IsNullOrEmpty(entry.LocalPath))
- {
- NSData data = NSFileManager.DefaultManager.Contents(entry.LocalPath);
- if (data.Length > DataConstants.MaximumAttachmentSize || attachmentsSize + (int)data.Length > DataConstants.MaximumAttachmentSize)
- {
- // if file size exceeds 8mb use link share
- shareFile = new NSString(file.URL);
- }
- else
- {
- attachmentsSize += (int) data.Length;
- NSUrl fileUrl = NSUrl.CreateFileUrl(new string[] { entry.LocalPath });
- var originalFileName = entry.GetFilename();
- if (string.IsNullOrEmpty(originalFileName))
- {
- shareFile = fileUrl;
- }
- else
- {
- NSUrl tmpFolder = NSFileManager.DefaultManager.GetTemporaryDirectory();
- var tmpFileUrl = tmpFolder.Append(originalFileName, false);
- // create a temporary file with the original filename instead of id.jpg (e.g. 3242.jpg -> pump_module.jpg)
- NSError copyError;
- NSFileManager.DefaultManager.Copy(fileUrl, tmpFileUrl, out copyError);
- // if there was no error copying or the file already existed before copy
- if (copyError == null || NSFileManager.DefaultManager.FileExists(tmpFileUrl.Path))
- {
- // the file in the tmp folder has the correct filename
- shareFile = tmpFileUrl;
- }
- else
- {
- // on error fall back to the former name
- shareFile = fileUrl;
- }
- }
- }
- }
- else
- shareFile = new NSString(file.URL);
- }
- catch (Exception ex)
- {
- Log.Err("DownloadsViewController.OpenShareDialog", ex, 2);
- }
- }
- // V I D E O and every other mimetyp
- else
- shareFile = new NSString(file.URL);
- if (shareFile != null)
- shareFiles.Add(shareFile);
- }
- NSObject[] activityItems = shareFiles.ToArray();
- UIActivityViewController activityViewController = new UIActivityViewController(activityItems, null);
- activityViewController.ExcludedActivityTypes = new NSString[] { };
- if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
- {
- activityViewController.PopoverPresentationController.PermittedArrowDirections = UIPopoverArrowDirection.Right;
- activityViewController.PopoverPresentationController.SourceView = cell != null ? (UIView) cell : (UIView) NavigationController.NavigationBar;
- activityViewController.PopoverPresentationController.SourceRect =
- new CGRect(UIScreen.MainScreen.Bounds.Width - DataConstants.DownloadRightPadding - DataConstants.DownloadButtonWidth * 2 - 20, (cellRect.Height / 2), 0, 0);
- }
- PresentViewController(activityViewController, true, null);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement