Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion DevLog/Presentation/ViewModel/TodoEditorViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@ import OrderedCollections

@Observable
final class TodoEditorViewModel: Store {
private struct Draft: Equatable {
let title: String
let content: String
let dueDate: Date?
let tags: [String]

init(todo: Todo) {
self.title = todo.title
self.content = todo.content
self.dueDate = todo.dueDate
self.tags = todo.tags
}

init(state: State) {
self.title = state.title
self.content = state.content
self.dueDate = state.dueDate
self.tags = Array(state.tags)
}
}

struct State: Equatable {
var title: String = ""
var content: String = ""
Expand Down Expand Up @@ -52,6 +73,16 @@ final class TodoEditorViewModel: Store {
private let createdAt: Date?
private let completedAt: Date?
private let kind: TodoKind
private let originalDraft: Draft?

var hasChanges: Bool {
guard let originalDraft else { return true }
return originalDraft != Draft(state: state)
}

var isReadyToSubmit: Bool {
state.isValidToSave && hasChanges
}

// 새로운 Todo 생성용 생성자
init(kind: TodoKind) {
Expand All @@ -63,6 +94,7 @@ final class TodoEditorViewModel: Store {
self.createdAt = nil
self.completedAt = nil
self.kind = kind
self.originalDraft = nil
}

// 기존 Todo 편집용 생성자
Expand All @@ -75,6 +107,7 @@ final class TodoEditorViewModel: Store {
self.createdAt = todo.createdAt
self.completedAt = todo.completedAt
self.kind = todo.kind
self.originalDraft = Draft(todo: todo)
state.title = todo.title
state.content = todo.content
state.dueDate = todo.dueDate
Expand Down Expand Up @@ -134,7 +167,7 @@ extension TodoEditorViewModel {
}
}

func upsertTodo() -> Todo {
func makeTodo() -> Todo {
let date = Date()
return Todo(
id: self.id,
Expand Down
32 changes: 7 additions & 25 deletions DevLog/UI/Home/TodoEditorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,11 @@ struct TodoEditorView: View {
.navigationBarTitleDisplayMode(.inline)
.toolbarBackground(.background, for: .navigationBar)
.toolbar {
ToolbarLeadingButton {
dismiss()
}
ToolbarLeadingButton { dismiss() }
ToolbarTrailingButton {
onSubmit?(viewModel.upsertTodo())
dismiss()
submit()
}
.disabled(!viewModel.state.isValidToSave)
.disabled(!viewModel.isReadyToSubmit)
}
}
}
Expand Down Expand Up @@ -166,25 +163,10 @@ struct TodoEditorView: View {
}
}

@ToolbarContentBuilder
private var toolBar: some ToolbarContent {
ToolbarItem(placement: .topBarLeading) {
Button {
dismiss()
} label: {
Image(systemName: "xmark")
.bold()
}
}
ToolbarItem(placement: .topBarTrailing) {
Button(action: {
onSubmit?(viewModel.upsertTodo())
dismiss()
}) {
Text("추가")
}
.disabled(!viewModel.state.isValidToSave)
}
private func submit() {
let todo = viewModel.makeTodo()
onSubmit?(todo)
dismiss()
}
Comment on lines +166 to 170
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

submit() 함수는 viewModel.isReadyToSubmittrue일 때만 활성화되는 버튼에 의해 호출됩니다. 따라서 함수 내에서 !viewModel.isReadyToSubmit을 다시 확인하는 것은 중복입니다. 이 중복 확인을 제거하여 코드를 더 간결하게 만들 수 있습니다.

    private func submit() {
        let todo = viewModel.makeTodo()
        onSubmit?(todo)
        dismiss()
    }


private enum Field: Hashable {
Expand Down