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) AudioServicesPlaySystemSound(soundID)
} }
SSKEnvironment.shared.typingIndicators.didSendOutgoingMessage(inThread: thread) SSKEnvironment.shared.typingIndicators.didSendOutgoingMessage(inThread: thread)
Storage.write { transaction in
self.thread.setDraft("", transaction: transaction)
}
} }
// MARK: Input View // 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) notificationCenter.addObserver(self, selector: #selector(handleGroupUpdatedNotification), name: .groupThreadUpdated, object: nil)
// Mentions // Mentions
MentionsManager.populateUserPublicKeyCacheIfNeeded(for: thread.uniqueId!) 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() { override func viewDidLayoutSubviews() {
@ -174,6 +182,16 @@ final class ConversationVC : BaseVC, ConversationViewModelDelegate, OWSConversat
markAllAsRead() 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) { override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated) super.viewDidDisappear(animated)
mediaCache.removeAllObjects() mediaCache.removeAllObjects()

View file

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

View file

@ -51,7 +51,11 @@ final class InputView : UIView, InputViewButtonDelegate, InputTextViewDelegate,
return result 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() private lazy var additionalContentContainer = UIView()