|
| 1 | +import Foundation |
| 2 | + |
| 3 | +enum SpecialTableColumn: String, Codable { |
| 4 | + case title = "title" |
| 5 | + case duration = "duration" |
| 6 | + |
| 7 | + var displayName: String { |
| 8 | + switch self { |
| 9 | + case .title: return "Title" |
| 10 | + case .duration: return "Duration" |
| 11 | + } |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +enum TrackTableColumn: Codable, Hashable { |
| 16 | + case special(SpecialTableColumn) |
| 17 | + case libraryFilter(LibraryFilterType) |
| 18 | + |
| 19 | + // All available columns in order |
| 20 | + static var allColumns: [TrackTableColumn] { |
| 21 | + var columns: [TrackTableColumn] = [.special(.title)] |
| 22 | + |
| 23 | + // Add all LibraryFilterType columns |
| 24 | + for filterType in LibraryFilterType.allCases { |
| 25 | + columns.append(.libraryFilter(filterType)) |
| 26 | + } |
| 27 | + |
| 28 | + columns.append(.special(.duration)) |
| 29 | + return columns |
| 30 | + } |
| 31 | + |
| 32 | + var displayName: String { |
| 33 | + switch self { |
| 34 | + case .special(let specialColumn): |
| 35 | + return specialColumn.displayName |
| 36 | + case .libraryFilter(let filterType): |
| 37 | + return filterType.singularDisplayName |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + var identifier: String { |
| 42 | + switch self { |
| 43 | + case .special(let specialColumn): |
| 44 | + return specialColumn.rawValue |
| 45 | + case .libraryFilter(let filterType): |
| 46 | + return filterType.rawValue |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + var isRequired: Bool { |
| 51 | + switch self { |
| 52 | + case .special(.title): |
| 53 | + return true |
| 54 | + default: |
| 55 | + return false |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + var defaultVisibility: Bool { |
| 60 | + switch self { |
| 61 | + case .special(.title), .special(.duration): |
| 62 | + return true |
| 63 | + case .libraryFilter(let filterType): |
| 64 | + switch filterType { |
| 65 | + case .artists, .albums: |
| 66 | + return true |
| 67 | + default: |
| 68 | + return false |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // Codable implementation |
| 74 | + enum CodingKeys: String, CodingKey { |
| 75 | + case type |
| 76 | + case value |
| 77 | + } |
| 78 | + |
| 79 | + func encode(to encoder: Encoder) throws { |
| 80 | + var container = encoder.container(keyedBy: CodingKeys.self) |
| 81 | + switch self { |
| 82 | + case .special(let specialColumn): |
| 83 | + try container.encode("special", forKey: .type) |
| 84 | + try container.encode(specialColumn.rawValue, forKey: .value) |
| 85 | + case .libraryFilter(let filterType): |
| 86 | + try container.encode("filter", forKey: .type) |
| 87 | + try container.encode(filterType.rawValue, forKey: .value) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + init(from decoder: Decoder) throws { |
| 92 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 93 | + let type = try container.decode(String.self, forKey: .type) |
| 94 | + |
| 95 | + switch type { |
| 96 | + case "special": |
| 97 | + let value = try container.decode(String.self, forKey: .value) |
| 98 | + guard let specialColumn = SpecialTableColumn(rawValue: value) else { |
| 99 | + throw DecodingError.dataCorruptedError(forKey: .value, in: container, debugDescription: "Invalid special column") |
| 100 | + } |
| 101 | + self = .special(specialColumn) |
| 102 | + case "filter": |
| 103 | + let value = try container.decode(String.self, forKey: .value) |
| 104 | + guard let filterType = LibraryFilterType(rawValue: value) else { |
| 105 | + throw DecodingError.dataCorruptedError(forKey: .value, in: container, debugDescription: "Invalid filter type") |
| 106 | + } |
| 107 | + self = .libraryFilter(filterType) |
| 108 | + default: |
| 109 | + throw DecodingError.dataCorruptedError(forKey: .type, in: container, debugDescription: "Invalid column type") |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +struct TrackTableColumnVisibility: Codable { |
| 115 | + private var hiddenColumns: Set<String> // Store identifiers of hidden columns |
| 116 | + |
| 117 | + init() { |
| 118 | + self.hiddenColumns = [] |
| 119 | + |
| 120 | + // Hide non-default columns |
| 121 | + for column in TrackTableColumn.allColumns { |
| 122 | + if !column.defaultVisibility && !column.isRequired { |
| 123 | + hiddenColumns.insert(column.identifier) |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + func isVisible(_ column: TrackTableColumn) -> Bool { |
| 129 | + column.isRequired || !hiddenColumns.contains(column.identifier) |
| 130 | + } |
| 131 | + |
| 132 | + mutating func setVisibility(_ column: TrackTableColumn, isVisible: Bool) { |
| 133 | + guard !column.isRequired else { return } |
| 134 | + |
| 135 | + if isVisible { |
| 136 | + hiddenColumns.remove(column.identifier) |
| 137 | + } else { |
| 138 | + hiddenColumns.insert(column.identifier) |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + mutating func toggleVisibility(_ column: TrackTableColumn) { |
| 143 | + setVisibility(column, isVisible: !isVisible(column)) |
| 144 | + } |
| 145 | +} |
0 commit comments