생성일: 2022년 2월 15일 오후 9:52
protocol FeedCellDelegate: AnyObject {
func cell(_ cell: FeedCell, wantsToShowCommentsFor post: Post)
func cell(_ cell: FeedCell, didLike post: Post)
func cell(_ cell: FeedCell, wantsToShowProfileFor uid: String)
}
class FeedCell: UICollectionViewCell {
//MARK: - Properties
var viewModel: PostViewModel? {
didSet { configure() }
}
weak var delegate: FeedCellDelegate?
private lazy var profileImageView: UIImageView = {
let iv = UIImageView()
iv.contentMode = .scaleAspectFill
iv.clipsToBounds = true
iv.isUserInteractionEnabled = true
iv.backgroundColor = .lightGray
let tap = UITapGestureRecognizer(target: self, action: #selector(showUserProfile))
iv.isUserInteractionEnabled = true
iv.addGestureRecognizer(tap)
return iv
}()
... 중략 ...
//MARK: - Actions
@objc func showUserProfile() {
guard let viewModel = viewModel else { return }
delegate?.cell(self, wantsToShowProfileFor: viewModel.post.ownerUid)
}
}
//MARK: - FeedCellDelegate
extension FeedController: FeedCellDelegate {
func cell(_ cell: FeedCell, wantsToShowProfileFor uid: String) {
UserService.fetchUser(withUid: uid) { user in
let controller = ProfileController(user: user)
self.navigationController?.pushViewController(controller, animated: true)
}
}
}