Skip to content

Commit f23a568

Browse files
authored
Bugfixes (#47)
1 parent 08361af commit f23a568

36 files changed

+408
-175
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
**/xcuserdata/
77

88
# Test results files
9-
*result.yml
9+
result.xml
1010

1111
# Files
1212
*.cer

CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ Added
1414
number of days and then stop taking the pill for a certain number of days. You can set the "Days on" and
1515
"Days off" properties accordingly to any numbers 1 - 25.
1616

17+
Fixed
18+
19+
- Issue where you could not take a newly added pill.
20+
- Issue that causes the app to crash in situations when creating a new pill or site, then backgrounding the app, then
21+
foregrounding again.
22+
- Issue where an arrow component in the Site Schedule View would not hide during editing mode.
23+
- Bug where if you create a site when you don't have any sites in the Site Schedule View, it would not load some
24+
of the components in the cell.
25+
- Bug preventing reordering sites.
26+
1727
## 3.2.3
1828

1929
Fixed

Sources/PDKit/PDTO.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ public struct SiteCellProperties {
134134
}
135135
public var row: Index
136136
public var site: Bodily?
137-
public var totalSiteCount: Int = 0
138-
public var nextSiteIndex: Int = 0
137+
public var totalSiteCount = 0
138+
public var nextSiteIndex = 0
139139
}
140140

141141
public struct BarItemInitializationProperties {

Sources/PDKit/Pills/PillAttributes.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public class PillAttributes {
5050
_expirationInterval = PillExpirationInterval(nil, xDays: nil)
5151
}
5252

53-
/// Returns true if these attributes contain any non-nil attributes. Optionally pass in exceptions and it will also make sure
53+
/// Returns true if these attributes contain any non-nil attributes.
54+
/// Optionally pass in exceptions and it will also make sure
5455
/// the attribute is not equal to the property in the exceptions.
5556
public func anyAttributeExists(exclusions: PillAttributes? = nil) -> Bool {
5657
let exclusions = exclusions != nil ? exclusions! : PillAttributes()

Sources/PDKit/Pills/PillExpirationInterval.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public class PillExpirationInterval {
2424
}
2525

2626
/// The normal initializer that would exist if we did not have to include migrations.
27-
/// It will take a `xDays` value and convert it to the days integer values. Call `.xDays` to form the new value for storing.
27+
/// It will take a `xDays` value and convert it to the days integer values.
28+
/// Call `.xDays`to form the new value for storing.
2829
public init(_ value: PillExpirationIntervalSetting?, xDays: String?) {
2930
self._value = value
3031
if let xDaysValue = xDays, let val = value, _xDaysIntervals.contains(val) {

Sources/PDKit/Pills/PillExpirationIntervalXDays.swift

+16
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ public class PillExpirationIntervalXDays {
6363
// setting to nil is allowed
6464
_one = newValue
6565
}
66+
67+
// Set position to 1 if we are lowering the limit
68+
guard let isOn = isOn, isOn else { return }
69+
guard let one = _one else { return }
70+
guard let pos = position else { return }
71+
if one < pos {
72+
position = one
73+
}
6674
}
6775
}
6876

@@ -77,6 +85,14 @@ public class PillExpirationIntervalXDays {
7785
// setting to nil is allowed
7886
_two = newValue
7987
}
88+
89+
// Set position to 1 if we are lowering the limit
90+
guard let isOn = isOn, !isOn else { return }
91+
guard let two = _two else { return }
92+
guard let pos = position else { return }
93+
if two < pos {
94+
position = two
95+
}
8096
}
8197
}
8298

Sources/PDKit/Protocols/UI/Pills/PillCellProtocol.swift

-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@ import Foundation
88

99
public protocol PillCellProtocol {
1010
@discardableResult func configure(_ params: PillCellConfigurationParameters) -> PillCellProtocol
11-
@discardableResult func stamp() -> PillCellProtocol
1211
func loadBackground()
1312
}

