-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from boostcampwm-2022/feature/QuestCell
QuestView와 QuestCell 완성
- Loading branch information
Showing
3 changed files
with
316 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
DailyQuest/DailyQuest/Presentation/Common/QuestCell.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
// | ||
// QuestCell.swift | ||
// DailyQuest | ||
// | ||
// Created by 이다연 on 2022/11/14. | ||
// | ||
|
||
import UIKit | ||
import SnapKit | ||
|
||
class QuestCell: UITableViewCell { | ||
|
||
static let reuseIdentifier = "QuestCell" | ||
|
||
/* | ||
override func setSelected(_ selected: Bool, animated: Bool) { | ||
super.setSelected(selected, animated: animated) | ||
// Configure the view for the selected state | ||
} | ||
*/ | ||
|
||
private lazy var userStackView: UIStackView = { | ||
let stackView = UIStackView() | ||
stackView.axis = .horizontal | ||
stackView.alignment = .center | ||
stackView.backgroundColor = .lightGray | ||
stackView.spacing = 10 | ||
stackView.isLayoutMarginsRelativeArrangement = true | ||
stackView.layoutMargins = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10) | ||
stackView.layer.cornerRadius = 15 | ||
return stackView | ||
}() | ||
|
||
private lazy var questStackView: UIStackView = { | ||
let stackView = UIStackView() | ||
stackView.axis = .vertical | ||
stackView.backgroundColor = .lightGray | ||
stackView.spacing = 10 | ||
stackView.distribution = .fillEqually | ||
stackView.layer.cornerRadius = 15 | ||
return stackView | ||
}() | ||
|
||
private lazy var userImageView: UIImageView = { | ||
let userImage = UIImageView() | ||
userImage.image = UIImage(systemName: "heart.fill") | ||
userImage.clipsToBounds = true | ||
userImage.backgroundColor = .white | ||
userImage.layer.masksToBounds = true | ||
return userImage | ||
}() | ||
|
||
private lazy var userQuestLabel: UILabel = { | ||
let userQuest = UILabel() | ||
userQuest.frame = CGRect(x: 0, y: 0, width: 60, height: 25) | ||
userQuest.font = UIFont.boldSystemFont(ofSize: 16) | ||
userQuest.text = " " | ||
return userQuest | ||
}() | ||
|
||
|
||
|
||
// MARK: - Methods | ||
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { | ||
super.init(style: style, reuseIdentifier: reuseIdentifier) | ||
configureUI() | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
override func layoutSubviews() { | ||
super.layoutSubviews() | ||
userImageView.layer.cornerRadius = userImageView.frame.height / 2 | ||
} | ||
|
||
private func configureUI() { | ||
userStackView.addArrangedSubview(userImageView) | ||
userStackView.addArrangedSubview(userQuestLabel) | ||
questStackView.addArrangedSubview(userStackView) | ||
|
||
addSubview(questStackView) | ||
|
||
userStackView.snp.makeConstraints { make in | ||
make.width.equalTo(questStackView).inset(10) | ||
make.height.equalTo(80) | ||
} | ||
|
||
questStackView.snp.makeConstraints { make in | ||
make.left.equalToSuperview().offset(20) | ||
make.right.equalToSuperview().offset(-20) | ||
} | ||
|
||
|
||
userImageView.snp.makeConstraints { make in | ||
make.height.equalTo(userStackView.snp.height).offset(-50) | ||
make.width.equalTo(userImageView.snp.height) | ||
} | ||
|
||
for subview in questStackView.subviews { | ||
subview.snp.makeConstraints { (make) in | ||
make.height.equalTo(subview.frame.height) | ||
make.width.equalTo(questStackView.snp.width) | ||
} | ||
} | ||
} | ||
|
||
func setup(user: User, quest: [Quest]) { | ||
userQuestLabel.text = user.nickName + "님의 Today Quest" | ||
//guard let image = UIImage(data: user.profile) else { return } | ||
//userImageView.image = image | ||
|
||
let height:Double = 100.0 * (1.0 + Double(quest.count)) | ||
|
||
print(height) | ||
|
||
questStackView.snp.makeConstraints { make in | ||
make.height.equalTo(height) | ||
} | ||
|
||
for q in quest { | ||
var questView = QuestView() | ||
questView.setUp(with: q) | ||
questStackView.addArrangedSubview(questView) | ||
} | ||
|
||
|
||
} | ||
} | ||
|
||
#if canImport(SwiftUI) && DEBUG | ||
import SwiftUI | ||
struct UIViewPreview<View: UIView>: UIViewRepresentable { | ||
let view: View | ||
|
||
init(_ builder: @escaping () -> View) { | ||
view = builder() | ||
} | ||
|
||
// MARK: - UIViewRepresentable | ||
|
||
func makeUIView(context: Context) -> UIView { | ||
return view | ||
} | ||
|
||
func updateUIView(_ view: UIView, context: Context) { | ||
view.setContentHuggingPriority(.defaultHigh, for: .horizontal) | ||
view.setContentHuggingPriority(.defaultHigh, for: .vertical) | ||
} | ||
} | ||
#endif | ||
|
||
#if canImport(SwiftUI) && DEBUG | ||
import SwiftUI | ||
|
||
struct QuestCellPreview: PreviewProvider{ | ||
static var previews: some View { | ||
UIViewPreview { | ||
let cell = QuestCell(frame: .zero) | ||
cell.setup(user: User(uuid: UUID(), nickName: "맥스", profile: Data(), backgroundImage: Data(), description: ""), quest: [Quest(title: "물 마시기", startDay: Date(), endDay: Date(), repeat: 0, currentCount: 1, totalCount: 5), Quest(title: "물 마시기", startDay: Date(), endDay: Date(), repeat: 0, currentCount: 1, totalCount: 5), Quest(title: "물 마시기", startDay: Date(), endDay: Date(), repeat: 0, currentCount: 1, totalCount: 5)]) | ||
return cell | ||
} | ||
.previewLayout(.sizeThatFits) | ||
} | ||
} | ||
#endif |
140 changes: 140 additions & 0 deletions
140
DailyQuest/DailyQuest/Presentation/Common/QuestView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// | ||
// QuestView.swift | ||
// DailyQuest | ||
// | ||
// Created by 이다연 on 2022/11/14. | ||
// | ||
|
||
import UIKit | ||
import SnapKit | ||
|
||
class QuestView: UIView { | ||
|
||
// assets 에 한번에 나중에 추가하려고 일단은 임의로 색상 넣음 | ||
private var backgroundLabel: UILabel = { | ||
let label = UILabel() | ||
label.frame = CGRect(x: 0, y: 0, width: 300, height: 80) | ||
label.backgroundColor = .yellow | ||
label.layer.masksToBounds = true | ||
return label | ||
}() | ||
|
||
private var progressLabel: UILabel = { | ||
let label = UILabel() | ||
label.frame = CGRect(x: 0, y: 0, width: 0, height: 80) | ||
label.backgroundColor = .systemYellow | ||
label.layer.masksToBounds = true | ||
return label | ||
}() | ||
|
||
private var questLabel: UILabel = { | ||
let label = UILabel() | ||
label.frame = CGRect(x: 0, y: 0, width: 60, height: 25) | ||
label.textAlignment = .center | ||
label.text = "" | ||
label.font = UIFont.boldSystemFont(ofSize: 16) | ||
label.sizeToFit() | ||
return label | ||
}() | ||
|
||
private var countLabel: UILabel = { | ||
let label = UILabel() | ||
label.frame = CGRect(x: 0, y: 0, width: 60, height: 25) | ||
label.textAlignment = .center | ||
label.text = "" | ||
label.font = UIFont.boldSystemFont(ofSize: 16) | ||
label.sizeToFit() | ||
return label | ||
}() | ||
|
||
override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
configureUI() | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
override func layoutSubviews() { | ||
super.layoutSubviews() | ||
backgroundLabel.layer.cornerRadius = backgroundLabel.bounds.size.height / 7 | ||
progressLabel.layer.cornerRadius = progressLabel.bounds.size.height / 8 | ||
} | ||
|
||
private func configureUI() { | ||
self.addSubview(backgroundLabel) | ||
self.addSubview(progressLabel) | ||
self.addSubview(questLabel) | ||
self.addSubview(countLabel) | ||
|
||
backgroundLabel.snp.makeConstraints { make in | ||
make.height.equalTo(80) | ||
make.width.equalTo(300) | ||
} | ||
|
||
progressLabel.snp.makeConstraints { make in | ||
make.height.equalTo(80) | ||
make.leading.equalTo(backgroundLabel) | ||
} | ||
|
||
questLabel.snp.makeConstraints { make in | ||
make.top.equalTo(backgroundLabel).inset(30) | ||
make.left.equalTo(backgroundLabel).inset(10) | ||
} | ||
|
||
countLabel.snp.makeConstraints { make in | ||
make.top.equalTo(backgroundLabel).inset(30) | ||
make.right.equalTo(backgroundLabel).inset(10) | ||
} | ||
|
||
} | ||
|
||
func setUp(with quest: Quest) { | ||
let progressWidth = 300.0 * (Double(quest.currentCount)/Double(quest.totalCount)) | ||
|
||
questLabel.text = quest.title | ||
countLabel.text = String(quest.currentCount) + " / " + String(quest.totalCount) | ||
progressLabel.snp.makeConstraints { make in | ||
make.width.equalTo(progressWidth) | ||
} | ||
} | ||
} | ||
/* | ||
#if canImport(SwiftUI) && DEBUG | ||
import SwiftUI | ||
struct UIViewPreview<View: UIView>: UIViewRepresentable { | ||
let view: View | ||
init(_ builder: @escaping () -> View) { | ||
view = builder() | ||
} | ||
// MARK: - UIViewRepresentable | ||
func makeUIView(context: Context) -> UIView { | ||
return view | ||
} | ||
func updateUIView(_ view: UIView, context: Context) { | ||
view.setContentHuggingPriority(.defaultHigh, for: .horizontal) | ||
view.setContentHuggingPriority(.defaultHigh, for: .vertical) | ||
} | ||
} | ||
#endif | ||
#if canImport(SwiftUI) && DEBUG | ||
import SwiftUI | ||
struct QuestionViewPreview: PreviewProvider{ | ||
static var previews: some View { | ||
UIViewPreview { | ||
let view = QuestView(frame: .zero) | ||
view.setUp(with: Quest(title: "물 마시기", startDay: Date(), endDay: Date(), repeat: 0, currentCount: 2, totalCount: 5)) | ||
return view | ||
}.previewLayout((.fixed(width: 300, height: 80))) | ||
} | ||
} | ||
#endif | ||
*/ |