SecureStorageSample/SecureStorgageSample/WatchOptimized.swift

62 lines
1.7 KiB
Swift

import Foundation
// Example shared models for Watch-optimized data
struct Workout: Codable {
let date: Date
// Add other properties as needed
}
struct Preferences: Codable {
let isPremium: Bool
let appearance: Appearance
}
enum Appearance: Codable {
case light, dark
}
// Shared model (iOS + watchOS)
struct FullUserProfile: Codable {
let id: UUID
let fullName: String
let email: String
let avatarURL: URL?
let allWorkouts: [Workout] // potentially huge
let preferences: Preferences
// Lightweight Watch version computed
var watchVersion: WatchUserProfile {
WatchUserProfile(
id: id,
displayName: String(fullName.split(separator: " ").first ?? Substring(fullName)),
hasAvatar: avatarURL != nil,
recentWorkoutCount: allWorkouts.count,
lastWorkoutDate: allWorkouts.last?.date,
showPremiumBadge: preferences.isPremium,
darkModeEnabled: preferences.appearance == .dark
)
}
}
struct WatchUserProfile: Codable, Sendable {
let id: UUID
let displayName: String
let hasAvatar: Bool
let recentWorkoutCount: Int
let lastWorkoutDate: Date?
let showPremiumBadge: Bool
let darkModeEnabled: Bool
}
// Example usage on phone (commented out)
// let profile = FullUserProfile(...) // full heavy object
// let watchData = try JSONEncoder().encode(profile.watchVersion)
// WCSession.default.updateApplicationContext(["profile": watchData])
// Alternative: WatchRepresentable protocol
public protocol WatchRepresentable {
associatedtype WatchVersion: Codable, Sendable
var watchVersion: WatchVersion { get }
}