Sources/PDKit/Protocols/UI/Pills/PillDetailViewModelProtocol.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public protocol PillDetailViewModelProtocol {
1212
var index: Index { get }
1313

1414
/// The view model's `Pill`.
15-
var pill: Swallowable { get }
15+
var pill: Swallowable? { get }
1616

1717
/// The user selections from the UI.
1818
var selections: PillAttributes { get }
@@ -113,7 +113,8 @@ public protocol PillDetailViewModelProtocol {
113113
/// Select a days value for either `daysOne`, `daysTwo`, or the `position`, depending on the given days number.
114114
func selectFromDaysPicker(_ row: Index, daysNumber: Int?)
115115

116-
/// Returns either the days options or the positions options, based on the picker number. [1,2] -> days, 0 -> positions.
116+
/// Returns either the days options or the positions options,
117+
/// based on the picker number. [1,2] -> days, 0 -> positions.
117118
func getOptionsForSelectedPicker(_ pickerNumber: Int) -> [String]
118119

119120
/// Enable or disable the provided pickers, based on `pill` data.

Sources/PDKit/Protocols/UI/Sites/SiteCellViewModelProtocol.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ import Foundation
99
public protocol SiteCellViewModelProtocol {
1010
var siteNameText: String { get }
1111
var orderText: String { get }
12-
func getVisibilityBools(cellIsInEditMode: Bool) -> (showNext: Bool, showOrder: Bool)
12+
func getVisibilityBools(
13+
cellIsInEditMode: Bool) -> (hideNext: Bool, hideOrder: Bool, hideArrow: Bool
14+
)
1315
}

Sources/PDKit/Protocols/UI/Sites/SiteDetailViewModelProtocol.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import Foundation
99
public protocol SiteDetailViewModelProtocol {
1010
var selections: SiteSelectionState { get set }
1111
var imagePickerDelegate: SiteImagePicker? { get }
12-
var siteDetailViewControllerTitle: String { get }
13-
var siteName: SiteName { get }
12+
var siteName: SiteName? { get }
1413
var sitesCount: Int { get }
1514
var siteNameOptions: [SiteName] { get }
1615
var siteNamePickerStartIndex: Index { get }

Sources/PDKit/Protocols/UI/Sites/SitesTableProtocol.swift

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public protocol SitesTableProtocol {
1414
func reloadCells()
1515
subscript(index: Index) -> SiteCellProtocol { get }
1616
func toggleEdit(isEditing: Bool)
17-
func turnOffEditingMode()
1817
func deleteCell(indexPath: IndexPath)
1918
func reloadData()
2019
}

Sources/PDMock/Mocks/MockApp/Pills/MockPillCell.swift

-6
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ public class MockPillCell: PillCellProtocol {
1515
return self
1616
}
1717

18-
public var stampCallCount = 0
19-
public func stamp() -> PillCellProtocol {
20-
stampCallCount += 1
21-
return self
22-
}
23-
2418
public var loadBackgroundCallCount = 0
2519
public func loadBackground() {
2620
loadBackgroundCallCount += 1

Sources/PDMock/Mocks/MockApp/Sites/MockSitesTable.swift

-5
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ public class MockSitesTable: SitesTableProtocol {
3939
toggleEditCallArgs.append(isEditing)
4040
}
4141

42-
public var turnOffEditingModeCallCount = 0
43-
public func turnOffEditingMode() {
44-
turnOffEditingModeCallCount += 1
45-
}
46-
4742
public var deleteCellCallArgs: [IndexPath] = []
4843
public func deleteCell(indexPath: IndexPath) {
4944
deleteCellCallArgs.append(indexPath)

Sources/PatchData/Objects/Pill.swift

+33-17
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,27 @@ public class Pill: Swallowable {
116116

117117
public func set(attributes: PillAttributes) {
118118
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+
}
119133
}
120134

121135
public func swallow() {
122136
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 {
124140
pillData.attributes.expirationInterval.startPositioning()
125141
}
126142
let currentTimesTaken = pillData.attributes.timesTakenToday ?? 0
@@ -232,30 +248,30 @@ public class Pill: Swallowable {
232248

233249
private var dueDateForXDaysOnXDaysOff: Date? {
234250
/*
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 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
237253

238-
# Constants
254+
# Constants
239255

240-
On = X, Off = O
241-
Span = len(O[]) = 9
256+
On = X, Off = O
257+
Span = len(O[]) = 9
242258

243-
# Variables
259+
# Variables
244260

245-
Pos = P = 2 // Change position to calculate next due date
261+
Pos = P = 2 // Change position to calculate next due date
246262

247-
# Evaluation
263+
# Evaluation
248264

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
254270

255-
# Conclusion
271+
# Conclusion
256272

257-
We are 8 days away from the next due date.
258-
*/
273+
We are 8 days away from the next due date.
274+
*/
259275
guard let isOn = expirationInterval.xDaysIsOn else { return nil }
260276
guard let pos = expirationInterval.xDaysPosition else { return nil }
261277
guard let onSpan = expirationInterval.daysOne else { return nil }

Sources/PatchData/Schedules/PillSchedule.swift

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public class PillSchedule: NSObject, PillScheduling {
135135
shareData()
136136
}
137137

138+
/// Awaken the properties that are relevant to the current date and time.
138139
private func awaken() {
139140
for pill in all {
140141
pill.awaken()

Sources/PatchData/Schedules/SiteSchedule.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ public class SiteSchedule: NSObject, SiteScheduling {
3232

3333
public var count: Int { all.count }
3434

35-
public var all: [Bodily] { context }
35+
public var all: [Bodily] {
36+
sort()
37+
return context
38+
}
3639

3740
public var suggested: Bodily? {
3841
guard count > 0 else { return nil }
@@ -86,7 +89,7 @@ public class SiteSchedule: NSObject, SiteScheduling {
8689
site.order = count
8790
context.append(site)
8891
onSuccess?()
89-
save() // Save name and order
92+
save()
9093
return site
9194
}
9295
return nil

Sources/PatchDay/Hormones/Details/HormoneDetailViewModel.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class HormoneDetailViewModel: CodeBehindDependencies<HormoneDetailViewModel>, Ho
9595
guard let site = getSite() else { return 0 }
9696
let order = site.order
9797
let end = siteCount
98-
if order >= 1 && order <= end {
98+
if order >= 0 && order < end {
9999
selections.site = site
100100
return order
101101
}

Sources/PatchDay/Pills/Cell/PillCell.swift

-6
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ class PillCell: TableCell, PillCellProtocol {
3535
return self
3636
}
3737

38-
/// Set the "last taken" label to the current date as a string.
39-
@discardableResult func stamp() -> PillCellProtocol {
40-
lastTakenLabel?.text = PDDateFormatter.formatDate(Date())
41-
return self
42-
}
43-
4438
func loadBackground() {
4539
backgroundColor = UIColor.systemBackground
4640
let backgroundView = UIView()

Sources/PatchDay/Pills/Details/PillDetailViewController.swift

+9
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,15 @@ class PillDetailViewController: UIViewController, UIPickerViewDelegate, UIPicker
291291
}
292292

293293
private func reflectPillAttributes() {
294+
guard viewModel.pill != nil else {
295+
// Remove outdated observer
296+
NotificationCenter.default.removeObserver(
297+
self,
298+
name: UIApplication.willEnterForegroundNotification,
299+
object: nil
300+
)
301+
return
302+
}
294303
loadName()
295304
loadTimes()
296305
loadNotify()

0 commit comments

Comments
 (0)