Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP ID & Card Vault7// File : SPVault/Features/Expirations/ExpirationSummary.swift8// Module : SPVault9// Repository : https://git.spboucher.ai/sp-id-card-vault10// Project Website : Not specified11// Created : 2026-12-0212// Last Modified : 2026-12-0213//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation4243/// Pure grouping of cards by expiry, for the dashboard.44struct ExpirationSummary: Equatable, Sendable {45 /// Expired first (most recently expired on top), by date.46 var expired: [Card]47 /// Soonest first.48 var expiringSoon: [Card]49 /// Soonest first.50 var valid: [Card]51 var noExpiry: [Card]5253 /// Documents to renew: everything expired or expiring soon, most urgent first.54 var toRenew: [Card] { expired + expiringSoon }55 var datedCount: Int { expired.count + expiringSoon.count + valid.count }56 var isEmpty: Bool { datedCount == 0 && noExpiry.isEmpty }57 /// The next card that will expire, if any.58 var next: Card? { expiringSoon.first ?? valid.first }5960 init(cards: [Card], now: Date = Date(), soonWithinDays: Int = 90) {61 var expired: [Card] = [], soon: [Card] = [], valid: [Card] = [], none: [Card] = []62 for card in cards {63 switch card.expiryStatus(now: now, soonWithinDays: soonWithinDays) {64 case .expired: expired.append(card)65 case .expiringSoon: soon.append(card)66 case .valid: valid.append(card)67 case .noExpiry: none.append(card)68 }69 }70 func byDate(_ a: Card, _ b: Card) -> Bool { (a.expiry ?? .distantFuture, a.name) < (b.expiry ?? .distantFuture, b.name) }71 self.expired = expired.sorted { ($0.expiry ?? .distantPast, $1.name) > ($1.expiry ?? .distantPast, $0.name) }72 self.expiringSoon = soon.sorted(by: byDate)73 self.valid = valid.sorted(by: byDate)74 self.noExpiry = none.sorted { $0.name.localizedCaseInsensitiveCompare($1.name) == .orderedAscending }75 }7677 /// Long wording used in rows.78 static func describe(daysUntilExpiry days: Int) -> String {79 switch days {80 case ..<(-1): "Expired \(-days) days ago"81 case -1: "Expired yesterday"82 case 0: "Expires today"83 case 1: "Expires tomorrow"84 default: "Expires in \(days) days"85 }86 }87}88