Skip to content

Commit

Permalink
Introduces a basic ShareLink implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
shaps80 committed Oct 3, 2022
1 parent e05c689 commit b7800ca
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 171 deletions.
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
import SwiftUI

@available(iOS, deprecated: 16)
@available(macOS, deprecated: 13)
@available(watchOS, deprecated: 9)
@available(tvOS, unavailable)
public extension Backport.ShareLink where Wrapped == Any {
init<S: StringProtocol>(_ title: S, items: Data, subject: String? = nil, message: String? = nil, preview: @escaping (Data.Element) -> SharePreview<PreviewImage, PreviewIcon>)
where Data.Element: BackportTransferable, Label == DefaultShareLinkLabel
{
self.label = .init(title)
self.data = items
self.subject = subject
self.message = message
self.preview = preview
}

init(_ titleKey: LocalizedStringKey, items: Data, subject: String? = nil, message: String? = nil, preview: @escaping (Data.Element) -> SharePreview<PreviewImage, PreviewIcon>)
where Data.Element: BackportTransferable, Label == DefaultShareLinkLabel
{
self.label = .init(titleKey)
self.data = items
self.subject = subject
self.message = message
self.preview = preview
}

init(_ title: Text, items: Data, subject: String? = nil, message: String? = nil, preview: @escaping (Data.Element) -> SharePreview<PreviewImage, PreviewIcon>)
where Data.Element: BackportTransferable, Label == DefaultShareLinkLabel
{
self.label = .init(title)
self.data = items
self.subject = subject
self.message = message
self.preview = preview
}
}
//import SwiftUI
//
//@available(iOS, deprecated: 16)
//@available(macOS, deprecated: 13)
//@available(watchOS, deprecated: 9)
//@available(tvOS, unavailable)
//public extension Backport.ShareLink where Wrapped == Any {
// init<S: StringProtocol>(_ title: S, items: Data, subject: String? = nil, message: String? = nil, preview: @escaping (Data.Element) -> SharePreview<PreviewImage, PreviewIcon>)
// where Data.Element: Shareable, Label == DefaultShareLinkLabel
// {
// self.label = .init(title)
// self.data = items
// self.subject = subject
// self.message = message
// self.preview = preview
// }
//
// init(_ titleKey: LocalizedStringKey, items: Data, subject: String? = nil, message: String? = nil, preview: @escaping (Data.Element) -> SharePreview<PreviewImage, PreviewIcon>)
// where Data.Element: Shareable, Label == DefaultShareLinkLabel
// {
// self.label = .init(titleKey)
// self.data = items
// self.subject = subject
// self.message = message
// self.preview = preview
// }
//
// init(_ title: Text, items: Data, subject: String? = nil, message: String? = nil, preview: @escaping (Data.Element) -> SharePreview<PreviewImage, PreviewIcon>)
// where Data.Element: Shareable, Label == DefaultShareLinkLabel
// {
// self.label = .init(title)
// self.data = items
// self.subject = subject
// self.message = message
// self.preview = preview
// }
//}
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import SwiftUI

