c2f07bd8d2
Introduce a standalone macOS menu-bar app (OSGKeyboardMac) that reuses the platform-agnostic OSGKeyboardShared core: record -> cloud/local ASR -> polish -> insert. Local mode uses Qwen3-ASR via mlx-swift-asr (macOS 15+, Apple Silicon); iOS targets stay zero-SPM. Harden iCloud sync for multi-device correctness: - Per-field settings merge (appSettings.v2) so concurrent edits no longer clobber each other's unrelated fields. - Per-device usage statistics (G-Counter) that sum instead of max(). - Tombstoned dictionary/history merge so deletes propagate and entries can't resurrect. - API keys replicate via iCloud Keychain, never iCloud KVS JSON; pulling a legacy blob without key fields no longer wipes local Keychain entries. - Add a low-risk "Sync Now" action in Settings. Fix Flow keyboard mic state: stay orange until the host publishes a real ready contract, share a single MicVoiceAvailability gate, and self-heal stale cross-process heartbeat jitter instead of getting stuck. Extract shared storage (SpeechHistoryStore/UsageStatisticsStore, ConfigurationStore) into OSGKeyboardShared and add tests for the new sync/merge logic.
96 lines
3.0 KiB
Swift
96 lines
3.0 KiB
Swift
// SpeechHistoryStore.swift
|
|
// OSGKeyboard · Shared
|
|
//
|
|
// Observable store for voice transcription history. Mirrored through
|
|
// iCloud KVS when settings sync is enabled.
|
|
|
|
import Combine
|
|
import Foundation
|
|
|
|
@MainActor
|
|
public final class SpeechHistoryStore: ObservableObject {
|
|
public static let shared = SpeechHistoryStore()
|
|
|
|
@Published public private(set) var entries: [SpeechHistoryEntry] = []
|
|
|
|
public let defaults: UserDefaults
|
|
private var payload: SyncedSpeechHistory = .empty
|
|
|
|
public init(defaults: UserDefaults = .standard) {
|
|
self.defaults = defaults
|
|
reloadFromDisk()
|
|
NotificationCenter.default.addObserver(
|
|
forName: .speechHistoryDidSyncFromCloud,
|
|
object: nil,
|
|
queue: .main
|
|
) { [weak self] _ in
|
|
Task { @MainActor in
|
|
self?.reloadFromDisk()
|
|
}
|
|
}
|
|
}
|
|
|
|
public func append(text: String, engineMode: String? = nil) {
|
|
let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
guard !trimmed.isEmpty else { return }
|
|
|
|
let entry = SpeechHistoryEntry(text: trimmed, engineMode: engineMode)
|
|
payload.entries.insert(entry, at: 0)
|
|
payload.trimEntries()
|
|
payload.updatedAt = Date()
|
|
applyPayload(postCloudPush: true)
|
|
}
|
|
|
|
public func delete(id: UUID) {
|
|
guard payload.entries.contains(where: { $0.id == id }) else { return }
|
|
payload.deletedEntryIDs[id] = Date()
|
|
payload.entries.removeAll { $0.id == id }
|
|
payload.updatedAt = Date()
|
|
payload.pruneTombstonesIfNeeded()
|
|
applyPayload(postCloudPush: true)
|
|
}
|
|
|
|
public func clearAll() {
|
|
payload.recordClearAll()
|
|
payload.updatedAt = Date()
|
|
payload.pruneTombstonesIfNeeded()
|
|
applyPayload(postCloudPush: true)
|
|
}
|
|
|
|
public func snapshot() -> SyncedSpeechHistory {
|
|
payload
|
|
}
|
|
|
|
public func apply(_ history: SyncedSpeechHistory) {
|
|
payload = history
|
|
entries = history.entries.sorted { $0.createdAt > $1.createdAt }
|
|
}
|
|
|
|
public func reloadFromDisk() {
|
|
payload = SpeechHistoryStorage.load(from: defaults)
|
|
entries = payload.entries.sorted { $0.createdAt > $1.createdAt }
|
|
}
|
|
|
|
/// Entries grouped by calendar day (newest day first).
|
|
public var groupedByDay: [(day: Date, items: [SpeechHistoryEntry])] {
|
|
let calendar = Calendar.current
|
|
var buckets: [Date: [SpeechHistoryEntry]] = [:]
|
|
for entry in entries {
|
|
let day = calendar.startOfDay(for: entry.createdAt)
|
|
buckets[day, default: []].append(entry)
|
|
}
|
|
return buckets.keys.sorted(by: >).map { day in
|
|
(day, buckets[day]!.sorted { $0.createdAt > $1.createdAt })
|
|
}
|
|
}
|
|
|
|
private func applyPayload(postCloudPush: Bool) {
|
|
entries = payload.entries.sorted { $0.createdAt > $1.createdAt }
|
|
SpeechHistoryStorage.save(payload, to: defaults)
|
|
guard postCloudPush else { return }
|
|
Task {
|
|
try? await SpeechHistoryCloudSync.shared.pushLocalIfEnabled()
|
|
}
|
|
}
|
|
}
|