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

Refactor/quest view #113

Merged
merged 2 commits into from
Dec 8, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,8 @@ extension DefaultEnrollUseCase: EnrollUseCase {
}
.catchAndReturn(false)
.do(onSuccess: { _ in
let today = quests
.filter { quest in
Calendar.current.isDateInToday(quest.date)
}
if !today.isEmpty {
NotificationCenter.default.post(name: .updated, object: Date())
}
let dates = quests.map { $0.date }
NotificationCenter.default.post(name: .updated, object: dates)
})
.asObservable()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import RxCocoa
final class HomeViewModel {
private let questUseCase: QuestUseCase
private var calendarUseCase: CalendarUseCase
private var currentDate = Date()

init(questUseCase: QuestUseCase, calendarUseCase: CalendarUseCase) {
self.questUseCase = questUseCase
Expand Down Expand Up @@ -41,17 +42,26 @@ final class HomeViewModel {
.compactMap { $0.increaseCount() }
.flatMap(questUseCase.update(with:))
.filter({ $0 })
.map { _ in Date() }
.compactMap { [weak self] _ in self?.currentDate }
.asObservable()

let notification = NotificationCenter
.default
.rx
.notification(.updated)
.compactMap({ $0.object as? Date })
.compactMap({ $0.object as? [Date] })
.withUnretained(self)
.compactMap { owner, dates in
let result = dates.filter { date in
Calendar.current.isDate(owner.currentDate, inSameDayAs: date)
}

return !result.isEmpty ? owner.currentDate : nil
}

let data = Observable
.merge(updated, input.viewDidLoad, notification, input.daySelected)
.merge(updated, input.viewDidLoad, input.daySelected, notification)
.do(onNext: { [weak self] date in self?.currentDate = date })
.flatMap(questUseCase.fetch(by:))
.asDriver(onErrorJustReturn: [])

Expand Down