From b5fa0bceab36947576bc51b46046d991015b3737 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 16 Mar 2020 19:07:30 -0400 Subject: [PATCH] Fix pasting using compose app shortcut while app isn't running --- Tusker/Controllers/MastodonController.swift | 15 ++++++++++----- .../Screens/Compose/ComposeViewController.swift | 12 +++++++++--- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/Tusker/Controllers/MastodonController.swift b/Tusker/Controllers/MastodonController.swift index 5e2ae194..7bde68b4 100644 --- a/Tusker/Controllers/MastodonController.swift +++ b/Tusker/Controllers/MastodonController.swift @@ -88,11 +88,16 @@ class MastodonController { } } - func getOwnInstance() { - let request = Client.getInstance() - run(request) { (response) in - guard case let .success(instance, _) = response else { fatalError() } - self.instance = instance + func getOwnInstance(completion: ((Instance) -> Void)? = nil) { + if let instance = self.instance { + completion?(instance) + } else { + let request = Client.getInstance() + run(request) { (response) in + guard case let .success(instance, _) = response else { fatalError() } + self.instance = instance + completion?(instance) + } } } diff --git a/Tusker/Screens/Compose/ComposeViewController.swift b/Tusker/Screens/Compose/ComposeViewController.swift index 9aa40dae..8e89cc09 100644 --- a/Tusker/Screens/Compose/ComposeViewController.swift +++ b/Tusker/Screens/Compose/ComposeViewController.swift @@ -137,9 +137,15 @@ class ComposeViewController: UIViewController { // we have to set the font here, because the monospaced digit font is not available in IB charactersRemainingLabel.font = .monospacedDigitSystemFont(ofSize: 17, weight: .regular) - updateCharactersRemaining() updatePlaceholder() - + // if the compose screen is opened via the home screen shortcut and app isn't running, + // the msatodon instance may not have been loaded yet + mastodonController.getOwnInstance { (_) in + DispatchQueue.main.async { + self.updateCharactersRemaining() + } + } + composeAttachmentsViewController = ComposeAttachmentsViewController(attachments: currentDraft?.attachments ?? [], mastodonController: mastodonController) composeRequiresAttachmentDescriptionsDidChange() composeAttachmentsViewController.delegate = self @@ -266,7 +272,7 @@ class ComposeViewController: UIViewController { func updateCharactersRemaining() { let count = CharacterCounter.count(text: statusTextView.text) let cwCount = contentWarningEnabled ? (contentWarningTextField.text?.count ?? 0) : 0 - let remaining = (mastodonController.instance.maxStatusCharacters ?? 500) - count - cwCount + let remaining = (mastodonController.instance?.maxStatusCharacters ?? 500) - count - cwCount if remaining < 0 { charactersRemainingLabel.textColor = .red compositionState.formUnion(.tooManyCharacters)