|
| 1 | +// |
| 2 | +// ValueDisplayFormatService.swift |
| 3 | +// TablePro |
| 4 | +// |
| 5 | +// Applies display format transformations to raw cell values |
| 6 | +// and manages the effective format per column (auto-detected vs. user override). |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +import os |
| 11 | + |
| 12 | +@MainActor |
| 13 | +final class ValueDisplayFormatService { |
| 14 | + static let shared = ValueDisplayFormatService() |
| 15 | + |
| 16 | + private static let logger = Logger(subsystem: "com.TablePro", category: "ValueDisplayFormat") |
| 17 | + |
| 18 | + /// Auto-detected formats keyed by "connectionId.tableName.columnName" for per-connection isolation. |
| 19 | + private var autoDetectedFormats: [String: ValueDisplayFormat] = [:] |
| 20 | + |
| 21 | + private init() {} |
| 22 | + |
| 23 | + // MARK: - Format Application |
| 24 | + |
| 25 | + static func applyFormat(_ rawValue: String, format: ValueDisplayFormat) -> String { |
| 26 | + switch format { |
| 27 | + case .raw: |
| 28 | + return rawValue |
| 29 | + case .uuid: |
| 30 | + return formatAsUuid(rawValue) |
| 31 | + case .unixTimestamp: |
| 32 | + return formatAsTimestamp(rawValue, divideBy: 1) |
| 33 | + case .unixTimestampMillis: |
| 34 | + return formatAsTimestamp(rawValue, divideBy: 1_000) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // MARK: - Effective Format Resolution |
| 39 | + |
| 40 | + func effectiveFormat(columnName: String, connectionId: UUID?, tableName: String?) -> ValueDisplayFormat { |
| 41 | + // Stored overrides take priority |
| 42 | + if let connId = connectionId, let table = tableName { |
| 43 | + if let overrides = ValueDisplayFormatStorage.shared.load(for: table, connectionId: connId), |
| 44 | + let format = overrides[columnName] { |
| 45 | + return format |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // Then auto-detected (scoped by connection + table) |
| 50 | + let key = scopedKey(columnName: columnName, connectionId: connectionId, tableName: tableName) |
| 51 | + if let format = autoDetectedFormats[key] { |
| 52 | + return format |
| 53 | + } |
| 54 | + |
| 55 | + return .raw |
| 56 | + } |
| 57 | + |
| 58 | + func setAutoDetectedFormats(_ formats: [String: ValueDisplayFormat], connectionId: UUID?, tableName: String?) { |
| 59 | + // Clear previous entries for this scope |
| 60 | + let prefix = scopePrefix(connectionId: connectionId, tableName: tableName) |
| 61 | + autoDetectedFormats = autoDetectedFormats.filter { !$0.key.hasPrefix(prefix) } |
| 62 | + |
| 63 | + for (columnName, format) in formats { |
| 64 | + let key = scopedKey(columnName: columnName, connectionId: connectionId, tableName: tableName) |
| 65 | + autoDetectedFormats[key] = format |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + func clearAutoDetectedFormats(connectionId: UUID?, tableName: String?) { |
| 70 | + let prefix = scopePrefix(connectionId: connectionId, tableName: tableName) |
| 71 | + autoDetectedFormats = autoDetectedFormats.filter { !$0.key.hasPrefix(prefix) } |
| 72 | + } |
| 73 | + |
| 74 | + // MARK: - Scoping |
| 75 | + |
| 76 | + private func scopePrefix(connectionId: UUID?, tableName: String?) -> String { |
| 77 | + "\(connectionId?.uuidString ?? "_").\(tableName ?? "_")." |
| 78 | + } |
| 79 | + |
| 80 | + private func scopedKey(columnName: String, connectionId: UUID?, tableName: String?) -> String { |
| 81 | + "\(connectionId?.uuidString ?? "_").\(tableName ?? "_").\(columnName)" |
| 82 | + } |
| 83 | + |
| 84 | + // MARK: - Override Management |
| 85 | + |
| 86 | + func setOverride( |
| 87 | + _ format: ValueDisplayFormat?, |
| 88 | + columnName: String, |
| 89 | + connectionId: UUID, |
| 90 | + tableName: String |
| 91 | + ) { |
| 92 | + var overrides = ValueDisplayFormatStorage.shared.load(for: tableName, connectionId: connectionId) ?? [:] |
| 93 | + |
| 94 | + if let format, format != .raw { |
| 95 | + overrides[columnName] = format |
| 96 | + } else { |
| 97 | + overrides.removeValue(forKey: columnName) |
| 98 | + } |
| 99 | + |
| 100 | + if overrides.isEmpty { |
| 101 | + ValueDisplayFormatStorage.shared.clear(for: tableName, connectionId: connectionId) |
| 102 | + } else { |
| 103 | + ValueDisplayFormatStorage.shared.save(overrides, for: tableName, connectionId: connectionId) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + // MARK: - Private Formatting |
| 108 | + |
| 109 | + private static func formatAsUuid(_ rawValue: String) -> String { |
| 110 | + // Try raw binary bytes (isoLatin1 encoding from MySQL) |
| 111 | + if let data = rawValue.data(using: .isoLatin1), data.count == 16 { |
| 112 | + let bytes = [UInt8](data) |
| 113 | + let hex = bytes.map { String(format: "%02x", $0) }.joined() |
| 114 | + return insertUuidHyphens(hex) |
| 115 | + } |
| 116 | + |
| 117 | + // Try hex string (with or without 0x prefix) |
| 118 | + var hex = rawValue |
| 119 | + if hex.hasPrefix("0x") || hex.hasPrefix("0X") { |
| 120 | + hex = String(hex.dropFirst(2)) |
| 121 | + } |
| 122 | + hex = hex.replacingOccurrences(of: "-", with: "") |
| 123 | + |
| 124 | + guard (hex as NSString).length == 32, hex.allSatisfy({ $0.isHexDigit }) else { |
| 125 | + return rawValue |
| 126 | + } |
| 127 | + |
| 128 | + return insertUuidHyphens(hex.lowercased()) |
| 129 | + } |
| 130 | + |
| 131 | + private static func insertUuidHyphens(_ hex: String) -> String { |
| 132 | + let ns = hex as NSString |
| 133 | + let p1 = ns.substring(with: NSRange(location: 0, length: 8)) |
| 134 | + let p2 = ns.substring(with: NSRange(location: 8, length: 4)) |
| 135 | + let p3 = ns.substring(with: NSRange(location: 12, length: 4)) |
| 136 | + let p4 = ns.substring(with: NSRange(location: 16, length: 4)) |
| 137 | + let p5 = ns.substring(with: NSRange(location: 20, length: 12)) |
| 138 | + return "\(p1)-\(p2)-\(p3)-\(p4)-\(p5)" |
| 139 | + } |
| 140 | + |
| 141 | + private static func formatAsTimestamp(_ rawValue: String, divideBy divisor: Double) -> String { |
| 142 | + guard let numericValue = Double(rawValue) else { return rawValue } |
| 143 | + let seconds = numericValue / divisor |
| 144 | + let date = Date(timeIntervalSince1970: seconds) |
| 145 | + return DateFormattingService.shared.format(date) |
| 146 | + } |
| 147 | +} |
0 commit comments