-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTodoDetailView.swift
More file actions
221 lines (201 loc) · 7.33 KB
/
Copy pathTodoDetailView.swift
File metadata and controls
221 lines (201 loc) · 7.33 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
//
// TodoDetailView.swift
// DevLogPresentation
//
// Created by opfic on 6/12/25.
//
import SwiftUI
import DevLogCore
import DevLogDomain
struct TodoDetailView: View {
@Environment(\.diContainer) private var container: DIContainer
@Environment(\.openWindow) private var openWindow
@Environment(\.isiOSAppOnMac) private var isiOSAppOnMac
@State var viewModel: TodoDetailViewModel
var body: some View {
ZStack {
Color(.systemGroupedBackground).ignoresSafeArea()
if let todo = viewModel.state.todo, let number = todo.number {
TodoDetailContentView(
title: todo.title,
content: todo.content,
referenceItems: viewModel.state.referenceItems,
number: number,
onOpenTodoID: { viewModel.send(.setSelectedTodoId(TodoIdItem(id: $0))) }
)
} else if viewModel.state.isLoading {
LoadingView()
}
}
.onAppear { viewModel.send(.onAppear) }
.navigationBarTitleDisplayMode(.inline)
.sheet(isPresented: Binding(
get: { viewModel.state.showInfo },
set: { viewModel.send(.setShowInfo($0)) }
)) {
sheetContent
}
.sheet(item: Binding(
get: { viewModel.state.selectedTodoId },
set: { viewModel.send(.setSelectedTodoId($0)) }
)) { item in
NavigationStack {
TodoDetailView(viewModel: TodoDetailViewModel(
fetchTodoUseCase: container.resolve(FetchTodoByIdUseCase.self),
fetchReferenceItemsUseCase: container.resolve(FetchReferenceItemsUseCase.self),
todoId: item.id,
showEditButton: false
))
.toolbar {
ToolbarLeadingButton {
viewModel.send(.setSelectedTodoId(nil))
}
}
}
.background(Color(.systemGroupedBackground))
.presentationDragIndicator(.visible)
}
.fullScreenCover(isPresented: Binding(
get: { viewModel.state.showEditor },
set: { viewModel.send(.setShowEditor($0)) }
)) {
if let todo = viewModel.state.todo {
TodoEditorView(
viewModel: TodoEditorViewModel(
todo: todo,
fetchPreferencesUseCase: container.resolve(FetchTodoCategoryPreferencesUseCase.self),
fetchReferenceItemsUseCase: container.resolve(FetchReferenceItemsUseCase.self),
upsertTodoUseCase: container.resolve(UpsertTodoUseCase.self),
onUpsertSuccess: { todo in
viewModel.send(.setShowEditor(false))
viewModel.send(.setTodo(todo))
}
)
)
}
}
.toolbar { toolbarContent }
}
@ToolbarContentBuilder
private var toolbarContent: some ToolbarContent {
ToolbarItem(placement: .topBarTrailing) {
Button {
viewModel.send(.setShowInfo(true))
} label: {
Image(systemName: "info.circle")
}
}
if viewModel.showEditButton {
if #available(iOS 26.0, *) {
ToolbarSpacer(.fixed, placement: .topBarTrailing)
}
ToolbarItem(placement: .topBarTrailing) {
Button {
openTodoEditor()
} label: {
Text(String(localized: "todo_edit"))
}
}
}
}
private func openTodoEditor() {
if isiOSAppOnMac {
guard let todo = viewModel.state.todo else { return }
openWindow(
id: TodoEditorWindowValue.sceneId,
value: TodoEditorWindowValue(todo: todo)
)
} else {
viewModel.send(.setShowEditor(true))
}
}
@ViewBuilder
private var sheetContent: some View {
if let todo = viewModel.state.todo {
TodoDetailInfoSheetView(todo: todo) {
viewModel.send(.setShowInfo(false))
}
}
}
}
private struct TodoDetailInfoSheetView: View {
let todo: Todo
let onClose: () -> Void
private let calendar = Calendar.current
var body: some View {
NavigationStack {
List {
Section(String(localized: "todo_options_section")) {
HStack {
Text(String(localized: "todo_category"))
Spacer()
Text(TodoCategoryItem(from: todo.category).localizedName)
.foregroundStyle(.secondary)
}
statusRow(
title: String(localized: "todo_completed"),
systemImage: todo.isCompleted ? "checkmark.circle.fill" : "circle",
color: todo.isCompleted ? .green : .secondary
)
statusRow(
title: String(localized: "todo_pinned"),
systemImage: todo.isPinned ? "star.fill" : "star",
color: todo.isPinned ? .orange : .secondary
)
HStack {
Text(String(localized: "todo_due_date"))
Spacer()
if let dueDate = todo.dueDate {
Tag(dueDateText(for: dueDate), isEditing: false)
.padding(.vertical, -4)
} else {
Text(String(localized: "todo_none"))
.foregroundStyle(.secondary)
}
}
}
Section(String(localized: "todo_tags")) {
if todo.tags.isEmpty {
Text(String(localized: "todo_no_tags"))
.foregroundStyle(.secondary)
.padding(.vertical, 4)
} else {
TagList(todo.tags)
}
}
}
.navigationTitle(String(localized: "todo_details"))
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarLeadingButton {
onClose()
}
}
}
}
@ViewBuilder
private func statusRow(
title: String,
systemImage: String,
color: Color
) -> some View {
HStack {
Text(title)
Spacer()
Image(systemName: systemImage)
.foregroundStyle(color)
}
}
private func dueDateText(for dueDate: Date) -> String {
let currentYear = calendar.component(.year, from: Date())
let dueDateYear = calendar.component(.year, from: dueDate)
if currentYear == dueDateYear {
return dueDate.formatted(
.dateTime.month(.defaultDigits).day(.defaultDigits)
)
}
return dueDate.formatted(
.dateTime.year(.twoDigits).month(.defaultDigits).day(.defaultDigits)
)
}
}