From 64640edd2b3ef0c466be5053418f0c4fdd1e2653 Mon Sep 17 00:00:00 2001 From: CMK Date: Mon, 18 Apr 2022 17:44:19 +0800 Subject: [PATCH] feat: add ProfileCardView a11y supports --- .../Content/ProfileCardView+ViewModel.swift | 26 +++++++++++++++++++ .../View/Content/ProfileCardView.swift | 14 +++++++--- .../ProfileCardTableViewCell.swift | 6 +++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/MastodonSDK/Sources/MastodonUI/View/Content/ProfileCardView+ViewModel.swift b/MastodonSDK/Sources/MastodonUI/View/Content/ProfileCardView+ViewModel.swift index 99eb27a5..5b6c4c59 100644 --- a/MastodonSDK/Sources/MastodonUI/View/Content/ProfileCardView+ViewModel.swift +++ b/MastodonSDK/Sources/MastodonUI/View/Content/ProfileCardView+ViewModel.swift @@ -42,6 +42,8 @@ extension ProfileCardView { @Published public var isBlocking = false @Published public var isBlockedBy = false + @Published public var groupedAccessibilityLabel = "" + init() { backgroundColor = ThemeService.shared.currentTheme.value.systemBackgroundColor Publishers.CombineLatest( @@ -75,6 +77,7 @@ extension ProfileCardView.ViewModel { bindBio(view: view) bindRelationship(view: view) bindDashboard(view: view) + bindAccessibility(view: view) } private func bindAppearacne(view: ProfileCardView) { @@ -185,4 +188,27 @@ extension ProfileCardView.ViewModel { } .store(in: &disposeBag) } + + private func bindAccessibility(view: ProfileCardView) { + let authorAccessibilityLabel = Publishers.CombineLatest( + $authorName, + $bioContent + ) + .map { authorName, bioContent -> String? in + var strings: [String?] = [] + strings.append(authorName?.string) + strings.append(bioContent?.string) + return strings.compactMap { $0 }.joined(separator: ", ") + } + + authorAccessibilityLabel + .map { $0 ?? "" } + .assign(to: &$groupedAccessibilityLabel) + + $groupedAccessibilityLabel + .sink { accessibilityLabel in + view.accessibilityLabel = accessibilityLabel + } + .store(in: &disposeBag) + } } diff --git a/MastodonSDK/Sources/MastodonUI/View/Content/ProfileCardView.swift b/MastodonSDK/Sources/MastodonUI/View/Content/ProfileCardView.swift index ddc9afe4..16351ebe 100644 --- a/MastodonSDK/Sources/MastodonUI/View/Content/ProfileCardView.swift +++ b/MastodonSDK/Sources/MastodonUI/View/Content/ProfileCardView.swift @@ -204,6 +204,8 @@ extension ProfileCardView { let bioMetaTextAdaptiveMarginContainerView = AdaptiveMarginContainerView() bioMetaTextAdaptiveMarginContainerView.contentView = bioMetaText.textView bioMetaTextAdaptiveMarginContainerView.margin = ProfileCardView.contentMargin + bioMetaText.textView.setContentHuggingPriority(.required - 1, for: .vertical) + bioMetaText.textView.setContentCompressionResistancePriority(.required - 1, for: .vertical) container.addArrangedSubview(bioMetaTextAdaptiveMarginContainerView) container.setCustomSpacing(16, after: bioMetaTextAdaptiveMarginContainerView) @@ -218,6 +220,7 @@ extension ProfileCardView { infoContainer.addArrangedSubview(UIView()) let relationshipActionButtonShadowContainer = ShadowBackgroundContainer() infoContainer.addArrangedSubview(relationshipActionButtonShadowContainer) + updateInfoContainerLayout() relationshipActionButton.translatesAutoresizingMaskIntoConstraints = false relationshipActionButtonShadowContainer.addSubview(relationshipActionButton) @@ -227,7 +230,7 @@ extension ProfileCardView { relationshipActionButton.trailingAnchor.constraint(equalTo: relationshipActionButtonShadowContainer.trailingAnchor), relationshipActionButton.bottomAnchor.constraint(equalTo: relationshipActionButtonShadowContainer.bottomAnchor), relationshipActionButton.widthAnchor.constraint(greaterThanOrEqualToConstant: ProfileCardView.friendshipActionButtonSize.width).priority(.required - 1), - relationshipActionButton.heightAnchor.constraint(equalToConstant: ProfileCardView.friendshipActionButtonSize.height).priority(.required - 10), + relationshipActionButton.heightAnchor.constraint(equalToConstant: ProfileCardView.friendshipActionButtonSize.height).priority(.required - 2), ]) let bottomPadding = UIView() @@ -247,12 +250,17 @@ extension ProfileCardView { } public override func layoutSubviews() { + updateInfoContainerLayout() super.layoutSubviews() - + } + +} + +extension ProfileCardView { + private func updateInfoContainerLayout() { let isCompactAdaptive = bounds.width < 350 infoContainer.axis = isCompactAdaptive ? .vertical : .horizontal } - } extension ProfileCardView { diff --git a/MastodonSDK/Sources/MastodonUI/View/TableViewCell/ProfileCardTableViewCell.swift b/MastodonSDK/Sources/MastodonUI/View/TableViewCell/ProfileCardTableViewCell.swift index d3c8f223..5961ad10 100644 --- a/MastodonSDK/Sources/MastodonUI/View/TableViewCell/ProfileCardTableViewCell.swift +++ b/MastodonSDK/Sources/MastodonUI/View/TableViewCell/ProfileCardTableViewCell.swift @@ -70,6 +70,12 @@ extension ProfileCardTableViewCell { ]) profileCardView.delegate = self + + profileCardView.isAccessibilityElement = true + accessibilityElements = [ + profileCardView, + profileCardView.relationshipActionButton + ] } }