Implement drafts

This commit is contained in:
Niels Andriesse 2021-03-02 09:33:31 +11:00
parent 6ca6455734
commit 21c7d0ce03
4 changed files with 30 additions and 4 deletions

View File

@ -250,6 +250,9 @@ extension ConversationVC : InputViewDelegate, MessageCellDelegate, ContextMenuAc
AudioServicesPlaySystemSound(soundID)
}
SSKEnvironment.shared.typingIndicators.didSendOutgoingMessage(inThread: thread)
Storage.write { transaction in
self.thread.setDraft("", transaction: transaction)
}
}
// MARK: Input View

View File

@ -149,6 +149,14 @@ final class ConversationVC : BaseVC, ConversationViewModelDelegate, OWSConversat
notificationCenter.addObserver(self, selector: #selector(handleGroupUpdatedNotification), name: .groupThreadUpdated, object: nil)
// Mentions
MentionsManager.populateUserPublicKeyCacheIfNeeded(for: thread.uniqueId!)
// Draft
var draft = ""
Storage.read { transaction in
draft = self.thread.currentDraft(with: transaction)
}
if !draft.isEmpty {
snInputView.text = draft
}
}
override func viewDidLayoutSubviews() {
@ -174,6 +182,16 @@ final class ConversationVC : BaseVC, ConversationViewModelDelegate, OWSConversat
markAllAsRead()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
let text = snInputView.text
if !text.isEmpty {
Storage.write { transaction in
self.thread.setDraft(text, transaction: transaction)
}
}
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
mediaCache.removeAllObjects()

View File

@ -1,6 +1,7 @@
public final class InputTextView : UITextView, UITextViewDelegate {
private let snDelegate: InputTextViewDelegate
private let maxWidth: CGFloat
private lazy var heightConstraint = self.set(.height, to: minHeight)
public override var text: String! { didSet { handleTextChanged() } }
@ -19,8 +20,9 @@ public final class InputTextView : UITextView, UITextViewDelegate {
private let maxHeight: CGFloat = 80
// MARK: Lifecycle
init(delegate: InputTextViewDelegate) {
init(delegate: InputTextViewDelegate, maxWidth: CGFloat) {
snDelegate = delegate
self.maxWidth = maxWidth
super.init(frame: CGRect.zero, textContainer: nil)
setUpViewHierarchy()
self.delegate = self
@ -60,9 +62,8 @@ public final class InputTextView : UITextView, UITextViewDelegate {
private func handleTextChanged() {
defer { snDelegate.inputTextViewDidChangeContent(self) }
placeholderLabel.isHidden = !text.isEmpty
let width = frame.width
let height = frame.height
let size = sizeThatFits(CGSize(width: width, height: .greatestFiniteMagnitude))
let size = sizeThatFits(CGSize(width: maxWidth, height: .greatestFiniteMagnitude))
// `textView.contentSize` isn't accurate when restoring a multiline draft, so we set it here manually
self.contentSize = size
let newHeight = size.height.clamp(minHeight, maxHeight)

View File

@ -51,7 +51,11 @@ final class InputView : UIView, InputViewButtonDelegate, InputTextViewDelegate,
return result
}()
private lazy var inputTextView = InputTextView(delegate: self)
private lazy var inputTextView: InputTextView = {
let adjustment = (InputViewButton.expandedSize - InputViewButton.size) / 2
let maxWidth = UIScreen.main.bounds.width - 2 * InputViewButton.expandedSize - 2 * Values.smallSpacing - 2 * (Values.mediumSpacing - adjustment)
return InputTextView(delegate: self, maxWidth: maxWidth)
}()
private lazy var additionalContentContainer = UIView()