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

๐Ÿ”€ :: (#1148) ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ๋กœ๋”ฉ ์ค‘ ํ•„ํ„ฐ๋‚˜ ์ •๋ ฌ์„ ๋ˆ„๋ฅผ ๋•Œ ์•ฑ์ด ํ„ฐ์ง€๋Š” ๋ฒ„๊ทธ #1151

Merged
merged 10 commits into from
Aug 16, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,16 @@ extension ListSearchResultReactor {
private func updateSortType(_ type: SortType) -> Observable<Mutation> {
return .concat([
.just(.updateSortType(type)),
updateDataSource(order: type, text: self.text, scrollPage: 1, byOption: true)
updateDataSource(order: type, text: self.text, scrollPage: 1)
])
}

private func updateDataSource(
order: SortType,
text: String,
scrollPage: Int,
byOption: Bool = false // ํ•„ํ„ฐ๋˜๋Š” ์˜ต์…˜์œผ๋กœ ๋ฆฌํ”„๋ž˜์‰ฌ ํ•˜๋‚˜ , ์•„๋‹ˆ๋ฉด ์Šคํฌ๋กค์ด๋ƒ
scrollPage: Int
) -> Observable<Mutation> {
let prev: [SearchPlaylistEntity] = byOption ? [] : self.currentState.dataSource
let prev: [SearchPlaylistEntity] = scrollPage == 1 ? [] : self.currentState.dataSource

return .concat([
.just(Mutation.updateLoadingState(true)),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Localization
import LogManager
import ReactorKit
import RxCocoa
import RxSwift
import SearchDomainInterface
import SongsDomainInterface

Expand Down Expand Up @@ -41,6 +43,8 @@ final class SongSearchResultReactor: Reactor {
private let fetchSearchSongsUseCase: any FetchSearchSongsUseCase
private let text: String
private let limit: Int = 20
private var requestDisposeBag = DisposeBag()
private let subject = PublishSubject<Mutation>()

init(text: String, fetchSearchSongsUseCase: any FetchSearchSongsUseCase) {
self.initialState = State(
Expand Down Expand Up @@ -112,6 +116,20 @@ final class SongSearchResultReactor: Reactor {

return newState
}

func transform(mutation: Observable<Mutation>) -> Observable<Mutation> {
let flatMapMutation = subject
.withUnretained(self)
.flatMap { owner, subjectMutation -> Observable<Mutation> in
return .concat([
.just(subjectMutation),
.just(Mutation.updateScrollPage(owner.currentState.scrollPage + 1)),
.just(Mutation.updateLoadingState(false))
])
}

return Observable.merge(mutation, flatMapMutation)
}
}

extension SongSearchResultReactor {
Expand All @@ -121,7 +139,7 @@ extension SongSearchResultReactor {
return .concat([
.just(.updateSelectedCount(0)),
.just(.updateSortType(type)),
updateDataSource(order: type, filter: state.filterType, text: self.text, scrollPage: 1, byOption: true)
updateDataSource(order: type, filter: state.filterType, text: self.text, scrollPage: 1)
])
}

Expand All @@ -131,47 +149,48 @@ extension SongSearchResultReactor {
return .concat([
.just(.updateSelectedCount(0)),
.just(.updateFilterType(type)),
updateDataSource(order: state.sortType, filter: type, text: self.text, scrollPage: 1, byOption: true)
updateDataSource(order: state.sortType, filter: type, text: self.text, scrollPage: 1)
])
}

private func updateDataSource(
order: SortType,
filter: FilterType,
text: String,
scrollPage: Int,
byOption: Bool = false // ํ•„ํ„ฐ๋˜๋Š” ์˜ต์…˜์œผ๋กœ ๋ฆฌํ”„๋ž˜์‰ฌ ํ•˜๋‚˜ , ์•„๋‹ˆ๋ฉด ์Šคํฌ๋กค์ด๋ƒ
scrollPage: Int
) -> Observable<Mutation> {
return .concat([
.just(.updateLoadingState(true)),
fetchSearchSongsUseCase
.execute(order: order, filter: filter, text: text, page: scrollPage, limit: limit)
.asObservable()
.map { [weak self] dataSource -> Mutation in

guard let self else { return .updateDataSource(dataSource: [], canLoad: false) }

let prev: [SongEntity] = byOption ? [] : self.currentState.dataSource

if scrollPage == 1 {
LogManager.analytics(SearchAnalyticsLog.viewSearchResult(
keyword: self.text,
category: "song",
count: dataSource.count
))
}

return Mutation.updateDataSource(dataSource: prev + dataSource, canLoad: dataSource.count == limit)
requestDisposeBag = DisposeBag() // ๊ธฐ์กด ์ž‘์—… ์บ”์Šฌ

fetchSearchSongsUseCase
.execute(order: order, filter: filter, text: text, page: scrollPage, limit: limit)
.asObservable()
.map { [weak self] dataSource -> Mutation in

guard let self else { return .updateDataSource(dataSource: [], canLoad: false) }

let prev: [SongEntity] = scrollPage == 1 ? [] : self.currentState.dataSource
if scrollPage == 1 {
LogManager.analytics(SearchAnalyticsLog.viewSearchResult(
keyword: self.text,
category: "song",
count: dataSource.count
))
}
.catch { error in
let wmErorr = error.asWMError
return Observable.just(
Mutation.showToast(wmErorr.errorDescription ?? LocalizationStrings.unknownErrorWarning)
)
},
.just(Mutation.updateScrollPage(scrollPage + 1)), // ์Šคํฌ๋กค ํŽ˜์ด์ง€ ์ฆ๊ฐ€
.just(.updateLoadingState(false))
])

return Mutation.updateDataSource(dataSource: prev + dataSource, canLoad: dataSource.count == limit)
}
.catch { error in
let wmErorr = error.asWMError
return Observable.just(
Mutation.showToast(wmErorr.errorDescription ?? LocalizationStrings.unknownErrorWarning)
)
}
.bind(with: subject, onNext: { subject, mutation in
subject.onNext(mutation)
})
.disposed(by: requestDisposeBag)

return Observable.just(.updateLoadingState(true))
}

func updateItemSelected(_ index: Int) -> Observable<Mutation> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class ListSearchResultViewController: BaseReactorViewController<ListSearch

private lazy var collectionView: UICollectionView = createCollectionView().then {
$0.backgroundColor = DesignSystemAsset.BlueGrayColor.gray100.color
$0.bounces = false
$0.isHidden = true
}

private lazy var headerView: SearchOptionHeaderView = SearchOptionHeaderView(false)
Expand Down Expand Up @@ -122,32 +122,36 @@ final class ListSearchResultViewController: BaseReactorViewController<ListSearch
}
.disposed(by: disposeBag)

sharedState.map { ($0.isLoading, $0.dataSource) }
.bind(with: self) { owner, info in

let (isLoading, dataSource) = (info.0, info.1)
sharedState.map(\.isLoading)
.bind(with: self) { owner, isLoading in

if isLoading {
owner.indicator.startAnimating()
} else {
owner.collectionView.isHidden = false
owner.indicator.stopAnimating()
}
}
.disposed(by: disposeBag)

var snapshot = NSDiffableDataSourceSnapshot<ListSearchResultSection, SearchPlaylistEntity>()
sharedState.map(\.dataSource)
.bind(with: self) { owner, dataSource in

snapshot.appendSections([.list])
var snapshot = NSDiffableDataSourceSnapshot<ListSearchResultSection, SearchPlaylistEntity>()

snapshot.appendItems(dataSource, toSection: .list)
owner.dataSource.apply(snapshot, animatingDifferences: false)
snapshot.appendSections([.list])

let warningView = WMWarningView(
text: "๊ฒ€์ƒ‰๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค."
)
snapshot.appendItems(dataSource, toSection: .list)
owner.dataSource.apply(snapshot, animatingDifferences: false)

if dataSource.isEmpty {
owner.collectionView.setBackgroundView(warningView, 100)
} else {
owner.collectionView.restore()
}
let warningView = WMWarningView(
text: "๊ฒ€์ƒ‰๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค."
)

if dataSource.isEmpty {
owner.collectionView.setBackgroundView(warningView, 100)
} else {
owner.collectionView.restore()
}
}
.disposed(by: disposeBag)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ final class SongSearchResultViewController: BaseReactorViewController<SongSearch
.disposed(by: disposeBag)

sharedState.map { $0.dataSource }
.distinctUntilChanged()
.bind(with: self) { owner, dataSource in

var snapshot = NSDiffableDataSourceSnapshot<SongSearchResultSection, SongEntity>()

snapshot.appendSections([.song])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,6 @@ extension BeforeSearchContentViewController: BeforeSearchSectionHeaderViewDelega

extension BeforeSearchContentViewController {
func scrollToTop() {
tableView.setContentOffset(.zero, animated: true)
collectionView.scroll(to: .top)
}
}
Loading