fix: codebase audit fixes — safety, accessibility, and production hygiene
Address 16 issues from external audit: - Move StoreKit transaction listener ownership to StoreManager singleton with proper deinit - Remove noisy VoiceOver announcements, add missing accessibility on StatPill and BootstrapLoadingView - Replace String @retroactive Identifiable with IdentifiableShareCode wrapper - Add crash guard in AchievementEngine getContributingVisitIds + cache stadium lookups - Pre-compute GamesHistoryViewModel filtered properties to avoid redundant SwiftUI recomputation - Remove force-unwraps in ProgressMapView with safe guard-let fallback - Add diff-based update gating in ItineraryTableViewWrapper to prevent unnecessary reloads - Replace deprecated UIScreen.main with UIWindowScene lookup - Add deinit task cancellation in ScheduleViewModel and SuggestedTripsGenerator - Wrap ~234 unguarded print() calls across 27 files in #if DEBUG Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -7,40 +7,17 @@ final class GamesHistoryViewModel {
|
||||
private let modelContext: ModelContext
|
||||
|
||||
var allVisits: [StadiumVisit] = []
|
||||
var selectedSports: Set<Sport> = []
|
||||
var selectedSports: Set<Sport> = [] {
|
||||
didSet { recomputeFilteredData() }
|
||||
}
|
||||
var isLoading = false
|
||||
var error: String?
|
||||
|
||||
// Computed: visits grouped by year
|
||||
var visitsByYear: [Int: [StadiumVisit]] {
|
||||
let calendar = Calendar.current
|
||||
let filtered = filteredVisits
|
||||
return Dictionary(grouping: filtered) { visit in
|
||||
calendar.component(.year, from: visit.visitDate)
|
||||
}
|
||||
}
|
||||
|
||||
// Computed: sorted year keys (descending)
|
||||
var sortedYears: [Int] {
|
||||
visitsByYear.keys.sorted(by: >)
|
||||
}
|
||||
|
||||
// Computed: filtered by selected sports
|
||||
var filteredVisits: [StadiumVisit] {
|
||||
guard !selectedSports.isEmpty else { return allVisits }
|
||||
|
||||
return allVisits.filter { visit in
|
||||
guard let stadium = AppDataProvider.shared.stadium(for: visit.stadiumId) else {
|
||||
return false
|
||||
}
|
||||
return selectedSports.contains(stadium.sport)
|
||||
}
|
||||
}
|
||||
|
||||
// Total count
|
||||
var totalGamesCount: Int {
|
||||
filteredVisits.count
|
||||
}
|
||||
// Pre-computed stored properties (updated via recomputeFilteredData)
|
||||
private(set) var filteredVisits: [StadiumVisit] = []
|
||||
private(set) var visitsByYear: [Int: [StadiumVisit]] = [:]
|
||||
private(set) var sortedYears: [Int] = []
|
||||
private(set) var totalGamesCount: Int = 0
|
||||
|
||||
init(modelContext: ModelContext) {
|
||||
self.modelContext = modelContext
|
||||
@@ -60,6 +37,8 @@ final class GamesHistoryViewModel {
|
||||
self.error = "Failed to load games: \(error.localizedDescription)"
|
||||
allVisits = []
|
||||
}
|
||||
|
||||
recomputeFilteredData()
|
||||
}
|
||||
|
||||
func toggleSport(_ sport: Sport) {
|
||||
@@ -73,4 +52,24 @@ final class GamesHistoryViewModel {
|
||||
func clearFilters() {
|
||||
selectedSports.removeAll()
|
||||
}
|
||||
|
||||
private func recomputeFilteredData() {
|
||||
if selectedSports.isEmpty {
|
||||
filteredVisits = allVisits
|
||||
} else {
|
||||
filteredVisits = allVisits.filter { visit in
|
||||
guard let stadium = AppDataProvider.shared.stadium(for: visit.stadiumId) else {
|
||||
return false
|
||||
}
|
||||
return selectedSports.contains(stadium.sport)
|
||||
}
|
||||
}
|
||||
|
||||
let calendar = Calendar.current
|
||||
visitsByYear = Dictionary(grouping: filteredVisits) { visit in
|
||||
calendar.component(.year, from: visit.visitDate)
|
||||
}
|
||||
sortedYears = visitsByYear.keys.sorted(by: >)
|
||||
totalGamesCount = filteredVisits.count
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,8 +47,10 @@ final class PhotoImportViewModel {
|
||||
func processSelectedPhotos(_ items: [PhotosPickerItem]) async {
|
||||
guard !items.isEmpty else { return }
|
||||
|
||||
#if DEBUG
|
||||
print("📷 [PhotoImport] ════════════════════════════════════════════════")
|
||||
print("📷 [PhotoImport] Starting photo import with \(items.count) items")
|
||||
#endif
|
||||
|
||||
isProcessing = true
|
||||
totalCount = items.count
|
||||
@@ -61,16 +63,21 @@ final class PhotoImportViewModel {
|
||||
var assets: [PHAsset] = []
|
||||
|
||||
for (index, item) in items.enumerated() {
|
||||
#if DEBUG
|
||||
print("📷 [PhotoImport] ────────────────────────────────────────────────")
|
||||
print("📷 [PhotoImport] Processing item \(index + 1)/\(items.count)")
|
||||
print("📷 [PhotoImport] Item identifier: \(item.itemIdentifier ?? "nil")")
|
||||
print("📷 [PhotoImport] Item supportedContentTypes: \(item.supportedContentTypes)")
|
||||
#endif
|
||||
|
||||
if let assetId = item.itemIdentifier {
|
||||
let fetchResult = PHAsset.fetchAssets(withLocalIdentifiers: [assetId], options: nil)
|
||||
#if DEBUG
|
||||
print("📷 [PhotoImport] PHAsset fetch result count: \(fetchResult.count)")
|
||||
#endif
|
||||
|
||||
if let asset = fetchResult.firstObject {
|
||||
#if DEBUG
|
||||
print("📷 [PhotoImport] ✅ Found PHAsset")
|
||||
print("📷 [PhotoImport] - localIdentifier: \(asset.localIdentifier)")
|
||||
print("📷 [PhotoImport] - mediaType: \(asset.mediaType.rawValue)")
|
||||
@@ -79,22 +86,30 @@ final class PhotoImportViewModel {
|
||||
print("📷 [PhotoImport] - sourceType: \(asset.sourceType.rawValue)")
|
||||
print("📷 [PhotoImport] - pixelWidth: \(asset.pixelWidth)")
|
||||
print("📷 [PhotoImport] - pixelHeight: \(asset.pixelHeight)")
|
||||
#endif
|
||||
assets.append(asset)
|
||||
} else {
|
||||
#if DEBUG
|
||||
print("📷 [PhotoImport] ⚠️ No PHAsset found for identifier")
|
||||
#endif
|
||||
}
|
||||
} else {
|
||||
#if DEBUG
|
||||
print("📷 [PhotoImport] ⚠️ No itemIdentifier on PhotosPickerItem")
|
||||
#endif
|
||||
}
|
||||
processedCount += 1
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
print("📷 [PhotoImport] ────────────────────────────────────────────────")
|
||||
print("📷 [PhotoImport] Loaded \(assets.count) PHAssets, extracting metadata...")
|
||||
#endif
|
||||
|
||||
// Extract metadata from all assets
|
||||
let metadataList = await metadataExtractor.extractMetadata(from: assets)
|
||||
|
||||
#if DEBUG
|
||||
print("📷 [PhotoImport] ────────────────────────────────────────────────")
|
||||
print("📷 [PhotoImport] Extracted \(metadataList.count) metadata records")
|
||||
|
||||
@@ -103,25 +118,32 @@ final class PhotoImportViewModel {
|
||||
let withDate = metadataList.filter { $0.hasValidDate }.count
|
||||
print("📷 [PhotoImport] Photos with location: \(withLocation)/\(metadataList.count)")
|
||||
print("📷 [PhotoImport] Photos with date: \(withDate)/\(metadataList.count)")
|
||||
#endif
|
||||
|
||||
// Process each photo through game matcher
|
||||
processedCount = 0
|
||||
for (index, metadata) in metadataList.enumerated() {
|
||||
#if DEBUG
|
||||
print("📷 [PhotoImport] Matching photo \(index + 1): date=\(metadata.captureDate?.description ?? "nil"), location=\(metadata.hasValidLocation)")
|
||||
#endif
|
||||
let candidate = await gameMatcher.processPhotoForImport(metadata: metadata)
|
||||
processedPhotos.append(candidate)
|
||||
|
||||
// Auto-confirm high-confidence matches
|
||||
if candidate.canAutoProcess {
|
||||
confirmedImports.insert(candidate.id)
|
||||
#if DEBUG
|
||||
print("📷 [PhotoImport] ✅ Auto-confirmed match")
|
||||
#endif
|
||||
}
|
||||
|
||||
processedCount += 1
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
print("📷 [PhotoImport] ════════════════════════════════════════════════")
|
||||
print("📷 [PhotoImport] Import complete: \(processedPhotos.count) photos, \(confirmedImports.count) auto-confirmed")
|
||||
#endif
|
||||
|
||||
isProcessing = false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user