ContactSyncBadge.swift 32 lignes · 1072 octets
import Foundation

// Port of ui/carnet/ContactSyncBadge.kt.

/// Sync state of a contact row: queued (blue) or failed (red).
enum ContactSyncBadge: Equatable {
    case pending
    case error
}

enum ContactSyncBadges {
    private static let contactTypes: Set<String> = ["contact", "contact_media"]

    /// Maps each contact having a queued sync op to its badge; ERROR wins.
    static func compute(
        contacts: [CrmContactEntity],
        ops: [SyncOpEntity]
    ) -> [Int64: ContactSyncBadge] {
        let contactOps = ops.filter { contactTypes.contains($0.entityType) }
        if contactOps.isEmpty { return [:] }
        var result: [Int64: ContactSyncBadge] = [:]
        for contact in contacts {
            let matched = contactOps.filter {
                $0.localId == contact.id
                    || ($0.serverId != nil && $0.serverId == contact.serverId)
            }
            if matched.isEmpty { continue }
            result[contact.id] = matched.contains { $0.lastError != nil } ? .error : .pending
        }
        return result
    }
}