share sheet

This commit is contained in:
Levitating Pineapple 2024-09-04 21:25:30 +03:00
parent 69654e187e
commit 0064155825
No known key found for this signature in database
GPG key ID: 6EAADA587E14B73A
2 changed files with 41 additions and 1 deletions

View file

@ -20,3 +20,41 @@ func showShareSheet(items: [Any], completed: (() -> Void)? = nil) {
presentedViewController.present(activityViewController, animated: true) presentedViewController.present(activityViewController, animated: true)
} }
} }
extension View {
func shareSheet(item: Binding<ShareItem?>) -> some View {
sheet(item: item) { item in
Group {
if #available(iOS 16.0, *) {
ActivityView(item: item)
.presentationDetents([.medium, .large])
} else {
ActivityView(item: item)
}
}.ignoresSafeArea()
}
}
}
struct ShareItem: Identifiable {
let content: any Hashable
var id: Int { content.hashValue }
}
private struct ActivityView: UIViewControllerRepresentable {
let item: ShareItem
func makeUIViewController(
context: UIViewControllerRepresentableContext<ActivityView>
) -> UIActivityViewController {
UIActivityViewController(
activityItems: [item.content],
applicationActivities: nil
)
}
func updateUIViewController(
_ uiViewController: UIActivityViewController,
context: UIViewControllerRepresentableContext<ActivityView>
) { }
}

View file

@ -22,6 +22,7 @@ struct UserAddressView: View {
@State private var mailViewResult: Result<MFMailComposeResult, Error>? = nil @State private var mailViewResult: Result<MFMailComposeResult, Error>? = nil
@State private var alert: UserAddressAlert? @State private var alert: UserAddressAlert?
@State private var progressIndicator = false @State private var progressIndicator = false
@State private var shareItem: ShareItem?
@FocusState private var keyboardVisible: Bool @FocusState private var keyboardVisible: Bool
private enum UserAddressAlert: Identifiable { private enum UserAddressAlert: Identifiable {
@ -68,6 +69,7 @@ struct UserAddressView: View {
} }
} }
} }
.shareSheet(item: $shareItem)
} }
@Namespace private var bottomID @Namespace private var bottomID
@ -245,7 +247,7 @@ struct UserAddressView: View {
private func shareQRCodeButton(_ userAddress: UserContactLink) -> some View { private func shareQRCodeButton(_ userAddress: UserContactLink) -> some View {
Button { Button {
showShareSheet(items: [simplexChatLink(userAddress.connReqContact)]) shareItem = ShareItem(content: simplexChatLink(userAddress.connReqContact))
} label: { } label: {
settingsRow("square.and.arrow.up", color: theme.colors.secondary) { settingsRow("square.and.arrow.up", color: theme.colors.secondary) {
Text("Share address") Text("Share address")