|
@@ -20,35 +20,44 @@ let kAD_Refresh_Rate = 6000.0
|
|
|
class KMAdsWebView: NSView, WKNavigationDelegate, CAAnimationDelegate {
|
|
|
weak var adDelegate: AdsWebViewDelegate?
|
|
|
|
|
|
- private var closeButton: NSButton
|
|
|
+ private var closeButton: NSButton!
|
|
|
+ var clickButton: NSButton!
|
|
|
private var timer: Timer?
|
|
|
private var currentPage: Int = 0
|
|
|
private var completionHandler: ((Int) -> Void)?
|
|
|
|
|
|
- private var adsWebView: WKWebView
|
|
|
+ var adsImageView: NSImageView!
|
|
|
+ var adsInfo: KMAdsInfo!
|
|
|
|
|
|
var adPosY: CGFloat = 30.0
|
|
|
|
|
|
override init(frame frameRect: NSRect) {
|
|
|
- closeButton = NSButton(frame: NSRect(x: frameRect.size.width - 30, y: frameRect.size.height - 30, width: 30, height: 30))
|
|
|
- adsWebView = WKWebView(frame: frameRect)
|
|
|
-
|
|
|
super.init(frame: frameRect)
|
|
|
|
|
|
wantsLayer = true
|
|
|
self.frame = NSRect(x: 0, y: 0, width: kAD_View_Width, height: kAD_View_Height)
|
|
|
autoresizingMask = [.minXMargin, .maxXMargin]
|
|
|
|
|
|
- adsWebView.navigationDelegate = self
|
|
|
- adsWebView.autoresizingMask = [.width, .height]
|
|
|
- addSubview(adsWebView)
|
|
|
+ adsImageView = NSImageView.init(frame: self.bounds)
|
|
|
+ adsImageView.autoresizingMask = [.width, .height]
|
|
|
+ addSubview(adsImageView)
|
|
|
|
|
|
adPosY = 30.0
|
|
|
-
|
|
|
+
|
|
|
+ clickButton = NSButton.init(frame: self.bounds)
|
|
|
+ clickButton.isHidden = false
|
|
|
+ clickButton.autoresizingMask = [.width, .height]
|
|
|
+ clickButton.isBordered = false
|
|
|
+ clickButton.title = ""
|
|
|
+ clickButton.target = self
|
|
|
+ clickButton.action = #selector(buttonItemClicked(_:))
|
|
|
+ addSubview(clickButton)
|
|
|
+
|
|
|
+ closeButton = NSButton.init(frame: NSRect(x: frameRect.size.width - 30, y: frameRect.size.height - 30, width: 30, height: 30))
|
|
|
closeButton.isHidden = false
|
|
|
closeButton.imagePosition = .imageOnly
|
|
|
-// closeButton.cell?.highlightsBy = .contentsCellMask
|
|
|
closeButton.imageScaling = .scaleProportionallyUpOrDown
|
|
|
+ closeButton.autoresizingMask = [.maxXMargin, .minYMargin]
|
|
|
closeButton.image = NSImage(named: "ad_cancel_button00")
|
|
|
closeButton.isBordered = false
|
|
|
closeButton.target = self
|
|
@@ -65,8 +74,7 @@ class KMAdsWebView: NSView, WKNavigationDelegate, CAAnimationDelegate {
|
|
|
|
|
|
deinit {
|
|
|
adDelegate = nil
|
|
|
- adsWebView.navigationDelegate = nil
|
|
|
- adsWebView.stopLoading()
|
|
|
+
|
|
|
timer?.invalidate()
|
|
|
timer = nil
|
|
|
completionHandler = nil
|
|
@@ -133,17 +141,20 @@ class KMAdsWebView: NSView, WKNavigationDelegate, CAAnimationDelegate {
|
|
|
}
|
|
|
|
|
|
func startAdsDataRequest() {
|
|
|
- adsWebView.navigationDelegate = self
|
|
|
+
|
|
|
if adsData.count > 0 {
|
|
|
- let url = URL(string: adsData[currentPage])
|
|
|
- adsWebView.load(URLRequest(url: url!))
|
|
|
KMAdsManager.defaultManager.refreshLoadingDate()
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ func reloadData() {
|
|
|
+ DispatchQueue.main.async {
|
|
|
+ self.adsImageView.image = self.adsInfo.adsImage
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
func stopLoading() {
|
|
|
- adsWebView.navigationDelegate = nil
|
|
|
- adsWebView.stopLoading()
|
|
|
+
|
|
|
}
|
|
|
|
|
|
func resizeWithOldSuperviewSize(oldSize: NSSize) {
|
|
@@ -156,7 +167,7 @@ class KMAdsWebView: NSView, WKNavigationDelegate, CAAnimationDelegate {
|
|
|
}
|
|
|
|
|
|
self.frame = NSRect(x: (superview?.frame.size.width ?? 0 - width) / 2.0, y: frame.origin.y, width: width, height: height)
|
|
|
- adsWebView.frame = bounds
|
|
|
+
|
|
|
}
|
|
|
|
|
|
func beginSheetModalForView(view: NSView, directions: KMADViewDirections, animated: Bool, completionHandler handler: ((Int) -> Void)?) {
|
|
@@ -193,7 +204,8 @@ class KMAdsWebView: NSView, WKNavigationDelegate, CAAnimationDelegate {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- adsWebView.frame = bounds
|
|
|
+ adsImageView.frame = bounds
|
|
|
+ closeButton.frame = NSRect(x: self.bounds.size.width - 30, y: self.bounds.size.height - 30, width: 30, height: 30)
|
|
|
|
|
|
currentPage = 0
|
|
|
startAdsDataRequest()
|
|
@@ -242,7 +254,7 @@ class KMAdsWebView: NSView, WKNavigationDelegate, CAAnimationDelegate {
|
|
|
}
|
|
|
|
|
|
let url = URL(string: self.adsData[self.currentPage])
|
|
|
- self.adsWebView.load(URLRequest(url: url!))
|
|
|
+
|
|
|
KMAdsManager.defaultManager.refreshLoadingDate()
|
|
|
}
|
|
|
}
|
|
@@ -262,7 +274,24 @@ class KMAdsWebView: NSView, WKNavigationDelegate, CAAnimationDelegate {
|
|
|
}
|
|
|
removeFromSuperview()
|
|
|
}
|
|
|
+
|
|
|
+ @objc func buttonItemClicked(_ sender: Any) {
|
|
|
+
|
|
|
+ guard let newURL = NSURL(string: self.adsInfo.adsURLLink) else {
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
+ NSWorkspace.shared.open(newURL as URL)
|
|
|
+
|
|
|
+ adDelegate?.kmAdViewClicked(self)
|
|
|
+
|
|
|
+ if let completionHandler = self.completionHandler {
|
|
|
+ completionHandler(currentPage + 1)
|
|
|
+ self.completionHandler = nil
|
|
|
+ }
|
|
|
+ removeFromSuperview()
|
|
|
+ }
|
|
|
+
|
|
|
@objc func buttonItemClicked_Close(_ sender: Any) {
|
|
|
adDelegate?.kmAdViewClose(self)
|
|
|
|
|
@@ -301,39 +330,11 @@ class KMAdsWebView: NSView, WKNavigationDelegate, CAAnimationDelegate {
|
|
|
|
|
|
closeButton.alphaValue = 1.0
|
|
|
|
|
|
- adsWebView.frame = bounds
|
|
|
+ adsImageView.frame = bounds
|
|
|
}
|
|
|
|
|
|
// MARK: - WKNavigationDelegate
|
|
|
-
|
|
|
- func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
|
|
|
- if navigationAction.navigationType == .linkActivated {
|
|
|
- let tURL = navigationAction.request.url
|
|
|
- if tURL != nil {
|
|
|
- buttonItemClicked_Open(tURL as Any)
|
|
|
- decisionHandler(.cancel)
|
|
|
- return
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- decisionHandler(.allow)
|
|
|
- }
|
|
|
-
|
|
|
- func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
|
|
|
- webView.evaluateJavaScript("document.body.scrollHeight") { [weak self] (result, error) in
|
|
|
- if let height = result as? CGFloat, height > 0 {
|
|
|
- let newHeight = min(height, kAD_View_Height)
|
|
|
- self?.adsWebView.frame = NSRect(x: 0, y: 0, width: (self?.adsWebView.frame.size.width)!, height: newHeight)
|
|
|
- self?.frame.size.height = newHeight
|
|
|
- self?.closeButton.frame = NSRect(x: (self?.frame.size.width)! - 30, y: (self?.frame.size.height)! - 30, width: 30, height: 30)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
|
|
|
- print(error)
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
@objc func handleUserHaveClickRateUsNotification(_ notification: Notification) {
|
|
|
adsWebView_Switch()
|
|
|
}
|