// // QSLGuideusersToCommentManager.swift // QuickSearchLocation // // Created by Destiny on 2025/7/31. // import Foundation import UIKit enum QSLGuideusersToCommentType : Int{ case member //会员 case nonMember // 非会员 } class QSLGuideusersToCommentManager: NSObject { static let commentShare = QSLGuideusersToCommentManager() //归因渠道 public var clickCompanyChannel : String = "" var guideGobalModel: QSLGuideIsTriggeredModel = QSLGuideIsTriggeredModel() var commentType: QSLGuideusersToCommentType = .member private var timer: Timer? // Initialize and setup notifications override init() { super.init() setupAppStateNotifications() startTimer() } deinit { stopTimer() NotificationCenter.default.removeObserver(self) } // MARK: - Timer Management private func setupAppStateNotifications() { NotificationCenter.default.addObserver(self, selector: #selector(appDidEnterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(appWillEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil) } @objc private func appWillEnterForeground() { startTimer() } @objc private func appDidEnterBackground() { stopTimer() } private func startTimer() { //print("startTimersfsdf---A--\(self.commentType)") // Invalidate existing timer if any stopTimer() // Create new timer that fires every minute timer = Timer.scheduledTimer(withTimeInterval: 60.0, repeats: true) { [weak self] _ in self?.timerFired() } // Fire immediately on first start timerFired() } private func stopTimer() { //print("startTimersfsdf---B--\(self.commentType)") timer?.invalidate() timer = nil } private func timerFired() { // Call your existing method here manageWhetherTriggerPopUpWindow(commentType) } // MARK: - Existing Methods (unchanged) //管理处理是否触发好评引导弹窗 func manageWhetherTriggerPopUpWindow(_ showType: QSLGuideusersToCommentType) { //print("startTimersfsdf---C--\(showType)---\(self.commentType)") // QSLNetwork().request(.guideIsTriggered(dict: [String: Any]())) { response in // let guideIsModel: QSLGuideIsTriggeredModel = response.mapObject(QSLGuideIsTriggeredModel.self, modelKey: "data") // self.commentType = showType // self.guideGobalModel = guideIsModel // if guideIsModel.trigger && self.areThisTwoChannel(){ // DispatchQueue.main.async { // self.noMemberPositiveReviewPopWindow() // } // /*if showType == .member { // DispatchQueue.main.async { // self.memberPositiveReviewPopWindow() // } // } else { // DispatchQueue.main.async { // self.noMemberPositiveReviewPopWindow() // } // }*/ // } // } fail: { code, msg in // } } //判断是不是这两个渠道 ASA渠道和自然流量两个渠道 func areThisTwoChannel() -> Bool { if self.clickCompanyChannel == "Apple Search Ads" || self.clickCompanyChannel == "natural" { return false } return true } ///领取好评引导奖励请求 func requestForReceivingGoodReviewGuidance() { QSLNetwork().request(.guideReceiveReward(dict: [String: Any]())) { response in if self.commentType == .member { DispatchQueue.main.async { self.popUpWindowForMemberReview() } } else { DispatchQueue.main.async { self.popUpWindowForNonMemberReviews() } } } fail: { code, msg in } } } //管理各种类型的弹窗 extension QSLGuideusersToCommentManager { ///会员的好评弹窗 @MainActor func memberPositiveReviewPopWindow() { // 创建深灰色文本样式 let darkGrayAttributes: [NSAttributedString.Key: Any] = [ .foregroundColor: UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0), .font: UIFont(name: "Noto Sans SC", size: 20)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 20, weight: .black), .paragraphStyle: { let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = .center return paragraphStyle }(), .kern: -1 ] // 创建绿色文本样式 let greenAttributes: [NSAttributedString.Key: Any] = [ .foregroundColor: UIColor(red: 17/255.0, green: 186/255.0, blue: 152/255.0, alpha: 1.0), .font: UIFont(name: "Noto Sans SC", size: 20)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 20, weight: .black), .paragraphStyle: { let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = .center return paragraphStyle }(), .kern: -1 ] let inputtr = NSMutableAttributedString() let titleOneAttr = NSAttributedString(string: "五星好评,", attributes: darkGrayAttributes) let titleTwoAttr = NSAttributedString(string: "多送\(self.guideGobalModel.extraMemberDays)天会员", attributes: greenAttributes) inputtr.append(titleOneAttr) inputtr.append(titleTwoAttr) if let currentWindow = UIApplication.keyWindow { QSLVipAlertView.alert(view: currentWindow, title: "", titleAttring: inputtr, content: "你的一条五星好评是我们前进的动力", isOneBtn: true, oneBtnText: "立即好评", oneBtnClosure: { // 同步调用 QSLInAppReviewmanager.requestReview() /*DispatchQueue.main.asyncAfter(deadline: .now() + 1) { self?.requestForReceivingGoodReviewGuidance() }*/ }) } } ///非会员的好评弹窗 @MainActor func noMemberPositiveReviewPopWindow(closeBtnClosure: @escaping () -> () = {}) { // 创建深灰色文本样式 let darkGrayAttributes: [NSAttributedString.Key: Any] = [ .foregroundColor: UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0), .font: UIFont(name: "Noto Sans SC", size: 20)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 20, weight: .black), .paragraphStyle: { let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = .center return paragraphStyle }(), .kern: -1 ] let inputtr = NSMutableAttributedString() let titleOneAttr = NSAttributedString(string: "给我们一个好评呗~", attributes: darkGrayAttributes) inputtr.append(titleOneAttr) if let currentWindow = UIApplication.keyWindow { QSLVipAlertView.alert(view: currentWindow, title: "", titleAttring: inputtr, content: "我们将回馈用户更多的优惠福利", isOneBtn: true, oneBtnText: "立即好评", oneBtnClosure: { // 同步调用 QSLInAppReviewmanager.requestReview() // 延迟1秒执行 /*DispatchQueue.main.asyncAfter(deadline: .now() + 1) { self?.requestForReceivingGoodReviewGuidance() }*/ //self?.requestAddFri() },closeBtnClosure: { closeBtnClosure() }) } } ///会员好评的结果弹窗 @MainActor func popUpWindowForMemberReview() { // 创建深灰色文本样式 let darkGrayAttributes: [NSAttributedString.Key: Any] = [ .foregroundColor: UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0), .font: UIFont(name: "Noto Sans SC", size: 20)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 20, weight: .black), .paragraphStyle: { let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = .center return paragraphStyle }(), .kern: -1 ] // 创建绿色文本样式 let greenAttributes: [NSAttributedString.Key: Any] = [ .foregroundColor: UIColor(red: 17/255.0, green: 186/255.0, blue: 152/255.0, alpha: 1.0), .font: UIFont(name: "Noto Sans SC", size: 28)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 28, weight: .black), .paragraphStyle: { let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = .center return paragraphStyle }(), .kern: -1 ] let inputtr = NSMutableAttributedString() let titleOneAttr = NSAttributedString(string: "你已成功增加", attributes: darkGrayAttributes) let titleTwoAttr = NSAttributedString(string: " \(self.guideGobalModel.extraMemberDays) ", attributes: greenAttributes) let titleThreeAttr = NSAttributedString(string: "天会员", attributes: darkGrayAttributes) inputtr.append(titleOneAttr) inputtr.append(titleTwoAttr) inputtr.append(titleThreeAttr) if let currentWindow = UIApplication.keyWindow { QSLVipResultsAlertView.alert(view: currentWindow, title: "", titleAttring: inputtr, content: "恭喜你,领取成功!", isOneBtn: true, oneBtnText: "我知道了", oneBtnClosure: { [weak self] in //self?.requestAddFri() }) } } ///非会员好评的结果弹窗 @MainActor func popUpWindowForNonMemberReviews() { ///标题 let titleDarkGrayAttributes: [NSAttributedString.Key: Any] = [ .foregroundColor: UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0), .font: UIFont(name: "Noto Sans SC", size: 20)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 20, weight: .black), .paragraphStyle: { let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = .center return paragraphStyle }(), .kern: -1 ] // 创建深灰色文本样式 let darkGrayAttributes: [NSAttributedString.Key: Any] = [ .foregroundColor: UIColor(red: 255/255.0, green: 230/255.0, blue: 192/255.0, alpha: 1.0), .font: UIFont(name: "Noto Sans SC", size: 16)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 20, weight: .black), .paragraphStyle: { let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = .center return paragraphStyle }(), .kern: -1 ] // 创建绿色文本样式 let greenAttributes: [NSAttributedString.Key: Any] = [ .foregroundColor: UIColor(red: 255/255.0, green: 230/255.0, blue: 192/255.0, alpha: 1.0), .font: UIFont(name: "Noto Sans SC", size: 20)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 28, weight: .black), .paragraphStyle: { let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = .center return paragraphStyle }(), .kern: -1 ] let inputtr = NSMutableAttributedString() let titleOneAttr = NSAttributedString(string: "感谢支持~", attributes: titleDarkGrayAttributes) inputtr.append(titleOneAttr) let contentAtter = NSMutableAttributedString() let contentAtterOne = NSAttributedString(string: "会员套餐体验", attributes: darkGrayAttributes) contentAtter.append(contentAtterOne) let contentAtterTwo = NSAttributedString(string: " \(self.guideGobalModel.returnDaysBasedOnRegistration())+\(self.guideGobalModel.extraMemberDays) ", attributes: greenAttributes) contentAtter.append(contentAtterTwo) let contentAtterTherr = NSAttributedString(string: "天", attributes: darkGrayAttributes) contentAtter.append(contentAtterTherr) if let currentWindow = UIApplication.keyWindow { QSLVipResultsNoMemberAlertView.alert(view: currentWindow, title: "", titleAttring: inputtr, content: "已解锁会员套餐体验7+3福利套餐",bottomContentAttring: contentAtter, isOneBtn: true, oneBtnText: "立即查看", oneBtnClosure: { [weak self] in QSLJumpManager.shared.pushToVip(type: .guideComments) }) } } }