Skip to content

Commit

Permalink
[#166] refactor: add SelectEmotionView
Browse files Browse the repository at this point in the history
  • Loading branch information
akrudal committed Dec 5, 2023
1 parent 51171d6 commit 3133e7e
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import UIKit

class EmotionCollectionViewCell: UICollectionViewCell {
static let id = "imageCell"

let imageView: UIImageView = {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
Expand All @@ -25,12 +26,19 @@ class EmotionCollectionViewCell: UICollectionViewCell {
contentView.addSubview(imageView)
}

func setData(with text: String) {
func setCell(with text: String) {
imageView.widthAnchor.constraint(equalToConstant: 56).isActive = true
imageView.heightAnchor.constraint(equalToConstant: 56).isActive = true
imageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
imageView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
imageView.image = UIImage(named: text)
}

func setAlpha() {
imageView.alpha = 0.5
}

func setOrigin() {
imageView.alpha = 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,48 @@ import Then
final class SelectEmotionView: BBaseView {
private let disposeBag = DisposeBag()

private let viewModel: AnswerViewModel

private let contentView = UIView()
private let titleLabel = UILabel()
private let emotionLabel = UILabel()
private let emotionCollectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewLayout())
let emotionCollectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
private let buttonStackView = UIStackView()
private let cancelButton = TeritaryTextButton()
private let confirmButton = SecondaryTextButton()

override init(frame: CGRect) {
init(viewModel: AnswerViewModel, frame: CGRect) {
self.viewModel = viewModel
super.init(frame: frame)

}

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

override func bindViewModel() {
// viewModel.emotionList
// .bind(to: emotionCollectionView.rx.items(cellIdentifier: EmotionCollectionViewCell.id, cellType: EmotionCollectionViewCell.self)) { (row, element, cell) in
// cell.setCell(data: element)
// }
// .disposed(by: disposeBag)

cancelButton.rx.tap
.bind(onNext: { [weak self] _ in
guard let self else { return }
self.isHidden = true
})
.disposed(by: disposeBag)

viewModel.outputs.selectedEmotionIndexSubject
.bind(onNext: { [weak self] indexPath in
guard let self else { return }
self.selectEmotion(indexPath: indexPath)
})
.disposed(by: disposeBag)
}

override func setStyles() {
backgroundColor = .AlphaBlackColor

contentView.do {
$0.backgroundColor = .Side100
}

titleLabel.do {
$0.font = .fontNanum(.B1_Regular)
$0.textColor = .Black
Expand All @@ -54,13 +69,23 @@ final class SelectEmotionView: BBaseView {
$0.text = AnswerStrings.emotionPlaceHolder.stringValue
}

emotionCollectionView.do {
$0.backgroundColor = .Side100
$0.register(EmotionCollectionViewCell.self, forCellWithReuseIdentifier: EmotionCollectionViewCell.id)
}

cancelButton.do {
$0.setText(text: AnswerStrings.cancelTitle.stringValue)
}

confirmButton.do {
$0.setText(text: AnswerStrings.confirmTitle.stringValue)
}

buttonStackView.do {
$0.distribution = .fillEqually
$0.spacing = 15
}
}

override func setLayout() {
Expand All @@ -69,6 +94,10 @@ final class SelectEmotionView: BBaseView {
buttonStackView)
buttonStackView.addArrangedSubviews(cancelButton, confirmButton)

contentView.snp.makeConstraints {
$0.horizontalEdges.bottom.equalToSuperview()
}

titleLabel.snp.makeConstraints {
$0.top.equalToSuperview().inset(42)
$0.centerX.equalToSuperview()
Expand All @@ -81,6 +110,7 @@ final class SelectEmotionView: BBaseView {

emotionCollectionView.snp.makeConstraints {
$0.top.equalTo(emotionLabel.snp.bottom).offset(32)
$0.height.equalTo(132)
$0.horizontalEdges.equalToSuperview().inset(55)
}

Expand All @@ -92,3 +122,27 @@ final class SelectEmotionView: BBaseView {
}
}
}

extension SelectEmotionView {
private func selectEmotion(indexPath: IndexPath) {
if let cells = emotionCollectionView.visibleCells as? [EmotionCollectionViewCell] {
cells.forEach {
$0.setAlpha()
}
}

if let cell = emotionCollectionView.cellForItem(at: indexPath) as? EmotionCollectionViewCell {
cell.setOrigin()
}
self.emotionLabel.text = viewModel.emotionList[indexPath.row].stringValue
}
}

extension SelectEmotionView {
func showOpenAnimation() {
contentView.transform = CGAffineTransform(translationX: 0, y: contentView.frame.height)
UIView.animate(withDuration: 0.3) { [weak self] in
self?.contentView.transform = .identity
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,17 @@ class AAnswerViewController: BaseViewController {
private let answerTextView = AnswerTextView()
private let answerBottomView = AnswerBottomView()
private let emotionView = EmotionView()
private let emotionSheetView = SelectEmotionView()
private let emotionSheetView: SelectEmotionView

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
emotionSheetView = SelectEmotionView(viewModel: viewModel, frame: .zero)
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}

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

override func viewDidLoad() {
super.viewDidLoad()

Expand All @@ -48,11 +57,28 @@ class AAnswerViewController: BaseViewController {
})
.disposed(by: disposeBag)

emotionSheetView.emotionCollectionView.rx.setDelegate(self)
.disposed(by: disposeBag)

emotionSheetView.emotionCollectionView.rx.itemSelected
.bind(onNext: { [weak self] indexPath in
guard let self = self else { return }
self.viewModel.inputs.selectEmotion(indexPath: indexPath)
})
.disposed(by: disposeBag)

viewModel.outputs.questionSubject
.bind(onNext: { question in
self.questionView.setQuestion(data: question)
})
.disposed(by: disposeBag)

Observable.just(viewModel.emotionList)
.bind(to: emotionSheetView.emotionCollectionView.rx.items(cellIdentifier: EmotionCollectionViewCell.id, cellType: EmotionCollectionViewCell.self)) { (row, emotion, cell) in
cell.setAlpha()
cell.setCell(with: emotion.rawValue)
}
.disposed(by: disposeBag)
}

override func setStyles() {
Expand All @@ -63,11 +89,15 @@ class AAnswerViewController: BaseViewController {
seperateLine.do {
$0.backgroundColor = .Side300
}

emotionSheetView.do {
$0.isHidden = true
}
}

override func setLayout() {
view.addSubviews(backHeaderView, questionView, seperateLine,
answerTextView, answerBottomView)
answerTextView, answerBottomView, emotionSheetView)
backHeaderView.addSubview(emotionView)

backHeaderView.snp.makeConstraints {
Expand Down Expand Up @@ -104,6 +134,10 @@ class AAnswerViewController: BaseViewController {
$0.horizontalEdges.equalToSuperview()
$0.height.equalTo(72)
}

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

Expand Down Expand Up @@ -142,7 +176,8 @@ extension AAnswerViewController {
}

private func showEmotionSheetView() {

emotionSheetView.isHidden = false
emotionSheetView.showOpenAnimation()
}

private func setPremium() {
Expand All @@ -151,3 +186,13 @@ extension AAnswerViewController {
}
}
}

extension AAnswerViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 56, height: 56)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return (collectionView.frame.width - 3 * 56) / 3
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ extension EmotionViewController: UICollectionViewDelegate, UICollectionViewDataS

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as? EmotionCollectionViewCell else { return UICollectionViewCell()}
cell.setData(with: viewModel.emotions[indexPath.row].image)
cell.setCell(with: viewModel.emotions[indexPath.row].image)

if let selectedIndexPath = viewModel.selectedEmotion {
if selectedIndexPath == indexPath.row {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ protocol AnswerViewModelInputs {
// var inputTextField
// var toggleSwitch
// var clickedRegisterButton
func selectEmotion(indexPath: IndexPath)
}

protocol AnswerViewModelOutputs {
Expand All @@ -27,6 +28,7 @@ protocol AnswerViewModelOutputs {
var countTextRelay: BehaviorRelay<String> { get }
var toastSubject: PublishSubject<String> { get }
var questionSubject: PublishSubject<Question> { get }
var selectedEmotionIndexSubject: PublishSubject<IndexPath> { get }
}

protocol AnswerViewModelType {
Expand All @@ -35,35 +37,36 @@ protocol AnswerViewModelType {
}

final class AnswerViewModel: AnswerViewModelType, AnswerViewModelInputs, AnswerViewModelOutputs {

// 옛날꺼
var questionDate: String? = Date().getQuestionDate()
var modalChanged: Int = 0
let content: String = ""
var date: String = Date().todayFormat()
var emotion: Int? = nil
var emotions = [Emotion(image: "Happy", text: "행복해요"), Emotion(image: "Proud", text: "뿌듯해요"), Emotion(image: "Meh", text: "그저 그래요"), Emotion(image: "Tired", text: "피곤해요"), Emotion(image: "Sad", text: "슬퍼요"), Emotion(image: "Angry", text: "화나요")]
let plusEmotions = [
Emotion(image: "Happy", text: "행복해요"), Emotion(image: "Proud", text: "뿌듯해요"), Emotion(image: "Meh", text: "그저 그래요"), Emotion(image: "Tired", text: "피곤해요"), Emotion(image: "Sad", text: "슬퍼요"), Emotion(image: "Angry", text: "화나요"),
Emotion(image: "Excited", text: "설레요"), Emotion(image: "Thrilled", text: "신나요"), Emotion(image: "Relaxed", text: "편안해요"), Emotion(image: "Lethargic", text: "무기력해요"), Emotion(image: "Lonely", text: "외로워요"), Emotion(image: "Complicated", text: "복잡해요")
]
// var questionDate: String? = "2023-11-29"

var isFull: Bool = false
var emotionList: [Emotions] = Emotions.standardEmotionArray()

var disposeBag = DisposeBag()

// inputs
var inputs: AnswerViewModelInputs { return self }
func selectEmotion(indexPath: IndexPath) {
selectedEmotionIndexSubject.onNext(indexPath)
}

// outputs
var outputs: AnswerViewModelOutputs { return self }
var changeQuestionSubject: PublishSubject<QuestionType> = PublishSubject<QuestionType>()
var foldViewSubject: BehaviorRelay<Bool> = BehaviorRelay(value: false)
var inputTextRelay: BehaviorRelay<String> = BehaviorRelay(value: "")
var tolggleSwitchSubject: BehaviorRelay<Void> = BehaviorRelay(value: ())
var countTextRelay: BehaviorRelay<String> = BehaviorRelay(value: "")
var toastSubject: PublishSubject<String> = PublishSubject<String>()
var questionSubject: PublishSubject<Question> = PublishSubject<Question>()
var foldViewSubject = BehaviorRelay(value: false)
var inputTextRelay = BehaviorRelay(value: "")
var tolggleSwitchSubject = BehaviorRelay(value: ())
var countTextRelay = BehaviorRelay(value: "")
var toastSubject = PublishSubject<String>()
var questionSubject = PublishSubject<Question>()
var selectedEmotionIndexSubject = PublishSubject<IndexPath>()

init() {
getQuestion()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class LibraryInfoView: UIView {
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
let okButton = SecondaryTextButton()

let items = Observable.just(Emotions.emotionArray)
let items = Observable.just(Emotions.standardEmotionArray() )
let disposeBag = DisposeBag()

override init(frame: CGRect) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,6 @@ final class SignUpViewModel: SignUpViewModelType, SignUpViewModelInputs, SignUpV
let errorToastSubject = BehaviorSubject<String>(value: "")
let nextButtonEnabledRelay = BehaviorRelay<Bool>(value: false)
let signInSuccessSubject = PublishSubject<SignInResponse>()

// init() {
// signUpSuccessSubject
// .skip(1)
// .bind(onNext: { [weak self] _ in
// guard let self else { return }
// postSignIn()
// })
// .disposed(by: disposeBag)
// }
}

extension SignUpViewModel {
Expand Down
6 changes: 3 additions & 3 deletions TellingMe/tellingMe/util/nameSpace/Emotions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ enum Emotions: String {
}
}

static var emotionArray: [Emotions] {
static func standardEmotionArray() -> [Emotions] {
return [.happy, .proud, .meh, .tired, .sad, .angry]
}

static var emotionsArray: [Emotions] {
return [.happy, .proud, .meh, .tired,
static var premeiumEmotionArray: [Emotions] {
return [.happy, .proud, .meh, .tired,
.sad, .angry, .excited, .thrilled, .relaxed,
.lethargic, .lonely, .complicated]
}
Expand Down

0 comments on commit 3133e7e

Please sign in to comment.