Commit bfbf0374 authored by Moe Ahmadi's avatar Moe Ahmadi Committed by Commit Bot

[AF][IOS] Fixes the GooglePay logo animation in the FormSuggestionView

After crrev.com/c/1078871 that updated FormSuggestionView and
FormSuggestionLabel to use AutoLayout, the Google Pay logo stopped
animating. This CL fixes that regression and also changes the code
slightly so that the subviews are more lazily created.

Bug: 852890
Cq-Include-Trybots: luci.chromium.try:ios-simulator-full-configs;master.tryserver.chromium.mac:ios-simulator-cronet
Change-Id: I5388a3c4a7df2819e14b5772368e07f16c98564e
Reviewed-on: https://chromium-review.googlesource.com/1101543
Commit-Queue: Moe Ahmadi <mahmadi@chromium.org>
Reviewed-by: default avatarVasilii Sukhanov <vasilii@chromium.org>
Cr-Commit-Position: refs/heads/master@{#567997}
parent b142a563
......@@ -28,9 +28,19 @@ const CGFloat kSuggestionHorizontalMargin = 6;
} // namespace
@interface FormSuggestionView ()
// Creates and adds subviews.
- (void)setupSubviews;
@end
@implementation FormSuggestionView {
// The FormSuggestions that are displayed by this view.
NSArray* _suggestions;
// Handles user interactions.
id<FormSuggestionViewClient> _client;
}
- (instancetype)initWithFrame:(CGRect)frame
......@@ -38,8 +48,25 @@ const CGFloat kSuggestionHorizontalMargin = 6;
suggestions:(NSArray*)suggestions {
self = [super initWithFrame:frame];
if (self) {
_client = client;
_suggestions = [suggestions copy];
}
return self;
}
#pragma mark - UIView
- (void)willMoveToSuperview:(UIView*)newSuperview {
// Create and add subviews the first time this moves to a superview.
if (newSuperview && self.subviews.count == 0) {
[self setupSubviews];
}
[super willMoveToSuperview:newSuperview];
}
#pragma mark - Helper methods
- (void)setupSubviews {
self.showsVerticalScrollIndicator = NO;
self.showsHorizontalScrollIndicator = NO;
self.bounces = NO;
......@@ -48,42 +75,36 @@ const CGFloat kSuggestionHorizontalMargin = 6;
UIStackView* stackView = [[UIStackView alloc] initWithArrangedSubviews:@[]];
stackView.axis = UILayoutConstraintAxisHorizontal;
stackView.layoutMarginsRelativeArrangement = YES;
stackView.layoutMargins = UIEdgeInsetsMake(
kSuggestionVerticalMargin, kSuggestionHorizontalMargin,
stackView.layoutMargins =
UIEdgeInsetsMake(kSuggestionVerticalMargin, kSuggestionHorizontalMargin,
kSuggestionVerticalMargin, kSuggestionHorizontalMargin);
stackView.spacing = kSuggestionHorizontalMargin;
stackView.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:stackView];
AddSameConstraints(stackView, self);
[stackView.heightAnchor constraintEqualToAnchor:self.heightAnchor].active =
true;
void (^setupBlock)(FormSuggestion* suggestion, NSUInteger idx, BOOL* stop) =
^(FormSuggestion* suggestion, NSUInteger idx, BOOL* stop) {
auto setupBlock = ^(FormSuggestion* suggestion, NSUInteger idx, BOOL* stop) {
// Disable user interaction with suggestion if it is Google Pay logo.
BOOL userInteractionEnabled =
suggestion.identifier !=
autofill::POPUP_ITEM_ID_GOOGLE_PAY_BRANDING;
UIView* label = [[FormSuggestionLabel alloc]
initWithSuggestion:suggestion
suggestion.identifier != autofill::POPUP_ITEM_ID_GOOGLE_PAY_BRANDING;
UIView* label =
[[FormSuggestionLabel alloc] initWithSuggestion:suggestion
index:idx
userInteractionEnabled:userInteractionEnabled
numSuggestions:[_suggestions count]
client:client];
client:_client];
[stackView addArrangedSubview:label];
};
[_suggestions enumerateObjectsUsingBlock:setupBlock];
AddSameConstraints(stackView, self);
[stackView.heightAnchor constraintEqualToAnchor:self.heightAnchor].active =
true;
}
return self;
}
- (void)willMoveToSuperview:(UIView*)newSuperview {
FormSuggestion* firstSuggestion = [_suggestions firstObject];
if (firstSuggestion.identifier ==
autofill::POPUP_ITEM_ID_GOOGLE_PAY_BRANDING) {
UIView* firstLabel = [self.subviews firstObject];
DCHECK(firstLabel);
// If first suggestion is Google Pay logo animate it below the fold.
if (idx == 0U &&
suggestion.identifier == autofill::POPUP_ITEM_ID_GOOGLE_PAY_BRANDING) {
const CGFloat firstLabelWidth =
CGRectGetWidth([firstLabel frame]) + kSuggestionHorizontalMargin;
[label systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]
.width +
kSuggestionHorizontalMargin;
dispatch_time_t popTime =
dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC);
__weak FormSuggestionView* weakSelf = self;
......@@ -93,7 +114,8 @@ const CGFloat kSuggestionHorizontalMargin = 6;
animated:YES];
});
}
[super willMoveToSuperview:newSuperview];
};
[_suggestions enumerateObjectsUsingBlock:setupBlock];
}
- (NSArray*)suggestions {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment