         dismiss(animated: true, completion: nil)
     }
 }
+
+extension ChatViewController: UICollectionViewDataSource {
+    
+    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
+        return messages.count
+    }
+    
+    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
+        let cell = ChatOtherMessageCell.dequeue(from: collectionView, at: indexPath)
+        cell.messageLabel.text = messages[indexPath.row]
+        cell.setNeedsLayout()
+        cell.layoutIfNeeded()
+        return cell
+    }
+}
+
+extension ChatViewController: UICollectionViewDelegateFlowLayout {
+    
+    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
+        otherMessageCellPrototype.messageLabel.text = messages[indexPath.row]
+        otherMessageCellPrototype.setNeedsLayout()
+        otherMessageCellPrototype.layoutIfNeeded()
+        let width = collectionView.frame.width
+        let height = otherMessageCellPrototype.containerView.frame.maxY + LayoutDimension().spacing * 0.5
+        return CGSize(width: width, height: height)
+    }
+    
+    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
+        return 0
+    }
+}

class ChatViewController: UIViewController {

    var chatView: ChatView!

    
    override func loadView() {
        let size = UIScreen.main.bounds.size

        chatView = ChatView()
        chatView.frame.size = size
        chatView.topBar.delegate = self
        chatView.collectionView.delegate = self
        chatView.collectionView.dataSource = self
        
        ChatOtherMessageCell.register(in: chatView.collectionView)
        
        otherMessageCellPrototype.frame.size.width = size.width
        
        view = chatView
    }
