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.

98 lines
3.2 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 Foundation
  19. import FBSDKShareKit
  20. @testable import FacebookCore
  21. /**
  22. An Open Graph action for sharing.
  23. */
  24. public struct OpenGraphAction {
  25. // TODO (richardross): Make ActionType an enum with common action types.
  26. /// The action type.
  27. public var type: String
  28. fileprivate var properties: [OpenGraphPropertyName : OpenGraphPropertyValue]
  29. /**
  30. Create an `OpenGraphAction` with a specific action type.
  31. - parameter type: The type of the action.
  32. */
  33. public init(type: String) {
  34. self.type = type
  35. self.properties = [:]
  36. }
  37. }
  38. extension OpenGraphAction: OpenGraphPropertyContaining {
  39. /// Get the property names contained in this container.
  40. public var propertyNames: Set<OpenGraphPropertyName> {
  41. return Set(properties.keys)
  42. }
  43. public subscript(key: OpenGraphPropertyName) -> OpenGraphPropertyValue? {
  44. get {
  45. return properties[key]
  46. }
  47. set {
  48. properties[key] = newValue
  49. }
  50. }
  51. }
  52. extension OpenGraphAction {
  53. internal var sdkActionRepresentation: FBSDKShareOpenGraphAction {
  54. let sdkAction = FBSDKShareOpenGraphAction()
  55. sdkAction.actionType = type
  56. sdkAction.parseProperties(properties.keyValueMap { key, value in
  57. (key.rawValue, value.openGraphPropertyValue)
  58. })
  59. return sdkAction
  60. }
  61. internal init(sdkAction: FBSDKShareOpenGraphAction) {
  62. self.type = sdkAction.actionType
  63. var properties = [OpenGraphPropertyName : OpenGraphPropertyValue]()
  64. sdkAction.enumerateKeysAndObjects { (key: String?, value: Any?, stop) in
  65. guard let key = key.map(OpenGraphPropertyName.init(rawValue:)),
  66. let value = value.map(OpenGraphPropertyValueConverter.valueFrom) else {
  67. return
  68. }
  69. properties[key] = value
  70. }
  71. self.properties = properties
  72. }
  73. }
  74. extension OpenGraphAction: Equatable {
  75. /**
  76. Compare two `OpenGraphAction`s for equality.
  77. - parameter lhs: The first action to compare.
  78. - parameter rhs: The second action to compare.
  79. - returns: Whether or not the actions are equal.
  80. */
  81. public static func == (lhs: OpenGraphAction, rhs: OpenGraphAction) -> Bool {
  82. return lhs.sdkActionRepresentation == rhs.sdkActionRepresentation
  83. }
  84. }