QSLGuideusersToCommentManager.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //
  2. // QSLGuideusersToCommentManager.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by Destiny on 2025/7/31.
  6. //
  7. import Foundation
  8. import UIKit
  9. enum QSLGuideusersToCommentType : Int{
  10. case member //会员
  11. case nonMember // 非会员
  12. }
  13. class QSLGuideusersToCommentManager: NSObject {
  14. static let commentShare = QSLGuideusersToCommentManager()
  15. //归因渠道
  16. public var clickCompanyChannel : String = ""
  17. var guideGobalModel: QSLGuideIsTriggeredModel = QSLGuideIsTriggeredModel()
  18. var commentType: QSLGuideusersToCommentType = .member
  19. private var timer: Timer?
  20. // Initialize and setup notifications
  21. override init() {
  22. super.init()
  23. setupAppStateNotifications()
  24. startTimer()
  25. }
  26. deinit {
  27. stopTimer()
  28. NotificationCenter.default.removeObserver(self)
  29. }
  30. // MARK: - Timer Management
  31. private func setupAppStateNotifications() {
  32. NotificationCenter.default.addObserver(self,
  33. selector: #selector(appDidEnterBackground),
  34. name: UIApplication.didEnterBackgroundNotification,
  35. object: nil)
  36. NotificationCenter.default.addObserver(self,
  37. selector: #selector(appWillEnterForeground),
  38. name: UIApplication.willEnterForegroundNotification,
  39. object: nil)
  40. }
  41. @objc private func appWillEnterForeground() {
  42. startTimer()
  43. }
  44. @objc private func appDidEnterBackground() {
  45. stopTimer()
  46. }
  47. private func startTimer() {
  48. //print("startTimersfsdf---A--\(self.commentType)")
  49. // Invalidate existing timer if any
  50. stopTimer()
  51. // Create new timer that fires every minute
  52. timer = Timer.scheduledTimer(withTimeInterval: 60.0, repeats: true) { [weak self] _ in
  53. self?.timerFired()
  54. }
  55. // Fire immediately on first start
  56. timerFired()
  57. }
  58. private func stopTimer() {
  59. //print("startTimersfsdf---B--\(self.commentType)")
  60. timer?.invalidate()
  61. timer = nil
  62. }
  63. private func timerFired() {
  64. // Call your existing method here
  65. // manageWhetherTriggerPopUpWindow(commentType)
  66. }
  67. // MARK: - Existing Methods (unchanged)
  68. //管理处理是否触发好评引导弹窗
  69. func manageWhetherTriggerPopUpWindow(_ showType: QSLGuideusersToCommentType, closeBtnClosure: @escaping () -> () = {}) {
  70. //print("startTimersfsdf---C--\(showType)---\(self.commentType)")
  71. QSLNetwork().request(.guideIsTriggered(dict: [String: Any]())) { response in
  72. let guideIsModel: QSLGuideIsTriggeredModel = response.mapObject(QSLGuideIsTriggeredModel.self, modelKey: "data")
  73. self.commentType = showType
  74. self.guideGobalModel = guideIsModel
  75. if guideIsModel.trigger{
  76. DispatchQueue.main.async {
  77. self.noMemberPositiveReviewPopWindow(closeBtnClosure: closeBtnClosure)
  78. }
  79. }else{
  80. closeBtnClosure()
  81. }
  82. } fail: { code, msg in
  83. }
  84. }
  85. //判断是不是这两个渠道 ASA渠道和自然流量两个渠道
  86. func areThisTwoChannel() -> Bool {
  87. if self.clickCompanyChannel == "Apple Search Ads" || self.clickCompanyChannel == "natural" {
  88. return false
  89. }
  90. return true
  91. }
  92. ///领取好评引导奖励请求
  93. func requestForReceivingGoodReviewGuidance() {
  94. QSLNetwork().request(.guideReceiveReward(dict: [String: Any]())) { response in
  95. if self.commentType == .member {
  96. DispatchQueue.main.async {
  97. self.popUpWindowForMemberReview()
  98. }
  99. } else {
  100. DispatchQueue.main.async {
  101. self.popUpWindowForNonMemberReviews()
  102. }
  103. }
  104. } fail: { code, msg in
  105. }
  106. }
  107. }
  108. //管理各种类型的弹窗
  109. extension QSLGuideusersToCommentManager {
  110. ///会员的好评弹窗
  111. @MainActor func memberPositiveReviewPopWindow() {
  112. // 创建深灰色文本样式
  113. let darkGrayAttributes: [NSAttributedString.Key: Any] = [
  114. .foregroundColor: UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0),
  115. .font: UIFont(name: "Noto Sans SC", size: 20)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 20, weight: .black),
  116. .paragraphStyle: {
  117. let paragraphStyle = NSMutableParagraphStyle()
  118. paragraphStyle.alignment = .center
  119. return paragraphStyle
  120. }(),
  121. .kern: -1
  122. ]
  123. // 创建绿色文本样式
  124. let greenAttributes: [NSAttributedString.Key: Any] = [
  125. .foregroundColor: UIColor(red: 17/255.0, green: 186/255.0, blue: 152/255.0, alpha: 1.0),
  126. .font: UIFont(name: "Noto Sans SC", size: 20)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 20, weight: .black),
  127. .paragraphStyle: {
  128. let paragraphStyle = NSMutableParagraphStyle()
  129. paragraphStyle.alignment = .center
  130. return paragraphStyle
  131. }(),
  132. .kern: -1
  133. ]
  134. let inputtr = NSMutableAttributedString()
  135. let titleOneAttr = NSAttributedString(string: "五星好评,", attributes: darkGrayAttributes)
  136. let titleTwoAttr = NSAttributedString(string: "多送\(self.guideGobalModel.extraMemberDays)天会员", attributes: greenAttributes)
  137. inputtr.append(titleOneAttr)
  138. inputtr.append(titleTwoAttr)
  139. if let currentWindow = UIApplication.keyWindow {
  140. QSLVipAlertView.alert(view: currentWindow, title: "", titleAttring: inputtr, content: "你的一条五星好评是我们前进的动力", isOneBtn: true, oneBtnText: "立即好评", oneBtnClosure: {
  141. // 同步调用
  142. QSLInAppReviewmanager.requestReview()
  143. /*DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  144. self?.requestForReceivingGoodReviewGuidance()
  145. }*/
  146. })
  147. }
  148. }
  149. ///非会员的好评弹窗
  150. @MainActor func noMemberPositiveReviewPopWindow(closeBtnClosure: @escaping () -> () = {}) {
  151. // 创建深灰色文本样式
  152. let darkGrayAttributes: [NSAttributedString.Key: Any] = [
  153. .foregroundColor: UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0),
  154. .font: UIFont(name: "Noto Sans SC", size: 20)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 20, weight: .black),
  155. .paragraphStyle: {
  156. let paragraphStyle = NSMutableParagraphStyle()
  157. paragraphStyle.alignment = .center
  158. return paragraphStyle
  159. }(),
  160. .kern: -1
  161. ]
  162. let inputtr = NSMutableAttributedString()
  163. let titleOneAttr = NSAttributedString(string: "给我们一个好评呗~", attributes: darkGrayAttributes)
  164. inputtr.append(titleOneAttr)
  165. if let currentWindow = UIApplication.keyWindow {
  166. QSLVipAlertView.alert(view: currentWindow, title: "", titleAttring: inputtr, content: "我们将回馈用户更多的优惠福利", isOneBtn: true, oneBtnText: "立即好评", oneBtnClosure: {
  167. // 同步调用
  168. QSLInAppReviewmanager.requestReview()
  169. },closeBtnClosure: {
  170. closeBtnClosure()
  171. })
  172. }
  173. }
  174. ///会员好评的结果弹窗
  175. @MainActor func popUpWindowForMemberReview() {
  176. // 创建深灰色文本样式
  177. let darkGrayAttributes: [NSAttributedString.Key: Any] = [
  178. .foregroundColor: UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0),
  179. .font: UIFont(name: "Noto Sans SC", size: 20)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 20, weight: .black),
  180. .paragraphStyle: {
  181. let paragraphStyle = NSMutableParagraphStyle()
  182. paragraphStyle.alignment = .center
  183. return paragraphStyle
  184. }(),
  185. .kern: -1
  186. ]
  187. // 创建绿色文本样式
  188. let greenAttributes: [NSAttributedString.Key: Any] = [
  189. .foregroundColor: UIColor(red: 17/255.0, green: 186/255.0, blue: 152/255.0, alpha: 1.0),
  190. .font: UIFont(name: "Noto Sans SC", size: 28)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 28, weight: .black),
  191. .paragraphStyle: {
  192. let paragraphStyle = NSMutableParagraphStyle()
  193. paragraphStyle.alignment = .center
  194. return paragraphStyle
  195. }(),
  196. .kern: -1
  197. ]
  198. let inputtr = NSMutableAttributedString()
  199. let titleOneAttr = NSAttributedString(string: "你已成功增加", attributes: darkGrayAttributes)
  200. let titleTwoAttr = NSAttributedString(string: " \(self.guideGobalModel.extraMemberDays) ", attributes: greenAttributes)
  201. let titleThreeAttr = NSAttributedString(string: "天会员", attributes: darkGrayAttributes)
  202. inputtr.append(titleOneAttr)
  203. inputtr.append(titleTwoAttr)
  204. inputtr.append(titleThreeAttr)
  205. if let currentWindow = UIApplication.keyWindow {
  206. QSLVipResultsAlertView.alert(view: currentWindow, title: "", titleAttring: inputtr, content: "恭喜你,领取成功!", isOneBtn: true, oneBtnText: "我知道了", oneBtnClosure: { [weak self] in
  207. //self?.requestAddFri()
  208. })
  209. }
  210. }
  211. ///非会员好评的结果弹窗
  212. @MainActor func popUpWindowForNonMemberReviews() {
  213. ///标题
  214. let titleDarkGrayAttributes: [NSAttributedString.Key: Any] = [
  215. .foregroundColor: UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0),
  216. .font: UIFont(name: "Noto Sans SC", size: 20)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 20, weight: .black),
  217. .paragraphStyle: {
  218. let paragraphStyle = NSMutableParagraphStyle()
  219. paragraphStyle.alignment = .center
  220. return paragraphStyle
  221. }(),
  222. .kern: -1
  223. ]
  224. // 创建深灰色文本样式
  225. let darkGrayAttributes: [NSAttributedString.Key: Any] = [
  226. .foregroundColor: UIColor(red: 255/255.0, green: 230/255.0, blue: 192/255.0, alpha: 1.0),
  227. .font: UIFont(name: "Noto Sans SC", size: 16)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 20, weight: .black),
  228. .paragraphStyle: {
  229. let paragraphStyle = NSMutableParagraphStyle()
  230. paragraphStyle.alignment = .center
  231. return paragraphStyle
  232. }(),
  233. .kern: -1
  234. ]
  235. // 创建绿色文本样式
  236. let greenAttributes: [NSAttributedString.Key: Any] = [
  237. .foregroundColor: UIColor(red: 255/255.0, green: 230/255.0, blue: 192/255.0, alpha: 1.0),
  238. .font: UIFont(name: "Noto Sans SC", size: 20)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 28, weight: .black),
  239. .paragraphStyle: {
  240. let paragraphStyle = NSMutableParagraphStyle()
  241. paragraphStyle.alignment = .center
  242. return paragraphStyle
  243. }(),
  244. .kern: -1
  245. ]
  246. let inputtr = NSMutableAttributedString()
  247. let titleOneAttr = NSAttributedString(string: "感谢支持~", attributes: titleDarkGrayAttributes)
  248. inputtr.append(titleOneAttr)
  249. let contentAtter = NSMutableAttributedString()
  250. let contentAtterOne = NSAttributedString(string: "会员套餐体验", attributes: darkGrayAttributes)
  251. contentAtter.append(contentAtterOne)
  252. let contentAtterTwo = NSAttributedString(string: " \(self.guideGobalModel.returnDaysBasedOnRegistration())+\(self.guideGobalModel.extraMemberDays) ", attributes: greenAttributes)
  253. contentAtter.append(contentAtterTwo)
  254. let contentAtterTherr = NSAttributedString(string: "天", attributes: darkGrayAttributes)
  255. contentAtter.append(contentAtterTherr)
  256. if let currentWindow = UIApplication.keyWindow {
  257. QSLVipResultsNoMemberAlertView.alert(view: currentWindow, title: "", titleAttring: inputtr, content: "已解锁会员套餐体验7+3福利套餐",bottomContentAttring: contentAtter, isOneBtn: true, oneBtnText: "立即查看", oneBtnClosure: { [weak self] in
  258. QSLJumpManager.shared.pushToVip(type: .guideComments)
  259. })
  260. }
  261. }
  262. }