Skip to content

Commit adb6f3d

Browse files
authored
Merge pull request #70 from boostcampwm-2022/feature/QuestViewCellActions
Feature/quest view cell actions
2 parents fa42e04 + 02f9e41 commit adb6f3d

File tree

6 files changed

+40
-12
lines changed

6 files changed

+40
-12
lines changed

DailyQuest/DailyQuest/Domain/Entities/Quest.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,18 @@ struct Quest {
2626
- Parameters:
2727
- value: 0보다 큰 정수값입니다. 기본값은 1입니다.
2828
*/
29-
mutating func increaseCount(with value: Int=1) {
29+
// mutating func increaseCount(with value: Int=1) {
30+
// guard currentCount + value <= totalCount else {
31+
// self.currentCount = totalCount
32+
// return
33+
// }
34+
// self.currentCount += value
35+
// }
36+
func increaseCount(with value: Int=1) -> Self? {
3037
guard currentCount + value <= totalCount else {
31-
self.currentCount = totalCount
32-
return
38+
return nil
3339
}
34-
self.currentCount += value
40+
return .init(groupId: groupId, uuid: uuid, date: date, title: title, currentCount: currentCount+value, totalCount: totalCount)
3541
}
3642

3743
/**

DailyQuest/DailyQuest/Domain/UseCases/Home/DefaultQuestUseCase.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,12 @@ extension DefaultQuestUseCase: QuestUseCase {
2121
func fetch(by date: Date) -> Observable<[Quest]> {
2222
return questsRepository.fetch(by: date)
2323
}
24+
25+
func update(with quest: Quest) -> Observable<Bool> {
26+
return questsRepository
27+
.update(with: quest)
28+
.map { _ in true }
29+
.catchAndReturn(false)
30+
.asObservable()
31+
}
2432
}

DailyQuest/DailyQuest/Domain/UseCases/Home/Protocols/QuestUseCase.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ import RxSwift
1111

1212
protocol QuestUseCase {
1313
func fetch(by date: Date) -> Observable<[Quest]>
14+
func update(with quest: Quest) -> Observable<Bool>
1415
}

DailyQuest/DailyQuest/Presentation/Home/View/QuestView.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ final class QuestView: UITableView {
2828

2929
func setup(with viewModel: QuestViewModel) {
3030
self.viewModel = viewModel
31-
32-
bind()
3331
}
3432

35-
private func bind() {
36-
let output = viewModel.transform(input: QuestViewModel.Input(viewDidLoad: .just(Date()).asObservable()), disposeBag: disposableBag)
33+
func bind() {
34+
let output = viewModel.transform(
35+
input: QuestViewModel
36+
.Input(viewDidLoad: .just(Date()).asObservable(),
37+
itemDidClicked: rx.modelSelected(Quest.self).asObservable())
38+
)
3739

3840
output
3941
.data

DailyQuest/DailyQuest/Presentation/Home/ViewController/HomeViewController.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ final class HomeViewController: UIViewController {
5656
}
5757

5858
private func bind() {
59+
questView.bind()
60+
5961
/**
6062
Header에서 버튼이 눌려졌는지를 수신하고 있습니다.
6163
버튼이 눌러지면 bind내의 클로저가 실행되고, 이는 다시 coordinatorPublisher가 이벤트를 방출하게 합니다.

DailyQuest/DailyQuest/Presentation/Home/ViewModel/QuestViewModel.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,28 @@ final class QuestViewModel {
1919

2020
struct Input {
2121
let viewDidLoad: Observable<Date>
22+
let itemDidClicked: Observable<Quest>
2223
}
2324

2425
struct Output {
2526
let data: Driver<[Quest]>
2627
}
2728

28-
func transform(input: Input, disposeBag: DisposeBag) -> Output {
29+
func transform(input: Input) -> Output {
2930

30-
let data = input
31-
.viewDidLoad
31+
let updated = input
32+
.itemDidClicked
33+
.compactMap { $0.increaseCount() }
34+
.flatMap(questUseCase.update(with:))
35+
.filter({ $0 })
36+
.map { _ in Date() }
37+
.asObservable()
38+
39+
let data = Observable
40+
.merge(updated, input.viewDidLoad)
3241
.flatMap(questUseCase.fetch(by:))
3342
.asDriver(onErrorJustReturn: [])
34-
43+
3544
return Output(data: data)
3645
}
3746
}

0 commit comments

Comments
 (0)