From 02c96b7b0eac18a0a44077d12e7b1879bfa0d6d0 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 18 Aug 2017 14:46:03 -0400 Subject: [PATCH] Rework the contact offers. // FREEBIE --- Signal/src/views/OWSContactOffersCell.m | 71 +++++++++++++------------ 1 file changed, 36 insertions(+), 35 deletions(-) diff --git a/Signal/src/views/OWSContactOffersCell.m b/Signal/src/views/OWSContactOffersCell.m index c94bf1b30..70d825543 100644 --- a/Signal/src/views/OWSContactOffersCell.m +++ b/Signal/src/views/OWSContactOffersCell.m @@ -21,8 +21,6 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic) UIButton *addToProfileWhitelistButton; @property (nonatomic) UIButton *blockButton; -@property (nonatomic) NSArray *layoutConstraints; - @end #pragma mark - @@ -97,39 +95,6 @@ NS_ASSUME_NONNULL_BEGIN OWSAssert( interaction.hasBlockOffer || interaction.hasAddToContactsOffer || interaction.hasAddToProfileWhitelistOffer); - if (self.layoutConstraints) { - [NSLayoutConstraint deactivateConstraints:self.layoutConstraints]; - } - NSMutableArray *layoutConstraints = [NSMutableArray new]; - - [layoutConstraints addObject:[self.titleLabel autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:self.topVMargin]]; - [layoutConstraints addObjectsFromArray:[self.titleLabel autoPinLeadingAndTrailingToSuperview]]; - - __block UIView *lastView = self.titleLabel; - void (^layoutButton)(UIButton *, BOOL) = ^(UIButton *button, bool isVisible) { - if (isVisible) { - button.hidden = NO; - - [layoutConstraints addObject:[button autoPinEdge:ALEdgeTop - toEdge:ALEdgeBottom - ofView:lastView - withOffset:self.buttonVSpacing]]; - [layoutConstraints addObject:[button autoSetDimension:ALDimensionHeight - toSize:ceil([button sizeThatFits:CGSizeZero].height - + self.buttonVPadding)]]; - [layoutConstraints addObjectsFromArray:[button autoPinLeadingAndTrailingToSuperview]]; - lastView = button; - } else { - button.hidden = YES; - } - }; - - layoutButton(self.addToContactsButton, self.interaction.hasAddToContactsOffer); - layoutButton(self.addToProfileWhitelistButton, self.interaction.hasAddToProfileWhitelistOffer); - layoutButton(self.blockButton, self.interaction.hasBlockOffer); - - self.layoutConstraints = layoutConstraints; - [self setNeedsLayout]; } @@ -168,6 +133,42 @@ NS_ASSUME_NONNULL_BEGIN return 5.f; } +- (void)layoutSubviews +{ + [super layoutSubviews]; + + // We're using a bit of a hack to get JSQ to layout this and the unread indicator as + // "full width" cells. These cells will end up with an erroneous left margin that we + // want to reverse. + CGFloat contentWidth = self.width; + CGFloat left = -self.left; + + CGRect titleFrame = self.contentView.bounds; + titleFrame.origin = CGPointMake(left + self.hMargin, self.topVMargin); + titleFrame.size.width = contentWidth - 2 * self.hMargin; + titleFrame.size.height = ceil([self.titleLabel sizeThatFits:CGSizeZero].height); + self.titleLabel.frame = titleFrame; + + __block CGFloat y = round(self.titleLabel.bottom + self.buttonVSpacing); + void (^layoutButton)(UIButton *, BOOL) = ^(UIButton *button, bool isVisible) { + if (isVisible) { + button.hidden = NO; + + button.frame = CGRectMake(round(left + self.hMargin), + round(y), + floor(contentWidth - 2 * self.hMargin), + ceil([button sizeThatFits:CGSizeZero].height + self.buttonVPadding)); + y = round(button.bottom + self.buttonVSpacing); + } else { + button.hidden = YES; + } + }; + + layoutButton(self.addToContactsButton, self.interaction.hasAddToContactsOffer); + layoutButton(self.addToProfileWhitelistButton, self.interaction.hasAddToProfileWhitelistOffer); + layoutButton(self.blockButton, self.interaction.hasBlockOffer); +} + - (CGSize)bubbleSizeForInteraction:(OWSContactOffersInteraction *)interaction collectionViewWidth:(CGFloat)collectionViewWidth {