From d008b882cb1a42cce5085a025fe0957ab405d450 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 31 Aug 2020 23:07:41 -0400 Subject: [PATCH] Use context menu for visibility on iOS 14 --- .../Compose/ComposeHostingController.swift | 34 ++++++++++++++----- .../Screens/Compose/MainComposeTextView.swift | 21 +++++++++++- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/Tusker/Screens/Compose/ComposeHostingController.swift b/Tusker/Screens/Compose/ComposeHostingController.swift index bd717ac6..d996197c 100644 --- a/Tusker/Screens/Compose/ComposeHostingController.swift +++ b/Tusker/Screens/Compose/ComposeHostingController.swift @@ -116,9 +116,15 @@ class ComposeHostingController: UIHostingController { let toolbar = UIToolbar() toolbar.translatesAutoresizingMaskIntoConstraints = false - let visibilityItem = UIBarButtonItem(image: UIImage(systemName: draft.visibility.imageName), style: .plain, target: self, action: #selector(visibilityButtonPressed)) - visibilityItem.accessibilityLabel = String(format: NSLocalizedString("Visibility: %@", comment: "compose visiblity accessibility label"), draft.visibility.displayName) + let visibilityAction: Selector? + if #available(iOS 14.0, *) { + visibilityAction = nil + } else { + visibilityAction = #selector(visibilityButtonPressed(_:)) + } + let visibilityItem = UIBarButtonItem(image: UIImage(systemName: draft.visibility.imageName), style: .plain, target: self, action: visibilityAction) visibilityBarButtonItems.append(visibilityItem) + visibilityChanged(draft.visibility) toolbar.items = [ UIBarButtonItem(title: "CW", style: .plain, target: self, action: #selector(cwButtonPressed)), @@ -185,6 +191,15 @@ class ComposeHostingController: UIHostingController { for item in visibilityBarButtonItems { item.image = UIImage(systemName: newVisibility.imageName) item.accessibilityLabel = String(format: NSLocalizedString("Visibility: %@", comment: "compose visiblity accessibility label"), draft.visibility.displayName) + if #available(iOS 14.0, *) { + let elements = Status.Visibility.allCases.map { (visibility) -> UIMenuElement in + let state = visibility == newVisibility ? UIMenuElement.State.on : .off + return UIAction(title: visibility.displayName, image: UIImage(systemName: visibility.unfilledImageName), identifier: nil, discoverabilityTitle: nil, attributes: [], state: state) { (_) in + self.draft.visibility = visibility + } + } + item.menu = UIMenu(title: "", image: nil, identifier: nil, options: [], children: elements) + } } } @@ -217,12 +232,15 @@ class ComposeHostingController: UIHostingController { } @objc func visibilityButtonPressed(_ sender: UIBarButtonItem) { - let alertController = UIAlertController(currentVisibility: draft.visibility) { (visibility) in - guard let visibility = visibility else { return } - self.draft.visibility = visibility - } - alertController.popoverPresentationController?.barButtonItem = sender - present(alertController, animated: true) +// if #available(iOS 14.0, *) { +// } else { + let alertController = UIAlertController(currentVisibility: draft.visibility) { (visibility) in + guard let visibility = visibility else { return } + self.draft.visibility = visibility + } + alertController.popoverPresentationController?.barButtonItem = sender + present(alertController, animated: true) +// } } @objc func draftsButtonPresed() { diff --git a/Tusker/Screens/Compose/MainComposeTextView.swift b/Tusker/Screens/Compose/MainComposeTextView.swift index 5bc32aba..aeddf260 100644 --- a/Tusker/Screens/Compose/MainComposeTextView.swift +++ b/Tusker/Screens/Compose/MainComposeTextView.swift @@ -64,7 +64,14 @@ struct MainComposeWrappedTextView: UIViewRepresentable { textView.textContainer.lineBreakMode = .byWordWrapping context.coordinator.textView = textView - let visibilityButton = UIBarButtonItem(image: UIImage(systemName: visibility.imageName), style: .plain, target: nil, action: #selector(ComposeHostingController.visibilityButtonPressed(_:))) + let visibilityAction: Selector? + if #available(iOS 14.0, *) { + visibilityAction = nil + } else { + visibilityAction = #selector(ComposeHostingController.visibilityButtonPressed(_:)) + } + let visibilityButton = UIBarButtonItem(image: UIImage(systemName: visibility.imageName), style: .plain, target: nil, action: visibilityAction) + updateVisibilityMenu(visibilityButton) let toolbar = UIToolbar() toolbar.translatesAutoresizingMaskIntoConstraints = false toolbar.items = [ @@ -118,6 +125,18 @@ struct MainComposeWrappedTextView: UIViewRepresentable { return formatButtons } + private func updateVisibilityMenu(_ visibilityButton: UIBarButtonItem) { + if #available(iOS 14.0, *) { + let elements = Status.Visibility.allCases.map { (visibility) -> UIMenuElement in + let state = visibility == self.visibility ? UIMenuElement.State.on : .off + return UIAction(title: visibility.displayName, image: UIImage(systemName: visibility.unfilledImageName), identifier: nil, discoverabilityTitle: nil, attributes: [], state: state) { (_) in + self.uiState.draft.visibility = visibility + } + } + visibilityButton.menu = UIMenu(title: "", image: nil, identifier: nil, options: [], children: elements) + } + } + func updateUIView(_ uiView: UITextView, context: Context) { uiView.text = text visibilityButton?.image = UIImage(systemName: visibility.imageName)