-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTodoCategoryMapping.swift
More file actions
101 lines (89 loc) · 3.01 KB
/
Copy pathTodoCategoryMapping.swift
File metadata and controls
101 lines (89 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
// TodoCategoryMapping.swift
// DevLogData
//
// Created by opfic on 5/16/26.
//
import DevLogDomain
extension TodoCategoryPreferenceResponse {
static func fromDomain(_ preference: TodoCategoryPreference) -> Self {
switch preference.category {
case .system(let systemTodoCategory):
return TodoCategoryPreferenceResponse(
category: .system(systemTodoCategory.rawValue),
isVisible: preference.isVisible
)
case .user(let userTodoCategory):
return TodoCategoryPreferenceResponse(
category: .user(
UserCategory(
id: userTodoCategory.id,
name: userTodoCategory.name,
colorHex: userTodoCategory.colorHex
)
),
isVisible: preference.isVisible
)
}
}
func toDomain() -> TodoCategoryPreference? {
switch category {
case .system(let rawValue):
guard let systemTodoCategory = SystemTodoCategory(rawValue: rawValue) else {
return nil
}
return TodoCategoryPreference(
category: .system(systemTodoCategory),
isVisible: isVisible
)
case .user(let userCategory):
return TodoCategoryPreference(
category: .user(
UserTodoCategory(
id: userCategory.id,
name: userCategory.name,
colorHex: userCategory.colorHex
)
),
isVisible: isVisible
)
}
}
}
extension Array where Element == TodoCategoryPreferenceResponse {
func toDomain() -> [TodoCategoryPreference] {
let preferences = compactMap { $0.toDomain() }
guard !preferences.isEmpty else {
return defaultTodoCategoryPreferences()
}
return mergedTodoCategoryPreferences(preferences)
}
}
private func defaultTodoCategoryPreferences() -> [TodoCategoryPreference] {
SystemTodoCategory.allCases.map {
TodoCategoryPreference(category: .system($0), isVisible: true)
}
}
private func mergedTodoCategoryPreferences(
_ preferences: [TodoCategoryPreference]
) -> [TodoCategoryPreference] {
var mergedPreferences = preferences
let existingSystemTodoCategories = Set<SystemTodoCategory>(
preferences.compactMap { preference in
guard case .system(let systemTodoCategory) = preference.category else {
return nil
}
return systemTodoCategory
}
)
for systemTodoCategory in SystemTodoCategory.allCases {
if existingSystemTodoCategories.contains(systemTodoCategory) { continue }
mergedPreferences.append(
TodoCategoryPreference(
category: .system(systemTodoCategory),
isVisible: true
)
)
}
return mergedPreferences
}