// // KMNewUserGiftWindowController.swift // PDF Reader Pro // // Created by User-Tangchao on 2024/12/25. // import Cocoa @objcMembers class KMNewUserGiftWindowController: KMBaseWindowController { @IBOutlet weak var contentBox: NSBox! @IBOutlet weak var backgroundIv: NSImageView! @IBOutlet weak var titleLabel: NSTextField! @IBOutlet weak var subTitleLabel: NSTextField! @IBOutlet weak var listBox: NSBox! @IBOutlet weak var listBos2: NSBox! @IBOutlet weak var buttonBox: NSBox! private lazy var listBackgroundIv: NSImageView = { let view = NSImageView() return view }() private lazy var listBackgroundIv2: NSImageView = { let view = NSImageView() return view }() private lazy var giftItemView_: KMNewUserGiftItemView = { let view = KMNewUserGiftItemView() return view }() private lazy var giftItemView2_: KMNewUserGiftItemView = { let view = KMNewUserGiftItemView() return view }() private lazy var submitButton_: NSButton = { let view = NSButton() view.isBordered = false return view }() private lazy var buttonIv_: NSImageView = { let view = NSImageView() return view }() static let shared = KMNewUserGiftWindowController(windowNibName: "KMNewUserGiftWindowController") private let hasVaildShowKey_ = "hasVaildShow" override func windowDidLoad() { super.windowDidLoad() let closeButton = window?.standardWindowButton(.closeButton) closeButton?.target = self closeButton?.action = #selector(closeAction) window?.standardWindowButton(.zoomButton)?.isHidden = true window?.standardWindowButton(.miniaturizeButton)?.isHidden = true window?.delegate = self backgroundIv.image = NSImage(named: "KMImageNewUserGiftBg") backgroundIv.image?.size = window?.frame.size ?? NSMakeSize(660, 490) titleLabel.stringValue = NSLocalizedString("Welcome Gift For New Users!", comment: "") subTitleLabel.stringValue = NSLocalizedString("Hi! We’re delighted to meet you. Dive in and enjoy your PDF journey with us!", comment: "") titleLabel.font = .UbuntuMediumFontWithSize(30) titleLabel.alignment = .center subTitleLabel.font = .UbuntuMediumFontWithSize(16) contentBox.contentView?.addSubview(listBackgroundIv, positioned: .below, relativeTo: listBox) listBackgroundIv.mas_makeConstraints { make in make?.centerX.equalTo()(listBox)?.offset()(-3) make?.centerY.equalTo()(listBox)?.offset()(3) } listBackgroundIv.image = NSImage(named: "KMImageNewUserGiftItemBg") contentBox.contentView?.addSubview(listBackgroundIv2, positioned: .below, relativeTo: listBos2) listBackgroundIv2.mas_makeConstraints { make in make?.centerX.equalTo()(listBos2)?.offset()(-3) make?.centerY.equalTo()(listBos2)?.offset()(3) } listBackgroundIv2.image = NSImage(named: "KMImageNewUserGiftItemBg2") listBox.borderWidth = 0 listBos2.borderWidth = 0 listBox.contentView = giftItemView_ listBos2.contentView = giftItemView2_ giftItemView_.titleLabel.stringValue = String(format: NSLocalizedString("%d-day VIP Free Trial", comment: ""), 7) giftItemView_.titleLabel.font = .SFProTextRegularFont(20) giftItemView_.subTitleLabel.stringValue = NSLocalizedString("Enjoy the full functions of PDF Reader Pro.\nEdit, convert, annotate, and combine PDFs.", comment: "") giftItemView_.subTitleLabel.font = .SFProTextRegularFont(14) giftItemView2_.titleLabel.font = .SFProTextRegularFont(20) if KMIsDMGVersion() { if NSLocalizedString("USD", comment: "") == "USD" { giftItemView2_.titleLabel.stringValue = String(format: NSLocalizedString("%@ Coupons in Total", comment: ""), "$60") giftItemView2_.subTitleLabel.stringValue = String(format: NSLocalizedString("New users can get %@ Coupons to upgrade the plan.", comment: ""), "$60") } else { giftItemView2_.titleLabel.stringValue = String(format: NSLocalizedString("%@ Coupons in Total", comment: ""), "360元") giftItemView2_.subTitleLabel.stringValue = String(format: NSLocalizedString("New users can get %@ Coupons to upgrade the plan.", comment: ""), "360元") } } else { var showCNY = false #if VERSION_DMG #else if let localeId = IAPProductsManager.default()?.allAccessPackPermanent_lite?.product?.priceLocale.identifier { showCNY = localeId.contains("zh_CN") || localeId.contains("zh_TW") } #endif if showCNY == false { // if NSLocalizedString("USD", comment: "") == "USD" { giftItemView2_.titleLabel.stringValue = String(format: NSLocalizedString("%@ Coupons in Total", comment: ""), "USD 30") giftItemView2_.subTitleLabel.stringValue = String(format: NSLocalizedString("New users can get %@ Coupons to upgrade the plan.", comment: ""), "USD 30") } else { giftItemView2_.titleLabel.stringValue = String(format: NSLocalizedString("%@ Coupons in Total", comment: ""), "180元") giftItemView2_.subTitleLabel.stringValue = String(format: NSLocalizedString("New users can get %@ Coupons to upgrade the plan.", comment: ""), "180元") } } giftItemView2_.subTitleLabel.font = .SFProTextRegularFont(14) buttonBox.borderWidth = 0 contentBox.contentView?.addSubview(buttonIv_, positioned: .below, relativeTo: buttonBox) buttonIv_.image = NSImage(named: "KMImageNameCancelSubscribeSuccessButton") buttonIv_.mas_makeConstraints { make in make?.centerX.mas_equalTo() make?.centerY.equalTo()(buttonBox)?.offset()(6) } buttonBox.contentView?.addSubview(submitButton_) submitButton_.frame = buttonBox.contentView?.frame ?? .zero submitButton_.autoresizingMask = [.width, .height] submitButton_.title = NSLocalizedString("Sign in to Get Gift", comment: "") submitButton_.font = .UbuntuMediumFontWithSize(16) submitButton_.target = self submitButton_.action = #selector(submitAction) interfaceThemeDidChanged(self.window?.appearance?.name ?? .aqua) } override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) { super.interfaceThemeDidChanged(appearance) KMMainThreadExecute { if KMAppearance.isDarkMode() { self.titleLabel.textColor = .white self.subTitleLabel.textColor = .white self.submitButton_.setTitleColor(NSColor(hex: "#050022")) } else { self.titleLabel.textColor = NSColor(hex: "#000150") self.subTitleLabel.textColor = NSColor(hex: "#000150") self.submitButton_.setTitleColor(.white) } self.giftItemView_.titleLabel.textColor = .white self.giftItemView_.subTitleLabel.textColor = .white self.giftItemView2_.titleLabel.textColor = .white self.giftItemView2_.subTitleLabel.textColor = .white } } // MARK: - Actions @objc func submitAction() { trackEvent(eventName: "PUW", params: ["PUW_Btn" : "Welcom_Signin"], platform: .AppCenter) saveShowRecord() closeWindow() _showLoginWindow() } @objc func closeAction() { trackEvent(eventName: "PUW", params: ["PUW_Btn" : "Welcom_Cancel"], platform: .AppCenter) saveShowRecord() closeWindow() _showLoginWindow() } // MARK: - Private Methods private func _showNewGuideWindow() { if KMMemberInfo.shared.isMemberAllFunction { // 有权益(本地 或 账号) return } KMNewUserGuideWindowController.shared.openWindow() } private func _showLoginWindow() { if KMMemberInfo.shared.isLogin { _showNewGuideWindow() return } KMLoginWindowsController.shared.openWindow { [unowned self] success in if success == false { return } _showNewGuideWindow() } } private func _showCenter(animate: Bool){ guard let screenFrame = NSScreen.main?.frame else { return } guard let win = self.window else { return } var frame = win.frame frame.origin.y = (screenFrame.size.height-frame.size.height)*0.5 frame.origin.x = (screenFrame.size.width-frame.size.width)*0.5 win.setFrame(frame, display: true, animate: animate) } // MARK: - Public Methods public func needShowForlocalCheck() -> Bool { if KMDataManager.ud_bool(forKey: hasVaildShowKey_) { // 不重复显示 #if DEBUG #else return false #endif } let appFirstLaunch = KMDataManager.default.appFirstLaunch let appFirstLaunchForUpdate = KMDataManager.default.appFirstLaunchForUpdate if appFirstLaunch == false && appFirstLaunchForUpdate == false { #if DEBUG #else // return false #endif } return true } public func needShow() -> Bool { if needShowForlocalCheck() == false { return false } if KMNewUserGiftManager.default.needShow() == false { return false } if KMMemberInfo.shared.isLogin { // 登陆 return false } return true } public func saveShowRecord() { KMDataManager.ud_set(true, forKey: hasVaildShowKey_) } public func openWindow() { showWindow(nil) _showCenter(animate: false) window?.makeKeyAndOrderFront(nil) trackEvent(eventName: "PUW", params: ["PUW_Exposure" : "PUW_WelcomGift"], platform: .AppCenter) } public func closeWindow() { window?.close() } } // MARK: - NSWindowDelegate extension KMNewUserGiftWindowController: NSWindowDelegate { func windowDidResize(_ notification: Notification) { guard let data = window?.isEqual(to: notification.object), data == true else { return } backgroundIv.image?.size = window?.frame.size ?? NSMakeSize(660, 490) } }