QSLGuideusersToCommentManager.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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) {
  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 && self.areThisTwoChannel(){
  76. // DispatchQueue.main.async {
  77. // self.noMemberPositiveReviewPopWindow()
  78. // }
  79. // /*if showType == .member {
  80. // DispatchQueue.main.async {
  81. // self.memberPositiveReviewPopWindow()
  82. // }
  83. // } else {
  84. // DispatchQueue.main.async {
  85. // self.noMemberPositiveReviewPopWindow()
  86. // }
  87. // }*/
  88. // }
  89. // } fail: { code, msg in
  90. // }
  91. }
  92. //判断是不是这两个渠道 ASA渠道和自然流量两个渠道
  93. func areThisTwoChannel() -> Bool {
  94. if self.clickCompanyChannel == "Apple Search Ads" || self.clickCompanyChannel == "natural" {
  95. return false
  96. }
  97. return true
  98. }
  99. ///领取好评引导奖励请求
  100. func requestForReceivingGoodReviewGuidance() {
  101. QSLNetwork().request(.guideReceiveReward(dict: [String: Any]())) { response in
  102. if self.commentType == .member {
  103. DispatchQueue.main.async {
  104. self.popUpWindowForMemberReview()
  105. }
  106. } else {
  107. DispatchQueue.main.async {
  108. self.popUpWindowForNonMemberReviews()
  109. }
  110. }
  111. } fail: { code, msg in
  112. }
  113. }
  114. }
  115. //管理各种类型的弹窗
  116. extension QSLGuideusersToCommentManager {
  117. ///会员的好评弹窗
  118. @MainActor func memberPositiveReviewPopWindow() {
  119. // 创建深灰色文本样式
  120. let darkGrayAttributes: [NSAttributedString.Key: Any] = [
  121. .foregroundColor: UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0),
  122. .font: UIFont(name: "Noto Sans SC", size: 20)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 20, weight: .black),
  123. .paragraphStyle: {
  124. let paragraphStyle = NSMutableParagraphStyle()
  125. paragraphStyle.alignment = .center
  126. return paragraphStyle
  127. }(),
  128. .kern: -1
  129. ]
  130. // 创建绿色文本样式
  131. let greenAttributes: [NSAttributedString.Key: Any] = [
  132. .foregroundColor: UIColor(red: 17/255.0, green: 186/255.0, blue: 152/255.0, alpha: 1.0),
  133. .font: UIFont(name: "Noto Sans SC", size: 20)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 20, weight: .black),
  134. .paragraphStyle: {
  135. let paragraphStyle = NSMutableParagraphStyle()
  136. paragraphStyle.alignment = .center
  137. return paragraphStyle
  138. }(),
  139. .kern: -1
  140. ]
  141. let inputtr = NSMutableAttributedString()
  142. let titleOneAttr = NSAttributedString(string: "五星好评,", attributes: darkGrayAttributes)
  143. let titleTwoAttr = NSAttributedString(string: "多送\(self.guideGobalModel.extraMemberDays)天会员", attributes: greenAttributes)
  144. inputtr.append(titleOneAttr)
  145. inputtr.append(titleTwoAttr)
  146. if let currentWindow = UIApplication.keyWindow {
  147. QSLVipAlertView.alert(view: currentWindow, title: "", titleAttring: inputtr, content: "你的一条五星好评是我们前进的动力", isOneBtn: true, oneBtnText: "立即好评", oneBtnClosure: {
  148. // 同步调用
  149. QSLInAppReviewmanager.requestReview()
  150. /*DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  151. self?.requestForReceivingGoodReviewGuidance()
  152. }*/
  153. })
  154. }
  155. }
  156. ///非会员的好评弹窗
  157. @MainActor func noMemberPositiveReviewPopWindow(closeBtnClosure: @escaping () -> () = {}) {
  158. // 创建深灰色文本样式
  159. let darkGrayAttributes: [NSAttributedString.Key: Any] = [
  160. .foregroundColor: UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0),
  161. .font: UIFont(name: "Noto Sans SC", size: 20)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 20, weight: .black),
  162. .paragraphStyle: {
  163. let paragraphStyle = NSMutableParagraphStyle()
  164. paragraphStyle.alignment = .center
  165. return paragraphStyle
  166. }(),
  167. .kern: -1
  168. ]
  169. let inputtr = NSMutableAttributedString()
  170. let titleOneAttr = NSAttributedString(string: "给我们一个好评呗~", attributes: darkGrayAttributes)
  171. inputtr.append(titleOneAttr)
  172. if let currentWindow = UIApplication.keyWindow {
  173. QSLVipAlertView.alert(view: currentWindow, title: "", titleAttring: inputtr, content: "我们将回馈用户更多的优惠福利", isOneBtn: true, oneBtnText: "立即好评", oneBtnClosure: {
  174. // 同步调用
  175. QSLInAppReviewmanager.requestReview()
  176. // 延迟1秒执行
  177. /*DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  178. self?.requestForReceivingGoodReviewGuidance()
  179. }*/
  180. //self?.requestAddFri()
  181. },closeBtnClosure: {
  182. closeBtnClosure()
  183. })
  184. }
  185. }
  186. ///会员好评的结果弹窗
  187. @MainActor func popUpWindowForMemberReview() {
  188. // 创建深灰色文本样式
  189. let darkGrayAttributes: [NSAttributedString.Key: Any] = [
  190. .foregroundColor: UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0),
  191. .font: UIFont(name: "Noto Sans SC", size: 20)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 20, weight: .black),
  192. .paragraphStyle: {
  193. let paragraphStyle = NSMutableParagraphStyle()
  194. paragraphStyle.alignment = .center
  195. return paragraphStyle
  196. }(),
  197. .kern: -1
  198. ]
  199. // 创建绿色文本样式
  200. let greenAttributes: [NSAttributedString.Key: Any] = [
  201. .foregroundColor: UIColor(red: 17/255.0, green: 186/255.0, blue: 152/255.0, alpha: 1.0),
  202. .font: UIFont(name: "Noto Sans SC", size: 28)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 28, weight: .black),
  203. .paragraphStyle: {
  204. let paragraphStyle = NSMutableParagraphStyle()
  205. paragraphStyle.alignment = .center
  206. return paragraphStyle
  207. }(),
  208. .kern: -1
  209. ]
  210. let inputtr = NSMutableAttributedString()
  211. let titleOneAttr = NSAttributedString(string: "你已成功增加", attributes: darkGrayAttributes)
  212. let titleTwoAttr = NSAttributedString(string: " \(self.guideGobalModel.extraMemberDays) ", attributes: greenAttributes)
  213. let titleThreeAttr = NSAttributedString(string: "天会员", attributes: darkGrayAttributes)
  214. inputtr.append(titleOneAttr)
  215. inputtr.append(titleTwoAttr)
  216. inputtr.append(titleThreeAttr)
  217. if let currentWindow = UIApplication.keyWindow {
  218. QSLVipResultsAlertView.alert(view: currentWindow, title: "", titleAttring: inputtr, content: "恭喜你,领取成功!", isOneBtn: true, oneBtnText: "我知道了", oneBtnClosure: { [weak self] in
  219. //self?.requestAddFri()
  220. })
  221. }
  222. }
  223. ///非会员好评的结果弹窗
  224. @MainActor func popUpWindowForNonMemberReviews() {
  225. ///标题
  226. let titleDarkGrayAttributes: [NSAttributedString.Key: Any] = [
  227. .foregroundColor: UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0),
  228. .font: UIFont(name: "Noto Sans SC", size: 20)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 20, weight: .black),
  229. .paragraphStyle: {
  230. let paragraphStyle = NSMutableParagraphStyle()
  231. paragraphStyle.alignment = .center
  232. return paragraphStyle
  233. }(),
  234. .kern: -1
  235. ]
  236. // 创建深灰色文本样式
  237. let darkGrayAttributes: [NSAttributedString.Key: Any] = [
  238. .foregroundColor: UIColor(red: 255/255.0, green: 230/255.0, blue: 192/255.0, alpha: 1.0),
  239. .font: UIFont(name: "Noto Sans SC", size: 16)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 20, weight: .black),
  240. .paragraphStyle: {
  241. let paragraphStyle = NSMutableParagraphStyle()
  242. paragraphStyle.alignment = .center
  243. return paragraphStyle
  244. }(),
  245. .kern: -1
  246. ]
  247. // 创建绿色文本样式
  248. let greenAttributes: [NSAttributedString.Key: Any] = [
  249. .foregroundColor: UIColor(red: 255/255.0, green: 230/255.0, blue: 192/255.0, alpha: 1.0),
  250. .font: UIFont(name: "Noto Sans SC", size: 20)?.withTraits(traits: .traitBold) ?? UIFont.systemFont(ofSize: 28, weight: .black),
  251. .paragraphStyle: {
  252. let paragraphStyle = NSMutableParagraphStyle()
  253. paragraphStyle.alignment = .center
  254. return paragraphStyle
  255. }(),
  256. .kern: -1
  257. ]
  258. let inputtr = NSMutableAttributedString()
  259. let titleOneAttr = NSAttributedString(string: "感谢支持~", attributes: titleDarkGrayAttributes)
  260. inputtr.append(titleOneAttr)
  261. let contentAtter = NSMutableAttributedString()
  262. let contentAtterOne = NSAttributedString(string: "会员套餐体验", attributes: darkGrayAttributes)
  263. contentAtter.append(contentAtterOne)
  264. let contentAtterTwo = NSAttributedString(string: " \(self.guideGobalModel.returnDaysBasedOnRegistration())+\(self.guideGobalModel.extraMemberDays) ", attributes: greenAttributes)
  265. contentAtter.append(contentAtterTwo)
  266. let contentAtterTherr = NSAttributedString(string: "天", attributes: darkGrayAttributes)
  267. contentAtter.append(contentAtterTherr)
  268. if let currentWindow = UIApplication.keyWindow {
  269. QSLVipResultsNoMemberAlertView.alert(view: currentWindow, title: "", titleAttring: inputtr, content: "已解锁会员套餐体验7+3福利套餐",bottomContentAttring: contentAtter, isOneBtn: true, oneBtnText: "立即查看", oneBtnClosure: { [weak self] in
  270. QSLJumpManager.shared.pushToVip(type: .guideComments)
  271. })
  272. }
  273. }
  274. }