Track how long users spend studying by timing foreground sessions. StudyTimerService starts on app active, stops on background, and accumulates seconds into DailyLog.studySeconds (CloudKit-synced). Dashboard shows today/total study time with a 7-day bar chart. Closes #1 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
51 lines
1.3 KiB
Swift
51 lines
1.3 KiB
Swift
import SwiftData
|
|
import Foundation
|
|
|
|
@Model
|
|
final class DailyLog {
|
|
var id: String = ""
|
|
var dateString: String = ""
|
|
var reviewCount: Int = 0
|
|
var correctCount: Int = 0
|
|
var studySeconds: Int = 0
|
|
|
|
var accuracy: Double {
|
|
guard reviewCount > 0 else { return 0 }
|
|
return Double(correctCount) / Double(reviewCount)
|
|
}
|
|
|
|
init(dateString: String, reviewCount: Int = 0, correctCount: Int = 0) {
|
|
self.id = Self.makeID(dateString)
|
|
self.dateString = dateString
|
|
self.reviewCount = reviewCount
|
|
self.correctCount = correctCount
|
|
}
|
|
|
|
static func makeID(_ dateString: String) -> String {
|
|
dateString
|
|
}
|
|
|
|
func refreshIdentityIfNeeded() {
|
|
let expected = Self.makeID(dateString)
|
|
if id != expected {
|
|
id = expected
|
|
}
|
|
}
|
|
|
|
static func dateString(from date: Date) -> String {
|
|
let formatter = DateFormatter()
|
|
formatter.dateFormat = "yyyy-MM-dd"
|
|
return formatter.string(from: date)
|
|
}
|
|
|
|
static func todayString() -> String {
|
|
dateString(from: Date())
|
|
}
|
|
|
|
static func date(from string: String) -> Date? {
|
|
let formatter = DateFormatter()
|
|
formatter.dateFormat = "yyyy-MM-dd"
|
|
return formatter.date(from: string)
|
|
}
|
|
}
|