123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- //
- // KMHomeRecommondView.swift
- // PDF Reader Pro
- //
- // Created by Niehaoyu on 2024/10/10.
- //
- import Cocoa
- import KMComponentLibrary
- class KMHomeRecommondView: BaseXibView {
-
- @IBOutlet var dividerView: ComponentDivider!
-
- @IBOutlet var productsBGView: NSView!
- @IBOutlet var productsLabel: NSTextField!
-
- @IBOutlet var othersBGView: NSView!
- @IBOutlet var othersLabel: NSTextField!
- @IBOutlet var othersTopConst: NSLayoutConstraint!
-
- @IBOutlet var adsView: NSView!
- @IBOutlet var adsImageView: NSImageView!
- @IBOutlet var adsCloseBtn: NSButton!
-
-
- //MARK: - func
- //MARK: - func
- public override func draw(_ dirtyRect: NSRect) {
- super.draw(dirtyRect)
-
- // Drawing code here.
- }
-
- public required init?(coder decoder: NSCoder) {
- super.init(coder: decoder)
-
- }
-
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
- }
-
- public override func awakeFromNib() {
- super.awakeFromNib()
-
- setupUI()
- }
-
- //MARK: - Setter
- open var inputData: KMAdvertisementContent? {
- didSet {
- reloadData()
- }
- }
-
- func setupUI() {
- productsLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
- othersLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
-
- productsLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
- othersLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
-
- }
-
- func reloadData() {
-
- if let subviews = productsBGView?.subviews {
- for view in subviews {
- if view is ComponentCSelector {
- view.removeFromSuperview()
- }
- }
- }
-
- if let subviews = othersBGView?.subviews {
- for view in subviews {
- if view is ComponentNavBarItem {
- view.removeFromSuperview()
- }
- }
- }
-
- let products = KMAdvertisementManager.manager.info.recommondContent?.recommondContentPDFPro
-
- productsBGView.isHidden = true
- othersTopConst.constant = 24
- if let productContents = products?.content {
- productsBGView.isHidden = false
- othersTopConst.constant = 100
-
- productsLabel.stringValue = KMAdvertisementModelTransition.transitionLanguage(langeuage: products?.name)
-
- var viewXValue: CGFloat = 0
-
- for item: KMAdvertisementItemInfo in productContents {
- let view = ComponentCSelector()
- view.frame = CGRectMake(viewXValue, 0, 43, 24)
- productsBGView.addSubview(view)
- viewXValue += 55
- view.properties = ComponentCSelectorProperty(size: .s, state: .normal, text: nil, iconImage: nil)
-
- let normalUrl = URL(string: KMAdvertisementModelTransition.transitionImagePath(image: item.image, highlight: false))
- let highURL = URL(string: KMAdvertisementModelTransition.transitionImagePath(image: item.image, highlight: true))
- let toolTip = KMAdvertisementModelTransition.transitionLanguage(langeuage: item.tooltip)
-
- var normalImage = NSImage()
- normalImage = KMAdvertisementImage.imageWithURL(url: normalUrl) { image in
- normalImage = image
- view.reloadData()
- }
- view.properties.iconImage = normalImage
- view.toolTip = toolTip
-
- var hov_Image = NSImage()
- hov_Image = KMAdvertisementImage.imageWithURL(url: highURL) { image in
- hov_Image = image
- view.reloadData()
- }
- view.properties.icon_hov_Image = hov_Image
- view.reloadData()
-
- view.customData = item
- view.setTarget(self, action: #selector(productsItemClick(_:)))
- }
- }
-
- let others = KMAdvertisementManager.manager.info.recommondContent?.recommondContentOther
- if let productContents = others?.content {
- othersBGView.isHidden = false
- var viewYValue: CGFloat = CGRectGetHeight(othersBGView.frame) - 28 - 36
-
- othersLabel.stringValue = KMAdvertisementModelTransition.transitionLanguage(langeuage: others?.name)
- for item: KMAdvertisementItemInfo in productContents {
- let view = ComponentNavBarItem()
- view.frame = CGRectMake(0, viewYValue, CGRectGetWidth(othersBGView.frame), 36)
- view.autoresizingMask = [.width, .minYMargin]
- othersBGView.addSubview(view)
- view.properties = ComponentNavbarItemProperty(state: .normal,
- text: KMAdvertisementModelTransition.transitionLanguage(langeuage: item.name),
- iconImage: nil,
- keepState: false)
-
- let normalUrl = URL(string: KMAdvertisementModelTransition.transitionImagePath(image: item.image, highlight: false))
- let highURL = URL(string: KMAdvertisementModelTransition.transitionImagePath(image: item.image, highlight: true))
- let toolTip = KMAdvertisementModelTransition.transitionLanguage(langeuage: item.tooltip)
- var normalImage = NSImage()
- normalImage = KMAdvertisementImage.imageWithURL(url: normalUrl) { image in
- normalImage = image
- view.reloadData()
- }
- view.properties.iconImage = normalImage
- view.toolTip = toolTip
-
- var hov_Image = NSImage()
- hov_Image = KMAdvertisementImage.imageWithURL(url: highURL) { image in
- hov_Image = image
- view.reloadData()
- }
- view.properties.icon_hov_Image = hov_Image
- view.reloadData()
-
- view.customData = item
- view.setTarget(self, action: #selector(othersItemClick(_:)))
- viewYValue -= 44
- }
-
- } else {
- othersBGView.isHidden = true
- }
-
- if IAPProductsManager.default().isAvailableAllFunction() {
- adsView.isHidden = true
- } else {
- adsView.isHidden = false
- let adsData = KMAdvertisementManager.manager.info.advertisement?.content
- guard let model = adsData?.first else {
- adsView.isHidden = true
- return
- }
- if let versionKey = model.version {
- if UserDefaults.standard.object(forKey: versionKey) != nil {
- adsView.isHidden = true
- return
- }
- }
- let url = URL(string: KMAdvertisementModelTransition.transitionImagePath(image: model.image, highlight: false))
- adsImageView.image = KMAdvertisementImage.imageWithURL(url: url, completion: { [weak self] image in
- self?.adsImageView.image = image
- })
- }
-
- }
-
- //MARK: - func
- @objc func productsItemClick(_ sender: ComponentCSelector) {
- if let subviews = productsBGView?.subviews {
- for view in subviews {
- if view is ComponentCSelector {
- (view as! ComponentCSelector).properties.state = .normal
- (view as! ComponentCSelector).reloadData()
- }
- }
- }
-
- let itemData: KMAdvertisementItemInfo = sender.customData as! KMAdvertisementItemInfo
- let string = KMAdvertisementModelTransition.transitionLanguage(langeuage: itemData.linkURL)
- if string.count != 0 {
- NSWorkspace.shared.open(URL.init(string: string)!)
- }
-
- }
-
- @objc func othersItemClick(_ sender: ComponentNavBarItem) {
- if let subviews = othersBGView?.subviews {
- for view in subviews {
- if view is ComponentNavBarItem {
- (view as! ComponentNavBarItem).properties.state = .normal
- (view as! ComponentNavBarItem).reloadData()
- }
- }
- }
-
- let itemData: KMAdvertisementItemInfo = sender.customData as! KMAdvertisementItemInfo
- let string = KMAdvertisementModelTransition.transitionLanguage(langeuage: itemData.linkURL)
- if string.count != 0 {
- NSWorkspace.shared.open(URL.init(string: string)!)
- }
- }
-
- @IBAction func adsViewClicked(_ sender: Any) {
- let adsData = KMAdvertisementManager.manager.info.advertisement?.content
- guard let model = adsData?.first else {
- adsView.isHidden = true
- return
- }
-
- let string = KMAdvertisementModelTransition.transitionLanguage(langeuage: model.linkURL)
- if string.count != 0 {
- NSWorkspace.shared.open(URL.init(string: string)!)
- }
- if let versionKey = model.version {
- UserDefaults.standard.set("show", forKey: versionKey)
- UserDefaults.standard.synchronize()
- }
-
- adsView.isHidden = true
- }
-
- @IBAction func closeAdsImageview(_ sender: Any) {
-
- adsView.isHidden = true
-
- }
-
- }
|