|
| 1 | +// |
| 2 | +// TransportableItem.swift |
| 3 | +// |
| 4 | +// Created by Igor Shel on 29.10.2023. |
| 5 | + |
| 6 | +import UIKit |
| 7 | +import LinkPresentation |
| 8 | + |
| 9 | +/// A class that conforms to `UIActivityItemSource` to provide transportable items for sharing activities. |
| 10 | +final class TransportableItem<T: Transportable>: NSObject, UIActivityItemSource { |
| 11 | + /// The item to be shared, conforming to the `Transportable` protocol. |
| 12 | + private let item: T |
| 13 | + |
| 14 | + /// An optional title for the item. |
| 15 | + private let title: String? |
| 16 | + |
| 17 | + /// An optional icon for the item. |
| 18 | + private let icon: UIImage? |
| 19 | + |
| 20 | + /// Initializes a new transportable item with optional title and icon. |
| 21 | + /// |
| 22 | + /// - Parameters: |
| 23 | + /// - item: The item to be shared, conforming to the `Transportable` protocol. |
| 24 | + /// - icon: An optional icon for the item. |
| 25 | + /// - title: An optional title for the item. |
| 26 | + init(item: T, icon: UIImage? = nil, title: String? = nil) { |
| 27 | + self.item = item |
| 28 | + self.title = title |
| 29 | + self.icon = icon |
| 30 | + super.init() |
| 31 | + } |
| 32 | + |
| 33 | + /// Provides a placeholder item for the activity view controller. |
| 34 | + /// |
| 35 | + /// - Parameter activityViewController: The `UIActivityViewController` requesting the placeholder item. |
| 36 | + /// - Returns: The item to be shared. |
| 37 | + func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any { |
| 38 | + return item |
| 39 | + } |
| 40 | + |
| 41 | + /// Provides the actual item to be shared for the specified activity type. |
| 42 | + /// |
| 43 | + /// - Parameters: |
| 44 | + /// - activityViewController: The `UIActivityViewController` requesting the item. |
| 45 | + /// - activityType: The type of activity requesting the item. |
| 46 | + /// - Returns: The item to be shared. |
| 47 | + func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? { |
| 48 | + return item |
| 49 | + } |
| 50 | + |
| 51 | + /// Provides metadata for the link being shared. |
| 52 | + /// |
| 53 | + /// - Parameter activityViewController: The `UIActivityViewController` requesting the metadata. |
| 54 | + /// - Returns: The `LPLinkMetadata` object containing metadata about the item. |
| 55 | + func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? { |
| 56 | + let metadata = LPLinkMetadata() |
| 57 | + |
| 58 | + // Set icon if available |
| 59 | + if let icon = icon { |
| 60 | + metadata.iconProvider = NSItemProvider(object: icon) |
| 61 | + } else if let icon = item as? UIImage { |
| 62 | + metadata.iconProvider = NSItemProvider(object: icon) |
| 63 | + } |
| 64 | + |
| 65 | + // Set image if available |
| 66 | + if let image = item as? UIImage { |
| 67 | + metadata.imageProvider = NSItemProvider(object: image) |
| 68 | + } |
| 69 | + |
| 70 | + // Set title if available |
| 71 | + if let title = title { |
| 72 | + metadata.title = title |
| 73 | + } |
| 74 | + |
| 75 | + // Set item URL if the item is a URL |
| 76 | + if let urlString = item as? String, let itemURL = URL(string: urlString) { |
| 77 | + metadata.url = itemURL |
| 78 | + } else if let itemURL = item as? URL { |
| 79 | + metadata.url = itemURL |
| 80 | + } |
| 81 | + |
| 82 | + return metadata |
| 83 | + } |
| 84 | +} |
0 commit comments