You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.8 KiB

6 years ago
  1. // Copyright (c) 2016-present, Facebook, Inc. All rights reserved.
  2. //
  3. // You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
  4. // copy, modify, and distribute this software in source code or binary form for use
  5. // in connection with the web services and APIs provided by Facebook.
  6. //
  7. // As with any software that integrates with the Facebook platform, your use of
  8. // this software is subject to the Facebook Developer Principles and Policies
  9. // [http://developers.facebook.com/policy/]. This copyright notice shall be
  10. // included in all copies or substantial portions of the software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. import FBSDKShareKit
  19. /// A dialog for sharing content through Messenger.
  20. public final class MessageDialog<Content: ContentProtocol> {
  21. fileprivate let sdkSharer: FBSDKMessageDialog
  22. fileprivate let sdkShareDelegate: SDKSharingDelegateBridge<Content>
  23. /**
  24. Create a `MessageDialog` with a given content.
  25. - parameter content: The content to share.
  26. */
  27. public init(content: Content) {
  28. sdkSharer = FBSDKMessageDialog()
  29. sdkShareDelegate = SDKSharingDelegateBridge<Content>()
  30. sdkShareDelegate.setupAsDelegateFor(sdkSharer)
  31. sdkSharer.shareContent = ContentBridger.bridgeToObjC(content)
  32. }
  33. }
  34. extension MessageDialog: ContentSharingProtocol {
  35. /// The content that is being shared.
  36. public var content: Content {
  37. get {
  38. guard let swiftContent: Content = ContentBridger.bridgeToSwift(sdkSharer.shareContent) else {
  39. fatalError("Content of our private share dialog has changed type. Something horrible has happened.")
  40. }
  41. return swiftContent
  42. }
  43. }
  44. /// The completion handler to be invoked upon the share performing.
  45. public var completion: ((ContentSharerResult<Content>) -> Void)? {
  46. get {
  47. return sdkShareDelegate.completion
  48. }
  49. set {
  50. sdkShareDelegate.completion = newValue
  51. }
  52. }
  53. /// Whether or not this sharer fails on invalid data.
  54. public var failsOnInvalidData: Bool {
  55. get {
  56. return sdkSharer.shouldFailOnDataError
  57. }
  58. set {
  59. sdkSharer.shouldFailOnDataError = newValue
  60. }
  61. }
  62. /**
  63. Validates the content on the receiver.
  64. - throws: If The content could not be validated.
  65. */
  66. public func validate() throws {
  67. try sdkSharer.validate()
  68. }
  69. }
  70. extension MessageDialog: ContentSharingDialogProtocol {
  71. /**
  72. Shows the dialog.
  73. - throws: If the dialog cannot be presented.
  74. */
  75. public func show() throws {
  76. var error: Error?
  77. let completionHandler = sdkShareDelegate.completion
  78. sdkShareDelegate.completion = {
  79. if case .failed(let resultError) = $0 {
  80. error = resultError
  81. }
  82. }
  83. sdkSharer.show()
  84. sdkShareDelegate.completion = completionHandler
  85. if let error = error {
  86. throw error
  87. }
  88. }
  89. }
  90. extension MessageDialog {
  91. /**
  92. Convenience method to show a Message Share Dialog with content and a completion handler.
  93. - parameter content: The content to share.
  94. - parameter completion: The completion handler to invoke.
  95. - returns: The dialog that has been presented.
  96. - throws: If the dialog fails to validate.
  97. */
  98. @discardableResult
  99. public static func show(_ content: Content, completion: ((ContentSharerResult<Content>) -> Void)? = nil) throws -> Self {
  100. let dialog = self.init(content: content)
  101. dialog.completion = completion
  102. try dialog.show()
  103. return dialog
  104. }
  105. }