Swift: Firebase Chat App Part 8 - Building User Interface & Dependencies (Real-time) - Xcode 11
import JGProgressHUD
...
private let spinner = JGProgressHUD(style: .dark)
...
@objc private func loginButtonTapped() {
emailField.resignFirstResponder()
passwordField.resignFirstResponder()
guard
let email = emailField.text, !email.isEmpty else {
alertUserLoginError("Enter your email")
return
}
guard
let password = passwordField.text, !password.isEmpty else {
alertUserLoginError("Enter your password")
return
}
guard password.count >= 6 else {
alertUserLoginError("Password must be at least 6 characters")
return
}
spinner.show(in: view)
// Firebase Sign In
FirebaseAuth.Auth.auth().signIn(withEmail: email, password: password) { [weak self] result, error in
guard let self = self else { return }
DispatchQueue.main.async {
self.spinner.dismiss()
}
guard
let result = result,
error == nil else {
print("Failed to sign in user with email: \(email)")
DispatchQueue.main.async {
self.alertUserLoginError("Failed to sign in your email")
}
return
}
let user = result.user
self.navigationController?.dismiss(animated: true)
}
}
import UIKit
import MessageKit
class ChatViewController: MessagesViewController {
private var messages = [Message]()
private let selfSender = Sender(photoURLString: "",
senderId: "1",
displayName: "Junyeong Park")
override func viewDidLoad() {
super.viewDidLoad()
messages.append(Message(sender: selfSender, messageId: "Mock Id 1", sentDate: Date(), kind: .text("Hello, World!")))
messages.append(Message(sender: selfSender, messageId: "Mock Id 2", sentDate: Date(), kind: .text("Hello, World! Hey, There!")))
setChatViewUI()
}
private func setChatViewUI() {
view.backgroundColor = .systemBackground
messagesCollectionView.messagesDataSource = self
messagesCollectionView.messagesDisplayDelegate = self
messagesCollectionView.messagesLayoutDelegate = self
}
}
extension ChatViewController: MessagesDataSource, MessagesLayoutDelegate, MessagesDisplayDelegate {
func currentSender() -> MessageKit.SenderType {
return selfSender
}
func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessageKit.MessagesCollectionView) -> MessageKit.MessageType {
return messages[indexPath.section]
}
func numberOfSections(in messagesCollectionView: MessageKit.MessagesCollectionView) -> Int {
return messages.count
}
}
SenderType
에 따라 작성자의 UI를 그리는 currentSender
MessageType
에 따라 메시지를 그리는 messageForItem
함수import Foundation
import MessageKit
struct Message: MessageType {
var sender: SenderType
var messageId: String
var sentDate: Date
var kind: MessageKind
}
struct Sender: SenderType {
var photoURLString: String
var senderId: String
var displayName: String
}
MessageType
사용