@@ -116,11 +116,27 @@ public class Pill: Swallowable {
116
116
117
117
public func set( attributes: PillAttributes ) {
118
118
pillData. attributes. update ( attributes)
119
+
120
+ // Prevent pills with 0 set times
121
+ if timesaday == 0 {
122
+ let timeString = DefaultPillAttributes . time
123
+ let defaultTime = DateFactory . createTimesFromCommaSeparatedString ( timeString, now: _now)
124
+ self . appendTime ( defaultTime [ 0 ] )
125
+ }
126
+
127
+ let interval = attributes. expirationInterval
128
+ let wasGivenPosition = interval. xDaysPosition != nil || interval. xDaysIsOn != nil
129
+ if wasGivenPosition && lastTaken == nil {
130
+ // Set to arbitrary date in the past so it appears the schedule is in-progress.
131
+ lastTaken = DateFactory . createDate ( byAddingHours: - 24 , to: now)
132
+ }
119
133
}
120
134
121
135
public func swallow( ) {
122
136
guard timesTakenToday < timesaday || lastTaken == nil else { return }
123
- if lastTaken == nil && expirationInterval. value == . XDaysOnXDaysOff {
137
+ if lastTaken == nil
138
+ && expirationInterval. value == . XDaysOnXDaysOff
139
+ && pillData. attributes. expirationInterval. xDaysIsOn == nil {
124
140
pillData. attributes. expirationInterval. startPositioning ( )
125
141
}
126
142
let currentTimesTaken = pillData. attributes. timesTakenToday ?? 0
@@ -232,30 +248,30 @@ public class Pill: Swallowable {
232
248
233
249
private var dueDateForXDaysOnXDaysOff : Date ? {
234
250
/*
235
- X X X X X O O O O O O O O O X X X X X O O O O O O O O O
236
- _ _ _ _ _ _ P _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
251
+ X X X X X O O O O O O O O O X X X X X O O O O O O O O O
252
+ _ _ _ _ _ _ P _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
237
253
238
- # Constants
254
+ # Constants
239
255
240
- On = X, Off = O
241
- Span = len(O[]) = 9
256
+ On = X, Off = O
257
+ Span = len(O[]) = 9
242
258
243
- # Variables
259
+ # Variables
244
260
245
- Pos = P = 2 // Change position to calculate next due date
261
+ Pos = P = 2 // Change position to calculate next due date
246
262
247
- # Evaluation
263
+ # Evaluation
248
264
249
- Next = SPAN + 1 // The next start of "on" from "off" position 1
250
- Next = 10 // Eval
251
- Diff = Next - Pos // The amount we are away from Next
252
- Diff = 10 - 2 // Eval
253
- Diff = 8 // Eval
265
+ Next = SPAN + 1 // The next start of "on" from "off" position 1
266
+ Next = 10 // Eval
267
+ Diff = Next - Pos // The amount we are away from Next
268
+ Diff = 10 - 2 // Eval
269
+ Diff = 8 // Eval
254
270
255
- # Conclusion
271
+ # Conclusion
256
272
257
- We are 8 days away from the next due date.
258
- */
273
+ We are 8 days away from the next due date.
274
+ */
259
275
guard let isOn = expirationInterval. xDaysIsOn else { return nil }
260
276
guard let pos = expirationInterval. xDaysPosition else { return nil }
261
277
guard let onSpan = expirationInterval. daysOne else { return nil }
0 commit comments