KMHomeRecommondView.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. override func draw(_ dirtyRect: NSRect) {
  21. super.draw(dirtyRect)
  22. // Drawing code here.
  23. }
  24. //MARK: - Setter
  25. open var inputData: KMAdvertisementContent? {
  26. didSet {
  27. self.reloadData()
  28. }
  29. }
  30. func reloadData() {
  31. if let subviews = productsBGView?.subviews {
  32. for view in subviews {
  33. if view is ComponentCSelector {
  34. view.removeFromSuperview()
  35. }
  36. }
  37. }
  38. if let subviews = othersBGView?.subviews {
  39. for view in subviews {
  40. if view is ComponentNavBarItem {
  41. view.removeFromSuperview()
  42. }
  43. }
  44. }
  45. let products = KMAdvertisementManager.manager.info.recommondContent?.recommondContentPDFPro
  46. if let productContents = products?.content {
  47. productsBGView.isHidden = false
  48. othersTopConst.constant = 100
  49. var viewXValue: CGFloat = 0
  50. for item: KMAdvertisementItemInfo in productContents {
  51. let view = ComponentCSelector()
  52. view.frame = CGRectMake(viewXValue, 0, 43, 24)
  53. productsBGView.addSubview(view)
  54. viewXValue += 55
  55. view.properties = ComponentCSelectorProperty(size: .s, state: .normal, text: nil, iconImage: NSImage(named: "file_icon"))
  56. view.setTarget(self, action: #selector(productsItemClick(_:)))
  57. }
  58. } else {
  59. productsBGView.isHidden = true
  60. othersTopConst.constant = 24
  61. }
  62. let others = KMAdvertisementManager.manager.info.recommondContent?.recommondContentOther
  63. if let productContents = others?.content {
  64. othersBGView.isHidden = false
  65. var viewYValue: CGFloat = CGRectGetHeight(othersBGView.frame) - 28 - 36
  66. for item: KMAdvertisementItemInfo in productContents {
  67. let view = ComponentNavBarItem()
  68. view.frame = CGRectMake(0, viewYValue, CGRectGetWidth(othersBGView.frame), 36)
  69. view.autoresizingMask = [.width, .minYMargin]
  70. othersBGView.addSubview(view)
  71. view.properties = ComponentNavbarItemProperty(state: .normal,
  72. text: KMAdvertisementModelTransition.transitionLanguage(langeuage: item.name),
  73. iconImage: NSImage(named: "file_icon"))
  74. view.setTarget(self, action: #selector(othersItemClick(_:)))
  75. viewYValue -= 44
  76. }
  77. } else {
  78. othersBGView.isHidden = true
  79. }
  80. if IAPProductsManager.default().isAvailableAllFunction() {
  81. adsView.isHidden = true
  82. } else {
  83. adsView.isHidden = false
  84. let adsData = KMAdvertisementManager.manager.info.advertisement?.content
  85. guard let model = adsData?.first else {
  86. adsView.isHidden = true
  87. return
  88. }
  89. let url = URL(string: KMAdvertisementModelTransition.transitionImagePath(image: model.image, highlight: false))
  90. self.adsImageView.image = KMAdvertisementImage.imageWithURL(url: url, completion: { [weak self] image in
  91. self?.adsImageView.image = image
  92. })
  93. }
  94. }
  95. //MARK: - func
  96. @objc func productsItemClick(_ sender: NSView) {
  97. if let subviews = productsBGView?.subviews {
  98. for view in subviews {
  99. if view is ComponentCSelector {
  100. (view as! ComponentCSelector).properties.state = .normal
  101. (view as! ComponentCSelector).reloadData()
  102. }
  103. }
  104. }
  105. }
  106. @objc func othersItemClick(_ sender: NSView) {
  107. if let subviews = othersBGView?.subviews {
  108. for view in subviews {
  109. if view is ComponentNavBarItem {
  110. (view as! ComponentNavBarItem).properties.state = .normal
  111. (view as! ComponentNavBarItem).reloadData()
  112. }
  113. }
  114. }
  115. }
  116. @IBAction func closeAdsImageview(_ sender: Any) {
  117. }
  118. }