-
Notifications
You must be signed in to change notification settings - Fork 22
/
SymptomFlow.swift
86 lines (72 loc) · 2.09 KB
/
SymptomFlow.swift
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import Foundation
import RxSwift
class SymptomFlow {
private var steps: [SymptomStep]
init(steps: [SymptomStep]) {
if steps.isEmpty {
fatalError("Symptoms steps must not be empty")
}
self.steps = steps
currentStep = self.steps.first!
}
private(set) var currentStep: SymptomStep
func previous() -> SymptomStep? {
updateStep(incr: -1)
}
func next() -> SymptomStep? {
updateStep(incr: 1)
}
private func updateStep(incr: Int) -> SymptomStep? {
let step = steps[safe: steps.firstIndex(of: currentStep)! + incr]
if let step = step {
currentStep = step
}
return step
}
static func create(symptomIds: [SymptomId]) -> SymptomFlow? {
if symptomIds.isEmpty {
log.d("Symptoms ids empty")
return nil
}
let steps = toSteps(symptomIds: symptomIds)
if steps.isEmpty {
log.d("Symptoms have no steps. Not creating a flow.")
return nil
}
return SymptomFlow(steps: steps)
}
}
private func toSteps(symptomIds: [SymptomId]) -> [SymptomStep] {
if symptomIds.contains(.none), symptomIds.count > 1 {
fatalError("There must be no other symptoms selected when .none is selected")
}
if symptomIds != [.none] {
return symptomIds.flatMap { $0.toSteps() } // + [.earliestSymptomDate]
} else {
return []
}
}
private extension SymptomId {
func toSteps() -> [SymptomStep] {
switch self {
case .cough: return [
.coughType,
.coughDescription,
]
case .breathlessness: return [
.breathlessnessDescription,
]
case .fever: return [
.feverTemperatureTakenToday,
.feverTemperatureSpot,
.feverHighestTemperature,
]
case .muscleAches: return []
case .lossSmellOrTaste: return []
case .diarrhea: return []
case .runnyNose: return []
case .other: return []
case .none: return []
}
}
}