123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- //
- // KMAdvertisementShowView.swift
- // KMAdvertisement
- //
- // Created by lizhe on 2022/11/29.
- //
- import Cocoa
- open class KMAdvertisementShowView: KMAdvertisementBaseView {
- @IBOutlet var contentView: NSView!
-
- @IBOutlet weak var cancelButton: NSButton!
-
- @IBOutlet weak var imageView: NSImageView!
-
- lazy var presenter: KMAdvertisementTableViewPresenter! = KMAdvertisementTableViewPresenter()
-
- //内部使用数据
- fileprivate var data: [KMAdvertisementModel.Section]?
- /**
- @abstract 外部传入数据
- @param inputData 文件路劲
- */
- open var inputData: KMAdvertisementModel! {
- didSet {
- self.presenter.initPresenter(view: self, data: inputData)
- }
- }
-
- convenience init (data: KMAdvertisementModel, superView: NSView) {
- self.init(frame: superView.bounds)
- self.presenter.initPresenter(view: self, data: data)
- superView.addSubview(self)
- self.autoresizingMask = [.height , .width]
- }
-
- // MARK: 初始化
- public required init?(coder decoder: NSCoder) {
- super.init(coder: decoder)
- initContentView()
- setup()
- }
-
- public override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
- initContentView()
- setup()
- }
-
- private func initContentView() {
- //绑定xib
- let resource = NSNib(nibNamed: String(describing: self.classForCoder.self),
- bundle: Bundle(for: self.classForCoder.self))!
- resource.instantiate(withOwner: self, topLevelObjects: nil)
- addSubview(contentView)
- contentView.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- contentView.topAnchor.constraint(equalTo: topAnchor),
- contentView.leftAnchor.constraint(equalTo: leftAnchor),
- contentView.rightAnchor.constraint(equalTo: rightAnchor),
- contentView.bottomAnchor.constraint(equalTo: bottomAnchor)])
- contentView.updateConstraintsForSubtreeIfNeeded()
- }
-
- func setup() {
- let bundle = Bundle(for: self.classForCoder.self)
- let path = bundle.path(forResource: "KMAdvertisement", ofType: "bundle") ?? ""
- let sdkBundle = Bundle(path: path)
- let filePath = sdkBundle?.pathForImageResource("ad_cancel_button00")
- let image = NSImage.init(contentsOfFile: filePath!)
- self.cancelButton.image = image
- }
-
- public override func draw(_ dirtyRect: NSRect) {
- super.draw(dirtyRect)
- // Drawing code here.
- }
-
- func reloadData() {
-
- let url = URL(string: self.data?.first?.content.first?.imageURL?.normal?.en ?? "")
- self.imageView.kf.setImage(with: url)
- }
-
- @IBAction func cancelAction(_ sender: Any) {
- self.removeFromSuperview()
- }
-
- open override func mouseDown(with event: NSEvent) {
- let content = self.data?.first?.content.first
- if actionCompletion != nil {
- actionCompletion!(content!)
- }
- }
- }
- extension KMAdvertisementShowView: KMAdvertisementTableViewPresenterDelegate {
- func showData(presenter: KMAdvertisementTableViewPresenter, data: Array<KMAdvertisementModel.Section>) {
- self.data = data
- self.reloadData()
- }
- }
|