forked from LoopKit/Loop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoopCompletionFreshness.swift
More file actions
52 lines (44 loc) · 1.18 KB
/
LoopCompletionFreshness.swift
File metadata and controls
52 lines (44 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
// LoopCompletionFreshness.swift
// Loop
//
// Created by Pete Schwamb on 1/17/20.
// Copyright © 2020 LoopKit Authors. All rights reserved.
//
import Foundation
public enum LoopCompletionFreshness {
case fresh
case aging
case stale
public var maxAge: TimeInterval? {
switch self {
case .fresh:
return TimeInterval(minutes: 6)
case .aging:
return TimeInterval(minutes: 16)
case .stale:
return nil
}
}
public init(age: TimeInterval?) {
guard let age = age else {
self = .stale
return
}
switch age {
case let t where t <= LoopCompletionFreshness.fresh.maxAge!:
self = .fresh
case let t where t <= LoopCompletionFreshness.aging.maxAge!:
self = .aging
default:
self = .stale
}
}
public init(lastCompletion: Date?, at date: Date = Date()) {
guard let lastCompletion = lastCompletion else {
self = .stale
return
}
self = LoopCompletionFreshness(age: date.timeIntervalSince(lastCompletion))
}
}