Skip to content

Commit

Permalink
feat: filterButton 상태 관리 로직 구현(#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chandrarla committed Oct 28, 2024
1 parent 1a7304d commit 65536bb
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 37 deletions.
14 changes: 7 additions & 7 deletions Common/Sources/Components/ChipKeyWordButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import UIKit
import SnapKit
import Then

enum ChipState {
public enum ChipState {
case active
case inactive
}

public class ChipKeyWordButton: UIButton {
var chipstate: ChipState = .inactive {
public var chipstate: ChipState = .inactive {
didSet {
updateChipAppearance()
}
Expand All @@ -37,23 +37,23 @@ public class ChipKeyWordButton: UIButton {
updateChipAppearance()
}

func setState(state: ChipState) {
public func setState(state: ChipState) {
self.chipstate = state
}

private func updateChipAppearance() {
switch chipstate {
case .active:
backgroundColor = CommonAsset.recordyGrey01.color
backgroundColor = CommonAsset.viskitGray01.color
setTitleColor(
CommonAsset.recordyGrey09.color,
CommonAsset.viskitBlack.color,
for: .normal
)

case .inactive:
backgroundColor = CommonAsset.recordyGrey09.color
backgroundColor = CommonAsset.viskitGray09.color
setTitleColor(
CommonAsset.recordyGrey04.color,
CommonAsset.viskitGray03.color,
for: .normal
)
}
Expand Down
6 changes: 3 additions & 3 deletions Presentation/Sources/Home/View/ExhibitionListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import Common
final class ExhibitionListView: UIView {

let exhibitionCount = UILabel()
let allFilterButton = ChipKeyWordButton()
let freeFilterButton = ChipKeyWordButton()
let endSoonFilterButton = ChipKeyWordButton()
public let allFilterButton = ChipKeyWordButton()
public let freeFilterButton = ChipKeyWordButton()
public let endSoonFilterButton = ChipKeyWordButton()
var exhibitionCollectionView: UICollectionView?

public override init(frame: CGRect) {
Expand Down
81 changes: 58 additions & 23 deletions Presentation/Sources/Home/View/PlaceDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@ import UIKit
import Common
import Core

public enum PlaceDetailControlType: String {
case exhibitionList = "전시 리스트"
case reviewFeed = "후기 영상"
}

@available(iOS 16.0, *)
final public class PlaceDetailViewController: UIViewController{

let placeNameLabel = UILabel()
let detailLocationLabel = UILabel()
let findRouteButton = UIButton()
Expand All @@ -29,15 +25,15 @@ final public class PlaceDetailViewController: UIViewController{
let exhibitionListView = ExhibitionListView()
let reviewFeedView = ReviewFeedView()

var viewModel = PlaceDetailViewModel(initialControlType: .exhibitionList)
var viewModel = PlaceDetailViewModel()

public init(viewModel: PlaceDetailViewModel) {
self.viewModel = viewModel
super.init(nibName: nil, bundle: nil)
self.viewModel = viewModel
super.init(nibName: nil, bundle: nil)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
fatalError("init(coder:) has not been implemented")
}

public override func viewDidLoad() {
Expand All @@ -47,6 +43,7 @@ final public class PlaceDetailViewController: UIViewController{
setAutolayout()
setDelegate()
bind()
setTarget()
}

func setStyle() {
Expand Down Expand Up @@ -157,13 +154,55 @@ final public class PlaceDetailViewController: UIViewController{
reviewFeedView.reviewFeedCollectionView?.dataSource = self
}

private func setTarget() {
exhibitionListView.allFilterButton.addTarget(self, action: #selector(filterButtonTapped(_:)), for: .touchUpInside)
exhibitionListView.freeFilterButton.addTarget(self, action: #selector(filterButtonTapped(_:)), for: .touchUpInside)
exhibitionListView.endSoonFilterButton.addTarget(self, action: #selector(filterButtonTapped(_:)), for: .touchUpInside)
}

@objc private func filterButtonTapped(_ sender: UIButton) {
if sender == exhibitionListView.allFilterButton {
viewModel.updateFilterState(selected: .all)
} else if sender == exhibitionListView.freeFilterButton {
viewModel.updateFilterState(selected: .free)
} else if sender == exhibitionListView.endSoonFilterButton {
viewModel.updateFilterState(selected: .endSoon)
}
}

private func bind() {
viewModel.onControlTypeChanged = { [weak self] type in
guard let self = self else { return }
exhibitionListView.isHidden = type != .exhibitionList
reviewFeedView.isHidden = type != .reviewFeed
self?.updateView(for: type)
}
viewModel.onControlTypeChanged?(viewModel.currentControlType)

viewModel.onFilterChanged = { [weak self] allState, freeState, endSoonState in
self?.updateFilterButtonStates(
allState: allState,
freeState: freeState,
endSoonState: endSoonState
)
}
viewModel.onFilterChanged?(
viewModel.allFilterState,
viewModel.freeFilterState,
viewModel.endSoonFilterState
)
}

private func updateView(for type: PlaceDetailControlType) {
exhibitionListView.isHidden = type != .exhibitionList
reviewFeedView.isHidden = type != .reviewFeed
}

private func updateFilterButtonStates(
allState: ChipState,
freeState: ChipState,
endSoonState: ChipState
) {
exhibitionListView.allFilterButton.setState(state: allState)
exhibitionListView.freeFilterButton.setState(state: freeState)
exhibitionListView.endSoonFilterButton.setState(state: endSoonState)
}
}

Expand All @@ -183,15 +222,13 @@ extension PlaceDetailViewController: UICollectionViewDataSource {
switch collectionView {
case exhibitionListView.exhibitionCollectionView:
return 10

case reviewFeedView.reviewFeedCollectionView:
return 10

default:
return 0
}
}

public func collectionView(
_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: UICollectionViewCell
Expand Down Expand Up @@ -223,10 +260,10 @@ extension PlaceDetailViewController: UICollectionViewDelegateFlowLayout {
sizeForItemAt indexPath: IndexPath
) -> CGSize {
switch collectionView {
// TODO: 두줄일 때 height 늘어나게 설정
// TODO: 두줄일 때 height 늘어나게 설정
case exhibitionListView.exhibitionCollectionView:
return CGSize(width: 335.adaptiveWidth, height: 74.adaptiveHeight)

case reviewFeedView.reviewFeedCollectionView:
return CGSize(width: 162.adaptiveWidth, height: 288.adaptiveHeight)

Expand All @@ -239,19 +276,17 @@ extension PlaceDetailViewController: UICollectionViewDelegateFlowLayout {
switch collectionView {
case exhibitionListView.exhibitionCollectionView:
return 12.adaptiveHeight

case reviewFeedView.reviewFeedCollectionView:
return 16.adaptiveHeight

default:
return 0
}
}

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
if collectionView == reviewFeedView.reviewFeedCollectionView {
return 11.adaptiveWidth
}
return 0
if collectionView == reviewFeedView.reviewFeedCollectionView {
return 11.adaptiveWidth
}
return 0
}
}
32 changes: 28 additions & 4 deletions Presentation/Sources/Home/ViewModel/PlaceDetailViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,44 @@
// Copyright © 2024 com.recordy. All rights reserved.
//

import Common

public enum FilterType {
case all
case free
case endSoon
}

public enum PlaceDetailControlType: String {
case exhibitionList = "전시 리스트"
case reviewFeed = "후기 영상"
}

public class PlaceDetailViewModel {
var onControlTypeChanged: ((PlaceDetailControlType) -> Void)?
var onFilterChanged: ((ChipState, ChipState, ChipState) -> Void)?

private(set) var currentControlType: PlaceDetailControlType = .exhibitionList {
didSet {
onControlTypeChanged?(currentControlType)
onControlTypeChanged?(currentControlType)
}
}

public init(initialControlType: PlaceDetailControlType = .exhibitionList) {
self.currentControlType = initialControlType
}
private(set) var allFilterState: ChipState = .active
private(set) var freeFilterState: ChipState = .inactive
private(set) var endSoonFilterState: ChipState = .inactive

public init() {}

func updateControlType(to type: PlaceDetailControlType) {
currentControlType = type
}

func updateFilterState(selected: FilterType) {
allFilterState = (selected == .all) ? .active : .inactive
freeFilterState = (selected == .free) ? .active : .inactive
endSoonFilterState = (selected == .endSoon) ? .active : .inactive

onFilterChanged?(allFilterState, freeFilterState, endSoonFilterState)
}
}

0 comments on commit 65536bb

Please sign in to comment.