KMAdvertisementShowView.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 inputData != nil {
  72. if (UserDefaults.standard.object(forKey: "KMAdvertisementShowTypeView") == nil) {
  73. UserDefaults.standard.set([], forKey: "KMAdvertisementShowTypeView")
  74. }
  75. let cacheArray: [String] = UserDefaults.standard.object(forKey: "KMAdvertisementShowTypeView") as! [String]
  76. self.data.removeAll()
  77. let array = inputData!.content
  78. if array != nil {
  79. for section in array! {
  80. for item in section.content! {
  81. if !cacheArray.contains(item.productID!) {
  82. self.data.append(item)
  83. }
  84. }
  85. }
  86. }
  87. if self.data.count == 0 {
  88. self.removeTimer()
  89. self.removeFromSuperview()
  90. }
  91. self.recordTime()
  92. }
  93. }
  94. //MARK: 定时器
  95. func addTimer() {
  96. self.removeTimer()
  97. timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector (recordTime),
  98. userInfo: nil, repeats: true)
  99. if let curTimer: Timer = timer {
  100. RunLoop.main.add(curTimer, forMode: .common)
  101. }
  102. }
  103. func removeTimer() {
  104. if (timer != nil) {
  105. timer?.invalidate ()
  106. timer = nil
  107. }
  108. }
  109. @objc func recordTime () {
  110. if self.data.count != 0 {
  111. index += 1
  112. if index >= self.data.count {
  113. index = 0
  114. }
  115. if self.data.count != 0 {
  116. let url = URL(string: KMAdvertisementModelTransition.transitionImagePath(image: self.data[index].imageURL!, highlight: false))
  117. weak var weakSelf = self
  118. self.imageView.image = KMAdvertisementImage.imageWithURL(url: url, completion: { image in
  119. if weakSelf != nil {
  120. weakSelf!.imageView.image = image
  121. }
  122. })
  123. }
  124. }
  125. }
  126. @IBAction func cancelAction(_ sender: Any) {
  127. if self.data.count > index {
  128. let content = self.data[index]
  129. if actionCompletion != nil {
  130. actionCompletion!(.cancel, content)
  131. }
  132. self.data.remove(at: index)
  133. if self.data.count == 0 {
  134. self.removeFromSuperview()
  135. }
  136. if (UserDefaults.standard.object(forKey: "KMAdvertisementShowTypeView") == nil) {
  137. UserDefaults.standard.set([], forKey: "KMAdvertisementShowTypeView")
  138. }
  139. var array: [String] = UserDefaults.standard.object(forKey: "KMAdvertisementShowTypeView") as! [String]
  140. array.append(content.productID!)
  141. UserDefaults.standard.set(array, forKey: "KMAdvertisementShowTypeView")
  142. UserDefaults.standard.synchronize()
  143. self.recordTime()
  144. }
  145. }
  146. open override func mouseDown(with event: NSEvent) {
  147. if self.data.count > index {
  148. let content = self.data[index]
  149. if actionCompletion != nil {
  150. actionCompletion!(.tap, content)
  151. }
  152. }
  153. }
  154. }