Skip to content

Commit d47bf2b

Browse files
committed
make share sheet from preview better
1 parent 0c5a287 commit d47bf2b

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

Django Files/Views/Preview.swift

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -792,20 +792,8 @@ struct FilePreviewView: View {
792792
.menuStyle(.button)
793793

794794
Button(action: {
795-
// share sheet call goes here
796-
// Dismiss any presented views first
797-
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
798-
let window = windowScene.windows.first,
799-
let rootVC = window.rootViewController {
800-
rootVC.dismiss(animated: true) {
801-
let activityVC = UIActivityViewController(activityItems: [URL(string: file.url) ?? ""], applicationActivities: nil)
802-
if let popover = activityVC.popoverPresentationController {
803-
popover.sourceView = rootVC.view
804-
popover.sourceRect = CGRect(x: rootVC.view.bounds.midX, y: rootVC.view.bounds.midY, width: 0, height: 0)
805-
}
806-
rootVC.present(activityVC, animated: true)
807-
}
808-
}
795+
ShareSheet.present(items: [URL(string: file.url) ?? ""])
796+
809797
}) {
810798
Image(systemName: "square.and.arrow.up")
811799
.font(.system(size: 20))
@@ -819,10 +807,28 @@ struct FilePreviewView: View {
819807
.frame(width: 155, height: 44)
820808
.cornerRadius(20)
821809
}
822-
823810
}
824811
}
812+
}
813+
}
814+
815+
struct ShareSheet {
816+
static func present(items: [Any]) {
817+
guard let rootVC = UIApplication.shared.connectedScenes
818+
.compactMap({ ($0 as? UIWindowScene)?.keyWindow })
819+
.first?.rootViewController else {
820+
return
821+
}
822+
823+
let activityVC = UIActivityViewController(activityItems: items, applicationActivities: nil)
824+
825+
// Find the topmost presented view controller
826+
var topVC = rootVC
827+
while let presented = topVC.presentedViewController {
828+
topVC = presented
829+
}
825830

831+
topVC.present(activityVC, animated: true)
826832
}
827833
}
828834

0 commit comments

Comments
 (0)