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.

165 lines
5.3 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 UIKit
  20. import FBSDKShareKit
  21. /**
  22. UI control to like an object in the Facebook graph.
  23. Taps on the like button within this control will invoke an API call to the Facebook app through a fast-app-switch
  24. that allows the user to like the object. Upon return to the calling app, the view will update with the new state.
  25. */
  26. public class LikeControl: UIView {
  27. fileprivate let sdkLikeControl: FBSDKLikeControl
  28. /**
  29. Create a new LikeControl with an optional frame and object.
  30. - parameter frame: The frame to use for this control. If `nil`, defaults to a default size.
  31. - parameter object: The object to like.
  32. */
  33. public init(frame: CGRect? = nil, object: LikableObject) {
  34. let sdkLikeControl = FBSDKLikeControl()
  35. let frame = frame ?? sdkLikeControl.bounds
  36. self.sdkLikeControl = sdkLikeControl
  37. super.init(frame: frame)
  38. self.object = object
  39. addSubview(sdkLikeControl)
  40. }
  41. /**
  42. Create a new LikeControl from an encoded interface file.
  43. - parameter coder: The coder to initialize from.
  44. */
  45. public required init?(coder: NSCoder) {
  46. sdkLikeControl = FBSDKLikeControl()
  47. super.init(coder: coder)
  48. self.addSubview(sdkLikeControl)
  49. }
  50. /// The foreground color to use for the content of the control.
  51. public var foregroundColor: UIColor {
  52. get {
  53. return sdkLikeControl.foregroundColor
  54. }
  55. set {
  56. sdkLikeControl.foregroundColor = newValue
  57. }
  58. }
  59. /// The object to like.
  60. public var object: LikableObject {
  61. get {
  62. return LikableObject(sdkObjectType: sdkLikeControl.objectType, sdkObjectId: sdkLikeControl.objectID)
  63. }
  64. set {
  65. let sdkRepresentation = newValue.sdkObjectRepresntation
  66. sdkLikeControl.objectType = sdkRepresentation.objectType
  67. sdkLikeControl.objectID = sdkRepresentation.objectId
  68. }
  69. }
  70. /// The style to use for this control.
  71. public var auxilaryStyle: AuxilaryStyle {
  72. get {
  73. return AuxilaryStyle(
  74. sdkStyle: sdkLikeControl.likeControlStyle,
  75. sdkHorizontalAlignment: sdkLikeControl.likeControlHorizontalAlignment,
  76. sdkAuxilaryPosition: sdkLikeControl.likeControlAuxiliaryPosition
  77. )
  78. }
  79. set {
  80. (
  81. sdkLikeControl.likeControlStyle,
  82. sdkLikeControl.likeControlHorizontalAlignment,
  83. sdkLikeControl.likeControlAuxiliaryPosition
  84. ) = newValue.sdkStyleRepresentation
  85. }
  86. }
  87. /**
  88. The preferred maximum width (in points) for autolayout.
  89. This property affects the size of the receiver when layout constraints are applied to it. During layout,
  90. if the text extends beyond the width specified by this property, the additional text is flowed to one or more new
  91. lines, thereby increasing the height of the receiver.
  92. */
  93. public var preferredMaxLayoutWidth: CGFloat {
  94. get {
  95. return sdkLikeControl.preferredMaxLayoutWidth
  96. }
  97. set {
  98. sdkLikeControl.preferredMaxLayoutWidth = newValue
  99. }
  100. }
  101. /// If `true`, a sound is played when the control is toggled.
  102. public var isSoundEnabled: Bool {
  103. get {
  104. return sdkLikeControl.isSoundEnabled
  105. }
  106. set {
  107. sdkLikeControl.isSoundEnabled = newValue
  108. }
  109. }
  110. }
  111. extension LikeControl {
  112. /**
  113. Performs logic for laying out subviews.
  114. */
  115. public override func layoutSubviews() {
  116. super.layoutSubviews()
  117. sdkLikeControl.frame = CGRect(origin: .zero, size: bounds.size)
  118. }
  119. /**
  120. Resizes and moves the receiver view so it just encloses its subviews.
  121. */
  122. public override func sizeToFit() {
  123. bounds.size = sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude))
  124. }
  125. /**
  126. Asks the view to calculate and return the size that best fits the specified size.
  127. - parameter size: A new size that fits the receivers subviews.
  128. - returns: A new size that fits the receivers subviews.
  129. */
  130. public override func sizeThatFits(_ size: CGSize) -> CGSize {
  131. return sdkLikeControl.sizeThatFits(size)
  132. }
  133. /**
  134. Returns the natural size for the receiving view, considering only properties of the view itself.
  135. - returns: A size indicating the natural size for the receiving view based on its intrinsic properties.
  136. */
  137. public override var intrinsicContentSize: CGSize {
  138. return sdkLikeControl.intrinsicContentSize
  139. }
  140. }