KMAdsWebView.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. import Cocoa
  2. import WebKit
  3. protocol AdsWebViewDelegate: AnyObject {
  4. func kmAdViewClicked(_ adView: KMAdsWebView)
  5. func kmAdViewClose(_ adView: KMAdsWebView)
  6. }
  7. enum KMADViewDirections: Int {
  8. case up
  9. case down
  10. }
  11. // Banner Ads Width,Height
  12. let kAD_View_Width = 728.0
  13. let kAD_View_Height = 90.0
  14. // Banner Ads refresh rate in second, 新广告会自动刷新,所以时间延长
  15. let kAD_Refresh_Rate = 6000.0
  16. class KMAdsWebView: NSView, WKNavigationDelegate, CAAnimationDelegate {
  17. weak var adDelegate: AdsWebViewDelegate?
  18. private var closeButton: NSButton!
  19. var clickButton: NSButton!
  20. private var timer: Timer?
  21. private var currentPage: Int = 0
  22. private var completionHandler: ((Int) -> Void)?
  23. var adsImageView: NSImageView!
  24. var adsInfo: KMAdsInfo!
  25. var adPosY: CGFloat = 30.0
  26. override init(frame frameRect: NSRect) {
  27. super.init(frame: frameRect)
  28. wantsLayer = true
  29. self.frame = NSRect(x: 0, y: 0, width: kAD_View_Width, height: kAD_View_Height)
  30. autoresizingMask = [.minXMargin, .maxXMargin]
  31. adsImageView = NSImageView.init(frame: self.bounds)
  32. adsImageView.autoresizingMask = [.width, .height]
  33. addSubview(adsImageView)
  34. adPosY = 30.0
  35. clickButton = NSButton.init(frame: self.bounds)
  36. clickButton.isHidden = false
  37. clickButton.autoresizingMask = [.width, .height]
  38. clickButton.isBordered = false
  39. clickButton.title = ""
  40. clickButton.target = self
  41. clickButton.action = #selector(buttonItemClicked(_:))
  42. addSubview(clickButton)
  43. closeButton = NSButton.init(frame: NSRect(x: frameRect.size.width - 30, y: frameRect.size.height - 30, width: 30, height: 30))
  44. closeButton.isHidden = false
  45. closeButton.imagePosition = .imageOnly
  46. closeButton.imageScaling = .scaleProportionallyUpOrDown
  47. closeButton.autoresizingMask = [.maxXMargin, .minYMargin]
  48. closeButton.image = NSImage(named: "ad_cancel_button00")
  49. closeButton.isBordered = false
  50. closeButton.target = self
  51. closeButton.action = #selector(buttonItemClicked_Close(_:))
  52. addSubview(closeButton)
  53. NotificationCenter.default.addObserver(self, selector: #selector(handleUserHaveClickRateUsNotification(_:)), name: NSNotification.Name("kUserHaveClickRateUsNotification"), object: nil)
  54. NotificationCenter.default.addObserver(self, selector: #selector(adsWebView_Switch), name: NSNotification.Name("KMFirebaseRemateConfigRequestIsSuccessful"), object: nil)
  55. NotificationCenter.default.addObserver(self, selector: #selector(recommondInfoUpdateNoti), name: NSNotification.Name("KMRecommondInfoUpdateNoti"), object: nil)
  56. }
  57. required init?(coder: NSCoder) {
  58. fatalError("init(coder:) has not been implemented")
  59. }
  60. deinit {
  61. adDelegate = nil
  62. timer?.invalidate()
  63. timer = nil
  64. completionHandler = nil
  65. NotificationCenter.default.removeObserver(self)
  66. }
  67. var adsData: [String] {
  68. get {
  69. var data: [String] = []
  70. #if VERSION_DMG
  71. if !UserDefaults.standard.bool(forKey: "kUserHaveClickRateUsKey") {
  72. data = ["https://test.sitemaji.com/native/pdfreaderpro.html?s=PDFReaderPro_Mac_DMG_HouseAD_728x90"]
  73. } else {
  74. data = ["https://test.sitemaji.com/native/pdfreaderpro.html?s=PDFReaderPro_Mac_DMG_728x90"]
  75. }
  76. #else
  77. if !UserDefaults.standard.bool(forKey: "kUserHaveClickRateUsKey") {
  78. data = ["https://test.sitemaji.com/native/pdfreaderpro.html?s=PDFReaderPro_Mac_Store_HouseAD_728x90"]
  79. } else {
  80. data = ["https://test.sitemaji.com/native/pdfreaderpro.html?s=PDFReaderPro_Mac_Store_728x90"]
  81. }
  82. #endif
  83. if !UserDefaults.standard.bool(forKey: "kUserHaveClickRateUsKey") {
  84. data = [KMKdanRemoteConfig.remoteConfig.displayHouseAdsUrl()]
  85. } else {
  86. data = [KMKdanRemoteConfig.remoteConfig.displayAdsUrl()]
  87. }
  88. #if DEBUG
  89. #if VERSION_DMG
  90. data = ["http://test-pdf-pro.kdan.cn:3021/native?s=PDFReaderPro_Mac_DMG_HouseAD_728x90&testflag=on"]
  91. #else
  92. data = ["https://test.sitemaji.com/native/pdfreaderpro.html?s=PDFReaderPro_Mac_Store_728x90"]
  93. #endif
  94. #endif
  95. return data
  96. }
  97. set {
  98. }
  99. }
  100. func restoreDefaultAdPosY() {
  101. adPosY = 30.0
  102. }
  103. func sortAdsData() {
  104. return
  105. let tLength = self.adsData.count
  106. for _ in 0..<24 {
  107. let tIndex0 = Int(arc4random()) % tLength
  108. var tIndex1 = Int(arc4random()) % tLength
  109. if tIndex0 == tIndex1 {
  110. tIndex1 = (tIndex1 + 1) % tLength
  111. }
  112. self.adsData.swapAt(tIndex0, tIndex1)
  113. }
  114. }
  115. func startAdsDataRequest() {
  116. if adsData.count > 0 {
  117. KMAdsManager.defaultManager.refreshLoadingDate()
  118. }
  119. }
  120. func reloadData() {
  121. DispatchQueue.main.async {
  122. self.adsImageView.image = self.adsInfo.adsImage
  123. }
  124. }
  125. func stopLoading() {
  126. }
  127. func resizeWithOldSuperviewSize(oldSize: NSSize) {
  128. var width = kAD_View_Width
  129. var height = kAD_View_Height
  130. if superview?.frame.size.width ?? 0 < width {
  131. let newWidth = (superview?.frame.size.width ?? 0) - 20
  132. height = height / width * newWidth
  133. width = newWidth
  134. }
  135. self.frame = NSRect(x: (superview?.frame.size.width ?? 0 - width) / 2.0, y: frame.origin.y, width: width, height: height)
  136. }
  137. func beginSheetModalForView(view: NSView, directions: KMADViewDirections, animated: Bool, completionHandler handler: ((Int) -> Void)?) {
  138. self.completionHandler = handler
  139. if adPosY < 0 {
  140. restoreDefaultAdPosY()
  141. }
  142. let tWidth = frame.size.width
  143. let tHeight = frame.size.height
  144. view.addSubview(self)
  145. if animated {
  146. switch directions {
  147. case .up:
  148. frame = NSRect(x: (view.frame.size.width - tWidth) / 2.0, y: view.frame.size.height, width: tWidth, height: tHeight)
  149. animator().frame = NSRect(x: frame.origin.x, y: view.frame.size.height - tHeight - adPosY, width: tWidth, height: tHeight)
  150. case .down:
  151. frame = NSRect(x: (view.frame.size.width - tWidth) / 2.0, y: -tHeight, width: tWidth, height: tHeight)
  152. animator().frame = NSRect(x: frame.origin.x, y: adPosY, width: tWidth, height: tHeight)
  153. default:
  154. frame = NSRect(x: 0, y: 0, width: tWidth, height: tHeight)
  155. }
  156. } else {
  157. switch directions {
  158. case .up:
  159. frame = NSRect(x: (view.frame.size.width - tWidth) / 2.0, y: view.frame.size.height - tHeight - adPosY, width: tWidth, height: tHeight)
  160. case .down:
  161. frame = NSRect(x: (view.frame.size.width - tWidth) / 2.0, y: adPosY, width: tWidth, height: tHeight)
  162. default:
  163. frame = NSRect(x: 0, y: 0, width: tWidth, height: tHeight)
  164. }
  165. }
  166. adsImageView.frame = bounds
  167. closeButton.frame = NSRect(x: self.bounds.size.width - 30, y: self.bounds.size.height - 30, width: 30, height: 30)
  168. currentPage = 0
  169. startAdsDataRequest()
  170. if timer != nil {
  171. timer?.invalidate()
  172. timer = nil
  173. }
  174. var interval = 0
  175. if !UserDefaults.standard.bool(forKey: "kUserHaveClickRateUsKey") {
  176. interval = KMKdanRemoteConfig.remoteConfig.refreshAdsDate()
  177. } else {
  178. interval = KMKdanRemoteConfig.remoteConfig.refreshAdsDateEvaluateAfter()
  179. }
  180. timer = Timer.scheduledTimer(timeInterval: TimeInterval(interval), target: self, selector: #selector(adsWebView_Switch), userInfo: nil, repeats: true
  181. )
  182. RunLoop.current.add(timer!, forMode: .common)
  183. }
  184. @objc func adsWebView_Switch() {
  185. DispatchQueue.main.async {
  186. if !KMKdanRemoteConfig.remoteConfig.isDisplayAds() || !KMAdsManager.defaultManager.checkTheDate() || !KMAdsManager.defaultManager.isValidLastShowAds() {
  187. self.removeFromSuperview()
  188. return
  189. }
  190. let transition = CATransition()
  191. transition.type = .moveIn
  192. let tRandomData = arc4random() % 100
  193. if tRandomData > 50 {
  194. transition.subtype = .fromBottom
  195. } else {
  196. transition.subtype = .fromTop
  197. }
  198. transition.duration = 0.6
  199. transition.delegate = self
  200. self.layer?.add(transition, forKey: "KMTransitionAnimation")
  201. self.currentPage += 1
  202. if self.currentPage >= self.adsData.count {
  203. self.currentPage = 0
  204. }
  205. let url = URL(string: self.adsData[self.currentPage])
  206. KMAdsManager.defaultManager.refreshLoadingDate()
  207. }
  208. }
  209. @objc func recommondInfoUpdateNoti() {
  210. DispatchQueue.main.async {
  211. self.reloadData()
  212. }
  213. }
  214. @objc func buttonItemClicked_Open(_ sender: Any) {
  215. guard let newURL = (sender as? NSURL) else {
  216. return
  217. }
  218. NSWorkspace.shared.open(newURL as URL)
  219. adDelegate?.kmAdViewClicked(self)
  220. if let completionHandler = self.completionHandler {
  221. completionHandler(currentPage + 1)
  222. self.completionHandler = nil
  223. }
  224. removeFromSuperview()
  225. }
  226. @objc func buttonItemClicked(_ sender: Any) {
  227. guard let newURL = NSURL(string: self.adsInfo.adsURLLink) else {
  228. return
  229. }
  230. NSWorkspace.shared.open(newURL as URL)
  231. adDelegate?.kmAdViewClicked(self)
  232. if let completionHandler = self.completionHandler {
  233. completionHandler(currentPage + 1)
  234. self.completionHandler = nil
  235. }
  236. removeFromSuperview()
  237. }
  238. @objc func buttonItemClicked_Close(_ sender: Any) {
  239. adDelegate?.kmAdViewClose(self)
  240. if let completionHandler = self.completionHandler {
  241. completionHandler(0)
  242. self.completionHandler = nil
  243. }
  244. removeFromSuperview()
  245. }
  246. func animationDidStart(_ anim: CAAnimation) {
  247. if timer != nil {
  248. timer?.invalidate()
  249. }
  250. closeButton.alphaValue = 0
  251. }
  252. func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
  253. if !flag {
  254. return
  255. }
  256. if timer != nil {
  257. timer?.invalidate()
  258. }
  259. var interval = 0
  260. if !UserDefaults.standard.bool(forKey: "kUserHaveClickRateUsKey") {
  261. interval = KMKdanRemoteConfig.remoteConfig.refreshAdsDate()
  262. } else {
  263. interval = KMKdanRemoteConfig.remoteConfig.refreshAdsDateEvaluateAfter()
  264. }
  265. timer = Timer.scheduledTimer(timeInterval: TimeInterval(interval), target: self, selector: #selector(adsWebView_Switch), userInfo: nil, repeats: true)
  266. RunLoop.current.add(timer!, forMode: .common)
  267. closeButton.alphaValue = 1.0
  268. adsImageView.frame = bounds
  269. }
  270. // MARK: - WKNavigationDelegate
  271. @objc func handleUserHaveClickRateUsNotification(_ notification: Notification) {
  272. adsWebView_Switch()
  273. }
  274. }