From ab2ad57d6f021cb7f24238216dcc06f8ff44e5b6 Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Wed, 24 Feb 2021 16:58:50 -0800 Subject: [PATCH] VoiceOver improvement --- Views/UIKit/Content Views/StatusView.swift | 97 +++++++++++----------- Views/UIKit/StatusBodyView.swift | 44 ++++++---- 2 files changed, 76 insertions(+), 65 deletions(-) diff --git a/Views/UIKit/Content Views/StatusView.swift b/Views/UIKit/Content Views/StatusView.swift index 51ad769..fc54b50 100644 --- a/Views/UIKit/Content Views/StatusView.swift +++ b/Views/UIKit/Content Views/StatusView.swift @@ -66,8 +66,7 @@ final class StatusView: UIView { override func accessibilityActivate() -> Bool { if !statusConfiguration.viewModel.shouldShowContent { statusConfiguration.viewModel.toggleShowContent() - - UIAccessibility.post(notification: .screenChanged, argument: self) + accessibilityAttributedLabel = accessibilityAttributedLabel(forceShowContent: true) return true } else { @@ -409,7 +408,6 @@ private extension StatusView { .store(in: &cancellables) } - // swiftlint:disable:next cyclomatic_complexity func applyStatusConfiguration() { let viewModel = statusConfiguration.viewModel let isContextParent = viewModel.configuration.isContextParent @@ -588,50 +586,7 @@ private extension StatusView { isAccessibilityElement = !viewModel.configuration.isContextParent - let accessibilityAttributedLabel = NSMutableAttributedString(string: "") - - if !infoLabel.isHidden, let infoText = infoLabel.attributedText { - accessibilityAttributedLabel.appendWithSeparator(infoText) - } - - if accessibilityAttributedLabel.string.isEmpty { - accessibilityAttributedLabel.append(mutableDisplayName) - } else { - accessibilityAttributedLabel.appendWithSeparator(mutableDisplayName) - } - - if let bodyAccessibilityAttributedLabel = bodyView.accessibilityAttributedLabel { - accessibilityAttributedLabel.appendWithSeparator(bodyAccessibilityAttributedLabel) - } - - if let accessibilityTime = viewModel.accessibilityTime { - accessibilityAttributedLabel.appendWithSeparator(accessibilityTime) - } - - if viewModel.repliesCount > 0 { - accessibilityAttributedLabel.appendWithSeparator( - String.localizedStringWithFormat( - NSLocalizedString("status.replies-count", comment: ""), - viewModel.repliesCount)) - } - - if viewModel.identityContext.appPreferences.showReblogAndFavoriteCounts { - if viewModel.reblogsCount > 0 { - accessibilityAttributedLabel.appendWithSeparator( - String.localizedStringWithFormat( - NSLocalizedString("status.reblogs-count", comment: ""), - viewModel.reblogsCount)) - } - - if viewModel.favoritesCount > 0 { - accessibilityAttributedLabel.appendWithSeparator( - String.localizedStringWithFormat( - NSLocalizedString("status.favorites-count", comment: ""), - viewModel.favoritesCount)) - } - } - - self.accessibilityAttributedLabel = accessibilityAttributedLabel + accessibilityAttributedLabel = accessibilityAttributedLabel(forceShowContent: false) configureUserInteractionEnabledForAccessibility() @@ -764,6 +719,54 @@ private extension StatusView { } // swiftlint:enable function_body_length + func accessibilityAttributedLabel(forceShowContent: Bool) -> NSAttributedString { + let accessibilityAttributedLabel = NSMutableAttributedString(string: "") + + if !infoLabel.isHidden, let infoText = infoLabel.attributedText { + accessibilityAttributedLabel.appendWithSeparator(infoText) + } + + if let displayName = displayNameLabel.attributedText { + if accessibilityAttributedLabel.string.isEmpty { + accessibilityAttributedLabel.append(displayName) + } else { + accessibilityAttributedLabel.appendWithSeparator(displayName) + } + } + + accessibilityAttributedLabel.appendWithSeparator( + bodyView.accessibilityAttributedLabel(forceShowContent: forceShowContent)) + + if let accessibilityTime = statusConfiguration.viewModel.accessibilityTime { + accessibilityAttributedLabel.appendWithSeparator(accessibilityTime) + } + + if statusConfiguration.viewModel.repliesCount > 0 { + accessibilityAttributedLabel.appendWithSeparator( + String.localizedStringWithFormat( + NSLocalizedString("status.replies-count", comment: ""), + statusConfiguration.viewModel.repliesCount)) + } + + if statusConfiguration.viewModel.identityContext.appPreferences.showReblogAndFavoriteCounts { + if statusConfiguration.viewModel.reblogsCount > 0 { + accessibilityAttributedLabel.appendWithSeparator( + String.localizedStringWithFormat( + NSLocalizedString("status.reblogs-count", comment: ""), + statusConfiguration.viewModel.reblogsCount)) + } + + if statusConfiguration.viewModel.favoritesCount > 0 { + accessibilityAttributedLabel.appendWithSeparator( + String.localizedStringWithFormat( + NSLocalizedString("status.favorites-count", comment: ""), + statusConfiguration.viewModel.favoritesCount)) + } + } + + return accessibilityAttributedLabel + } + func setButtonImages(scale: UIImage.SymbolScale) { let visibility = statusConfiguration.viewModel.visibility let reblogSystemImageName: String diff --git a/Views/UIKit/StatusBodyView.swift b/Views/UIKit/StatusBodyView.swift index e8f1726..d7eb57d 100644 --- a/Views/UIKit/StatusBodyView.swift +++ b/Views/UIKit/StatusBodyView.swift @@ -61,24 +61,7 @@ final class StatusBodyView: UIView { cardView.viewModel = viewModel.cardViewModel cardView.isHidden = viewModel.cardViewModel == nil - let accessibilityAttributedLabel = NSMutableAttributedString(string: "") - - if !spoilerTextLabel.isHidden, !viewModel.shouldShowContent { - accessibilityAttributedLabel.appendWithSeparator( - NSLocalizedString("status.content-warning.accessibility", comment: "")) - - accessibilityAttributedLabel.appendWithSeparator(mutableSpoilerText) - } else if !contentTextView.isHidden { - accessibilityAttributedLabel.append(mutableContent) - } - - for view in [attachmentsView, pollView, cardView] where !view.isHidden { - guard let viewAccessibilityLabel = view.accessibilityLabel else { continue } - - accessibilityAttributedLabel.appendWithSeparator(viewAccessibilityLabel) - } - - self.accessibilityAttributedLabel = accessibilityAttributedLabel + accessibilityAttributedLabel = accessibilityAttributedLabel(forceShowContent: false) var accessibilityCustomActions = [UIAccessibilityCustomAction]() @@ -177,6 +160,31 @@ extension StatusBodyView { return height } + + func accessibilityAttributedLabel(forceShowContent: Bool) -> NSAttributedString { + let accessibilityAttributedLabel = NSMutableAttributedString(string: "") + + if !spoilerTextLabel.isHidden, + let spoilerText = spoilerTextLabel.attributedText, + let viewModel = viewModel, + !viewModel.shouldShowContent, + !forceShowContent { + accessibilityAttributedLabel.appendWithSeparator( + NSLocalizedString("status.content-warning.accessibility", comment: "")) + + accessibilityAttributedLabel.appendWithSeparator(spoilerText) + } else if (!contentTextView.isHidden || forceShowContent), let content = contentTextView.attributedText { + accessibilityAttributedLabel.append(content) + } + + for view in [attachmentsView, pollView, cardView] where !view.isHidden { + guard let viewAccessibilityLabel = view.accessibilityLabel else { continue } + + accessibilityAttributedLabel.appendWithSeparator(viewAccessibilityLabel) + } + + return accessibilityAttributedLabel + } } extension StatusBodyView: UITextViewDelegate {