-
Notifications
You must be signed in to change notification settings - Fork 0
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
[#6] 영화,드라마 선택 화면 구현 #13
Changes from 5 commits
a0523c3
81af2e5
d5d6f81
d9f9872
8b29ab9
1cbf74f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"filename" : "Vector.svg", | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,41 @@ | ||||||||||
// | ||||||||||
// FilterButton.swift | ||||||||||
// TodayVideo | ||||||||||
// | ||||||||||
// Created by iOS Dev on 1/16/25. | ||||||||||
// | ||||||||||
|
||||||||||
import UIKit | ||||||||||
|
||||||||||
class FilterButton: UIButton { | ||||||||||
let height = 50.0 | ||||||||||
var title = "" | ||||||||||
|
||||||||||
init(title: String) { | ||||||||||
super.init(frame: .zero) | ||||||||||
|
||||||||||
self.title = title | ||||||||||
self.setTitle(title, for: .normal) | ||||||||||
self.layer.cornerRadius = height / 2 | ||||||||||
} | ||||||||||
|
||||||||||
required init?(coder: NSCoder) { | ||||||||||
super.init(coder: coder) | ||||||||||
} | ||||||||||
|
||||||||||
func width() -> CGFloat { | ||||||||||
let titleWidth = title.width(size: 20) | ||||||||||
let leftRightMargin = 70.0 | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 70.0과 같은 매직 넘버는 상수로 바꾸어 가독성을 높일 수 있습니다. 예를 들어,
Suggested change
|
||||||||||
return titleWidth + leftRightMargin | ||||||||||
} | ||||||||||
|
||||||||||
func updateState() { | ||||||||||
if isSelected { | ||||||||||
self.setTitleColor(.buttonSelectedText, for: .normal) | ||||||||||
self.backgroundColor = .buttonSelectedBackground | ||||||||||
} else { | ||||||||||
self.setTitleColor(.buttonDefaultText, for: .normal) | ||||||||||
self.backgroundColor = .buttonDefaultBackground | ||||||||||
} | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||
} | ||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// NextButton.swift | ||
// TodayVideo | ||
// | ||
// Created by iOS Dev on 1/16/25. | ||
// | ||
|
||
import UIKit | ||
import SnapKit | ||
|
||
class NextButton: UIButton { | ||
var currentView: UIViewController! | ||
|
||
init(location: UIViewController) { | ||
super.init(frame: .zero) | ||
|
||
self.currentView = location | ||
self.setImage(.nextButton, for: .normal) | ||
self.addTarget(self, action: #selector(pushToNextView), for: .touchUpInside) | ||
self.currentView.view.addSubview(self) | ||
self.layout(in: currentView.view) | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
func layout(in view: UIView) { | ||
self.snp.makeConstraints { make in | ||
make.width.equalTo(15.4) | ||
make.height.equalTo(23) | ||
make.trailing.equalTo(view.snp.trailing).offset(-41.6) | ||
make.bottom.equalTo(view.snp.bottom).offset(-78) | ||
} | ||
} | ||
|
||
@objc private func pushToNextView() { | ||
switch currentView { | ||
case is ContentView: | ||
guard let contentView = currentView as? ContentView else { return } | ||
contentView.presenter?.pushToGenreView() | ||
default: break | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// ContentPresenter.swift | ||
// TodayVideo | ||
// | ||
// Created by iOS Dev on 1/15/25. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol ContentPresenterProtocol { | ||
func pushToGenreView() | ||
} | ||
|
||
final class ContentPresenter: ContentPresenterProtocol { | ||
var router: ContentRouterProtocol? | ||
|
||
func pushToGenreView() { | ||
DispatchQueue.main.async { | ||
self.router?.pushToGenreView() | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// ContentRouter.swift | ||
// TodayVideo | ||
// | ||
// Created by iOS Dev on 1/15/25. | ||
// | ||
|
||
import UIKit | ||
|
||
protocol ContentRouterProtocol { | ||
func pushToGenreView() | ||
} | ||
|
||
final class ContentRouter: ContentRouterProtocol { | ||
weak var contentView: ContentView? | ||
|
||
static func createContentViewModule() -> ContentView { | ||
let view = ContentView() | ||
let presenter = ContentPresenter() | ||
let router = ContentRouter() | ||
|
||
view.presenter = presenter | ||
presenter.router = router | ||
router.contentView = view | ||
|
||
return view | ||
} | ||
|
||
// 장르 선택 화면으로 push | ||
func pushToGenreView() { | ||
// let genreView = GenreRouter.createGenre | ||
// contentView?.navigationController?.pushViewController(genreView, animated: true) | ||
Comment on lines
+32
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PR 올리실 때에는 임의 주석 처리한 코드는 정리해주시는 것이 좋습니다. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// | ||
// ContentView.swift | ||
// TodayVideo | ||
// | ||
// Created by iOS Dev on 1/15/25. | ||
// | ||
|
||
import UIKit | ||
import SnapKit | ||
|
||
class ContentView: UIViewController { | ||
var presenter: ContentPresenterProtocol? | ||
|
||
private var movieButton: FilterButton! | ||
private var tvButton: FilterButton! | ||
private let movie = "영화" | ||
private let tv = "TV" | ||
private lazy var selectedContent = movie | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
drawUI() | ||
buttonSelected(movieButton) | ||
} | ||
|
||
private func drawUI() { | ||
self.view.backgroundColor = .background | ||
|
||
// 영화, 드라마 버튼 | ||
/// 컨테이너 | ||
let height = 50.0 | ||
let containerView = UIView() | ||
let containerViewWidth = 300.0 | ||
|
||
self.view.addSubview(containerView) | ||
|
||
containerView.snp.makeConstraints { make in | ||
make.center.equalToSuperview() | ||
make.height.equalTo(height) | ||
make.width.equalTo(containerViewWidth) | ||
} | ||
|
||
/// 버튼 | ||
movieButton = FilterButton(title: movie) | ||
tvButton = FilterButton(title: tv) | ||
|
||
[movieButton, tvButton].forEach { button in | ||
button.addTarget(self, action: #selector(buttonSelected(_:)), for: .touchUpInside) | ||
containerView.addSubview(button) | ||
} | ||
|
||
let margin = 8.0 | ||
let buttonWidth = (containerViewWidth / 2) - margin | ||
|
||
movieButton.snp.makeConstraints { make in | ||
make.left.top.bottom.equalToSuperview() | ||
make.width.equalTo(buttonWidth) | ||
} | ||
|
||
tvButton.snp.makeConstraints { make in | ||
make.right.top.bottom.equalToSuperview() | ||
make.width.equalTo(buttonWidth) | ||
} | ||
|
||
// 다음 버튼 | ||
let _ = NextButton(location: self) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분이 잘 이해가지 않네요. 할당하지 않는데 왜 생성되었을까요? |
||
} | ||
|
||
@objc private func buttonSelected(_ sender: UIButton) { | ||
// 이미 선택되어 있는 버튼이면 return | ||
if sender.isSelected { | ||
return | ||
} | ||
|
||
sender.isSelected = !sender.isSelected | ||
|
||
DispatchQueue.main.async { | ||
if sender == self.movieButton { | ||
self.selectedContent = self.movie | ||
self.tvButton.isSelected = false | ||
self.tvButton.updateState() | ||
self.movieButton.updateState() | ||
} else { | ||
self.selectedContent = self.tv | ||
self.movieButton.isSelected = false | ||
self.movieButton.updateState() | ||
self.tvButton.updateState() | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// String+Extension.swift | ||
// TodayVideo | ||
// | ||
// Created by iOS Dev on 1/17/25. | ||
// | ||
|
||
import UIKit | ||
|
||
extension String { | ||
func width(size: CGFloat) -> CGFloat { | ||
let font = UIFont.systemFont(ofSize: size) | ||
let attributes: [NSAttributedString.Key: Any] = [.font: font] | ||
return self.size(withAttributes: attributes).width | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// | ||
// UIImage+Extension.swift | ||
// TodayVideo | ||
// | ||
// Created by iOS Dev on 1/16/25. | ||
// | ||
|
||
import UIKit | ||
|
||
extension UIImage { | ||
static let nextButton = UIImage(named: "next") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FilterButton
클래스를 추후 확장하거나 상속시킬 계획이 없다면final
키워드를 추가해서 파이널 클래스로 정의해주는 것이 좋습니다.숙제) 그 이유에 대해서 찾아보시고, 다음 멘토링 때 이야기 나누도록 하시죠 :)