jsdw_ios/QuickLocation/Manager/CountDown/CountDownService.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
}
}