-
Notifications
You must be signed in to change notification settings - Fork 0
Add privacy-safe sharing and report Shortcut #9
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
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import Foundation | ||
|
|
||
| @MainActor | ||
| enum ReviewRequestGate { | ||
| static let lastRequestedVersionKey = "hourleaf.review.lastRequestedVersion" | ||
|
|
||
| @discardableResult | ||
| static func requestIfEligible( | ||
| bundle: Bundle = .main, | ||
| defaults: UserDefaults = .standard, | ||
| request: () -> Void | ||
| ) -> Bool { | ||
| requestIfEligible( | ||
| version: bundle.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String, | ||
| defaults: defaults, | ||
| request: request | ||
| ) | ||
| } | ||
|
|
||
| @discardableResult | ||
| static func requestIfEligible( | ||
| version: String?, | ||
| defaults: UserDefaults = .standard, | ||
| request: () -> Void | ||
| ) -> Bool { | ||
| guard let version, !version.isEmpty else { return false } | ||
| guard defaults.string(forKey: lastRequestedVersionKey) != version else { return false } | ||
|
|
||
| // Record before handing control to StoreKit so a repeated completion or | ||
| // a suppressed system request cannot ask again for this app version. | ||
| defaults.set(version, forKey: lastRequestedVersionKey) | ||
| request() | ||
| return true | ||
| } | ||
| } |
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,97 @@ | ||
| import AppIntents | ||
| import Foundation | ||
|
|
||
| enum PrepareMonthlyReportIntentError: LocalizedError, Equatable, Sendable { | ||
| case noDraft | ||
| case changed | ||
| case unavailable | ||
|
|
||
| var errorDescription: String? { | ||
| switch self { | ||
| case .noDraft: | ||
| String(localized: "intent.report.no_draft") | ||
| case .changed: | ||
| String(localized: "intent.report.changed") | ||
| case .unavailable: | ||
| String(localized: "intent.report.unavailable") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| struct PrepareMonthlyReportIntent: AppIntent { | ||
| static var title: LocalizedStringResource { | ||
| "intent.shortcut.prepare_report" | ||
| } | ||
|
|
||
| static var description: IntentDescription { | ||
| IntentDescription("intent.report.description") | ||
| } | ||
|
|
||
| static var openAppWhenRun: Bool { false } | ||
| static var isDiscoverable: Bool { true } | ||
| static var authenticationPolicy: IntentAuthenticationPolicy { | ||
| .requiresLocalDeviceAuthentication | ||
| } | ||
|
|
||
| @Parameter(title: "intent.report.month", kind: .date) | ||
| var month: Date? | ||
|
|
||
| @AppDependency private var repository: CoreDataLedgerRepository | ||
|
|
||
| init() { | ||
| _repository = AppDependency() | ||
| } | ||
|
|
||
| init( | ||
| month: Date? = nil, | ||
| dependencyManager: AppDependencyManager = .shared | ||
| ) { | ||
| self.month = month | ||
| _repository = AppDependency(manager: dependencyManager) | ||
| } | ||
|
|
||
| static var parameterSummary: some ParameterSummary { | ||
| Summary("intent.report.summary") { | ||
| \.$month | ||
| } | ||
| } | ||
|
|
||
| func perform() async throws -> some IntentResult & ReturnsValue<String> & ProvidesDialog { | ||
| let text = try await prepare(using: repository, now: .now) | ||
| return .result(value: text, dialog: IntentDialog("intent.report.success")) | ||
| } | ||
|
|
||
| /// Reads the existing report projection twice around formatting. The | ||
| /// equality check proves this read-only action did not cross a mutation | ||
| /// boundary while it prepared the text. | ||
| func prepare( | ||
| using repository: CoreDataLedgerRepository, | ||
| now: Date | ||
| ) async throws -> String { | ||
| let before: LedgerSnapshot | ||
| do { | ||
| before = try await repository.ledgerSnapshot() | ||
| } catch { | ||
| throw PrepareMonthlyReportIntentError.unavailable | ||
| } | ||
|
|
||
| let requestedMonth = MonthKey(month ?? now, calendar: .hourleaf) | ||
| let draft = ReportReadiness.draft(for: requestedMonth, in: before) | ||
|
|
||
| let after: LedgerSnapshot | ||
| do { | ||
| after = try await repository.ledgerSnapshot() | ||
| } catch { | ||
| throw PrepareMonthlyReportIntentError.unavailable | ||
| } | ||
| guard before == after else { | ||
| throw PrepareMonthlyReportIntentError.changed | ||
| } | ||
|
|
||
| guard let draft else { | ||
| throw PrepareMonthlyReportIntentError.noDraft | ||
| } | ||
|
|
||
| return draft.text | ||
| } | ||
| } | ||
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
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.
When the Shortcut supplies a date after the current month,
ReportReadiness.draftstill calculates a projection and the intent returns it with the success dialog, potentially presenting carry-forward or zero totals as a completed report for a period that has not occurred. The normal Progress flow prevents navigation beyond the current month (ProgressScreen.swift:127) and the persisted report lifecycle rejects current or future months (LedgerRepository.swift:882-887), so this path should validaterequestedMonthagainstnowbefore returning report text.Useful? React with 👍 / 👎.