72 lines
1.5 KiB
Swift
72 lines
1.5 KiB
Swift
//
|
|
// CountDownService.swift
|
|
// HealthyZG
|
|
//
|
|
// Created by 林 on 2020/8/11.
|
|
// Copyright © 2020 Lin. All rights reserved.
|
|
//
|
|
|
|
import RxSwift
|
|
|
|
class CountDownService {
|
|
|
|
/// 仅用来做token刷新倒计时
|
|
static let shared = CountDownService(countDownTime: 1800)
|
|
|
|
private var disposeBag = DisposeBag()
|
|
private var time = 0
|
|
private var countDownTime: Int
|
|
|
|
var timer: DispatchSourceTimer?
|
|
var currentTime = PublishSubject<Int>()
|
|
|
|
init(countDownTime: Int) {
|
|
self.countDownTime = countDownTime
|
|
time = countDownTime
|
|
}
|
|
|
|
func cancelTimer() {
|
|
timer?.cancel()
|
|
timer = nil
|
|
}
|
|
|
|
func startTimer() {
|
|
time += 1
|
|
|
|
if timer == nil {
|
|
let queue = DispatchQueue.global()
|
|
timer = DispatchSource.makeTimerSource(queue: queue)
|
|
timer?.schedule(deadline: .now(), repeating: .seconds(1))
|
|
}
|
|
|
|
timer?.setEventHandler(handler: { [weak self] in
|
|
guard let `self` = self else { return }
|
|
self.countDown()
|
|
})
|
|
timer?.resume()
|
|
}
|
|
|
|
func countDown() {
|
|
time -= 1
|
|
if time == 0 {
|
|
cancelTimer()
|
|
currentTime.onNext(time)
|
|
time = countDownTime
|
|
} else {
|
|
currentTime.onNext(time)
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Token刷新倒计时
|
|
extension CountDownService {
|
|
func tokenRefreshCountDown() {
|
|
|
|
}
|
|
|
|
func tokenRefreshStop() {
|
|
cancelTimer()
|
|
time = countDownTime
|
|
}
|
|
}
|