Skip to content

Commit

Permalink
[refactor] #245: 채팅 사이드메뉴 delegate 패턴 활용 리팩토링 + ViewModel 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-muuu committed Jun 4, 2023
1 parent 8f8591b commit 5d3f0c2
Showing 1 changed file with 34 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,27 @@

import UIKit

class ChattingSideSheetViewController: UIViewController, UIGestureRecognizerDelegate {
protocol ChattingSideMenuDelegate: AnyObject{
func willShowDeclarationBottomSheet(index: Int)
func willShowExitRoomAlert()
}

class ChattingSideSheetViewController: UIViewController, UIGestureRecognizerDelegate, DeclarationDelegate {

weak var delegate: ChattingSideMenuDelegate?

private let viewModel: any BlockUserInterface

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

//MARK: - Properties
var declarationHandler: ((IndexPath) -> Void)?
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

let mainView = ChattingSideSheetView()
private let mainView = ChattingSideSheetView()

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -26,9 +41,12 @@ class ChattingSideSheetViewController: UIViewController, UIGestureRecognizerDele
}

private func initialzie(){
mainView.tableView.separatorStyle = .none
mainView.tableView.delegate = self
mainView.tableView.dataSource = self
setTableViewDelegate()
mainView.exitStackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(exitBtnDidTap)))
}

private func setTableViewDelegate(){
mainView.tableView.initializeDelegate(self)
}

private func layout(){
Expand All @@ -39,38 +57,25 @@ class ChattingSideSheetViewController: UIViewController, UIGestureRecognizerDele
}
}

@objc func declarationBtnDidClicked(_ sender: UIButton){

let cell = sender.superview?.superview?.superview as! ChattingMemberTableViewCell

let index = mainView.tableView.indexPath(for: cell)

self.declarationHandler!(index!)
func willShowDelclarationBottomSheet(index: Int) {
delegate?.willShowDeclarationBottomSheet(index: index)
}

@objc private func exitBtnDidTap(){
delegate?.willShowExitRoomAlert()
}
}

extension ChattingSideSheetViewController: UITableViewDelegate, UITableViewDataSource{

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return 3
viewModel.chattingmMembers.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if(indexPath.row == 0){
guard let cell = tableView.dequeueReusableCell(withIdentifier: ChattingMemberTableViewCell.cellIdentifier, for: indexPath) as? ChattingMemberTableViewCell else{
fatalError()
}
cell.setMeTag()
cell.setCrownImage() // TODO: - 방장 데이터 불러오면 선언 위치 바꾸기
return cell
}else{
guard let cell = tableView.dequeueReusableCell(withIdentifier: ChattingMemberTableViewCell.cellIdentifier, for: indexPath) as? ChattingMemberTableViewCell else{
fatalError()
}
cell.setDeclarationBtn()
cell.declarationBtn.addTarget(self, action: #selector(declarationBtnDidClicked), for: .touchUpInside)
return cell
tableView.dequeueReusableCell(for: indexPath, cellType: ChattingMemberTableViewCell.self).then{
$0.delegate = self
$0.bindingData(viewModel.chattingmMembers[indexPath.row])
}
}
}

0 comments on commit 5d3f0c2

Please sign in to comment.