Skip to content

Commit

Permalink
⚡️ :: Search 관련 코드
Browse files Browse the repository at this point in the history
  • Loading branch information
jangseoyoung committed Dec 2, 2021
1 parent 51febbb commit 12cea33
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
6 changes: 5 additions & 1 deletion Sources/Model/ProductList/ProductList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct ProductResponse: Codable {
var product_id: String
var name: String
var brands: [String]
var selling: [Selling]
var selling: [Selling]?
var price: Int
var like_count: Int
var image_path: String?
Expand All @@ -24,3 +24,7 @@ struct Selling: Codable {
var selling_price: Int
var price: Int
}

struct Search: Codable {
var application_responses: [ProductResponse]
}
39 changes: 34 additions & 5 deletions Sources/View/Product/ProductListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,34 @@ class ProductListViewController: TabmanViewController {
private let getRecommendLists = PublishSubject<Void>()
private let getLowPriceLists = PublishSubject<Void>()

private let searchProduct = PublishSubject<String>()

var items = [String]()

lazy var button = UIDropDownButton().then {
$0.setAction().subscribe(onNext: {
switch $0 {
case .popularity:
self.getPopularLists.onNext(())
self.searchBar.text = ""
case .suggestion:
self.getRecommendLists.onNext(())
self.searchBar.text = ""
case .lowestPrice:
self.getLowPriceLists.onNext(())
self.searchBar.text = ""
}
})
.disposed(by: disposeBag)
}

lazy var barButtonItem = UIBarButtonItem(customView: button)

private var viewControllers: Array<StoreMainViewController> = []
let searchBar = UISearchBar().then {
$0.placeholder = "검색"
}

private var viewControllers = [StoreMainViewController]()

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -57,8 +68,6 @@ class ProductListViewController: TabmanViewController {
private func setNavigationBar() {
navigationItem.title = "제품"
navigationItem.rightBarButtonItem = barButtonItem
let searchBar = UISearchBar()
searchBar.placeholder = "검색"
navigationItem.titleView = searchBar
navigationItem.hidesSearchBarWhenScrolling = true
navigationController?.navigationBar.isTranslucent = false
Expand Down Expand Up @@ -89,6 +98,22 @@ class ProductListViewController: TabmanViewController {

addBar(bar, dataSource: self, at: .top)
}

private func search() {
searchBar.rx.searchButtonClicked
.subscribe(onNext: {
self.searchBar.resignFirstResponder()
self.searchProduct.onNext(self.searchBar.text ?? "")
print(self.searchBar.text ?? "")
}).disposed(by: disposeBag)

searchBar.rx.textDidBeginEditing
.subscribe(onNext: { [unowned self] _ in
if searchBar.text == "" {
searchProduct.onNext("")
}
}).disposed(by: disposeBag)
}
}

extension ProductListViewController: PageboyViewControllerDataSource, TMBarDataSource {
Expand Down Expand Up @@ -135,7 +160,9 @@ extension ProductListViewController: PageboyViewControllerDataSource, TMBarDataS
let input = ProductListViewModel.Input.init(
getPopularLists: self.getPopularLists.asDriver(onErrorJustReturn: ()),
getRecommendLists: self.getRecommendLists.asDriver(onErrorJustReturn: ()),
getLowPriceLists: self.getLowPriceLists.asDriver(onErrorJustReturn: ()))
getLowPriceLists: self.getLowPriceLists.asDriver(onErrorJustReturn: ()),
searchProductIsTapped: self.searchProduct.asDriver(onErrorJustReturn: "")
)

let output = viewModel.transform(input)

Expand Down Expand Up @@ -168,7 +195,9 @@ extension ProductListViewController: PageboyViewControllerDataSource, TMBarDataS
.subscribe(onNext: {
self.viewControllers[5].getLists.onNext($0)
}).disposed(by: disposeBag)

self.getPopularLists.onNext(())

search()
}
}

0 comments on commit 12cea33

Please sign in to comment.