Skip to content

Commit cb81938

Browse files
committed
Merge branch 'develop'
# Conflicts: # Package.resolved # Package.swift
2 parents f095047 + 7fab39b commit cb81938

9 files changed

Lines changed: 190 additions & 22 deletions

Package.resolved

Lines changed: 0 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ let package = Package(
1212
targets: ["MagicTimer"]),
1313
],
1414
dependencies: [
15-
.package(url: "https://github.com/MagicTimerFW/MagicTimerCore", branch: "main")
15+
.package(url: "https://github.com/sadeghgoo/MathOperators.git", branch: "main")
1616
],
1717
targets: [
1818
.target(
1919
name: "MagicTimer",
2020
dependencies: [
2121
.product(
22-
name: "MagicTimerCore",
23-
package: "MagicTimerCore"),
22+
name: "MathOperators",
23+
package: "MathOperators"),
2424
]),
2525
.testTarget(
2626
name: "MagicTimerTests",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Foundation
2+
3+
public extension Date {
4+
static func - (lhs: Date, rhs: Date) -> TimeInterval {
5+
return lhs.timeIntervalSinceReferenceDate.minus(rhs.timeIntervalSinceReferenceDate)
6+
}
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Foundation
2+
3+
public extension TimeInterval {
4+
/// Cast TimeInterval to Int.
5+
func convertToInteger() -> Int {
6+
return Int(self)
7+
}
8+
/// Cast TimeInterval to String.
9+
func convertToString() -> String {
10+
return String(self)
11+
}
12+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import Foundation
2+
3+
public extension Int {
4+
/// Cast Int to TimeInterval
5+
func convertToTimeInterval() -> TimeInterval {
6+
return TimeInterval(self)
7+
}
8+
/// Cast Int to String
9+
func convertToString() -> String {
10+
return String(self)
11+
}
12+
13+
}
14+
15+
public extension Double {
16+
/// Check the number is positive(bigger than zero)
17+
var isPositive: Bool {
18+
get {
19+
return self > 0
20+
}
21+
}
22+
/// Cast Double to TimeInterval
23+
func convertToTimeInterval() -> TimeInterval {
24+
return TimeInterval(self)
25+
}
26+
}

Sources/MagicTimer/MagicTimer.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import Foundation
2-
import MagicTimerCore
32
import MathOperators
43

54
@available(*, unavailable, renamed: "MagicTimerMode")
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import UIKit
2+
import MathOperators
3+
4+
public protocol MagicTimerBackgroundCalculatorInterface {
5+
/// The timer fired date.
6+
var timerFiredDate: Date? { get set }
7+
/// By changing this type timer decides to whether calcualte the time in background.
8+
var isActiveBackgroundMode: Bool { get set }
9+
10+
var backgroundTimeCalculateHandler: ((TimeInterval) -> Void)? { get set }
11+
}
12+
13+
public final class MagicTimerBackgroundCalculator: MagicTimerBackgroundCalculatorInterface {
14+
15+
public var timerFiredDate: Date?
16+
public var isActiveBackgroundMode: Bool = true
17+
public var backgroundTimeCalculateHandler: ((TimeInterval) -> Void)?
18+
19+
public init() {
20+
NotificationCenter.default.addObserver(self, selector: #selector(willEnterForegroundNotification), name: UIApplication.willEnterForegroundNotification, object: nil)
21+
}
22+
23+
deinit {
24+
NotificationCenter.default.removeObserver(self)
25+
}
26+
27+
private func invalidateFiredDate() {
28+
timerFiredDate = nil
29+
}
30+
31+
private func calculateDateDiffrence() -> TimeInterval? {
32+
guard let timerFiredDate else { return nil }
33+
let validTimeSubtraction = abs(timerFiredDate - Date())
34+
return validTimeSubtraction.convertToTimeInterval()
35+
}
36+
37+
@objc
38+
private func willEnterForegroundNotification() {
39+
guard isActiveBackgroundMode else { return }
40+
41+
if let timeInterval = calculateDateDiffrence() {
42+
backgroundTimeCalculateHandler?(timeInterval)
43+
}
44+
}
45+
}
46+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import Foundation
2+
import MathOperators
3+
4+
public protocol MagicTimerCounterInterface {
5+
/// The total counted value.
6+
var totalCountedValue: TimeInterval { get set }
7+
/// A number which is added or minused on each ``timeInterval``.
8+
var effectiveValue: TimeInterval { get set }
9+
/// The initial value of counter.
10+
var defultValue: TimeInterval { get set }
11+
/// Add effectiveValue to totalCountedValue.
12+
func add()
13+
/// Subtract effectiveValue from totalCountedValue.
14+
func subtract()
15+
/// Reset totalCountedValue to zero.
16+
func resetTotalCounted()
17+
/// Reset totalCountedValue to defultValue.
18+
func resetToDefaultValue()
19+
}
20+
21+
public final class MagicTimerCounter: MagicTimerCounterInterface {
22+
23+
// MARK: - Public properties
24+
public var totalCountedValue: TimeInterval = 0
25+
public var effectiveValue: TimeInterval = 1.0
26+
public var defultValue: TimeInterval = 0.0 {
27+
didSet {
28+
totalCountedValue = totalCountedValue.plus(defultValue)
29+
}
30+
}
31+
32+
// MARK: - Constructors
33+
public init() { }
34+
35+
// MARK: - Public methods
36+
public func add() {
37+
totalCountedValue = totalCountedValue.plus(effectiveValue)
38+
}
39+
40+
public func subtract() {
41+
totalCountedValue = totalCountedValue.minus(effectiveValue)
42+
}
43+
44+
public func resetTotalCounted() {
45+
totalCountedValue = 0
46+
}
47+
48+
public func resetToDefaultValue() {
49+
totalCountedValue = defultValue
50+
}
51+
}
52+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import Foundation
2+
3+
public protocol MagicTimerExecutiveInterface {
4+
/// Timer time interval.
5+
var timeInterval: TimeInterval { get set }
6+
/// Fire the timer.
7+
func fire(compeltionHandler: (() -> Void)?)
8+
/// Suspand the timer.
9+
func suspand(completionHandler: (() -> Void)?)
10+
/// A call back which is called on every time interval.
11+
var scheduleTimerHandler: (() -> Void)? { get set }
12+
}
13+
14+
public final class MagicTimerExecutive: MagicTimerExecutiveInterface {
15+
16+
// MARK: - Public properties
17+
public var scheduleTimerHandler: (() -> Void)?
18+
public var timeInterval: TimeInterval = 1.0
19+
20+
// MARK: - Private
21+
private var scheduleTimer: Timer?
22+
private var isTimerAlreadyStarted: Bool = false
23+
24+
// MARK: - Constructors
25+
public init() {
26+
scheduleTimer = Timer.scheduledTimer(withTimeInterval: timeInterval, repeats: true, block: { [weak self] _ in
27+
self?.scheduleTimerHandler?()
28+
})
29+
}
30+
31+
// MARK: - Public methods
32+
public func fire(compeltionHandler: (() -> Void)?) {
33+
isTimerAlreadyStarted = true
34+
scheduleTimer?.fire()
35+
RunLoop.main.add(scheduleTimer!, forMode: .common)
36+
compeltionHandler?()
37+
}
38+
39+
public func suspand(completionHandler: (() -> Void)?) {
40+
scheduleTimer?.invalidate()
41+
isTimerAlreadyStarted = false
42+
completionHandler?()
43+
}
44+
}

0 commit comments

Comments
 (0)