-
Notifications
You must be signed in to change notification settings - Fork 1
Animate and haptically signal updated location counts #192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import RegionKit | ||
| import Testing | ||
| @testable import WhereCore | ||
|
|
||
| struct WherePreferencesTests { | ||
| private func preferences() -> WherePreferences { | ||
| WherePreferences(store: InMemoryKeyValueStore()) | ||
| } | ||
|
|
||
| @Test func firstInstallDefaults() { | ||
| let preferences = preferences() | ||
|
|
||
| #expect(preferences.hasOnboarded == false) | ||
| #expect(preferences.wantsTracking) | ||
| #expect(preferences.remindersEnabled) | ||
| #expect(preferences.reminderTime == .defaultEvening) | ||
| #expect(preferences.summaryEnabled) | ||
| #expect(preferences.summaryTime == .defaultMorning) | ||
| #expect(preferences.issueAlertsEnabled) | ||
| #expect(preferences.driftThresholdMeters == DriftThreshold.default.rawValue) | ||
| #expect(preferences.lastSeenLocationDayCounts(in: 2026) == nil) | ||
| } | ||
|
|
||
| @Test func locationDayCountsRoundTripIndependentlyByYear() { | ||
| let preferences = preferences() | ||
| let counts2025: [Region: Int] = [.california: 42, .other: 3] | ||
| let counts2026: [Region: Int] = [.newYork: 81, .europeanUnion: 7] | ||
|
|
||
| preferences.setLastSeenLocationDayCounts(counts2025, in: 2025) | ||
| preferences.setLastSeenLocationDayCounts(counts2026, in: 2026) | ||
|
|
||
| #expect(preferences.lastSeenLocationDayCounts(in: 2025) == counts2025) | ||
| #expect(preferences.lastSeenLocationDayCounts(in: 2026) == counts2026) | ||
| } | ||
|
|
||
| @Test func resetRestoresEveryDefaultAndClearsLocationCounts() { | ||
| let preferences = preferences() | ||
| preferences.hasOnboarded = true | ||
| preferences.wantsTracking = false | ||
| preferences.remindersEnabled = false | ||
| preferences.reminderTime = ReminderTime(hour: 9, minute: 15) | ||
| preferences.summaryEnabled = false | ||
| preferences.summaryTime = ReminderTime(hour: 17, minute: 45) | ||
| preferences.issueAlertsEnabled = false | ||
| preferences.driftThresholdMeters = 25000 | ||
| preferences.setLastSeenLocationDayCounts([.california: 100], in: 2026) | ||
|
|
||
| preferences.reset() | ||
|
|
||
| #expect(preferences.hasOnboarded == false) | ||
| #expect(preferences.wantsTracking) | ||
| #expect(preferences.remindersEnabled) | ||
| #expect(preferences.reminderTime == .defaultEvening) | ||
| #expect(preferences.summaryEnabled) | ||
| #expect(preferences.summaryTime == .defaultMorning) | ||
| #expect(preferences.issueAlertsEnabled) | ||
| #expect(preferences.driftThresholdMeters == DriftThreshold.default.rawValue) | ||
| #expect(preferences.lastSeenLocationDayCounts(in: 2026) == nil) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
Where/WhereUI/Sources/Primary/LocationDayCountPresentationModel.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import Observation | ||
| import RegionKit | ||
| import WhereCore | ||
|
|
||
| /// Holds the Location cards at the counts the user last saw until their visible | ||
| /// card surface can reconcile to the current report and signal the change once. | ||
| @MainActor | ||
| @Observable | ||
| final class LocationDayCountPresentationModel { | ||
| /// Everything that decides whether SwiftUI should restart the card-surface | ||
| /// reconciliation task. | ||
| struct ReconciliationID: Hashable { | ||
| let counts: [RegionDays] | ||
| let year: Int | ||
| let isVisible: Bool | ||
| } | ||
|
|
||
| private let preferences: WherePreferences | ||
| private var lastSeenCounts: [Region: Int]? | ||
| private var displayedCounts: [Region: Int] | ||
| private(set) var year: Int | ||
| private(set) var feedbackTrigger = 0 | ||
|
|
||
| init(preferences: WherePreferences, year: Int) { | ||
| self.preferences = preferences | ||
| self.year = year | ||
| let savedCounts = preferences.lastSeenLocationDayCounts(in: year) | ||
| lastSeenCounts = savedCounts | ||
| displayedCounts = savedCounts ?? [:] | ||
| } | ||
|
|
||
| /// Load another year's baseline without marking its current report as seen. | ||
| /// `LocationsView` calls this while a year change is happening on another | ||
| /// tab, so the saved values are ready before the cards become visible. | ||
| func prepare(for year: Int) { | ||
| guard year != self.year else { return } | ||
| self.year = year | ||
| let savedCounts = preferences.lastSeenLocationDayCounts(in: year) | ||
| lastSeenCounts = savedCounts | ||
| displayedCounts = savedCounts ?? [:] | ||
| } | ||
|
|
||
| /// The card value to render before the visible surface reconciles. A card | ||
| /// absent from the previous snapshot starts at its current value rather than | ||
| /// inventing a zero the user never saw. | ||
| func presented(_ current: RegionDays) -> RegionDays { | ||
| RegionDays( | ||
| region: current.region, | ||
| days: displayedCounts[current.region] ?? current.days, | ||
| ) | ||
| } | ||
|
|
||
| /// When the cards are visible, advance them to the report, persist that | ||
| /// presentation, and emit one feedback trigger if any comparable saved value | ||
| /// increased. Hidden or obscured surfaces leave their baseline untouched. | ||
| func reconcile(_ current: [RegionDays], in year: Int, isVisible: Bool) { | ||
| guard isVisible else { return } | ||
| prepare(for: year) | ||
|
|
||
| let currentCounts = Dictionary(uniqueKeysWithValues: current.map { item in | ||
| (item.region, item.days) | ||
| }) | ||
| let shouldProvideFeedback = current.contains { item in | ||
| guard let previousDays = lastSeenCounts?[item.region] else { return false } | ||
| return previousDays < item.days | ||
| } | ||
|
|
||
| if displayedCounts != currentCounts { | ||
| displayedCounts = currentCounts | ||
| } | ||
| if lastSeenCounts != currentCounts { | ||
| preferences.setLastSeenLocationDayCounts(currentCounts, in: year) | ||
| lastSeenCounts = currentCounts | ||
| } | ||
| if shouldProvideFeedback { | ||
| feedbackTrigger += 1 | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets ensure this only happens for increases, not just changes.