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.

121 lines
4.0 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. /**
  21. A button to like an object.
  22. Tapping the receiver will invoke an API call to the Facebook app through a fast-app-switch that allows
  23. the object to be liked. Upon return to the calling app, the view will update with the new state. If the
  24. currentAccessToken has "publish_actions" permission and the object is an Open Graph object, then the like can happen
  25. seamlessly without the fast-app-switch.
  26. */
  27. public class LikeButton: UIView {
  28. fileprivate var sdkLikeButton: FBSDKLikeButton
  29. /// If `true`, a sound is played when the reciever is toggled.
  30. public var isSoundEnabled: Bool {
  31. get {
  32. return sdkLikeButton.isSoundEnabled
  33. }
  34. set {
  35. sdkLikeButton.isSoundEnabled = newValue
  36. }
  37. }
  38. /// The object to like
  39. public var object: LikableObject {
  40. get {
  41. return LikableObject(sdkObjectType: sdkLikeButton.objectType, sdkObjectId: sdkLikeButton.objectID)
  42. }
  43. set {
  44. let sdkRepresentation = newValue.sdkObjectRepresntation
  45. sdkLikeButton.objectType = sdkRepresentation.objectType
  46. sdkLikeButton.objectID = sdkRepresentation.objectId
  47. }
  48. }
  49. /**
  50. Create a new LikeButton with a given frame and object.
  51. - parameter frame: The frame to initialize with.
  52. - parameter object: The object to like.
  53. */
  54. public init(frame: CGRect? = nil, object: LikableObject) {
  55. let sdkLikeButton = FBSDKLikeButton()
  56. let frame = frame ?? sdkLikeButton.bounds
  57. self.sdkLikeButton = sdkLikeButton
  58. super.init(frame: frame)
  59. self.object = object
  60. self.addSubview(sdkLikeButton)
  61. }
  62. /**
  63. Create a new LikeButton from an encoded interface file.
  64. - parameter aDecoder: The coder to initialize from.
  65. */
  66. public required init?(coder aDecoder: NSCoder) {
  67. sdkLikeButton = FBSDKLikeButton()
  68. super.init(coder: aDecoder)
  69. addSubview(sdkLikeButton)
  70. }
  71. /**
  72. Performs logic for laying out subviews.
  73. */
  74. public override func layoutSubviews() {
  75. super.layoutSubviews()
  76. sdkLikeButton.frame = CGRect(origin: .zero, size: bounds.size)
  77. }
  78. /**
  79. Resizes and moves the receiver view so it just encloses its subviews.
  80. */
  81. public override func sizeToFit() {
  82. bounds.size = sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude))
  83. }
  84. /**
  85. Asks the view to calculate and return the size that best fits the specified size.
  86. - parameter size: A new size that fits the receivers subviews.
  87. - returns: A new size that fits the receivers subviews.
  88. */
  89. public override func sizeThatFits(_ size: CGSize) -> CGSize {
  90. return sdkLikeButton.sizeThatFits(size)
  91. }
  92. /**
  93. Returns the natural size for the receiving view, considering only properties of the view itself.
  94. - returns: A size indicating the natural size for the receiving view based on its intrinsic properties.
  95. */
  96. public override var intrinsicContentSize: CGSize {
  97. return sdkLikeButton.intrinsicContentSize
  98. }
  99. }