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/friend view calendar fetch #149

Merged
merged 6 commits into from
Dec 13, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import RxSwift

import RxSwift

final class DefaultFriendCalendarUseCase: FriendCalendarUseCase {
final class DefaultFriendCalendarUseCase: CalendarUseCase {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ธฐ์กด๊ณผ ๋งฅ๋ฝ์ƒ ๋™์ผํ•œ ๋กœ์ง์ด๋ผ๋ฉด ๋Œ๋ ค์“ฐ๋Š”๊ฒŒ ์ข‹๊ธดํ•œ๊ฒƒ๊ฐ™์Šต๋‹ˆ๋‹ค !

private let user: User
private let questsRepository: QuestsRepository
private let disposeBag = DisposeBag()
Expand All @@ -27,20 +27,104 @@ final class DefaultFriendCalendarUseCase: FriendCalendarUseCase {

func setupMonths() {
let currentMonth = try? currentMonth.value()
let startDayOfLastMonth = currentMonth?.startDayOfLastMonth
let startDayOfCurrentMonth = currentMonth?.startDayOfCurrentMonth
let startDayOfNextMonth = currentMonth?.startDayOfNextMonth

let months = [startDayOfLastMonth, startDayOfCurrentMonth, startDayOfNextMonth]

let months = startDayOfCurrentMonth

Observable.just(months)
.flatMap(fetchAMontlyCompletion(_:))
.map { [[], $0, []] }
.bind(to: completionOfMonths)
Observable.from(months)
.concatMap { [weak self] monthDate in
guard let self else { return Observable<[DailyQuestCompletion]>.empty() }

return self.fetchAMontlyCompletion(monthDate)
}
.toArray()
.subscribe(onSuccess: { [weak self] completionOfMonths in
self?.completionOfMonths.onNext(completionOfMonths)
})
.disposed(by: disposeBag)
}

func selectDate(_ date: Date) {
selectedDate.onNext(date)
}

func fetchNextMontlyCompletion() {
guard let nextMonth = try? currentMonth.value()?.startDayOfNextMonth else { return }
currentMonth.onNext(nextMonth)

let monthAfterNext = nextMonth.startDayOfNextMonth

fetchAMontlyCompletion(monthAfterNext)
.subscribe(onNext: { [weak self] monthlyCompletion in
guard
let self,
var values = try? self.completionOfMonths.value()
else {
return
}

values.removeFirst()
values.append(monthlyCompletion)

self.completionOfMonths.onNext(values)
})
.disposed(by: disposeBag)
}

func fetchLastMontlyCompletion() {
guard let lastMonth = try? currentMonth.value()?.startDayOfLastMonth else { return }
currentMonth.onNext(lastMonth)

let monthBeforeLast = lastMonth.startDayOfLastMonth

fetchAMontlyCompletion(monthBeforeLast)
.subscribe(onNext: { [weak self] monthlyCompletion in
guard
let self,
var values = try? self.completionOfMonths.value()
else {
return
}

values.removeLast()
values.insert(monthlyCompletion, at: 0)

self.completionOfMonths.onNext(values)
})
.disposed(by: disposeBag)
}

func refreshMontlyCompletion(for date: Date) {
guard
let months = try? self.completionOfMonths.value(),
let index = months.firstIndex(where: { month in
month.contains { dailyQuestCompletion in
(dailyQuestCompletion.state != .hidden)
&& (dailyQuestCompletion.day.startOfDay == date.startOfDay)
}
}),
let reloadMonth = months[index].last?.day.startDayOfCurrentMonth
else {
return
}

fetchAMontlyCompletion(reloadMonth)
.subscribe(onNext: { [weak self] monthlyCompletion in
guard
let self,
var values = try? self.completionOfMonths.value()
else {
return
}

values[index] = monthlyCompletion

self.completionOfMonths.onNext(values)
})
.disposed(by: disposeBag)
}
}

extension DefaultFriendCalendarUseCase {
Expand All @@ -62,31 +146,27 @@ extension DefaultFriendCalendarUseCase {
private func fetchAMontlyCompletion(_ month: Date?) -> Observable<[DailyQuestCompletion]> {
guard let month = month else { return .empty() }

return Observable.just(month)
.concatMap { [weak self] date -> Observable<DailyQuestCompletion> in
guard let self else { return Observable.empty() }

return self.questsRepository
.fetch(by: self.user.uuid, date: date, filter: date)
.asObservable()
.map { quests -> DailyQuestCompletion in
let isSelected = (try? self.selectedDate.value().startOfDay == date) ?? false

return questsRepository.fetch(by: self.user.uuid, date: month, filter: month)
.map { dict -> [DailyQuestCompletion] in
let completionEndDaysOfLastMonth = month.rangeFromStartWeekdayOfLastMonthToEndDayOfCurrentMonth
.map { date -> DailyQuestCompletion in
return DailyQuestCompletion(
day: date,
state: self.calculateDailyState(quests),
isSelected: isSelected
)
}
}
.toArray()
.map { states in
let firstWeekStates = month.rangeFromStartWeekdayOfLastMonthToEndDayOfCurrentMonth
.map { date -> DailyQuestCompletion in
return DailyQuestCompletion(day: date, state: .hidden, isSelected: false)
state: .hidden,
isSelected: false)
}

return firstWeekStates + states
let completionOfMonths = dict.keys.sorted().map { date -> DailyQuestCompletion in
let state = self.calculateDailyState(dict[date] ?? [])
let isSelected = (try? self.selectedDate.value().startOfDay == date) ?? false

return DailyQuestCompletion(
day: date,
state: state,
isSelected: isSelected)
}

return completionEndDaysOfLastMonth + completionOfMonths
}
.asObservable()
}
Expand Down