import Foundation #if os(iOS) && canImport(FinanceKit) import FinanceKit #endif /// Apple Wallet accounts (Apple Card, Apple Cash, Savings) through FinanceKit, iPhone only. The /// phone reads them with the user's permission and hands them to the Mac through iCloud. public enum WalletImport { public static let connectionID = "apple-wallet" public static var isSupported: Bool { #if os(iOS) && canImport(FinanceKit) FinanceStore.isDataAvailable(.financialData) #else false #endif } #if os(iOS) && canImport(FinanceKit) public static func requestAccess() async throws -> Bool { try await FinanceStore.shared.requestAuthorization() == .authorized } /// The last `days` days of Wallet transactions and current balances, ready for `BankSync.apply`. public static func pull(days: Int = 365) async throws -> BankPull { let store = FinanceStore.shared var pull = BankPull() let accounts = try await store.accounts(query: AccountQuery()) let balances = try await store.accountBalances(query: AccountBalanceQuery()) for account in accounts { let id = "\(connectionID):\(account.id.uuidString)" let liability: Bool switch account { case .liability: liability = true; default: liability = false } var value: Decimal = 0 let latest = balances.filter { $0.accountID == account.id }.compactMap { $0.booked ?? $0.available }.max { $0.asOfDate < $1.asOfDate } if let amount = latest { value = amount.amount.amount * (amount.creditDebitIndicator == .debit ? -1 : 1) if liability { value = -abs(value) } } pull.accounts.append(Account(id: id, connectionID: connectionID, name: account.displayName, institution: account.institutionName, kind: liability ? .credit : AccountKind.guess(from: account.displayName) == .other ? .savings : AccountKind.guess(from: account.displayName), balance: value, currency: account.currencyCode)) } let since = Calendar.current.date(byAdding: .day, value: -days, to: .now) ?? .now let query = TransactionQuery(sortDescriptors: [SortDescriptor(\.transactionDate, order: .reverse)], predicate: #Predicate { $0.transactionDate >= since }) for t in try await store.transactions(query: query) { let sign: Decimal = t.creditDebitIndicator == .debit ? -1 : 1 let mcc = t.merchantCategoryCode.map { "mcc:\($0.rawValue)" } pull.transactions.append(Transaction(id: "\(connectionID):\(t.accountID.uuidString):\(t.id.uuidString)", accountID: "\(connectionID):\(t.accountID.uuidString)", day: DayKey.from(t.transactionDate), amount: t.transactionAmount.amount * sign, description: t.originalTransactionDescription, payee: t.merchantName, bankCategory: mcc, pending: t.status == .pending || t.status == .authorized)) } return pull } #endif /// Mac: files a Wallet import from the iPhone under its own connection. public static func apply(_ pull: BankPull, store: WorkspaceStore) throws { let existing = try store.load().connections.first { $0.id == connectionID } let connection = existing ?? Connection(id: connectionID, kind: .financekit, name: "Apple Wallet (iPhone)") try BankSync(store: store).apply(pull, to: connection) } }