Skip to content

Commit

Permalink
๐Ÿ› [fix] ๋งˆ์„ ์ฃผ๋ฏผ์„ ๊ธธ๊ฒŒ ๋ˆ„๋ฅด๋ฉด ์ฒดํฌํ•ด๋‘์—ˆ๋˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ฌ๋ผ์ง€๋Š” ๋ฌธ์ œ ๊ฐœ์„ 
Browse files Browse the repository at this point in the history
  • Loading branch information
leeari95 committed Dec 6, 2024
1 parent 16f9031 commit ad4cd86
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@ final class VillagersSectionReactor: Reactor {
enum Action {
case fetch
case villagerLongPress(indexPath: IndexPath)
case villagersChecked(checked: Villager)
case resetCheckedVillagers
}

enum Mutation {
case transition(route: DashboardCoordinator.Route)
case setVillagers(_ villagers: [Villager])
case setCheckedVillager(_ villagers: Villager)
case resetCheckedVillagers
}

struct State {
var villagers: [Villager] = []
var checkedVillagers: [Villager] = []
}

let initialState: State = State()
Expand All @@ -43,6 +48,12 @@ final class VillagersSectionReactor: Reactor {
return Observable.empty()
}
return Observable.just(Mutation.transition(route: .villagerDetail(villager: villager)))

case let .villagersChecked(checkedVillager):
return Observable.just(Mutation.setCheckedVillager(checkedVillager))

case .resetCheckedVillagers:
return Observable.just(Mutation.resetCheckedVillagers)
}
}

Expand All @@ -53,6 +64,14 @@ final class VillagersSectionReactor: Reactor {
coordinator?.transition(for: route)
case .setVillagers(let villagers):
newState.villagers = villagers
case let .setCheckedVillager(villager):
if let index = newState.checkedVillagers.firstIndex(where: { $0.name == villager.name }) {
newState.checkedVillagers.remove(at: index)
} else {
newState.checkedVillagers.append(villager)
}
case .resetCheckedVillagers:
newState.checkedVillagers = []
}
return newState
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ final class VillagersView: UIView {
)
) { _, villager, cell in
cell.setImage(url: villager.iconImage)
if reactor.currentState.checkedVillagers.contains(where: { $0.name == villager.name }) {
cell.checkMark()
}
}.disposed(by: disposeBag)

reactor.state
Expand All @@ -151,12 +154,16 @@ final class VillagersView: UIView {
HapticManager.shared.selection()
let cell = self?.collectionView.cellForItem(at: indexPath) as? IconCell
cell?.checkMark()
if let villager = reactor.currentState.villagers[safe: indexPath.item] {
reactor.action.onNext(.villagersChecked(checked: villager))
}
}).disposed(by: disposeBag)

resetButton.rx.tap
.subscribe(onNext: { [weak self] _ in
let cells = self?.collectionView.visibleCells as? [IconCell]
cells?.forEach { $0.removeCheckMark() }
reactor.action.onNext(.resetCheckedVillagers)
}).disposed(by: disposeBag)
}
}
Expand Down

0 comments on commit ad4cd86

Please sign in to comment.