feat: Initial iOS Health Dashboard app (Swift + SwiftUI)

This commit is contained in:
Cosmo
2026-03-25 10:38:58 +00:00
commit 7cda5deaab
14 changed files with 443 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
import Foundation
enum APIError: Error, LocalizedError {
case unauthorized, networkError, decodingError
var errorDescription: String? {
switch self {
case .unauthorized: return "Неверный email или пароль"
case .networkError: return "Ошибка сети"
case .decodingError: return "Ошибка данных"
}
}
}
class APIService {
static let shared = APIService()
let baseURL = "https://health.digital-home.site"
func login(email: String, password: String) async throws -> LoginResponse {
let url = URL(string: "\(baseURL)/api/auth/login")!
var req = URLRequest(url: url)
req.httpMethod = "POST"
req.setValue("application/json", forHTTPHeaderField: "Content-Type")
req.httpBody = try JSONEncoder().encode(LoginRequest(email: email, password: password))
let (data, response) = try await URLSession.shared.data(for: req)
guard let r = response as? HTTPURLResponse, r.statusCode == 200 else { throw APIError.unauthorized }
return try JSONDecoder().decode(LoginResponse.self, from: data)
}
func getProfile(token: String) async throws -> ProfileResponse {
let url = URL(string: "\(baseURL)/api/profile")!
var req = URLRequest(url: url)
req.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
let (data, _) = try await URLSession.shared.data(for: req)
return try JSONDecoder().decode(ProfileResponse.self, from: data)
}
func getReadiness(token: String) async throws -> ReadinessResponse {
let url = URL(string: "\(baseURL)/api/health/readiness")!
var req = URLRequest(url: url)
req.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
let (data, _) = try await URLSession.shared.data(for: req)
return try JSONDecoder().decode(ReadinessResponse.self, from: data)
}
func getLatest(token: String) async throws -> LatestHealthResponse {
let url = URL(string: "\(baseURL)/api/health/latest")!
var req = URLRequest(url: url)
req.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
let (data, _) = try await URLSession.shared.data(for: req)
return try JSONDecoder().decode(LatestHealthResponse.self, from: data)
}
}

View File

@@ -0,0 +1,32 @@
import HealthKit
class HealthKitService: ObservableObject {
let healthStore = HKHealthStore()
var isAvailable: Bool { HKHealthStore.isHealthDataAvailable() }
func requestAuthorization() async throws {
let typesToRead: Set<HKObjectType> = [
HKQuantityType(.heartRate),
HKQuantityType(.restingHeartRate),
HKQuantityType(.heartRateVariabilitySDNN),
HKQuantityType(.stepCount),
HKQuantityType(.activeEnergyBurned),
HKQuantityType(.oxygenSaturation),
HKCategoryType(.sleepAnalysis),
]
try await healthStore.requestAuthorization(toShare: [], read: typesToRead)
}
func fetchTodaySteps() async -> Int {
guard let type = HKQuantityType.quantityType(forIdentifier: .stepCount) else { return 0 }
let now = Date()
let startOfDay = Calendar.current.startOfDay(for: now)
let predicate = HKQuery.predicateForSamples(withStart: startOfDay, end: now)
return await withCheckedContinuation { cont in
let q = HKStatisticsQuery(quantityType: type, quantitySamplePredicate: predicate, options: .cumulativeSum) { _, result, _ in
cont.resume(returning: Int(result?.sumQuantity()?.doubleValue(for: .count()) ?? 0))
}
healthStore.execute(q)
}
}
}