Skip to content

Commit

Permalink
Fix(HomePresent): Fix todo sort logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dodo849 committed Jul 30, 2024
1 parent b5d544b commit fab5903
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@ public struct TodoItemEntity: Codable, Identifiable, Equatable {
/// Contents of the todo item
public let contents: String
/// Status of the todo item
public var status: TodoItemStatus
public var status: Status

public init(
id: Int,
contents: String,
status: TodoItemStatus
status: Status
) {
self.id = id
self.contents = contents
self.status = status
}
}

public enum TodoItemStatus: String, Codable {
case done
case pending
public extension TodoItemEntity {
enum Status: String, Codable {
case done
case pending
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public struct TodoOrganizationEntity: Codable, Identifiable, Equatable {
id: 2,
name: "즐거운 동아리",
todos: [
.init(id: 4, contents: "4차 세션 사전 과제", status: .pending),
.init(id: 5, contents: "노션 이메일 제출", status: .pending),
.init(id: 3, contents: "4차 세션 사전 과제", status: .pending),
.init(id: 4, contents: "노션 이메일 제출", status: .pending),
.init(id: 5, contents: "뒷풀이 참석 여부 투표 뒷풀이 참석 여부 투표 뒷풀이 참석 여부 투표 뒷풀이 참석 여부 투표 뒷풀이 참석 여부 투표 뒷풀이 참석 여부 투표 뒷풀이 참석 여부 투표 뒷풀이 참석 여부 투표", status: .pending),
.init(id: 6, contents: "7월 회비 납부", status: .pending)
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import TodoEntity
struct TodoPageConverter {
static func convertToTodoSections(
_ entities: [TodoOrganizationEntity],
onTodoItemTap: @escaping (TodoItemEntity) -> Bool
onTodoItemTap: @escaping (TodoItemEntity) -> Void
) -> [any CompositionalSection] {
return entities.map { organizationEntity in
TodoSection(
Expand All @@ -20,9 +20,10 @@ struct TodoPageConverter {
items: organizationEntity.todos.map { todoEntity in
TodoItem(
id: todoEntity.id,
status: todoEntity.status,
contents: todoEntity.contents,
onTap: {
return onTodoItemTap(todoEntity)
onTodoItemTap(todoEntity)
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,31 @@ final class TodoItem: CompositionalItem {
typealias Cell = TodoItemCell

// MARK: Event
let onTap: () -> Bool
let onTap: () -> Void

// MARK: Data
let id: Int
let status: TodoItemEntity.Status
let contents: String

// MARK: DisposeBag
let disposeBag = DisposeBag()

init(
id: Int,
status: TodoItemEntity.Status,
contents: String,
onTap: @escaping () -> Bool
onTap: @escaping () -> Void
) {
self.id = id
self.status = status
self.contents = contents
self.onTap = onTap
}

func hash(into hasher: inout Hasher) {
hasher.combine(id)
hasher.combine(status)
hasher.combine(contents)
}
}
Expand Down Expand Up @@ -72,11 +76,12 @@ final class TodoItemCell: UIView, CompositionalItemCell {
func configure(with item: TodoItem) {
todo.text = item.contents

todo.status = item.status == .pending ? .pending : .done

todo.rx.tapGesture()
.subscribe(onNext: { [weak self] _ in
let result = item.onTap()

self?.todo.status = result ? .done : .pending
.when(.recognized)
.subscribe(onNext: { _ in
item.onTap()
})
.disposed(by: disposeBag)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ class TodoPageReactor: Reactor {
// MARK: Action
enum Action {
case viewWillAppear
case tapTodoItem(TodoItemEntity)
case tapTodo(TodoItemEntity)
}

enum Mutation {
case setOrganizations([TodoOrganizationEntity])
case updateTodoItemStatus(TodoItemEntity)
}

// MARK: State
Expand All @@ -43,7 +44,8 @@ class TodoPageReactor: Reactor {
case .viewWillAppear:
var temp = TodoOrganizationEntity.mock
temp = temp.map { organization in
let newTodos = organization.todos.sorted { $0.status == .pending && $1.status == .done }
let newTodos = organization.todos
.sorted { $0.status == .pending && $1.status == .done }
return TodoOrganizationEntity(
id: organization.id,
name: organization.name,
Expand All @@ -52,9 +54,20 @@ class TodoPageReactor: Reactor {
}
return .just(.setOrganizations(temp))

case let .tapTodoItem(todo):
var temp = TodoOrganizationEntity.mock
temp = temp.map { organization in
case let .tapTodo(todo):
return .just(.updateTodoItemStatus(todo))
}
}

func reduce(state: State, mutation: Mutation) -> State {
var state = state
switch mutation {
case let .setOrganizations(organizations):
state.organizations = organizations

case let .updateTodoItemStatus(todo):
let newOrganizations = state.organizations.map { organization in
// Update target todo status
var newTodos = organization.todos.map { todoEntity -> TodoItemEntity in
if todoEntity.id == todo.id {
var updatedTodo = todoEntity
Expand All @@ -63,22 +76,17 @@ class TodoPageReactor: Reactor {
}
return todoEntity
}

// Sort todos by status
newTodos.sort { $0.status == .pending && $1.status == .done }

return TodoOrganizationEntity(
id: organization.id,
name: organization.name,
todos: newTodos
)
}
return .just(.setOrganizations(temp))
}
}

func reduce(state: State, mutation: Mutation) -> State {
var state = state
switch mutation {
case let .setOrganizations(organizations):
state.organizations = organizations
state.organizations = newOrganizations
}
return state
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,8 @@ class TodoPageViewController: BaseViewController<TodoPageView> {
entities,
onTodoItemTap: { [weak owner] todoEntity in
owner?.reactor.action.onNext(
.tapTodoItem(todoEntity)
.tapTodo(todoEntity)
)

return owner?.reactor.currentState.organizations
.flatMap { $0.todos }
.first { $0.id == todoEntity.id }?.status == .done
}
)
}
Expand Down

0 comments on commit fab5903

Please sign in to comment.