From d9bae42f81e946b415a15225998cbc39315d6a4a Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 22 Feb 2020 15:43:17 -0500 Subject: [PATCH] Prevent empty drafts from being saved --- .../Screens/Compose/ComposeViewController.swift | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/Tusker/Screens/Compose/ComposeViewController.swift b/Tusker/Screens/Compose/ComposeViewController.swift index d4382ef954..d1c9cae596 100644 --- a/Tusker/Screens/Compose/ComposeViewController.swift +++ b/Tusker/Screens/Compose/ComposeViewController.swift @@ -367,12 +367,21 @@ class ComposeViewController: UIViewController { let description = mediaView.descriptionTextView.text ?? "" attachments.append(.init(attachment: attachment, description: description)) } - let cw = contentWarningEnabled ? contentWarningTextField.text : nil + let statusText = statusTextView.text.trimmingCharacters(in: .whitespacesAndNewlines) + let cw = contentWarningEnabled ? contentWarningTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) : nil let account = mastodonController.accountInfo! - if let currentDraft = self.currentDraft { - currentDraft.update(accountID: account.id, text: self.statusTextView.text, contentWarning: cw, attachments: attachments) + if attachments.count == 0, statusText.isEmpty, cw?.isEmpty ?? true { + if let currentDraft = self.currentDraft { + DraftsManager.shared.remove(currentDraft) + } else { + return + } } else { - self.currentDraft = DraftsManager.shared.create(accountID: account.id, text: self.statusTextView.text, contentWarning: cw, inReplyToID: inReplyToID, attachments: attachments) + if let currentDraft = self.currentDraft { + currentDraft.update(accountID: account.id, text: statusText, contentWarning: cw, attachments: attachments) + } else { + self.currentDraft = DraftsManager.shared.create(accountID: account.id, text: statusText, contentWarning: cw, inReplyToID: inReplyToID, attachments: attachments) + } } DraftsManager.save() }