Skip to content

Commit

Permalink
[Refactor] 페이지 컨트롤 버튼 뷰모델 분리 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
seuriseuljjeok committed May 31, 2024
1 parent b3fe999 commit 5184982
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
CA803D9A2C0866BA006B8C35 /* ObservablePattern.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA803D992C0866BA006B8C35 /* ObservablePattern.swift */; };
CA803D9C2C09C784006B8C35 /* MovieViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA803D9B2C09C784006B8C35 /* MovieViewModel.swift */; };
CA803D9E2C09C818006B8C35 /* String+.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA803D9D2C09C818006B8C35 /* String+.swift */; };
CA803DA02C09D878006B8C35 /* PageControlButtonViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA803D9F2C09D878006B8C35 /* PageControlButtonViewModel.swift */; };
CA8496E22BE7E36D0064E0CC /* Moya in Frameworks */ = {isa = PBXBuildFile; productRef = CA8496E12BE7E36D0064E0CC /* Moya */; };
CA8496E52BE7E54D0064E0CC /* Config.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA8496E42BE7E54D0064E0CC /* Config.swift */; };
CA8496E72BE7E8E60064E0CC /* GetMovieResponseModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA8496E62BE7E8E60064E0CC /* GetMovieResponseModel.swift */; };
Expand Down Expand Up @@ -98,6 +99,7 @@
CA803D992C0866BA006B8C35 /* ObservablePattern.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObservablePattern.swift; sourceTree = "<group>"; };
CA803D9B2C09C784006B8C35 /* MovieViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MovieViewModel.swift; sourceTree = "<group>"; };
CA803D9D2C09C818006B8C35 /* String+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+.swift"; sourceTree = "<group>"; };
CA803D9F2C09D878006B8C35 /* PageControlButtonViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PageControlButtonViewModel.swift; sourceTree = "<group>"; };
CA8496E42BE7E54D0064E0CC /* Config.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Config.swift; sourceTree = "<group>"; };
CA8496E62BE7E8E60064E0CC /* GetMovieResponseModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetMovieResponseModel.swift; sourceTree = "<group>"; };
CA8496E92BE89A840064E0CC /* BaseTargetType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseTargetType.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -360,6 +362,7 @@
children = (
CA3BA70E2BFE7E9800F77616 /* MainViewModel.swift */,
CA803D9B2C09C784006B8C35 /* MovieViewModel.swift */,
CA803D9F2C09D878006B8C35 /* PageControlButtonViewModel.swift */,
);
path = ViewModel;
sourceTree = "<group>";
Expand Down Expand Up @@ -579,6 +582,7 @@
CA3953252BC6FB1100B15E91 /* ScreenUtils.swift in Sources */,
CA8496E72BE7E8E60064E0CC /* GetMovieResponseModel.swift in Sources */,
CA8496F82BE947F90064E0CC /* TVProgramView.swift in Sources */,
CA803DA02C09D878006B8C35 /* PageControlButtonViewModel.swift in Sources */,
CA3953202BC6F6BE00B15E91 /* LoginView.swift in Sources */,
CA902EF72BDE9E5E00560D26 /* SearchViewController.swift in Sources */,
CA8496F62BE947040064E0CC /* LiveView.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// PageControlButtonViewModel.swift
// Tving_CloneProject
//
// Created by 윤희슬 on 5/31/24.
//

import UIKit

final class PageControlButtonViewModel: NSObject {
var buttonCount: ObservablePattern<Int> = ObservablePattern.init(0)
}

extension PageControlButtonViewModel: UICollectionViewDataSource {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
guard let count = buttonCount.value else { return 0 }
return count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PagerButtonCell.identifier, for: indexPath) as? PagerButtonCell
else { return UICollectionViewCell() }

cell.pagerButton.tag = indexPath.row

return cell
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import UIKit
import SnapKit
import Then

class PagerButtonCell: UICollectionViewCell {
final class PagerButtonCell: UICollectionViewCell {

// MARK: - UI Properties

Expand Down Expand Up @@ -44,21 +44,18 @@ class PagerButtonCell: UICollectionViewCell {
private extension PagerButtonCell {

func setHierarchy() {

self.addSubview(pagerButton)

}

func setLayout() {

pagerButton.snp.makeConstraints {
$0.edges.equalToSuperview()
}

}

func setStyle() {

pagerButton.do {
$0.layer.cornerRadius = 2
$0.backgroundColor = .grey3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ final class PageControlButtonView: UICollectionReusableView {

static let identifier: String = "PageControlButtonView"

private let pageControlButtonViewModel: PageControlButtonViewModel = PageControlButtonViewModel()

private var prevIndex: Int = 0 {
didSet {
// 이전에 선택된 버튼을 찾기
Expand All @@ -51,6 +53,7 @@ final class PageControlButtonView: UICollectionReusableView {

var buttonCount: Int = 0 {
didSet {
pageControlButtonViewModel.buttonCount.value = buttonCount
buttonCollectionView.reloadData()
}
}
Expand Down Expand Up @@ -104,7 +107,7 @@ private extension PageControlButtonView {

func setDelegate() {
buttonCollectionView.delegate = self
buttonCollectionView.dataSource = self
buttonCollectionView.dataSource = pageControlButtonViewModel
}

func setButtonStyle(isSelected: Bool, button: UIButton) {
Expand All @@ -128,26 +131,17 @@ extension PageControlButtonView: UICollectionViewDelegateFlowLayout {
}
}

extension PageControlButtonView: UICollectionViewDataSource {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return buttonCount
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PagerButtonCell.identifier, for: indexPath) as? PagerButtonCell
else { return UICollectionViewCell() }

cell.pagerButton.tag = indexPath.row
cell.pagerButton.addTarget(self, action: #selector(didTapControlButton(_:)), for: .touchUpInside)

if indexPath.row == index {
setButtonStyle(isSelected: true, button: cell.pagerButton)
} else {
setButtonStyle(isSelected: false, button: cell.pagerButton)
extension PageControlButtonView: UICollectionViewDelegate {

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if let pageButtonCell = cell as? PagerButtonCell {
pageButtonCell.pagerButton.addTarget(self, action: #selector(didTapControlButton(_:)), for: .touchUpInside)

if indexPath.row == index {
setButtonStyle(isSelected: true, button: pageButtonCell.pagerButton)
} else {
setButtonStyle(isSelected: false, button: pageButtonCell.pagerButton)
}
}

return cell
}

}

0 comments on commit 5184982

Please sign in to comment.