QSLCountdownManager.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. //
  2. // QSLCountdownManager.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by Trae AI on 2024-06-30.
  6. //
  7. import Foundation
  8. /// 倒计时类型
  9. enum QSLCountdownType {
  10. case homeProduct // 首页商品倒计时
  11. case trial // 试用倒计时
  12. }
  13. /// 倒计时管理器代理协议
  14. protocol QSLCountdownManagerDelegate: AnyObject {
  15. /// 倒计时更新
  16. /// - Parameters:
  17. /// - manager: 倒计时管理器
  18. /// - type: 倒计时类型
  19. /// - remainingSeconds: 剩余秒数
  20. func countdownManager(_ manager: QSLCountdownManager, didUpdateCountdown type: QSLCountdownType, remainingSeconds: Int)
  21. /// 倒计时结束
  22. /// - Parameters:
  23. /// - manager: 倒计时管理器
  24. /// - type: 倒计时类型
  25. func countdownManager(_ manager: QSLCountdownManager, didFinishCountdown type: QSLCountdownType)
  26. }
  27. // 使代理方法可选实现
  28. extension QSLCountdownManagerDelegate {
  29. func countdownManager(_ manager: QSLCountdownManager, didUpdateCountdown type: QSLCountdownType, remainingSeconds: Int) {}
  30. func countdownManager(_ manager: QSLCountdownManager, didFinishCountdown type: QSLCountdownType) {}
  31. }
  32. /// 倒计时管理器
  33. class QSLCountdownManager {
  34. // MARK: - 单例
  35. static let shared = QSLCountdownManager()
  36. private init() {}
  37. // MARK: - 属性
  38. private var timer: Timer?
  39. private var isHomeProductCountdownActive = false
  40. private var isTrialCountdownActive = false
  41. // 倒计时剩余时间(秒)
  42. private var homeProductRemainingSeconds: Int = 0
  43. private var trialRemainingSeconds: Int = 0
  44. // 代理集合
  45. private var delegates = NSHashTable<AnyObject>.weakObjects()
  46. // 倒计时回调(保留原有回调机制作为备选)
  47. typealias CountdownCallback = (Int) -> Void
  48. private var homeProductCallback: CountdownCallback?
  49. private var trialCallback: CountdownCallback?
  50. // 倒计时结束回调(保留原有回调机制作为备选)
  51. typealias CountdownFinishCallback = () -> Void
  52. private var homeProductFinishCallback: CountdownFinishCallback?
  53. private var trialFinishCallback: CountdownFinishCallback?
  54. var selectGood: QSLGoodModel?{
  55. didSet{
  56. if(oldValue != nil){
  57. NotificationCenter.default.post(
  58. name: Notification.Name("QSLHomeRefreshCouponViewNoti"),
  59. object: nil,
  60. userInfo: nil
  61. )
  62. }
  63. }
  64. }
  65. var trialGood: QSLGoodModel?{
  66. didSet{
  67. if(oldValue != nil){
  68. NotificationCenter.default.post(
  69. name: Notification.Name("QSLHomeRefreshCouponViewNoti"),
  70. object: nil,
  71. userInfo: nil
  72. )
  73. }
  74. }
  75. }
  76. // MARK: - 代理管理
  77. /// 添加代理
  78. /// - Parameter delegate: 代理对象
  79. func addDelegate(_ delegate: QSLCountdownManagerDelegate) {
  80. delegates.add(delegate)
  81. }
  82. /// 移除代理
  83. /// - Parameter delegate: 代理对象
  84. func removeDelegate(_ delegate: QSLCountdownManagerDelegate) {
  85. delegates.remove(delegate)
  86. }
  87. /// 移除所有代理
  88. func removeAllDelegates() {
  89. delegates.removeAllObjects()
  90. }
  91. // MARK: - 公共方法
  92. /// 开始倒计时
  93. /// - Parameters:
  94. /// - type: 倒计时类型
  95. /// - seconds: 倒计时秒数
  96. /// - callback: 倒计时回调,返回剩余秒数
  97. /// - finishCallback: 倒计时结束回调
  98. /// - Returns: 是否成功开启倒计时
  99. @discardableResult
  100. func startCountdown(type: QSLCountdownType, seconds: Int, callback: @escaping CountdownCallback, finishCallback: CountdownFinishCallback? = nil) -> Bool {
  101. // 检查该类型倒计时是否已结束,如果已结束则不能再开启
  102. switch type {
  103. case .homeProduct:
  104. if isHomeProductCountdownActive {
  105. return false
  106. }
  107. homeProductRemainingSeconds = seconds
  108. homeProductCallback = callback
  109. homeProductFinishCallback = finishCallback
  110. isHomeProductCountdownActive = true
  111. case .trial:
  112. if isTrialCountdownActive {
  113. return false
  114. }
  115. trialRemainingSeconds = seconds
  116. trialCallback = callback
  117. trialFinishCallback = finishCallback
  118. isTrialCountdownActive = true
  119. }
  120. // 如果timer不存在,创建timer
  121. if timer == nil {
  122. timer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector: #selector(updateCountdown), userInfo: nil, repeats: true)
  123. RunLoop.current.add(timer!, forMode: .common)
  124. }
  125. // 立即执行一次回调,更新UI
  126. executeCallback(for: type)
  127. return true
  128. }
  129. /// 暂停倒计时
  130. /// - Parameter type: 倒计时类型
  131. func pauseCountdown(type: QSLCountdownType) {
  132. switch type {
  133. case .homeProduct:
  134. isHomeProductCountdownActive = false
  135. case .trial:
  136. isTrialCountdownActive = false
  137. }
  138. // 如果两种倒计时都不活跃,则销毁timer
  139. if !isHomeProductCountdownActive && !isTrialCountdownActive {
  140. invalidateTimer()
  141. }
  142. }
  143. /// 恢复倒计时
  144. /// - Parameter type: 倒计时类型
  145. /// - Returns: 是否成功恢复倒计时
  146. @discardableResult
  147. func resumeCountdown(type: QSLCountdownType) -> Bool {
  148. switch type {
  149. case .homeProduct:
  150. if homeProductRemainingSeconds <= 0 {
  151. return false
  152. }
  153. isHomeProductCountdownActive = true
  154. case .trial:
  155. if trialRemainingSeconds <= 0 {
  156. return false
  157. }
  158. isTrialCountdownActive = true
  159. }
  160. // 如果timer不存在,创建timer
  161. if timer == nil {
  162. timer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector: #selector(updateCountdown), userInfo: nil, repeats: true)
  163. RunLoop.current.add(timer!, forMode: .common)
  164. }
  165. return true
  166. }
  167. /// 停止倒计时
  168. /// - Parameter type: 倒计时类型
  169. func stopCountdown(type: QSLCountdownType) {
  170. switch type {
  171. case .homeProduct:
  172. isHomeProductCountdownActive = false
  173. homeProductRemainingSeconds = 0
  174. homeProductCallback = nil
  175. homeProductFinishCallback = nil
  176. case .trial:
  177. isTrialCountdownActive = false
  178. trialRemainingSeconds = 0
  179. trialCallback = nil
  180. trialFinishCallback = nil
  181. }
  182. // 如果两种倒计时都不活跃,则销毁timer
  183. if !isHomeProductCountdownActive && !isTrialCountdownActive {
  184. invalidateTimer()
  185. }
  186. }
  187. /// 获取剩余时间
  188. /// - Parameter type: 倒计时类型
  189. /// - Returns: 剩余秒数
  190. func getRemainingSeconds(for type: QSLCountdownType) -> Int {
  191. switch type {
  192. case .homeProduct:
  193. return homeProductRemainingSeconds
  194. case .trial:
  195. return trialRemainingSeconds
  196. }
  197. }
  198. /// 是否正在倒计时
  199. /// - Parameter type: 倒计时类型
  200. /// - Returns: 是否正在倒计时
  201. func isCountdownActive(for type: QSLCountdownType) -> Bool {
  202. switch type {
  203. case .homeProduct:
  204. return isHomeProductCountdownActive
  205. case .trial:
  206. return isTrialCountdownActive
  207. }
  208. }
  209. // MARK: - 私有方法
  210. /// 更新倒计时
  211. @objc private func updateCountdown() {
  212. // 更新首页商品倒计时
  213. if isHomeProductCountdownActive {
  214. if homeProductRemainingSeconds > 0 {
  215. homeProductRemainingSeconds -= 1
  216. executeCallback(for: .homeProduct)
  217. notifyDelegatesCountdownUpdated(type: .homeProduct, remainingSeconds: homeProductRemainingSeconds)
  218. }
  219. // 检查是否结束
  220. if homeProductRemainingSeconds <= 0 {
  221. isHomeProductCountdownActive = false
  222. homeProductFinishCallback?()
  223. homeProductCallback = nil
  224. homeProductFinishCallback = nil
  225. notifyDelegatesCountdownFinished(type: .homeProduct)
  226. }
  227. }
  228. // 更新试用倒计时
  229. if isTrialCountdownActive {
  230. if trialRemainingSeconds > 0 {
  231. trialRemainingSeconds -= 1
  232. executeCallback(for: .trial)
  233. notifyDelegatesCountdownUpdated(type: .trial, remainingSeconds: trialRemainingSeconds)
  234. }
  235. // 检查是否结束
  236. if trialRemainingSeconds <= 0 {
  237. isTrialCountdownActive = false
  238. trialFinishCallback?()
  239. trialCallback = nil
  240. trialFinishCallback = nil
  241. notifyDelegatesCountdownFinished(type: .trial)
  242. }
  243. }
  244. // 如果两种倒计时都不活跃,则销毁timer
  245. if !isHomeProductCountdownActive && !isTrialCountdownActive {
  246. invalidateTimer()
  247. }
  248. }
  249. /// 执行倒计时回调
  250. /// - Parameter type: 倒计时类型
  251. private func executeCallback(for type: QSLCountdownType) {
  252. switch type {
  253. case .homeProduct:
  254. homeProductCallback?(homeProductRemainingSeconds)
  255. case .trial:
  256. trialCallback?(trialRemainingSeconds)
  257. }
  258. }
  259. /// 通知代理倒计时更新
  260. /// - Parameters:
  261. /// - type: 倒计时类型
  262. /// - remainingSeconds: 剩余秒数
  263. private func notifyDelegatesCountdownUpdated(type: QSLCountdownType, remainingSeconds: Int) {
  264. for case let delegate as QSLCountdownManagerDelegate in delegates.allObjects {
  265. delegate.countdownManager(self, didUpdateCountdown: type, remainingSeconds: remainingSeconds)
  266. }
  267. }
  268. /// 通知代理倒计时结束
  269. /// - Parameter type: 倒计时类型
  270. private func notifyDelegatesCountdownFinished(type: QSLCountdownType) {
  271. for case let delegate as QSLCountdownManagerDelegate in delegates.allObjects {
  272. delegate.countdownManager(self, didFinishCountdown: type)
  273. }
  274. }
  275. /// 销毁定时器
  276. private func invalidateTimer() {
  277. timer?.invalidate()
  278. timer = nil
  279. }
  280. }