85d0eaddbb
Replace Core Data + NSPersistentCloudKitContainer + App-Group store + WatchConnectivity dictionary sync with the QuickRabbit iCloud-documents architecture: - iCloud Drive JSON documents are the sole source of truth (one file per aggregate: Splits/<ULID>.json, Workouts/YYYY/MM/<ULID>.json), with a rebuildable SwiftData cache populated only by an NSMetadataQuery observer and a connect-time reconcile. Soft-delete tombstones; hard iCloud gate. - Shared model layer (ULID, Codable *Documents + stateless mappers, @Model cache entities, SwiftData container) compiled into both targets. - New iPhone<->Watch bridge over WatchConnectivity keyed by ULIDs; the phone is the sole writer of iCloud Drive, the watch round-trips documents. - AppServices DI + iCloud-required root gate; Swift 6 strict concurrency. - Starter splits generated on demand from the bundled YAML catalogs. - Migrate to XcodeGen (project.yml), iOS 26 / watchOS 26; CloudDocuments entitlement (drop CloudKit/App Group/aps-environment). - Duration stored as Int seconds (was a Date epoch hack); fix workout end-on-create, undismissable delete dialog, toolbar-hiding nav stacks, and the Settings placeholder. - Add README/CHANGELOG/LICENSE, .gitignore, refreshed REQUIREMENTS, and the Scripts/ TestFlight pipeline (release.sh + ASC API scripts). MARKETING_VERSION 2.0.
69 lines
2.7 KiB
Swift
69 lines
2.7 KiB
Swift
import Foundation
|
|
|
|
/// Minimal ULID generator.
|
|
///
|
|
/// 26-character Crockford base32 string: 10 chars of millisecond timestamp +
|
|
/// 16 chars of randomness. Lexicographic order matches chronological order,
|
|
/// so workout documents sort newest-last by id and file listings stay ordered.
|
|
///
|
|
/// Spec: https://github.com/ulid/spec
|
|
enum ULID {
|
|
/// Crockford base32 alphabet — no I, L, O, U to avoid visual confusion.
|
|
private static let alphabet: [Character] = Array("0123456789ABCDEFGHJKMNPQRSTVWXYZ")
|
|
|
|
/// Mints a fresh ULID for the current instant.
|
|
static func make() -> String {
|
|
let timestamp = UInt64(Date().timeIntervalSince1970 * 1000)
|
|
var randomness = [UInt8](repeating: 0, count: 10)
|
|
_ = randomness.withUnsafeMutableBytes { buffer in
|
|
SecRandomCopyBytes(kSecRandomDefault, buffer.count, buffer.baseAddress!)
|
|
}
|
|
return encode(timestamp: timestamp, randomness: randomness)
|
|
}
|
|
|
|
/// True if `s` is a 26-char ULID using the Crockford alphabet.
|
|
static func isValid(_ s: String) -> Bool {
|
|
guard s.count == 26 else { return false }
|
|
let allowed = Set(alphabet)
|
|
return s.allSatisfy { allowed.contains($0) }
|
|
}
|
|
|
|
/// Decodes the timestamp portion of a ULID, if valid.
|
|
static func timestamp(of ulid: String) -> Date? {
|
|
guard ulid.count == 26 else { return nil }
|
|
let prefix = ulid.prefix(10)
|
|
var value: UInt64 = 0
|
|
let lookup = Dictionary(uniqueKeysWithValues: alphabet.enumerated().map { ($1, UInt64($0)) })
|
|
for char in prefix {
|
|
guard let digit = lookup[char] else { return nil }
|
|
value = (value << 5) | digit
|
|
}
|
|
return Date(timeIntervalSince1970: Double(value) / 1000)
|
|
}
|
|
|
|
// MARK: - Internal
|
|
|
|
private static func encode(timestamp: UInt64, randomness: [UInt8]) -> String {
|
|
precondition(randomness.count == 10, "ULID randomness must be 10 bytes (80 bits)")
|
|
// 48-bit timestamp → 10 base32 chars (50 bits, top 2 unused).
|
|
var output = [Character](repeating: "0", count: 26)
|
|
var t = timestamp & ((1 << 48) - 1)
|
|
for i in stride(from: 9, through: 0, by: -1) {
|
|
output[i] = alphabet[Int(t & 0x1F)]
|
|
t >>= 5
|
|
}
|
|
// 80-bit randomness → 16 base32 chars.
|
|
var bigEnd = randomness
|
|
for i in stride(from: 25, through: 10, by: -1) {
|
|
var carry: UInt16 = 0
|
|
for j in 0..<bigEnd.count {
|
|
let combined = UInt16(bigEnd[j]) | (carry << 8)
|
|
bigEnd[j] = UInt8(combined >> 5)
|
|
carry = combined & 0x1F
|
|
}
|
|
output[i] = alphabet[Int(UInt8(carry))]
|
|
}
|
|
return String(output)
|
|
}
|
|
}
|