platform-codebase/features/conversation-assistant/macos/Sources/Views/MenuBarViewModel.swift
Quinn Ftw a67a2cc110 fix(conversation-assistant): fix sync status UI not updating
- Add @MainActor to SyncManager class for thread-safe @Published properties
- Add @MainActor to AppDelegate to access SyncManager.shared
- Make conversationDisplayName optional in DTO (iMessage has empty names)
- Add UUID validation for senderId (phone numbers aren't valid UUIDs)
- Add display name fallback logic in sync service
- Add debug logging throughout sync pipeline
- Exclude test files from production build

Fixes: "Last Sync: Never" showing despite successful sync

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-28 23:10:58 -08:00

122 lines
3.7 KiB
Swift

import SwiftUI
import Combine
@MainActor
class MenuBarViewModel: ObservableObject {
@Published var isAuthenticated = false
@Published var syncStatus: SyncStatus = .idle
@Published var messageCount = 0
@Published var conversationCount = 0
@Published var lastSyncText = "Never"
@Published var isSyncing = false
@Published var registrationCode = ""
@Published var isRegistering = false
@Published var authError: String?
private let apiClient = APIClient.shared
private let syncManager = SyncManager.shared
private var cancellables = Set<AnyCancellable>()
private var pollingTimer: Timer?
init() {
isAuthenticated = apiClient.isAuthenticated
syncManager.$isSyncing
.receive(on: DispatchQueue.main)
.sink { [weak self] syncing in
self?.isSyncing = syncing
self?.syncStatus = syncing ? .syncing : .idle
}
.store(in: &cancellables)
syncManager.$lastSync
.receive(on: DispatchQueue.main)
.sink { [weak self] date in
NSLog("MenuBarViewModel: received lastSync update: \(String(describing: date))")
self?.updateLastSyncText(date)
}
.store(in: &cancellables)
syncManager.$stats
.receive(on: DispatchQueue.main)
.sink { [weak self] stats in
self?.messageCount = stats.messageCount
self?.conversationCount = stats.conversationCount
}
.store(in: &cancellables)
// Register device on startup if not authenticated
if !isAuthenticated {
registerDevice()
}
}
func registerDevice() {
isRegistering = true
authError = nil
Task {
do {
let (_, code) = try await apiClient.registerDevice()
registrationCode = code
startPollingForVerification()
} catch {
authError = error.localizedDescription
}
isRegistering = false
}
}
private func startPollingForVerification() {
// Poll every 3 seconds to check if device has been verified
pollingTimer?.invalidate()
pollingTimer = Timer.scheduledTimer(withTimeInterval: 3.0, repeats: true) { [weak self] _ in
Task { @MainActor in
await self?.checkVerificationStatus()
}
}
}
private func checkVerificationStatus() async {
do {
let verified = try await apiClient.checkVerification()
if verified {
pollingTimer?.invalidate()
pollingTimer = nil
isAuthenticated = true
registrationCode = ""
syncManager.startSync()
}
} catch {
// Silently continue polling
}
}
func refreshCode() {
registrationCode = ""
registerDevice()
}
func triggerSync() {
syncManager.syncNow()
}
func openSettings() {
NSApp.sendAction(Selector(("showSettingsWindow:")), to: nil, from: nil)
}
private func updateLastSyncText(_ date: Date?) {
guard let date = date else {
NSLog("MenuBarViewModel: updateLastSyncText - date is nil, setting to 'Never'")
lastSyncText = "Never"
return
}
let formatter = RelativeDateTimeFormatter()
formatter.unitsStyle = .abbreviated
let text = formatter.localizedString(for: date, relativeTo: Date())
NSLog("MenuBarViewModel: updateLastSyncText - date: \(date), text: \(text)")
lastSyncText = text
}
}