LineSDKShareViewController.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // LineSDKShareViewController.swift
  3. //
  4. // Copyright (c) 2016-present, LY Corporation. All rights reserved.
  5. //
  6. // You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
  7. // copy and distribute this software in source code or binary form for use
  8. // in connection with the web services and APIs provided by LY Corporation.
  9. //
  10. // As with any software that integrates with the LY Corporation platform, your use of this software
  11. // is subject to the LINE Developers Agreement [http://terms2.line.me/LINE_Developers_Agreement].
  12. // This copyright notice shall be included in all copies or substantial portions of the software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  15. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  17. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. @objcMembers
  22. public class LineSDKShareViewController: NSObject {
  23. private var _binaryCompatibleViewController: ShareViewController
  24. public var viewController: UIViewController {
  25. return _binaryCompatibleViewController
  26. }
  27. var delegateProxy: LineSDKShareViewControllerDelegateProxy?
  28. public var shareNavigationBarTintColor: UIColor {
  29. get { return _binaryCompatibleViewController.navigationBarTintColor }
  30. set { _binaryCompatibleViewController.navigationBarTintColor = newValue }
  31. }
  32. public var shareNavigationBarTextColor: UIColor {
  33. get { return _binaryCompatibleViewController.navigationBarTextColor }
  34. set { _binaryCompatibleViewController.navigationBarTextColor = newValue }
  35. }
  36. public var shareStatusBarStyle: UIStatusBarStyle {
  37. get { return _binaryCompatibleViewController.statusBarStyle }
  38. set { _binaryCompatibleViewController.statusBarStyle = newValue }
  39. }
  40. public var shareMessages: [LineSDKMessage]? {
  41. get {
  42. return _binaryCompatibleViewController.messages?.compactMap { .message(with: $0) }
  43. }
  44. set {
  45. _binaryCompatibleViewController.messages = newValue?.compactMap { $0.unwrapped }
  46. }
  47. }
  48. public var delegate: LineSDKShareViewControllerDelegate? {
  49. get { return delegateProxy?.proxy }
  50. set {
  51. delegateProxy = newValue.map { .init(proxy: $0, owner: self) }
  52. _binaryCompatibleViewController.shareDelegate = delegateProxy
  53. }
  54. }
  55. public override init() {
  56. _binaryCompatibleViewController = ShareViewController()
  57. }
  58. @objc public static func localAuthorizationStatusForSendingMessage()
  59. -> LineSDKAuthorizationStatus
  60. {
  61. return LineSDKAuthorizationStatus.status(
  62. from: ShareViewController.localAuthorizationStatusForSendingMessage()
  63. )
  64. }
  65. }
  66. class LineSDKShareViewControllerDelegateProxy: ShareViewControllerDelegate {
  67. weak var proxy: LineSDKShareViewControllerDelegate?
  68. unowned var owner: LineSDKShareViewController
  69. init(proxy: LineSDKShareViewControllerDelegate, owner: LineSDKShareViewController) {
  70. self.proxy = proxy
  71. self.owner = owner
  72. }
  73. func shareViewController(
  74. _ controller: ShareViewController,
  75. didFailLoadingListType shareType: MessageShareTargetType,
  76. withError error: LineSDKError)
  77. {
  78. proxy?.shareViewController?(owner, didFailLoadingListType: .init(shareType), withError: error)
  79. }
  80. func shareViewControllerDidCancelSharing(_ controller: ShareViewController) {
  81. if let proxy = proxy {
  82. proxy.shareViewControllerDidCancelSharing?(owner) ?? owner.viewController.dismiss(animated: true)
  83. } else {
  84. owner.viewController.dismiss(animated: true)
  85. }
  86. }
  87. func shareViewController(
  88. _ controller: ShareViewController,
  89. didFailSendingMessages messages: [MessageConvertible],
  90. toTargets targets: [ShareTarget],
  91. withError error: LineSDKError)
  92. {
  93. proxy?.shareViewController?(
  94. owner,
  95. didFailSendingMessages: messages.compactMap { LineSDKMessage.message(with: $0) },
  96. toTargets: targets.map { $0.sdkShareTarget },
  97. withError: error)
  98. }
  99. func shareViewController(
  100. _ controller: ShareViewController,
  101. didSendMessages messages: [MessageConvertible],
  102. toTargets targets: [ShareTarget])
  103. {
  104. proxy?.shareViewController?(
  105. owner,
  106. didSendMessages: messages.compactMap { LineSDKMessage.message(with: $0) },
  107. toTargets: targets.map { $0.sdkShareTarget }
  108. )
  109. }
  110. func shareViewController(
  111. _ controller: ShareViewController,
  112. messagesForSendingToTargets targets: [ShareTarget]) -> [MessageConvertible]
  113. {
  114. guard let messages = controller.messages else {
  115. Log.fatalError(
  116. """
  117. You need at least set the `ShareViewController.message` or implement
  118. `shareViewController(:messageForSendingToTargets:)` before sharing a message.)
  119. """
  120. )
  121. }
  122. guard let proxy = proxy else { return messages }
  123. let targets = targets.map { $0.sdkShareTarget }
  124. guard let sdkMessages = proxy.shareViewController?(owner, messagesForSendingToTargets: targets) else {
  125. return messages
  126. }
  127. return sdkMessages.map { $0.unwrapped }
  128. }
  129. func shareViewControllerShouldDismissAfterSending(_ controller: ShareViewController) -> Bool {
  130. return proxy?.shareViewControllerShouldDismissAfterSending?(owner) ?? true
  131. }
  132. }