KMAdvertisementShowView.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // KMAdvertisementShowView.swift
  3. // KMAdvertisement
  4. //
  5. // Created by lizhe on 2022/11/29.
  6. //
  7. import Cocoa
  8. open class KMAdvertisementShowView: KMAdvertisementBaseView {
  9. @IBOutlet var contentView: NSView!
  10. @IBOutlet weak var cancelButton: NSButton!
  11. @IBOutlet weak var imageView: NSImageView!
  12. lazy var presenter: KMAdvertisementTableViewPresenter! = KMAdvertisementTableViewPresenter()
  13. //内部使用数据
  14. fileprivate var data: [KMAdvertisementModel.Section]?
  15. /**
  16. @abstract 外部传入数据
  17. @param inputData 文件路劲
  18. */
  19. open var inputData: KMAdvertisementModel! {
  20. didSet {
  21. self.presenter.initPresenter(view: self, data: inputData)
  22. }
  23. }
  24. convenience init (data: KMAdvertisementModel, superView: NSView) {
  25. self.init(frame: superView.bounds)
  26. self.presenter.initPresenter(view: self, data: data)
  27. superView.addSubview(self)
  28. self.autoresizingMask = [.height , .width]
  29. }
  30. // MARK: 初始化
  31. public required init?(coder decoder: NSCoder) {
  32. super.init(coder: decoder)
  33. initContentView()
  34. setup()
  35. }
  36. public override init(frame frameRect: NSRect) {
  37. super.init(frame: frameRect)
  38. initContentView()
  39. setup()
  40. }
  41. private func initContentView() {
  42. //绑定xib
  43. let resource = NSNib(nibNamed: String(describing: self.classForCoder.self),
  44. bundle: Bundle(for: self.classForCoder.self))!
  45. resource.instantiate(withOwner: self, topLevelObjects: nil)
  46. addSubview(contentView)
  47. contentView.translatesAutoresizingMaskIntoConstraints = false
  48. NSLayoutConstraint.activate([
  49. contentView.topAnchor.constraint(equalTo: topAnchor),
  50. contentView.leftAnchor.constraint(equalTo: leftAnchor),
  51. contentView.rightAnchor.constraint(equalTo: rightAnchor),
  52. contentView.bottomAnchor.constraint(equalTo: bottomAnchor)])
  53. contentView.updateConstraintsForSubtreeIfNeeded()
  54. }
  55. func setup() {
  56. let bundle = Bundle(for: self.classForCoder.self)
  57. let path = bundle.path(forResource: "KMAdvertisement", ofType: "bundle") ?? ""
  58. let sdkBundle = Bundle(path: path)
  59. let filePath = sdkBundle?.pathForImageResource("ad_cancel_button00")
  60. let image = NSImage.init(contentsOfFile: filePath!)
  61. self.cancelButton.image = image
  62. }
  63. public override func draw(_ dirtyRect: NSRect) {
  64. super.draw(dirtyRect)
  65. // Drawing code here.
  66. }
  67. func reloadData() {
  68. let url = URL(string: self.data?.first?.content.first?.imageURL?.normal?.en ?? "")
  69. self.imageView.kf.setImage(with: url)
  70. }
  71. @IBAction func cancelAction(_ sender: Any) {
  72. self.removeFromSuperview()
  73. }
  74. open override func mouseDown(with event: NSEvent) {
  75. let content = self.data?.first?.content.first
  76. if actionCompletion != nil {
  77. actionCompletion!(content!)
  78. }
  79. }
  80. }
  81. extension KMAdvertisementShowView: KMAdvertisementTableViewPresenterDelegate {
  82. func showData(presenter: KMAdvertisementTableViewPresenter, data: Array<KMAdvertisementModel.Section>) {
  83. self.data = data
  84. self.reloadData()
  85. }
  86. }