KMHomeRecommondView.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // KMHomeRecommondView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/10/10.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMHomeRecommondView: BaseXibView {
  10. @IBOutlet var dividerView: ComponentDivider!
  11. @IBOutlet var productsBGView: NSView!
  12. @IBOutlet var productsLabel: NSTextField!
  13. @IBOutlet var othersBGView: NSView!
  14. @IBOutlet var othersLabel: NSTextField!
  15. @IBOutlet var othersTopConst: NSLayoutConstraint!
  16. @IBOutlet var adsView: NSView!
  17. @IBOutlet var adsImageView: NSImageView!
  18. @IBOutlet var adsCloseBtn: NSButton!
  19. //MARK: - func
  20. //MARK: - func
  21. public override func draw(_ dirtyRect: NSRect) {
  22. super.draw(dirtyRect)
  23. // Drawing code here.
  24. }
  25. public required init?(coder decoder: NSCoder) {
  26. super.init(coder: decoder)
  27. }
  28. override init(frame frameRect: NSRect) {
  29. super.init(frame: frameRect)
  30. }
  31. public override func awakeFromNib() {
  32. super.awakeFromNib()
  33. setupUI()
  34. }
  35. //MARK: - Setter
  36. open var inputData: KMAdvertisementContent? {
  37. didSet {
  38. reloadData()
  39. }
  40. }
  41. func setupUI() {
  42. productsLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  43. othersLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  44. productsLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  45. othersLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  46. }
  47. func reloadData() {
  48. if let subviews = productsBGView?.subviews {
  49. for view in subviews {
  50. if view is ComponentCSelector {
  51. view.removeFromSuperview()
  52. }
  53. }
  54. }
  55. if let subviews = othersBGView?.subviews {
  56. for view in subviews {
  57. if view is ComponentNavBarItem {
  58. view.removeFromSuperview()
  59. }
  60. }
  61. }
  62. let products = KMAdvertisementManager.manager.info.recommondContent?.recommondContentPDFPro
  63. productsBGView.isHidden = true
  64. othersTopConst.constant = 24
  65. if let productContents = products?.content {
  66. productsBGView.isHidden = false
  67. othersTopConst.constant = 100
  68. productsLabel.stringValue = KMAdvertisementModelTransition.transitionLanguage(langeuage: products?.name)
  69. var viewXValue: CGFloat = 0
  70. for item: KMAdvertisementItemInfo in productContents {
  71. let view = ComponentCSelector()
  72. view.frame = CGRectMake(viewXValue, 0, 43, 24)
  73. productsBGView.addSubview(view)
  74. viewXValue += 55
  75. view.properties = ComponentCSelectorProperty(size: .s, state: .normal, text: nil, iconImage: nil)
  76. let normalUrl = URL(string: KMAdvertisementModelTransition.transitionImagePath(image: item.image, highlight: false))
  77. let highURL = URL(string: KMAdvertisementModelTransition.transitionImagePath(image: item.image, highlight: true))
  78. let toolTip = KMAdvertisementModelTransition.transitionLanguage(langeuage: item.tooltip)
  79. var normalImage = NSImage()
  80. normalImage = KMAdvertisementImage.imageWithURL(url: normalUrl) { image in
  81. normalImage = image
  82. view.reloadData()
  83. }
  84. view.properties.iconImage = normalImage
  85. view.toolTip = toolTip
  86. var hov_Image = NSImage()
  87. hov_Image = KMAdvertisementImage.imageWithURL(url: highURL) { image in
  88. hov_Image = image
  89. view.reloadData()
  90. }
  91. view.properties.icon_hov_Image = hov_Image
  92. view.reloadData()
  93. view.customData = item
  94. view.setTarget(self, action: #selector(productsItemClick(_:)))
  95. }
  96. }
  97. let others = KMAdvertisementManager.manager.info.recommondContent?.recommondContentOther
  98. if let productContents = others?.content {
  99. othersBGView.isHidden = false
  100. var viewYValue: CGFloat = CGRectGetHeight(othersBGView.frame) - 28 - 36
  101. othersLabel.stringValue = KMAdvertisementModelTransition.transitionLanguage(langeuage: others?.name)
  102. for item: KMAdvertisementItemInfo in productContents {
  103. let view = ComponentNavBarItem()
  104. view.frame = CGRectMake(0, viewYValue, CGRectGetWidth(othersBGView.frame), 36)
  105. view.autoresizingMask = [.width, .minYMargin]
  106. othersBGView.addSubview(view)
  107. view.properties = ComponentNavbarItemProperty(state: .normal,
  108. text: KMAdvertisementModelTransition.transitionLanguage(langeuage: item.name),
  109. iconImage: nil,
  110. keepState: false)
  111. let normalUrl = URL(string: KMAdvertisementModelTransition.transitionImagePath(image: item.image, highlight: false))
  112. let highURL = URL(string: KMAdvertisementModelTransition.transitionImagePath(image: item.image, highlight: true))
  113. let toolTip = KMAdvertisementModelTransition.transitionLanguage(langeuage: item.tooltip)
  114. var normalImage = NSImage()
  115. normalImage = KMAdvertisementImage.imageWithURL(url: normalUrl) { image in
  116. normalImage = image
  117. view.reloadData()
  118. }
  119. view.properties.iconImage = normalImage
  120. view.toolTip = toolTip
  121. var hov_Image = NSImage()
  122. hov_Image = KMAdvertisementImage.imageWithURL(url: highURL) { image in
  123. hov_Image = image
  124. view.reloadData()
  125. }
  126. view.properties.icon_hov_Image = hov_Image
  127. view.reloadData()
  128. view.customData = item
  129. view.setTarget(self, action: #selector(othersItemClick(_:)))
  130. viewYValue -= 44
  131. }
  132. } else {
  133. othersBGView.isHidden = true
  134. }
  135. if IAPProductsManager.default().isAvailableAllFunction() {
  136. adsView.isHidden = true
  137. } else {
  138. adsView.isHidden = false
  139. let adsData = KMAdvertisementManager.manager.info.advertisement?.content
  140. guard let model = adsData?.first else {
  141. adsView.isHidden = true
  142. return
  143. }
  144. if let versionKey = model.version {
  145. if UserDefaults.standard.object(forKey: versionKey) != nil {
  146. adsView.isHidden = true
  147. return
  148. }
  149. }
  150. let url = URL(string: KMAdvertisementModelTransition.transitionImagePath(image: model.image, highlight: false))
  151. adsImageView.image = KMAdvertisementImage.imageWithURL(url: url, completion: { [weak self] image in
  152. self?.adsImageView.image = image
  153. })
  154. }
  155. }
  156. //MARK: - func
  157. @objc func productsItemClick(_ sender: ComponentCSelector) {
  158. if let subviews = productsBGView?.subviews {
  159. for view in subviews {
  160. if view is ComponentCSelector {
  161. (view as! ComponentCSelector).properties.state = .normal
  162. (view as! ComponentCSelector).reloadData()
  163. }
  164. }
  165. }
  166. let itemData: KMAdvertisementItemInfo = sender.customData as! KMAdvertisementItemInfo
  167. let string = KMAdvertisementModelTransition.transitionLanguage(langeuage: itemData.linkURL)
  168. if string.count != 0 {
  169. NSWorkspace.shared.open(URL.init(string: string)!)
  170. }
  171. }
  172. @objc func othersItemClick(_ sender: ComponentNavBarItem) {
  173. if let subviews = othersBGView?.subviews {
  174. for view in subviews {
  175. if view is ComponentNavBarItem {
  176. (view as! ComponentNavBarItem).properties.state = .normal
  177. (view as! ComponentNavBarItem).reloadData()
  178. }
  179. }
  180. }
  181. let itemData: KMAdvertisementItemInfo = sender.customData as! KMAdvertisementItemInfo
  182. let string = KMAdvertisementModelTransition.transitionLanguage(langeuage: itemData.linkURL)
  183. if string.count != 0 {
  184. NSWorkspace.shared.open(URL.init(string: string)!)
  185. }
  186. }
  187. @IBAction func adsViewClicked(_ sender: Any) {
  188. let adsData = KMAdvertisementManager.manager.info.advertisement?.content
  189. guard let model = adsData?.first else {
  190. adsView.isHidden = true
  191. return
  192. }
  193. let string = KMAdvertisementModelTransition.transitionLanguage(langeuage: model.linkURL)
  194. if string.count != 0 {
  195. NSWorkspace.shared.open(URL.init(string: string)!)
  196. }
  197. if let versionKey = model.version {
  198. UserDefaults.standard.set("show", forKey: versionKey)
  199. UserDefaults.standard.synchronize()
  200. }
  201. adsView.isHidden = true
  202. }
  203. @IBAction func closeAdsImageview(_ sender: Any) {
  204. adsView.isHidden = true
  205. }
  206. }