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

Feat/#98 사용자가 검색한 키워드 기록 및 조회 기능 구현 #120

Merged
merged 14 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -81,12 +81,13 @@ class SearchHistoryListCVC : BaseCollectionViewCell {
extension SearchHistoryListCVC {

/// 검색기록 테이블 뷰 내용을 설정하는 함수
func configureSearchHistoryListCVC(categoryType: TabPlaceCategoryList,
func configureSearchHistoryListCVC(viewModel: SearchVM,
delegateSearchHistoryListAction: SearchHistoryListAction,
delegateSearchHistoryAction delegate: SearchHistoryAction) {
self.delegateSearchHistoryListAction = delegateSearchHistoryListAction

bindSearchHistroyTableView(categoryType: categoryType, delegateSearchHistoryAction: delegate)
bindSearchHistroyTableView(viewModel: viewModel,
delegateSearchHistoryAction: delegate)
}

}
Expand Down Expand Up @@ -122,7 +123,8 @@ extension SearchHistoryListCVC {

extension SearchHistoryListCVC {

private func bindSearchHistroyTableView(categoryType: TabPlaceCategoryList, delegateSearchHistoryAction delegate: SearchHistoryAction) {
private func bindSearchHistroyTableView(viewModel: SearchVM,
delegateSearchHistoryAction delegate: SearchHistoryAction) {
let dataSource = RxTableViewSectionedReloadDataSource<KeywordHistoryDataSource> { _,
tableView,
indexPath,
Expand All @@ -138,26 +140,37 @@ extension SearchHistoryListCVC {
return cell
}

let keywordHistoryList: BehaviorRelay<Array<String>> = BehaviorRelay(value: CoreDataManager.shared.getKeywordHistoryList())
var keywordHistoryDataSource: Observable<Array<KeywordHistoryDataSource>> {
keywordHistoryList.map { [KeywordHistoryDataSource(items: $0)] }
viewModel.output.searchHistory.keywordList.map { [KeywordHistoryDataSource(items: $0)] }
}

keywordHistoryDataSource
.bind(to: searchHistoryTableView.rx.items(dataSource: dataSource))
.disposed(by: bag)

keywordHistoryList
viewModel.output.searchHistory.isUpdated
.withUnretained(self)
.subscribe(onNext: { owner, items in
owner.searchHistoryTableView.isHidden = items.count == 0 ? true : false
.subscribe(onNext: { owner, isUpdated in
if isUpdated {
owner.searchHistoryTableView.scrollToTop()
viewModel.updateKeywordHistory()
owner.searchHistoryTableView.reloadData()
}
})
.disposed(by: bag)

viewModel.output.searchHistory.keywordList
.withUnretained(self)
.subscribe(onNext: { owner, list in
owner.searchHistoryTableView.isHidden = list.count == 0
})
.disposed(by: bag)

searchHistoryTableView.rx.modelSelected(String.self)
.withUnretained(self)
.bind(onNext: { owner, keyword in
owner.delegateSearchHistoryListAction?.didTapKeyword(keyword: keyword)
viewModel.output.searchHistory.isUpdated.accept(true)
})
.disposed(by: bag)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,20 +179,21 @@ class SearchVC: BaseViewController {

bindOutputHistoryCategoryTabBarView()
bindSearchHistroyListCollectionView()
bindSearchHistroy()
bindOutputSearchResultTableView()
}

// MARK: - Functions

private func searchPlaceKeyword() {
private func startSearchPlaceKeyword() {
searchResultTableView.scrollToTop()
requestSearchPlaceKeyword(requestPage: 1)
isShowSearchResultUI(show: true)
}

private func isShowSearchResultUI(show: Bool) {
if !show {
updateSearchHistoryListStatus()
viewModel.output.searchHistory.isUpdated.accept(true)
searchHistoryListCollectionView.scrollToTop()
}
Copy link
Member

Choose a reason for hiding this comment

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

이 부분도 저희 viewModel 관리 방법에 대해서 고민이 있는 부분인데요,,
VC에서 viewModel의 output에 새로운 값을 accept 한다는게 어색한 것 같아요ㅠㅜ (이전에 이런 경우가 꽤 있었지만)
viewModel -> VC 방향은 output으로,
VC -> viewModel 방향은 input으로 관리해주어야 발생할 수 있는 사이드 이펙트를 막을 수 있을 것 같습니다..

간단하게 요약하자면 VC는 viewModel의 output을 보여주기만 하고, 들어오는 입력은 input으로 처리하는 것이죠 🤔
이렇게 되면 output이 어디서 바뀌어서 화면에 잘못 노출될지 알 수 없으니까요!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

이슈로 등록한 후에 차후 MVVM 패턴 수정을 해보도록 하겠습니다!


Expand All @@ -201,11 +202,6 @@ class SearchVC: BaseViewController {
searchResultTableView.isHidden = !show
}

private func updateSearchHistoryListStatus() {
viewModel.output.searchHistory.keywordList.accept(CoreDataManager.shared.getKeywordHistoryList())
searchHistoryListCollectionView.reloadData()
}

private func requestSearchPlaceKeyword(requestPage: Int) {
if let curLocationCoordinate = delegateSearchPlaceAction?.getCurrentLocationCoordinate() {
if let keyword = searchTextField.text, !keyword.isEmpty {
Expand Down Expand Up @@ -362,7 +358,7 @@ extension SearchVC {
searchButton.rx.tap
.withUnretained(self)
.bind(onNext: { owner, _ in
owner.searchPlaceKeyword()
owner.startSearchPlaceKeyword()
})
.disposed(by: bag)

Expand All @@ -382,7 +378,7 @@ extension SearchVC {
.withUnretained(self)
.bind(onNext: { owner, _ in
if CoreDataManager.shared.deleteAllSearchKeyword() {
owner.updateSearchHistoryListStatus()
owner.viewModel.output.searchHistory.isUpdated.accept(true)
}
})
.disposed(by: bag)
Expand Down Expand Up @@ -414,7 +410,7 @@ extension SearchVC {
.asObservable()
.withUnretained(self)
.subscribe(onNext: { owner, _ in
owner.searchPlaceKeyword()
owner.startSearchPlaceKeyword()
})
.disposed(by: bag)

Expand Down Expand Up @@ -521,7 +517,7 @@ extension SearchVC {
for: indexPath) as? SearchHistoryListCVC else {
fatalError("Cannot deqeue cells named SearchHistoryListCVC")
}
cell.configureSearchHistoryListCVC(categoryType: categoryType,
cell.configureSearchHistoryListCVC(viewModel: self.viewModel,
delegateSearchHistoryListAction: self,
delegateSearchHistoryAction: self)

Expand All @@ -531,13 +527,12 @@ extension SearchVC {
viewModel.output.searchHistory.dataSource
.bind(to: searchHistoryListCollectionView.rx.items(dataSource: dataSource))
.disposed(by: bag)

}

private func bindSearchHistroy() {
viewModel.output.searchHistory.keywordList
.withUnretained(self)
.subscribe(onNext: { owner, list in

print(list.count > 0)

owner.removeAllKeywordButton.isEnabled = list.count > 0
})
.disposed(by: bag)
Expand Down Expand Up @@ -668,7 +663,7 @@ extension SearchVC: SearchHistoryListAction {

func didTapKeyword(keyword: String) {
searchTextField.text = keyword
searchPlaceKeyword()
startSearchPlaceKeyword()
}

}
Expand All @@ -679,7 +674,7 @@ extension SearchVC: SearchHistoryAction {

func didTapRemoveButton(keyword: String) {
if CoreDataManager.shared.deleteSearchKeyword(targetKeyword: keyword) {
updateSearchHistoryListStatus()
viewModel.output.searchHistory.isUpdated.accept(true)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ final class SearchVM: BaseViewModel {

struct Input {}
struct Output {
var isLoginUser = KeychainManager.shared.read(for: .accessToken) != nil

var categoryHistoryList: BehaviorRelay<Array<TabPlaceCategoryList>> = BehaviorRelay(value: TabPlaceCategoryList.allCases)
var tabPlaceCategoryDataSources: Observable<Array<TabPlaceCategoryListDataSource>> {
categoryHistoryList.map { [TabPlaceCategoryListDataSource(items: $0)] }
Expand All @@ -35,6 +37,7 @@ final class SearchVM: BaseViewModel {
/// 장소검색 키워드 히스토리
struct SearchHistory {
var keywordList: BehaviorRelay<Array<String>> = BehaviorRelay(value: CoreDataManager.shared.getKeywordHistoryList())
var isUpdated: BehaviorRelay<Bool> = BehaviorRelay(value: false)

var list: BehaviorRelay<Array<TabPlaceCategoryList>> = BehaviorRelay(value: TabPlaceCategoryList.allCases)
var dataSource: Observable<Array<SearchHistoryListDataSource>> {
Expand Down Expand Up @@ -63,6 +66,12 @@ final class SearchVM: BaseViewModel {
deinit {
bag = DisposeBag()
}

// MARK: - Functions

func updateKeywordHistory() {
output.searchHistory.keywordList.accept(CoreDataManager.shared.getKeywordHistoryList())
}
}

// MARK: - Input
Expand Down