前一天终於把bug都修好,让模拟器可以顺利地打开喔。今天继续把想要的主要功能做出来。
首先,我们把原本mock的资料换成从开新对话的对象,也就是方法didTapComposeButton
中的result
。
使用guard
确保资料有拿到,然後带入新的view controller。此时在这边除了拿名字作为聊天室的标题外,也确定为db key的email栏位有值。
private func createNewConversation(result: [String:String]) {
guard let name = result["name"], let email = result["email"] else {
return
}
let vc = ChatViewController()
vc.title = name
vc.navigationItem.largeTitleDisplayMode = .never
navigationController?.pushViewController(vc, animated: true)
}
接下来画面进入到chat view controller,因此我们来新增当中需要的内容。最终目标,我们希望可以可以送出讯息以及其他各种形式的对话。
首先,引入相关的函式库。
import InputBarAccessoryView
然後新增2个变数,otehrUserEmail
是从刚刚createNewConversation
拿到的email
,我们作为辨识在跟谁聊天的key值。
isNewConversation
将会被用於,如果这是一个新的对话,就要新增到db,然後显示在conversation的table view中,若值为false的话,则会append在原本conversation中。
public let otherUserEmail: String
public var isNewConversation = false
针对新增的property,我们新增上必须的initializer。
init(with email: String) {
self.otherUserEmail = email
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
接着把input bar的delegate指定给自己。并接着新增相对应的extension。
messageInputBar.delegate = self
我们首先确定传出去的讯息不为空白值,使用trim进行检查。
此时debug用,先print讯息确定真的内容有被抓取到、并显示在console。
extension ChatViewController: InputBarAccessoryViewDelegate {
func inputBar(_ inputBar: InputBarAccessoryView, didPressSendButtonWith text: String) {
guard !text.replacingOccurrences(of: " ", with: "").isEmpty else {
return
}
print("Sending: \(text)")
// send messages
if isNewConversation {
// add a new conversation to database
} else {
// append to existing conversation
}
}
}
当view成功跳转并显示後,我们再加上把滑鼠指针初始到打字区。
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
messageInputBar.inputTextView.becomeFirstResponder()
}
chat的框架已经差不多写好後,我们就可以针对後端资料库的逻辑来建立方法。一样先把框架写出来~
// MARK: - Sending messages, conversations
extension DatabaseManager {
/// Create a new conversation with target user email and first message sent
public func createNewConversation(with otherUserEmail: String, firstMessage: Message, completion: (Bool)) {
}
/// fetch and return all the conversation for the user and passed in emaill
public func getAllConversations(for email: String, completion: @escaping (Result<String, Error>) -> Void) {
}
/// Get all messages for a given conversation
public func getAllMessagesForConversation(with id: String, completion: @escaping (Result<String, Error>) -> Void) {
}
/// Send message with target conversation and messages
public func sendMessage(to conversation: String, message: Message, completion: @escaping (Bool) -> Void) {
}
}
今天虽然写不少,但主要都是针对接下来的逻辑在写框架,还没实际走到开发。不知道铁人赛是30天还是31天啊XD 希望明天跟後天可以把功能完成!
若上述内容有误或可以改进的部分,欢迎留言以及提出任何指教~
谢谢 ヘ| ´ω` |ノ
<<: [Day29] CI /CD with GitLab CI
为什麽要使用框架? 前端最重要的工作,就是让网页的资料与状态视觉化,有资料状态改变时,就要更动网页内...
Navigator 1.0 Flutter 有内建一个路由管理 API Navigator ,而Fl...
python有内建sort()与sorted()两种排序方法 以下将为各位详细介绍 sort() s...
前言 本章节,要讲的是SSH远端连线的机制与原理,以及SSH的使用方式。 什麽是OpenSSH? O...
1.computed & Watch Part_1 > Lab_Binding >...