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
20 changes: 16 additions & 4 deletions DevLog/Data/Protocol/Dictionaryable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,31 @@
// Created by 최윤진 on 12/14/25.
//

import Foundation
import FirebaseFirestore

protocol Dictionaryable: Encodable {
func toDictionary() -> [String: Any]
}

extension Dictionaryable {
func toDictionary() -> [String: Any] {
guard let encodedData = try? JSONEncoder().encode(self),
var dictionary = (try? JSONSerialization.jsonObject(with: encodedData)) as? [String: Any]
else { return [:] }
let encoder = Firestore.Encoder()
guard var dictionary = try? encoder.encode(self) else { return [:] }

let mirror = Mirror(reflecting: self)
for child in mirror.children {
guard let key = child.label else { continue }
if isNilValue(child.value) {
dictionary[key] = NSNull()
}
}

dictionary.removeValue(forKey: "id")
return dictionary
}

private func isNilValue(_ value: Any) -> Bool {
let mirror = Mirror(reflecting: value)
return mirror.displayStyle == .optional && mirror.children.isEmpty
}
}
13 changes: 12 additions & 1 deletion DevLog/Presentation/ViewModel/TodoViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,18 @@ final class TodoViewModel: Store {
}
}
case .togglePinned(let todo):
break
Task {
do {
defer { send(.didLoading(false)) }
send(.didLoading(true))
var todo = todo
todo.isPinned.toggle()
try await upsertTodoUseCase.execute(todo)
send(.didTogglePinned(todo))
} catch {
send(.didShowAlert(error.localizedDescription))
}
}
case .swipeTodo(let todo):
break
}
Expand Down