update ddtimerswiftkit

This commit is contained in:
DDIsFriend
2023-09-15 15:15:56 +08:00
parent 05a2ae2b91
commit f0c63d8c58
42 changed files with 7914 additions and 8255 deletions

View File

@@ -0,0 +1,159 @@
//
// GCDTimer.swift
// DDTimerSwiftKit_Private
// Created by DDIsFriend on 2023/9/15.
import Foundation
import DDLogKit_Private
public let GCDT = GCDTimer.default
open class GCDTimer {
public static let `default` = GCDTimer()
private var timerSources : [GCDTimerAbstract] = []
public var canceledCompletionHandler:(() -> Void)? //
///
/// - Parameters:
/// - named:
/// - delay:
/// - repeating:
/// - repeatCount:
/// - repeatHandler:
/// - cancelHandler: cancelHandlercompletionHandler
/// - completionHandler: completionHandlercancelHandler
public func addGCDTimer(named timerName:String,delay:DispatchTimeInterval,repeating:DispatchTimeInterval,repeatCount:Int = Int.max,repeatHandler:@escaping (() -> Void),cancelHandler:(() -> Void)? = nil,completionHandler:(() -> Void)? = nil) {
if let _ = timerSources.first(where: { gcdTimerA in
gcdTimerA.timerName == timerName
}) {
DDLog(message: "定时器\'\(timerName)\'重复添加,添加失败")
return
}
let gcdTimerA = GCDTimerAbstract(named: timerName)
gcdTimerA.addGCDTimer(delay: delay, repeating: repeating, repeatCount: repeatCount) {
repeatHandler()
} cancelHandler: {[weak self] timerName in
//
self?.removeGCDTimer(named: timerName)
//
if let cancelHandler {
cancelHandler()
}
//
if let canceledCompletionHandler = self?.canceledCompletionHandler {
canceledCompletionHandler()
}
} completionHandler: {[weak self] timerName in
//
self?.removeGCDTimer(named: timerName)
//
if let completionHandler {
completionHandler()
}
}
timerSources.append(gcdTimerA)
DDLog(message: "定时器\'\(timerName)\'添加成功")
//
logTimerName()
}
/// canceledCompletionHandlercanceledCompletionHandler
/// - Parameters:
/// - timerName:
/// - canceledCompletionHandler:
public func cancelGCDTimer(named timerName:String,canceledCompletionHandler:(() -> Void)? = nil) {
// timerSources
if let gcdTimerA = timerSources.first(where: { gcdTimerA in
gcdTimerA.timerName == timerName
}) {
self.canceledCompletionHandler = canceledCompletionHandler
gcdTimerA.cancelGCDTimer(isCanceledManually: true)
}else{
DDLog(message: "定时器\'\(timerName)\'取消失败,未包含")
}
}
public func containsGCDTimer(named timerName:String) -> Bool {
if let _ = timerSources.first(where: { gcdTimerA in
gcdTimerA.timerName == timerName
}) {
return true
}
return false
}
func removeGCDTimer(named timerName:String) {
//
timerSources.removeAll { gcdTimerA in
gcdTimerA.timerName == timerName
}
DDLog(message: "定时器\'\(timerName)\'移除成功")
//
logTimerName()
}
func logTimerName() {
for gcdTimerA in timerSources {
DDLog(message:("剩余定时器:\'\( gcdTimerA.timerName)\'"))
}
}
}
class GCDTimerAbstract {
private let timerSource = DispatchSource.makeTimerSource(flags: .strict,queue: dispatch_queue_concurrent_t(label: "GCDTimerConcurrentQueue"))
public var timerName : String
public var isCanceledManually : Bool = false //
init(named timerName: String) {
self.timerName = timerName
}
public func addGCDTimer(delay:DispatchTimeInterval,repeating:DispatchTimeInterval,repeatCount:Int,repeatHandler:@escaping (() -> Void),cancelHandler:((String) -> Void)? = nil,completionHandler: ((String) -> Void)? = nil) {
//
var leftRepeatCount = repeatCount
timerSource.setEventHandler(handler: {[weak self] in
//
repeatHandler()
leftRepeatCount-=1
//
if (leftRepeatCount > 0) == false {
self?.cancelGCDTimer(isCanceledManually: false)
}
})
timerSource.setCancelHandler(handler: {[weak self] in
if self?.isCanceledManually == true {
// ,
if let cancelHandler,let timerName = self?.timerName {
cancelHandler(timerName)
}
// ,
}else{
//
if let completionHandler,let timerName = self?.timerName {
completionHandler(timerName)
}
}
})
timerSource.schedule(wallDeadline: DispatchWallTime.now() + delay,repeating: repeating)
timerSource.activate()
}
// cancelHander
public func cancelGCDTimer(isCanceledManually:Bool) {
self.isCanceledManually = isCanceledManually
timerSource.cancel()
}
}

19
Pods/DDTimerSwiftKit_Private/LICENSE generated Normal file
View File

@@ -0,0 +1,19 @@
Copyright (c) 2023 DDIsFriend <DDIsFriend@163.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

29
Pods/DDTimerSwiftKit_Private/README.md generated Normal file
View File

@@ -0,0 +1,29 @@
# DDTimerSwiftKit_Private
[![CI Status](https://img.shields.io/travis/DDIsFriend/DDTimerSwiftKit_Private.svg?style=flat)](https://travis-ci.org/DDIsFriend/DDTimerSwiftKit_Private)
[![Version](https://img.shields.io/cocoapods/v/DDTimerSwiftKit_Private.svg?style=flat)](https://cocoapods.org/pods/DDTimerSwiftKit_Private)
[![License](https://img.shields.io/cocoapods/l/DDTimerSwiftKit_Private.svg?style=flat)](https://cocoapods.org/pods/DDTimerSwiftKit_Private)
[![Platform](https://img.shields.io/cocoapods/p/DDTimerSwiftKit_Private.svg?style=flat)](https://cocoapods.org/pods/DDTimerSwiftKit_Private)
## Example
To run the example project, clone the repo, and run `pod install` from the Example directory first.
## Requirements
## Installation
DDTimerSwiftKit_Private is available through [CocoaPods](https://cocoapods.org). To install
it, simply add the following line to your Podfile:
```ruby
pod 'DDTimerSwiftKit_Private'
```
## Author
DDIsFriend, DDIsFriend@163.com
## License
DDTimerSwiftKit_Private is available under the MIT license. See the LICENSE file for more info.