Top Alert Accessibility changes

This commit is contained in:
Pfeil, Scott Robert 2020-08-07 18:35:37 -04:00
parent d322764987
commit e2328b66d0
7 changed files with 75 additions and 39 deletions

View File

@ -29,4 +29,7 @@
// Handles making various parts accessible. // Handles making various parts accessible.
- (void)handleAccessibility; - (void)handleAccessibility;
/// Adds the top alert accessibility prefix to the view.
+ (void)amendAccesibilityLabelForView:(nonnull UIView *)view;
@end @end

View File

@ -71,18 +71,27 @@
} }
- (nonnull Button *)addCloseButtonWithAnimationDelegate:(nullable id <MVMCoreTopAlertAnimationDelegateProtocol>)animationDelegate { - (nonnull Button *)addCloseButtonWithAnimationDelegate:(nullable id <MVMCoreTopAlertAnimationDelegateProtocol>)animationDelegate {
Button *closeButton = [MVMCoreUICommonViewsUtility addCloseButtonTo:self action:^(Button * _Nonnull button) {
return [MVMCoreUICommonViewsUtility addCloseButtonTo:self action:^(Button * _Nonnull button) {
if (animationDelegate) { if (animationDelegate) {
[animationDelegate topAlertCloseButtonPressed]; [animationDelegate topAlertCloseButtonPressed];
} else { } else {
[[MVMCoreUISession sharedGlobal].topAlertView hideAlertView:nil]; [[MVMCoreUISession sharedGlobal].topAlertView hideAlertView:YES completionHandler:nil];
} }
} centeredVertically:YES]; } centeredVertically:YES];
[MVMCoreUITopAlertBaseView amendAccesibilityLabelForView:closeButton];
return closeButton;
} }
- (void)updateView:(CGFloat)size {} - (void)updateView:(CGFloat)size {}
- (void)handleAccessibility {} - (void)handleAccessibility {}
+ (void)amendAccesibilityLabelForView:(nonnull UIView *)view {
NSString *amendment = [MVMCoreUIUtility hardcodedStringWithKey:@"top_alert_notification"];
NSString *accessibilityLabel = view.accessibilityLabel;
if (accessibilityLabel && ![accessibilityLabel hasPrefix:amendment]) {
view.accessibilityLabel = [NSString stringWithFormat:@"%@ - %@", amendment, accessibilityLabel];
}
}
@end @end

View File

