Skip to content

Commit 658ff1c

Browse files
committed
impl
1 parent 33d6c0e commit 658ff1c

File tree

8 files changed

+35
-17
lines changed

8 files changed

+35
-17
lines changed

Sources/PDKit/Protocols/Schedules/PillScheduling.swift

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public protocol PillScheduling: Schedule, Deleting {
1919
/// The due count.
2020
var totalDue: Int { get }
2121

22+
/// Force reloads data from storage.
23+
func reloadContext()
24+
2225
/// Insert a new pill into the schedule.
2326
@discardableResult
2427
func insertNew(onSuccess: (() -> Void)?) -> Swallowable?

Sources/PDMock/Mocks/MockSchedules/MockPillSchedule.swift

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public class MockPillSchedule: PillScheduling {
2323

2424
public var totalDue: Int = 0
2525

26+
public var reloadContextCallCount = 0
27+
public func reloadContext() {
28+
reloadContextCallCount += 1
29+
}
30+
2631
public var insertNewCallArgs: [(() -> Void)?] = []
2732
public var insertNewReturnValue: Swallowable?
2833
public func insertNew(onSuccess: (() -> Void)?) -> Swallowable? {

Sources/PatchData/Schedules/HormoneSchedule.swift

+2-6
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class HormoneSchedule: NSObject, HormoneScheduling {
2929
self.store = store
3030
self.dataSharer = hormoneDataSharer
3131
self.settings = settings
32-
self.context = HormoneSchedule.getHormoneList(from: store, settings: settings)
32+
self.context = store.getStoredHormones(settings)
3333
super.init()
3434
resetIfEmpty()
3535
shareData()
@@ -57,7 +57,7 @@ public class HormoneSchedule: NSObject, HormoneScheduling {
5757
}
5858

5959
public func reloadContext() {
60-
self.context = HormoneSchedule.getHormoneList(from: store, settings: settings)
60+
self.context = store.getStoredHormones(settings)
6161
}
6262

6363
@discardableResult
@@ -222,10 +222,6 @@ public class HormoneSchedule: NSObject, HormoneScheduling {
222222
store.pushLocalChangesToManagedContext([hormone], doSave: true)
223223
}
224224

225-
private static func getHormoneList(from store: HormoneStoring, settings: UserDefaultsReading) -> [Hormonal] {
226-
store.getStoredHormones(settings)
227-
}
228-
229225
private func pushFromDateAndSiteChange(_ hormone: Hormonal) {
230226
store.pushLocalChangesToManagedContext([hormone], doSave: true)
231227
shareData()

Sources/PatchData/Schedules/PillSchedule.swift

+4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public class PillSchedule: NSObject, PillScheduling {
6262
}
6363
}
6464

65+
public func reloadContext() {
66+
self.context = store.getStoredPills()
67+
}
68+
6569
// MARK: - Override base class
6670

6771
@discardableResult

Sources/PatchDay/Pills/Alerts/PillCellActionAlert.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class PillCellActionAlert: PDAlert {
3030
private var takeAction: UIAlertAction? {
3131
!pill.isDone ? UIAlertAction(title: ActionStrings.Take, style: .default) {
3232
_ in self.handlers.takePill()
33-
} : nil
33+
} : nil
3434
}
3535

3636
override func present() {

Sources/PatchDay/Pills/PillsViewModel.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ class PillsViewModel: CodeBehindDependencies<PillsViewModel>, PillsViewModelProt
7575
at index: Index, viewController: UIViewController, takePillCompletion: @escaping () -> Void
7676
) {
7777
guard let pill = sdk?.pills[index] else { return }
78-
let goToDetails = { self.goToPillDetails(pillIndex: index, pillsViewController: viewController) }
78+
let goToDetails = {
79+
self.goToPillDetails(pillIndex: index, pillsViewController: viewController)
80+
}
7981
let takePill = {
8082
self.takePill(at: index)
8183
takePillCompletion()

Sources/PatchDay/Tabs/TabReflector.swift

+10-9
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,31 @@ class TabReflector: TabReflective {
3838

3939
func reflectHormones() {
4040
guard let sdk = sdk else { return }
41-
guard let hormonesVC = hormonesViewController else { return }
41+
guard let hormonesViewController = hormonesViewController else { return }
4242
sdk.hormones.reloadContext()
4343
let method = sdk.settings.deliveryMethod.value
4444
let icon = PDIcons[method]
4545
let expiredCount = sdk.hormones.totalExpired
4646
let title = PDTitleStrings.Hormones[method]
4747
let item = UITabBarItem(title: title, image: icon, selectedImage: icon)
4848
item.badgeValue = expiredCount > 0 ? "\(expiredCount)" : nil
49-
hormonesVC.title = title
50-
hormonesVC.tabBarItem = nil // Set to nil first to force redraw
51-
hormonesVC.tabBarItem = item
52-
hormonesVC.awakeFromNib()
49+
hormonesViewController.title = title
50+
hormonesViewController.tabBarItem = nil // Set to nil first to force redraw
51+
hormonesViewController.tabBarItem = item
52+
hormonesViewController.awakeFromNib()
5353
}
5454

5555
func reflectPills() {
56-
guard let pillsVC = pillsViewController else { return }
56+
guard let pillsViewController = pillsViewController else { return }
5757
guard let sdk = sdk else { return }
58-
guard let item = pillsVC.tabBarItem else { return }
58+
guard let item = pillsViewController.tabBarItem else { return }
59+
sdk.pills.reloadContext()
5960
let expiredCount = sdk.pills.totalDue
6061
item.badgeValue = expiredCount > 0 ? "\(expiredCount)" : nil
6162
let log = PDLog<TabReflector>()
6263
log.info("Settings pills tab to \(item.badgeValue ?? "nil")")
63-
pillsVC.tabBarItem = item
64-
pillsVC.awakeFromNib()
64+
pillsViewController.tabBarItem = item
65+
pillsViewController.awakeFromNib()
6566
}
6667

6768
private func loadViewControllerTabTextAttributes() {

Tests/PatchDayTests/Tabs/TabReflectorTests.swift

+7
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ class TabReflectorTests: XCTestCase {
6060
XCTAssertNil(hormonesViewController.tabBarItem.badgeValue)
6161
}
6262

63+
func testReflectPills_resetsContext() {
64+
let tabs = createTabs()
65+
tabs.reflectPills()
66+
let actual = (sdk.pills as! MockPillSchedule).reloadContextCallCount
67+
XCTAssertEqual(1, actual)
68+
}
69+
6370
func testReflectPills_whenZeroTotalDue_doesNotReflectBadgeValue() {
6471
let pills = sdk.pills as! MockPillSchedule
6572
pills.totalDue = 0

0 commit comments

Comments
 (0)