Fix pasting using compose app shortcut while app isn't running

This commit is contained in:
Shadowfacts 2020-03-16 19:07:30 -04:00
parent c224d11417
commit b5fa0bceab
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 19 additions and 8 deletions

View File

@ -88,11 +88,16 @@ class MastodonController {
} }
} }
func getOwnInstance() { func getOwnInstance(completion: ((Instance) -> Void)? = nil) {
let request = Client.getInstance() if let instance = self.instance {
run(request) { (response) in completion?(instance)
guard case let .success(instance, _) = response else { fatalError() } } else {
self.instance = instance let request = Client.getInstance()
run(request) { (response) in
guard case let .success(instance, _) = response else { fatalError() }
self.instance = instance
completion?(instance)
}
} }
} }

View File

@ -137,9 +137,15 @@ class ComposeViewController: UIViewController {
// we have to set the font here, because the monospaced digit font is not available in IB // we have to set the font here, because the monospaced digit font is not available in IB
charactersRemainingLabel.font = .monospacedDigitSystemFont(ofSize: 17, weight: .regular) charactersRemainingLabel.font = .monospacedDigitSystemFont(ofSize: 17, weight: .regular)
updateCharactersRemaining()
updatePlaceholder() 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) composeAttachmentsViewController = ComposeAttachmentsViewController(attachments: currentDraft?.attachments ?? [], mastodonController: mastodonController)
composeRequiresAttachmentDescriptionsDidChange() composeRequiresAttachmentDescriptionsDidChange()
composeAttachmentsViewController.delegate = self composeAttachmentsViewController.delegate = self
@ -266,7 +272,7 @@ class ComposeViewController: UIViewController {
func updateCharactersRemaining() { func updateCharactersRemaining() {
let count = CharacterCounter.count(text: statusTextView.text) let count = CharacterCounter.count(text: statusTextView.text)
let cwCount = contentWarningEnabled ? (contentWarningTextField.text?.count ?? 0) : 0 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 { if remaining < 0 {
charactersRemainingLabel.textColor = .red charactersRemainingLabel.textColor = .red
compositionState.formUnion(.tooManyCharacters) compositionState.formUnion(.tooManyCharacters)