Base fixes and test harness

This commit is contained in:
alexandrev-tibco
2026-02-01 11:12:57 +01:00
parent f5b13ec924
commit e328767c4a
39 changed files with 3575 additions and 142 deletions
@@ -6,6 +6,16 @@ enum CurrencyFormatter {
return AppSettings.getOrCreate(in: context).currency
}
static func locale(for currencyCode: String?) -> Locale {
guard let currencyCode, !currencyCode.isEmpty else { return Locale.current }
if let match = Locale.availableIdentifiers.first(where: {
Locale(identifier: $0).currency?.identifier == currencyCode
}) {
return Locale(identifier: match)
}
return Locale.current
}
static func format(_ decimal: Decimal, style: NumberFormatter.Style = .currency, maximumFractionDigits: Int = 2) -> String {
let formatter = NumberFormatter()
formatter.numberStyle = style
@@ -14,6 +24,21 @@ enum CurrencyFormatter {
return formatter.string(from: decimal as NSDecimalNumber) ?? "\(decimal)"
}
static func format(
_ decimal: Decimal,
currencyCode: String?,
style: NumberFormatter.Style = .currency,
maximumFractionDigits: Int = 2,
preferredLocale: Locale? = nil
) -> String {
let formatter = NumberFormatter()
formatter.numberStyle = style
formatter.currencyCode = currencyCode ?? currentCurrencyCode()
formatter.maximumFractionDigits = maximumFractionDigits
formatter.locale = preferredLocale ?? locale(for: currencyCode)
return formatter.string(from: decimal as NSDecimalNumber) ?? "\(decimal)"
}
static func symbol(for code: String) -> String {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
@@ -74,9 +74,39 @@ enum MonthlyCheckInStore {
}
static func setCompletionDate(_ completionDate: Date, for month: Date) {
updateEntry(for: month) { entry in
entry.completionTime = completionDate.timeIntervalSince1970
let targetMonth = month.startOfMonth
let targetKey = monthKey(for: targetMonth)
var entries = loadEntries()
var didChange = false
// Ensure the target month entry exists.
var targetEntry = entries[targetKey] ?? MonthlyCheckInEntry(
note: nil,
rating: nil,
mood: nil,
completionTime: legacyCompletion(for: targetKey),
createdAt: Date().timeIntervalSince1970
)
targetEntry.completionTime = completionDate.timeIntervalSince1970
entries[targetKey] = targetEntry
didChange = true
// Mark all previous pending entries as completed as well.
for (key, entry) in entries {
guard let entryMonth = monthFormatter.date(from: key)?.startOfMonth else { continue }
guard entryMonth < targetMonth else { continue }
guard entry.completionTime == nil else { continue }
var updated = entry
let fallbackCompletion = min(entryMonth.endOfMonth, completionDate)
updated.completionTime = fallbackCompletion.timeIntervalSince1970
entries[key] = updated
didChange = true
}
guard didChange else { return }
saveEntries(entries)
persistLegacyMirrors(entries)
}
static func latestCompletionDate() -> Date? {