Skip to content

Commit

Permalink
Add sourceView parameter for proper popover anchoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdchr committed Aug 19, 2021
1 parent df0ab63 commit 6a07b6d
Showing 1 changed file with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,15 @@ class ShareAppContentPresenter {

/// Fetches the content needed for sharing, and presents the share sheet through the provided `sender` instance.
///
func present(for appName: ShareAppName, in sender: UIViewController, completion: (() -> Void)? = nil) {
/// - Parameters:
/// - appName: The name of the app to be shared. Fetched contents will differ depending on the provided value.
/// - sender: The view that will be presenting the share sheet.
/// - sourceView: The view to be the anchor for the popover view on iPad.
/// - completion: A closure that's invoked after the process completes.
func present(for appName: ShareAppName, in sender: UIViewController, sourceView: UIView? = nil, completion: (() -> Void)? = nil) {
let anchorView = sourceView ?? sender.view
if let content = cachedContent {
presentShareSheet(with: content, in: sender)
presentShareSheet(with: content, in: sender, sourceView: anchorView)
completion?()
return
}
Expand All @@ -62,7 +68,7 @@ class ShareAppContentPresenter {
switch result {
case .success(let content):
self.cachedContent = content
self.presentShareSheet(with: content, in: sender)
self.presentShareSheet(with: content, in: sender, sourceView: anchorView)

case .failure:
self.showFailureNotice(in: sender)
Expand Down Expand Up @@ -91,7 +97,8 @@ private extension ShareAppContentPresenter {
/// - Parameters:
/// - content: The model containing information metadata for the sharing activity.
/// - viewController: The view controller that will be presenting the activity.
func presentShareSheet(with content: RemoteShareAppContent, in viewController: UIViewController) {
/// - sourceView: The view set to be the anchor for the popover.
func presentShareSheet(with content: RemoteShareAppContent, in viewController: UIViewController, sourceView: UIView?) {
guard let linkURL = content.linkURL() else {
return
}
Expand All @@ -102,7 +109,7 @@ private extension ShareAppContentPresenter {
]

let activityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = viewController.view
activityViewController.popoverPresentationController?.sourceView = sourceView
viewController.present(activityViewController, animated: true, completion: nil)
}

Expand Down

0 comments on commit 6a07b6d

Please sign in to comment.