@ -52,7 +52,7 @@
- (void)handleAccessibility { - (void)handleAccessibility {
if (self.shortView.label.text.length > 0 && (!self.expanded || !self.onlyShowTopMessageWhenCollapsed)) { if (self.shortView.label.text.length > 0 && !self.expanded) {
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, self.shortView.label); UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, self.shortView.label);
} else if (self.buttonView.label.text.length > 0) { } else if (self.buttonView.label.text.length > 0) {
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, self.buttonView.label); UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, self.buttonView.label);
@ -61,18 +61,6 @@
} }
} }
- (void)amendAccesibilityLabel {
NSString *amendment = [MVMCoreUIUtility hardcodedStringWithKey:@"top_alert_notification"];
NSString *accessibilityLabel = self.buttonView.label.accessibilityLabel;
if (!accessibilityLabel) {
// The accessibility label is nil when in non-voice over mode. Therefore assign the label text for the voice over is turned on mid-session (i.e. testers 🤦).
accessibilityLabel = self.buttonView.label.text;
}
if (accessibilityLabel && ![accessibilityLabel hasPrefix:amendment]) {
self.buttonView.label.accessibilityLabel = [NSString stringWithFormat:@"%@ - %@", amendment, accessibilityLabel];
}
}
#pragma mark - Setup View #pragma mark - Setup View
- (void)updateView:(CGFloat)size { - (void)updateView:(CGFloat)size {
@ -86,7 +74,6 @@
self.translatesAutoresizingMaskIntoConstraints = NO; self.translatesAutoresizingMaskIntoConstraints = NO;
self.clipsToBounds = YES; self.clipsToBounds = YES;
self.expanded = NO; self.expanded = NO;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accessibilityFocusChanged:) name:UIAccessibilityElementFocusedNotification object:nil];
} }
return self; return self;
} }
@ -180,8 +167,6 @@
[self insertSubview:topAlertWithButton belowSubview:self.shortView]; [self insertSubview:topAlertWithButton belowSubview:self.shortView];
self.buttonView = topAlertWithButton; self.buttonView = topAlertWithButton;
[self amendAccesibilityLabel];
self.topConstraint = [NSLayoutConstraint constraintWithItem:topAlertWithButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.shortView attribute:NSLayoutAttributeBottom multiplier:1 constant:0]; self.topConstraint = [NSLayoutConstraint constraintWithItem:topAlertWithButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.shortView attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
[NSLayoutConstraint constraintPinSubview:topAlertWithButton pinTop:NO topConstant:0 pinBottom:YES bottomConstant:0 pinLeft:YES leftConstant:0 pinRight:YES rightConstant:0]; [NSLayoutConstraint constraintPinSubview:topAlertWithButton pinTop:NO topConstant:0 pinBottom:YES bottomConstant:0 pinLeft:YES leftConstant:0 pinRight:YES rightConstant:0];
} }
@ -209,6 +194,8 @@
- (void)setTopMessage:(nullable NSString *)topMessage { - (void)setTopMessage:(nullable NSString *)topMessage {
[MVMCoreDispatchUtility performBlockOnMainThread:^{ [MVMCoreDispatchUtility performBlockOnMainThread:^{
self.shortView.label.text = topMessage; self.shortView.label.text = topMessage;
self.shortView.label.accessibilityLabel = topMessage;
[MVMCoreUITopAlertBaseView amendAccesibilityLabelForView:self.shortView.label];
if (topMessage && (!self.onlyShowTopMessageWhenCollapsed || !self.expanded)) { if (topMessage && (!self.onlyShowTopMessageWhenCollapsed || !self.expanded)) {
self.shortViewHeight.active = NO; self.shortViewHeight.active = NO;
@ -222,7 +209,6 @@
[MVMCoreDispatchUtility performBlockOnMainThread:^{ [MVMCoreDispatchUtility performBlockOnMainThread:^{
[self setTopMessage:topMessage]; [self setTopMessage:topMessage];
[self.buttonView setupWithMessage:message subMessage:subMessage color:contentColor actionMap:actionMap additionalData:additionalData]; [self.buttonView setupWithMessage:message subMessage:subMessage color:contentColor actionMap:actionMap additionalData:additionalData];
[self amendAccesibilityLabel];
}]; }];
} }
@ -230,7 +216,6 @@
[MVMCoreDispatchUtility performBlockOnMainThread:^{ [MVMCoreDispatchUtility performBlockOnMainThread:^{
[self setTopMessage:topMessage]; [self setTopMessage:topMessage];
[self.buttonView setupWithMessage:message subMessage:subMessage color:contentColor buttonTitle:buttonTitle userActionHandler:userActionHandler]; [self.buttonView setupWithMessage:message subMessage:subMessage color:contentColor buttonTitle:buttonTitle userActionHandler:userActionHandler];
[self amendAccesibilityLabel];
}]; }];
} }
@ -254,6 +239,7 @@
- (void)setShortViewPressToExpand { - (void)setShortViewPressToExpand {
__weak typeof(self) weakSelf = self; __weak typeof(self) weakSelf = self;
self.shortView.label.accessibilityTraits = UIAccessibilityTraitButton;
[self.shortView.button addActionBlockWithEvent:UIControlEventTouchUpInside :^(Button * _Nonnull button) { [self.shortView.button addActionBlockWithEvent:UIControlEventTouchUpInside :^(Button * _Nonnull button) {
if (weakSelf) { if (weakSelf) {
[weakSelf expand:YES]; [weakSelf expand:YES];
@ -263,6 +249,7 @@
- (void)setShortViewPressToCollapse { - (void)setShortViewPressToCollapse {
__weak typeof(self) weakSelf = self; __weak typeof(self) weakSelf = self;
self.shortView.label.accessibilityTraits = UIAccessibilityTraitButton;
[self.shortView.button addActionBlockWithEvent:UIControlEventTouchUpInside :^(Button * _Nonnull button) { [self.shortView.button addActionBlockWithEvent:UIControlEventTouchUpInside :^(Button * _Nonnull button) {
if (weakSelf) { if (weakSelf) {
[weakSelf collapse]; [weakSelf collapse];
@ -362,15 +349,19 @@
} }
dispatch_time_t dispatchTime = dispatch_time(DISPATCH_TIME_NOW, dismissTime * NSEC_PER_SEC); dispatch_time_t dispatchTime = dispatch_time(DISPATCH_TIME_NOW, dismissTime * NSEC_PER_SEC);
dispatch_after(dispatchTime, dispatch_get_main_queue(), ^(void){ dispatch_after(dispatchTime, dispatch_get_main_queue(), ^(void){
if (weakSelf && weakSelf.expanded && weakSelf.collapseAutomaticallyAfterExpanded && ![self containsAccessiblityFocus]) { if (weakSelf && weakSelf.expanded && weakSelf.collapseAutomaticallyAfterExpanded) {
[weakSelf collapse]; // If accessibility focused, delay collapse.
if ([MVMCoreUIUtility viewContainsAccessiblityFocus:self]) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accessibilityFocusChanged:) name:UIAccessibilityElementFocusedNotification object:nil];
} else {
[weakSelf collapse];
}
} }
}); });
} }
} }
- (void)collapse { - (void)collapse {
if (self.expanded) { if (self.expanded) {
__weak typeof(self) weakSelf = self; __weak typeof(self) weakSelf = self;
MVMCoreBlockOperation *operation = [MVMCoreBlockOperation blockOperationWithBlock:^(MVMCoreBlockOperation * _Nonnull operation) { MVMCoreBlockOperation *operation = [MVMCoreBlockOperation blockOperationWithBlock:^(MVMCoreBlockOperation * _Nonnull operation) {
@ -406,19 +397,9 @@
} }
} }
- (BOOL)containsAccessiblityFocus {
if (!UIAccessibilityIsVoiceOverRunning()) {
return NO;
}
id focusedElement = UIAccessibilityFocusedElement(UIAccessibilityNotificationVoiceOverIdentifier);
if (![focusedElement isKindOfClass:[UIView class]]) {
return NO;
}
return [(UIView *)focusedElement isDescendantOfView:self];
}
- (void)accessibilityFocusChanged:(NSNotification *)notification { - (void)accessibilityFocusChanged:(NSNotification *)notification {
if (![self containsAccessiblityFocus]) { if (![MVMCoreUIUtility viewContainsAccessiblityFocus:self]) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIAccessibilityElementFocusedNotification object:nil];
[self collapse]; [self collapse];
} }
} }

View File

@ -45,6 +45,8 @@
- (void)updateView:(CGFloat)size { - (void)updateView:(CGFloat)size {
[super updateView:size]; [super updateView:size];
self.label.attributedText = [MVMCoreUITopAlertBaseView getStringForMessage:self.message subMessage:self.subMessage color:self.contentColor]; self.label.attributedText = [MVMCoreUITopAlertBaseView getStringForMessage:self.message subMessage:self.subMessage color:self.contentColor];
self.label.accessibilityLabel = self.label.text;
[MVMCoreUITopAlertBaseView amendAccesibilityLabelForView:self.label];
[self.button updateView:size]; [self.button updateView:size];
} }
@ -226,6 +228,8 @@
// Sets the string // Sets the string
self.label.attributedText = [MVMCoreUITopAlertBaseView getStringForMessage:message subMessage:subMessage color:color]; self.label.attributedText = [MVMCoreUITopAlertBaseView getStringForMessage:message subMessage:subMessage color:color];
self.label.accessibilityLabel = self.label.text;
[MVMCoreUITopAlertBaseView amendAccesibilityLabelForView:self.label];
// Sets the button // Sets the button
[self setupButtonWithActionMap:actionMap additionalData:additionalData]; [self setupButtonWithActionMap:actionMap additionalData:additionalData];
@ -245,6 +249,8 @@
// Sets the string // Sets the string
self.label.attributedText = [MVMCoreUITopAlertBaseView getStringForMessage:message subMessage:subMessage color:color]; self.label.attributedText = [MVMCoreUITopAlertBaseView getStringForMessage:message subMessage:subMessage color:color];
self.label.accessibilityLabel = self.label.text;
[MVMCoreUITopAlertBaseView amendAccesibilityLabelForView:self.label];
// Sets the color // Sets the color
if (color) { if (color) {
@ -265,6 +271,9 @@
[self setupWithButton:showButton]; [self setupWithButton:showButton];
if (showButton) { if (showButton) {
[self.button setTitle:[actionMap stringForKey:KeyTitle] forState:UIControlStateNormal]; [self.button setTitle:[actionMap stringForKey:KeyTitle] forState:UIControlStateNormal];
self.button.accessibilityLabel = [self.button titleForState:UIControlStateNormal];
[MVMCoreUITopAlertBaseView amendAccesibilityLabelForView:self.button];
[MVMCoreUITopAlertBaseView addActionToButton:self.button actionMap:actionMap additionalData:additionalData]; [MVMCoreUITopAlertBaseView addActionToButton:self.button actionMap:actionMap additionalData:additionalData];
} }
}]; }];
@ -276,6 +285,8 @@
BOOL showButton = buttonTitle.length > 0; BOOL showButton = buttonTitle.length > 0;
[self setupWithButton:showButton]; [self setupWithButton:showButton];
[self.button setTitle:buttonTitle forState:UIControlStateNormal]; [self.button setTitle:buttonTitle forState:UIControlStateNormal];
self.button.accessibilityLabel = [self.button titleForState:UIControlStateNormal];
[MVMCoreUITopAlertBaseView amendAccesibilityLabelForView:self.button];
if (showButton && userActionHandler) { if (showButton && userActionHandler) {
[self.button addActionBlockWithEvent:UIControlEventTouchUpInside :userActionHandler]; [self.button addActionBlockWithEvent:UIControlEventTouchUpInside :userActionHandler];
} }

View File

@ -44,6 +44,9 @@ NSString * const MFAccTopAlertClosed = @"Top alert notification is closed.";
@property (weak, nonatomic) MVMCoreUITopAlertExpandableView *topAlertClearspotView; @property (weak, nonatomic) MVMCoreUITopAlertExpandableView *topAlertClearspotView;
@property (strong, nonatomic) NSString *time; @property (strong, nonatomic) NSString *time;
/// Used if we delayed the collapse due to accessibility.
@property (copy, nonatomic) void (^ hideCompletionHandler)(BOOL finished);
@end @end
@implementation MVMCoreUITopAlertView @implementation MVMCoreUITopAlertView
@ -210,7 +213,13 @@ NSString * const MFAccTopAlertClosed = @"Top alert notification is closed.";
[[MVMCoreNavigationHandler sharedNavigationHandler] addNavigationOperation:operation]; [[MVMCoreNavigationHandler sharedNavigationHandler] addNavigationOperation:operation];
} }
- (void)hideAlertView:(void (^ __nullable)(BOOL finished))completionHandler { - (void)hideAlertView:(BOOL)forceful completionHandler:(void (^ __nullable)(BOOL finished))completionHandler {
// If accessible and focused, do not collapse until unfocused.
if (!forceful && [MVMCoreUIUtility viewContainsAccessiblityFocus:self]) {
self.hideCompletionHandler = completionHandler;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accessibilityFocusChanged:) name:UIAccessibilityElementFocusedNotification object:nil];
return;
}
__weak typeof(self) weakSelf = self; __weak typeof(self) weakSelf = self;
MVMCoreBlockOperation *operation = [MVMCoreBlockOperation blockOperationWithBlock:^(MVMCoreBlockOperation * _Nonnull operation) { MVMCoreBlockOperation *operation = [MVMCoreBlockOperation blockOperationWithBlock:^(MVMCoreBlockOperation * _Nonnull operation) {
@ -257,7 +266,7 @@ NSString * const MFAccTopAlertClosed = @"Top alert notification is closed.";
[((MVMCoreUITopAlertExpandableView *)self.currentAlert) collapse]; [((MVMCoreUITopAlertExpandableView *)self.currentAlert) collapse];
} else { } else {
// Top alert is not collapsable, remove it instead. // Top alert is not collapsable, remove it instead.
[self hideAlertView:NULL]; [self hideAlertView:NO completionHandler:NULL];
} }
} }
} }
@ -294,4 +303,13 @@ NSString * const MFAccTopAlertClosed = @"Top alert notification is closed.";
} }
} }
/// If the voice over user leaves top alert focus, hide.
- (void)accessibilityFocusChanged:(NSNotification *)notification {
if (![MVMCoreUIUtility viewContainsAccessiblityFocus:self]) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIAccessibilityElementFocusedNotification object:nil];
[self hideAlertView:YES completionHandler:self.hideCompletionHandler];
self.hideCompletionHandler = nil;
}
}
@end @end

View File

@ -34,6 +34,9 @@ NS_ASSUME_NONNULL_BEGIN
/// Gets the current visible view controller. Checks presented view controllers first, and then it checks on the NavigationController in the session object. /// Gets the current visible view controller. Checks presented view controllers first, and then it checks on the NavigationController in the session object.
+ (UIViewController *)getCurrentVisibleController; + (UIViewController *)getCurrentVisibleController;
/// Checks if the view or any descendents of the view is currently focused for voice over.
+ (BOOL)viewContainsAccessiblityFocus:(nonnull UIView *)view;
#pragma mark - Setters #pragma mark - Setters
+ (void)setMarginsForView:(nullable UIView *)view leading:(CGFloat)leading top:(CGFloat)top trailing:(CGFloat)trailing bottom:(CGFloat)bottom; + (void)setMarginsForView:(nullable UIView *)view leading:(CGFloat)leading top:(CGFloat)top trailing:(CGFloat)trailing bottom:(CGFloat)bottom;

View File

@ -81,6 +81,17 @@
return viewController; return viewController;
} }
+ (BOOL)viewContainsAccessiblityFocus:(nonnull UIView *)view {
if (!UIAccessibilityIsVoiceOverRunning()) {
return NO;
}
id focusedElement = UIAccessibilityFocusedElement(UIAccessibilityNotificationVoiceOverIdentifier);
if (![focusedElement isKindOfClass:[UIView class]]) {
return NO;
}
return [(UIView *)focusedElement isDescendantOfView:view];
}
#pragma mark - Setters #pragma mark - Setters
+ (void)setMarginsForView:(nullable UIView *)view leading:(CGFloat)leading top:(CGFloat)top trailing:(CGFloat)trailing bottom:(CGFloat)bottom { + (void)setMarginsForView:(nullable UIView *)view leading:(CGFloat)leading top:(CGFloat)top trailing:(CGFloat)trailing bottom:(CGFloat)bottom {