restore: original UI views from first build, keep fixed models/API
All checks were successful
Security Checks / dependency-audit (push) Successful in 12s
Security Checks / secret-scanning (push) Successful in 3s
Security Checks / dockerfile-lint (push) Successful in 3s

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Yusuf Suleman
2026-04-03 01:54:46 -05:00
parent e852e98812
commit fdb8aeba8a
61 changed files with 6652 additions and 1178 deletions

View File

@@ -1,54 +1,49 @@
import SwiftUI
import PhotosUI
import Foundation
@Observable
@MainActor @Observable
final class HomeViewModel {
var backgroundImage: UIImage?
var caloriesConsumed: Double = 0
var caloriesGoal: Double = 2000
var isLoading = false
var todayEntries: [FoodEntry] = []
var goal: DailyGoal = .defaultGoal
var isLoading = true
var errorMessage: String?
private let bgKey = "homeBackgroundImage"
private let repo = FitnessRepository.shared
init() {
loadBackgroundFromDefaults()
var totalCalories: Double {
todayEntries.reduce(0) { $0 + $1.calories }
}
func loadData() async {
var totalProtein: Double {
todayEntries.reduce(0) { $0 + $1.protein }
}
var totalCarbs: Double {
todayEntries.reduce(0) { $0 + $1.carbs }
}
var totalFat: Double {
todayEntries.reduce(0) { $0 + $1.fat }
}
var entryCount: Int {
todayEntries.count
}
func load() async {
isLoading = true
errorMessage = nil
let today = Date().apiDateString
await repo.loadDay(date: today)
caloriesConsumed = repo.totalCalories
caloriesGoal = repo.goals.calories
do {
async let entriesTask = repo.entries(for: today, forceRefresh: true)
async let goalsTask = repo.goals(for: today)
todayEntries = try await entriesTask
goal = try await goalsTask
} catch {
errorMessage = error.localizedDescription
}
isLoading = false
}
func setBackground(_ image: UIImage) {
// Resize to max 1200px
let maxDim: CGFloat = 1200
let scale = min(maxDim / image.size.width, maxDim / image.size.height, 1.0)
let newSize = CGSize(width: image.size.width * scale, height: image.size.height * scale)
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
image.draw(in: CGRect(origin: .zero, size: newSize))
let resized = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
if let resized, let data = resized.jpegData(compressionQuality: 0.8) {
UserDefaults.standard.set(data, forKey: bgKey)
backgroundImage = resized
}
}
func removeBackground() {
UserDefaults.standard.removeObject(forKey: bgKey)
backgroundImage = nil
}
private func loadBackgroundFromDefaults() {
if let data = UserDefaults.standard.data(forKey: bgKey),
let img = UIImage(data: data) {
backgroundImage = img
}
}
}