@available(iOS, deprecated: 16)
@available(macOS, deprecated: 13)
@available(watchOS, deprecated: 9)
@available(tvOS, unavailable)
public extension Backport.ShareLink where Wrapped == Any {
init(items: Data, subject: String? = nil, message: String? = nil, preview: @escaping (Data.Element) -> SharePreview<PreviewImage, PreviewIcon>)
where Data.Element: BackportTransferable, Label == DefaultShareLinkLabel {
self.label = .init()
self.data = items
self.subject = subject
self.message = message
self.preview = preview
}

init(items: Data, subject: String? = nil, message: String? = nil, preview: @escaping (Data.Element) -> SharePreview<PreviewImage, PreviewIcon>, @ViewBuilder label: () -> Label)
where Data.Element: BackportTransferable {
self.label = label()
self.data = items
self.subject = subject
self.message = message
self.preview = preview
}
}
//import SwiftUI
//
//@available(iOS, deprecated: 16)
//@available(macOS, deprecated: 13)
//@available(watchOS, deprecated: 9)
//@available(tvOS, unavailable)
//public extension Backport.ShareLink where Wrapped == Any {
// init(items: Data, subject: String? = nil, message: String? = nil, preview: @escaping (Data.Element) -> SharePreview<PreviewImage, PreviewIcon>)
// where Data.Element: Shareable, Label == DefaultShareLinkLabel {
// self.label = .init()
// self.data = items
// self.subject = subject
// self.message = message
// self.preview = preview
// }
//
// init(items: Data, subject: String? = nil, message: String? = nil, preview: @escaping (Data.Element) -> SharePreview<PreviewImage, PreviewIcon>, @ViewBuilder label: () -> Label)
// where Data.Element: Shareable {
// self.label = label()
// self.data = items
// self.subject = subject
// self.message = message
// self.preview = preview
// }
//}
82 changes: 41 additions & 41 deletions Sources/SwiftUIBackports/Shared/ShareLink/ShareLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import LinkPresentation
@available(watchOS, deprecated: 9)
@available(tvOS, unavailable)
public extension Backport where Wrapped == Any {
struct ShareLink<Data, PreviewImage, PreviewIcon, Label>: View where Data: RandomAccessCollection, Data.Element: BackportTransferable, Label: View {
struct ShareLink<Data, PreviewImage, PreviewIcon, Label>: View where Data: RandomAccessCollection, Data.Element: Shareable, Label: View {
@State private var activity: ActivityItem<Data>?

let label: Label
Expand All @@ -17,7 +17,7 @@ public extension Backport where Wrapped == Any {

public var body: some View {
Button {

activity = ActivityItem(data: data)
} label: {
label
}
Expand All @@ -26,42 +26,42 @@ public extension Backport where Wrapped == Any {
}
}

final class TransferableActivityProvider<Data, Image: View, Icon: View>: UIActivityItemProvider where Data: BackportTransferable {
let title: String?
let subject: String?
let message: String?
let image: Image?
let icon: Icon?
let data: Data

init(data: Data, title: String?, subject: String?, message: String?, image: Image?, icon: Icon?) {
self.title = title
self.subject = subject
self.message = message
self.image = image
self.icon = icon
self.data = data

let url = URL(fileURLWithPath: NSTemporaryDirectory())
.appendingPathComponent("tmp")
.appendingPathExtension(data.pathExtension)

super.init(placeholderItem: url)
}

override var item: Any {
data.itemProvider as Any
}

override func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
let metadata = LPLinkMetadata()
metadata.title = title
// let icon = ImageRenderer(content: activity.icon)
// metadata.iconProvider = NSItemProvider(object: UIImage())
// metadata.imageProvider = NSItemProvider(object: UIImage())
return metadata
}

override func activityViewController(_ activityViewController: UIActivityViewController, subjectForActivityType activityType: UIActivity.ActivityType?) -> String { subject ?? "" }

}
//final class TransferableActivityProvider<Data: Shareable, Image: View, Icon: View>: UIActivityItemProvider {
// let title: String?
// let subject: String?
// let message: String?
// let image: Image?
// let icon: Icon?
// let data: Data
//
// init(data: Data, title: String?, subject: String?, message: String?, image: Image?, icon: Icon?) {
// self.title = title
// self.subject = subject
// self.message = message
// self.image = image
// self.icon = icon
// self.data = data
//
// let url = URL(fileURLWithPath: NSTemporaryDirectory())
// .appendingPathComponent("tmp")
// .appendingPathExtension(data.pathExtension)
//
// super.init(placeholderItem: url)
// }
//
// override var item: Any {
// data.itemProvider as Any
// }
//
// override func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
// let metadata = LPLinkMetadata()
// metadata.title = title
//// let icon = ImageRenderer(content: activity.icon)
//// metadata.iconProvider = NSItemProvider(object: UIImage())
//// metadata.imageProvider = NSItemProvider(object: UIImage())
// return metadata
// }
//
// override func activityViewController(_ activityViewController: UIActivityViewController, subjectForActivityType activityType: UIActivity.ActivityType?) -> String { subject ?? "" }
//
//}
6 changes: 3 additions & 3 deletions Sources/SwiftUIBackports/Shared/ShareLink/ShareSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SwiftUI

extension View {
@ViewBuilder
func shareSheet<Data>(item activityItems: Binding<ActivityItem<Data>?>) -> some View where Data: RandomAccessCollection, Data.Element: BackportTransferable {
func shareSheet<Data>(item activityItems: Binding<ActivityItem<Data>?>) -> some View where Data: RandomAccessCollection, Data.Element: Shareable {
#if os(macOS)
background(ShareSheet(item: activityItems))
#elseif os(iOS)
Expand All @@ -13,7 +13,7 @@ extension View {

#if os(macOS)

private struct ShareSheet: NSViewRepresentable where Data: RandomAccessCollection, Data.Element: BackportTransferable {
private struct ShareSheet: NSViewRepresentable where Data: RandomAccessCollection, Data.Element: Shareable {
@Binding var item: ActivityItem<Data>?

public func makeNSView(context: Context) -> SourceView {
Expand Down Expand Up @@ -86,7 +86,7 @@ private struct ShareSheet: NSViewRepresentable where Data: RandomAccessCollectio

#elseif os(iOS)

private struct ShareSheet<Data>: UIViewControllerRepresentable where Data: RandomAccessCollection, Data.Element: BackportTransferable {
private struct ShareSheet<Data>: UIViewControllerRepresentable where Data: RandomAccessCollection, Data.Element: Shareable {
@Binding var item: ActivityItem<Data>?

init(item: Binding<ActivityItem<Data>?>) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import SwiftUI

@available(iOS, deprecated: 16)
@available(macOS, deprecated: 13)
@available(watchOS, deprecated: 9)
@available(tvOS, unavailable)
public extension Backport.ShareLink where Wrapped == Any {
init<S: StringProtocol, I: BackportTransferable>(_ title: S, item: I, subject: String? = nil, message: String? = nil, preview: SharePreview<PreviewImage, PreviewIcon>)
where Data == CollectionOfOne<I>, Label == DefaultShareLinkLabel {
self.label = .init(title)
self.data = .init(item)
self.subject = subject
self.message = message
self.preview = { _ in preview }
}

init<I: BackportTransferable>(_ titleKey: LocalizedStringKey, item: I, subject: String? = nil, message: String? = nil, preview: SharePreview<PreviewImage, PreviewIcon>)
where Data == CollectionOfOne<I>, Label == DefaultShareLinkLabel {
self.label = .init(titleKey)
self.data = .init(item)
self.subject = subject
self.message = message
self.preview = { _ in preview }
}

init<I: BackportTransferable>(_ title: Text, item: I, subject: String? = nil, message: String? = nil, preview: SharePreview<PreviewImage, PreviewIcon>)
where Data == CollectionOfOne<I>, Label == DefaultShareLinkLabel {
self.label = .init(title)
self.data = .init(item)
self.subject = subject
self.message = message
self.preview = { _ in preview }
}
}
//import SwiftUI
//
//@available(iOS, deprecated: 16)
//@available(macOS, deprecated: 13)
//@available(watchOS, deprecated: 9)
//@available(tvOS, unavailable)
//public extension Backport.ShareLink where Wrapped == Any {
// init<S: StringProtocol, I: Shareable>(_ title: S, item: I, subject: String? = nil, message: String? = nil, preview: SharePreview<PreviewImage, PreviewIcon>)
// where Data == CollectionOfOne<I>, Label == DefaultShareLinkLabel {
// self.label = .init(title)
// self.data = .init(item)
// self.subject = subject
// self.message = message
// self.preview = { _ in preview }
// }
//
// init<I: Shareable>(_ titleKey: LocalizedStringKey, item: I, subject: String? = nil, message: String? = nil, preview: SharePreview<PreviewImage, PreviewIcon>)
// where Data == CollectionOfOne<I>, Label == DefaultShareLinkLabel {
// self.label = .init(titleKey)
// self.data = .init(item)
// self.subject = subject
// self.message = message
// self.preview = { _ in preview }
// }
//
// init<I: Shareable>(_ title: Text, item: I, subject: String? = nil, message: String? = nil, preview: SharePreview<PreviewImage, PreviewIcon>)
// where Data == CollectionOfOne<I>, Label == DefaultShareLinkLabel {
// self.label = .init(title)
// self.data = .init(item)
// self.subject = subject
// self.message = message
// self.preview = { _ in preview }
// }
//}
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import SwiftUI

@available(iOS, deprecated: 16)
@available(macOS, deprecated: 13)
@available(watchOS, deprecated: 9)
@available(tvOS, unavailable)
public extension Backport.ShareLink where Wrapped == Any {
init<I: BackportTransferable>(item: I, subject: String? = nil, message: String? = nil, preview: SharePreview<PreviewImage, PreviewIcon>)
where Data == CollectionOfOne<I>, Label == DefaultShareLinkLabel {
self.label = .init()
self.data = .init(item)
self.subject = subject
self.message = message
self.preview = { _ in preview }
}

init<I: BackportTransferable>(item: I, subject: String? = nil, message: String? = nil, preview: SharePreview<PreviewImage, PreviewIcon>, @ViewBuilder label: () -> Label)
where Data == CollectionOfOne<I> {
self.label = label()
self.data = .init(item)
self.subject = subject
self.message = message
self.preview = { _ in preview }
}
}
//import SwiftUI
//
//@available(iOS, deprecated: 16)
//@available(macOS, deprecated: 13)
//@available(watchOS, deprecated: 9)
//@available(tvOS, unavailable)
//public extension Backport.ShareLink where Wrapped == Any {
// init<I: Shareable>(item: I, subject: String? = nil, message: String? = nil, preview: SharePreview<PreviewImage, PreviewIcon>)
// where Data == CollectionOfOne<I>, Label == DefaultShareLinkLabel {
// self.label = .init()
// self.data = .init(item)
// self.subject = subject
// self.message = message
// self.preview = { _ in preview }
// }
//
// init<I: Shareable>(item: I, subject: String? = nil, message: String? = nil, preview: SharePreview<PreviewImage, PreviewIcon>, @ViewBuilder label: () -> Label)
// where Data == CollectionOfOne<I> {
// self.label = label()
// self.data = .init(item)
// self.subject = subject
// self.message = message
// self.preview = { _ in preview }
// }
//}
Loading

0 comments on commit b7800ca

Please sign in to comment.