diff --git a/Session/Conversations/ConversationVC+Interaction.swift b/Session/Conversations/ConversationVC+Interaction.swift index 387485033..90ef7a481 100644 --- a/Session/Conversations/ConversationVC+Interaction.swift +++ b/Session/Conversations/ConversationVC+Interaction.swift @@ -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 diff --git a/Session/Conversations/ConversationVC.swift b/Session/Conversations/ConversationVC.swift index 43db26239..25a0c933d 100644 --- a/Session/Conversations/ConversationVC.swift +++ b/Session/Conversations/ConversationVC.swift @@ -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() diff --git a/Session/Conversations/Input View/InputTextView.swift b/Session/Conversations/Input View/InputTextView.swift index d907823f0..e39e1ddfe 100644 --- a/Session/Conversations/Input View/InputTextView.swift +++ b/Session/Conversations/Input View/InputTextView.swift @@ -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) diff --git a/Session/Conversations/Input View/InputView.swift b/Session/Conversations/Input View/InputView.swift index d8dcf6186..08b4db040 100644 --- a/Session/Conversations/Input View/InputView.swift +++ b/Session/Conversations/Input View/InputView.swift @@ -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()