KMAdvertisementShowView.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // KMAdvertisementShowView.swift
  3. // KMAdvertisement
  4. //
  5. // Created by lizhe on 2022/11/29.
  6. //
  7. import Cocoa
  8. @objcMembers open class KMAdvertisementShowView: KMAdvertisementBaseView {
  9. @IBOutlet var contentView: NSView!
  10. @IBOutlet weak var cancelButton: NSButton!
  11. @IBOutlet weak var imageView: NSImageView!
  12. var timer: Timer?
  13. var index: Int = 0
  14. //内部使用数据
  15. fileprivate var data: [KMAdvertisementModelItem] = []
  16. /**
  17. @abstract 外部传入数据
  18. @param inputData 文件路劲
  19. */
  20. open var inputData: KMAdvertisementModel! {
  21. didSet {
  22. self.reloadData()
  23. }
  24. }
  25. convenience init (data: KMAdvertisementModel, superView: NSView) {
  26. self.init(frame: superView.bounds)
  27. superView.addSubview(self)
  28. self.autoresizingMask = [.height , .width]
  29. self.inputData = data
  30. self.reloadData()
  31. }
  32. // MARK: 初始化
  33. public required init?(coder decoder: NSCoder) {
  34. super.init(coder: decoder)
  35. initContentView()
  36. setup()
  37. }
  38. public override init(frame frameRect: NSRect) {
  39. super.init(frame: frameRect)
  40. initContentView()
  41. setup()
  42. }
  43. private func initContentView() {
  44. //绑定xib
  45. let resource = NSNib(nibNamed: String(describing: self.classForCoder.self),
  46. bundle: Bundle(for: self.classForCoder.self))!
  47. resource.instantiate(withOwner: self, topLevelObjects: nil)
  48. addSubview(contentView)
  49. contentView.translatesAutoresizingMaskIntoConstraints = false
  50. NSLayoutConstraint.activate([
  51. contentView.topAnchor.constraint(equalTo: topAnchor),
  52. contentView.leftAnchor.constraint(equalTo: leftAnchor),
  53. contentView.rightAnchor.constraint(equalTo: rightAnchor),
  54. contentView.bottomAnchor.constraint(equalTo: bottomAnchor)])
  55. contentView.updateConstraintsForSubtreeIfNeeded()
  56. }
  57. func setup() {
  58. let bundle = Bundle(for: self.classForCoder.self)
  59. let path = bundle.path(forResource: "KMAdvertisement", ofType: "bundle") ?? ""
  60. let sdkBundle = Bundle(path: path)
  61. let filePath = sdkBundle?.pathForImageResource("ad_cancel_button00")
  62. let image = NSImage.init(contentsOfFile: filePath!)
  63. self.cancelButton.image = image
  64. self.addTimer()
  65. }
  66. public override func draw(_ dirtyRect: NSRect) {
  67. super.draw(dirtyRect)
  68. // Drawing code here.
  69. }
  70. func reloadData() {
  71. // if (UserDefaults.standard.object(forKey: "KMAdvertisementShowTypeView") == nil) {
  72. // UserDefaults.standard.set([], forKey: "KMAdvertisementShowTypeView")
  73. // }
  74. // let cacheArray: [String] = UserDefaults.standard.object(forKey: "KMAdvertisementShowTypeView") as! [String]
  75. self.data.removeAll()
  76. let array = inputData.content!
  77. for section in array {
  78. for item in section.content! {
  79. // if !cacheArray.contains(item.productID!) {
  80. self.data.append(item)
  81. // }
  82. }
  83. }
  84. if self.data.count == 0 {
  85. self.removeTimer()
  86. self.removeFromSuperview()
  87. }
  88. self.recordTime()
  89. }
  90. //MARK: 定时器
  91. func addTimer() {
  92. self.removeTimer()
  93. timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector (recordTime),
  94. userInfo: nil, repeats: true)
  95. if let curTimer: Timer = timer {
  96. RunLoop.main.add(curTimer, forMode: .common)
  97. }
  98. }
  99. func removeTimer() {
  100. if (timer != nil) {
  101. timer?.invalidate ()
  102. timer = nil
  103. }
  104. }
  105. @objc func recordTime () {
  106. if self.data.count != 0 {
  107. index += 1
  108. if index >= self.data.count {
  109. index = 0
  110. }
  111. if self.data.count != 0 {
  112. let url = URL(string: KMAdvertisementModelTransition.transitionImagePath(image: self.data[index].imageURL!, highlight: false))
  113. self.imageView.image = KMAdvertisementImage.imageWithURL(url: url, completion: { [unowned self] image in
  114. self.imageView.image = image
  115. })
  116. }
  117. }
  118. }
  119. @IBAction func cancelAction(_ sender: Any) {
  120. if self.data.count > index {
  121. let content = self.data[index]
  122. if actionCompletion != nil {
  123. actionCompletion!(.cancel, content)
  124. }
  125. self.data.remove(at: index)
  126. if self.data.count == 0 {
  127. self.removeFromSuperview()
  128. }
  129. if (UserDefaults.standard.object(forKey: "KMAdvertisementShowTypeView") == nil) {
  130. UserDefaults.standard.set([], forKey: "KMAdvertisementShowTypeView")
  131. }
  132. var array: [String] = UserDefaults.standard.object(forKey: "KMAdvertisementShowTypeView") as! [String]
  133. array.append(content.productID!)
  134. UserDefaults.standard.set(array, forKey: "KMAdvertisementShowTypeView")
  135. UserDefaults.standard.synchronize()
  136. self.recordTime()
  137. }
  138. }
  139. open override func mouseDown(with event: NSEvent) {
  140. if self.data.count > index {
  141. let content = self.data[index]
  142. if actionCompletion != nil {
  143. actionCompletion!(.tap, content)
  144. }
  145. }
  146. }
  147. }