KMAdvertisementShowView.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. weak var weakSelf = self
  114. self.imageView.image = KMAdvertisementImage.imageWithURL(url: url, completion: { image in
  115. if weakSelf != nil {
  116. weakSelf!.imageView.image = image
  117. }
  118. })
  119. }
  120. }
  121. }
  122. @IBAction func cancelAction(_ sender: Any) {
  123. if self.data.count > index {
  124. let content = self.data[index]
  125. if actionCompletion != nil {
  126. actionCompletion!(.cancel, content)
  127. }
  128. self.data.remove(at: index)
  129. if self.data.count == 0 {
  130. self.removeFromSuperview()
  131. }
  132. if (UserDefaults.standard.object(forKey: "KMAdvertisementShowTypeView") == nil) {
  133. UserDefaults.standard.set([], forKey: "KMAdvertisementShowTypeView")
  134. }
  135. var array: [String] = UserDefaults.standard.object(forKey: "KMAdvertisementShowTypeView") as! [String]
  136. array.append(content.productID!)
  137. UserDefaults.standard.set(array, forKey: "KMAdvertisementShowTypeView")
  138. UserDefaults.standard.synchronize()
  139. self.recordTime()
  140. }
  141. }
  142. open override func mouseDown(with event: NSEvent) {
  143. if self.data.count > index {
  144. let content = self.data[index]
  145. if actionCompletion != nil {
  146. actionCompletion!(.tap, content)
  147. }
  148. }
  149. }
  150. }