Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/quest view cell actions #70

Merged
merged 6 commits into from
Dec 1, 2022
14 changes: 10 additions & 4 deletions DailyQuest/DailyQuest/Domain/Entities/Quest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@ struct Quest {
- Parameters:
- value: 0๋ณด๋‹ค ํฐ ์ •์ˆ˜๊ฐ’์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ 1์ž…๋‹ˆ๋‹ค.
*/
mutating func increaseCount(with value: Int=1) {
// mutating func increaseCount(with value: Int=1) {
// guard currentCount + value <= totalCount else {
// self.currentCount = totalCount
// return
// }
// self.currentCount += value
// }
func increaseCount(with value: Int=1) -> Self? {
guard currentCount + value <= totalCount else {
self.currentCount = totalCount
return
return nil
}
self.currentCount += value
return .init(groupId: groupId, uuid: uuid, date: date, title: title, currentCount: currentCount+value, totalCount: totalCount)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@ extension DefaultQuestUseCase: QuestUseCase {
func fetch(by date: Date) -> Observable<[Quest]> {
return questsRepository.fetch(by: date)
}

func update(with quest: Quest) -> Observable<Bool> {
return questsRepository
.update(with: quest)
.map { _ in true }
.catchAndReturn(false)
.asObservable()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ import RxSwift

protocol QuestUseCase {
func fetch(by date: Date) -> Observable<[Quest]>
func update(with quest: Quest) -> Observable<Bool>
}
10 changes: 6 additions & 4 deletions DailyQuest/DailyQuest/Presentation/Home/View/QuestView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ final class QuestView: UITableView {

func setup(with viewModel: QuestViewModel) {
self.viewModel = viewModel

bind()
}

private func bind() {
let output = viewModel.transform(input: QuestViewModel.Input(viewDidLoad: .just(Date()).asObservable()), disposeBag: disposableBag)
func bind() {
let output = viewModel.transform(
input: QuestViewModel
.Input(viewDidLoad: .just(Date()).asObservable(),
itemDidClicked: rx.modelSelected(Quest.self).asObservable())
)

output
.data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ final class HomeViewController: UIViewController {
}

private func bind() {
questView.bind()

/**
Header์—์„œ ๋ฒ„ํŠผ์ด ๋ˆŒ๋ ค์กŒ๋Š”์ง€๋ฅผ ์ˆ˜์‹ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
๋ฒ„ํŠผ์ด ๋ˆŒ๋Ÿฌ์ง€๋ฉด bind๋‚ด์˜ ํด๋กœ์ €๊ฐ€ ์‹คํ–‰๋˜๊ณ , ์ด๋Š” ๋‹ค์‹œ coordinatorPublisher๊ฐ€ ์ด๋ฒคํŠธ๋ฅผ ๋ฐฉ์ถœํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,28 @@ final class QuestViewModel {

struct Input {
let viewDidLoad: Observable<Date>
let itemDidClicked: Observable<Quest>
}

struct Output {
let data: Driver<[Quest]>
}

func transform(input: Input, disposeBag: DisposeBag) -> Output {
func transform(input: Input) -> Output {

let data = input
.viewDidLoad
let updated = input
.itemDidClicked
.compactMap { $0.increaseCount() }
.flatMap(questUseCase.update(with:))
.filter({ $0 })
.map { _ in Date() }
.asObservable()

let data = Observable
.merge(updated, input.viewDidLoad)
.flatMap(questUseCase.fetch(by:))
.asDriver(onErrorJustReturn: [])

return Output(data: data)
}
}