-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTodoManageViewModel.swift
More file actions
242 lines (208 loc) · 7.16 KB
/
Copy pathTodoManageViewModel.swift
File metadata and controls
242 lines (208 loc) · 7.16 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//
// TodoManageViewModel.swift
// DevLogPresentation
//
// Created by 최윤진 on 11/30/25.
//
import SwiftUI
import DevLogDomain
@Observable
final class TodoManageViewModel: Store {
struct State: Equatable {
var preferences: [TodoCategoryItem]
var category: TodoCategoryItem?
var showSheet: Bool = false
var showAlert: Bool = false
}
enum Action {
case tapAddUserCategory
case moveItem(from: IndexSet, target: Int)
case tapItem(TodoCategoryItem)
case tapEditUserCategory(TodoCategoryItem)
case tapDeleteUserCategory(TodoCategoryItem)
case confirmDeleteUserCategory
case setShowSheet(Bool)
case setShowAlert(Bool)
case setCategoryName(String)
case setCategoryColor(Color)
case setRandomCategoryColor
case saveUserCategory
}
enum SideEffect { }
private(set) var state: State
var isEditing: Bool {
guard let categoryItem = state.category else {
return false
}
return state.preferences.contains { $0.id == categoryItem.id }
}
var navigationTitle: String {
isEditing
? String(localized: "todo_manage_edit_category_title")
: String(localized: "todo_manage_add_category_title")
}
var submitTitle: String {
isEditing
? String(localized: "todo_manage_save")
: String(localized: "todo_add")
}
var placeholder: String {
guard
let item = state.category,
case .user(let category) = item.category
else {
return String(localized: "todo_manage_name_placeholder")
}
return category.name
}
var categoryNameCountText: String {
guard
let item = state.category,
case .user(let category) = item.category
else {
return "0/20"
}
return "\(category.name.count)/20"
}
var canSubmitUserCategory: Bool {
guard
let item = state.category,
case .user(let category) = item.category
else {
return false
}
let name = category.name.trimmingCharacters(in: .whitespacesAndNewlines)
if name.isEmpty {
return false
}
if SystemTodoCategory.allCases.contains(where: {
$0.rawValue.caseInsensitiveCompare(name) == .orderedSame
}) {
return false
}
if state.preferences.contains(where: { item in
guard case .user(let userCategory) = item.category,
userCategory.id != category.id else {
return false
}
return userCategory.name.caseInsensitiveCompare(name) == .orderedSame
}) {
return false
}
if let item = state.preferences.first(where: { $0.id == item.id }),
case .user(let originalCategory) = item.category {
let originalName = originalCategory.name.trimmingCharacters(in: .whitespacesAndNewlines)
if originalName == name && originalCategory.colorHex == category.colorHex {
return false
}
}
return true
}
init(_ preferences: [TodoCategoryItem]) {
self.state = State(preferences: preferences)
}
func reduce(with action: Action) -> [SideEffect] {
var state = self.state
switch action {
case .tapAddUserCategory:
guard let randomHexValue = Color.randomValue.hexValue else {
break
}
state.category = TodoCategoryItem(
from: .user(
UserTodoCategory(
id: UUID().uuidString.lowercased(),
name: "",
colorHex: randomHexValue
)
)
)
state.showSheet = true
case .moveItem(let from, let target):
state.preferences.move(fromOffsets: from, toOffset: target)
case .tapItem(let item):
if let index = state.preferences.firstIndex(where: { $0.id == item.id }) {
state.preferences[index].isVisible.toggle()
}
case .tapEditUserCategory(let item):
guard item.isUserCategory else {
break
}
state.category = item
state.showSheet = true
case .tapDeleteUserCategory(let item):
guard item.isUserCategory else {
break
}
state.category = item
state.showAlert = true
case .confirmDeleteUserCategory:
guard let categoryItem = state.category else {
break
}
if let index = state.preferences.firstIndex(where: { $0.id == categoryItem.id }) {
state.preferences.remove(at: index)
}
state.showAlert = false
state.category = nil
case .setShowSheet(let isPresented):
state.showSheet = isPresented
if !isPresented {
state.category = nil
}
case .setShowAlert(let isPresented):
state.showAlert = isPresented
if !isPresented {
state.category = nil
}
case .setCategoryName(let name):
guard var item = state.category,
case .user(var category) = item.category else {
break
}
category.name = String(name.prefix(20))
item.category = .user(category)
state.category = item
case .setCategoryColor(let color):
guard var item = state.category,
case .user(var category) = item.category,
let hexValue = color.hexValue else {
break
}
category.colorHex = hexValue
item.category = .user(category)
state.category = item
case .setRandomCategoryColor:
guard var item = state.category,
case .user(var category) = item.category,
let randomHexValue = Color.randomValue.hexValue else {
break
}
category.colorHex = randomHexValue
item.category = .user(category)
state.category = item
case .saveUserCategory:
guard var item = state.category,
case .user(let category) = item.category else {
break
}
item.category = .user(
UserTodoCategory(
id: category.id,
name: category.name.trimmingCharacters(in: .whitespacesAndNewlines),
colorHex: category.colorHex
)
)
if let index = state.preferences.firstIndex(where: { $0.id == item.id }) {
item.isVisible = state.preferences[index].isVisible
state.preferences[index] = item
} else {
state.preferences.append(item)
}
state.showSheet = false
state.category = nil
}
if self.state != state { self.state = state }
return []
}
}