diff --git a/Signal/src/ViewControllers/ConversationView/Cells/OWSExpirationTimerView.m b/Signal/src/ViewControllers/ConversationView/Cells/OWSExpirationTimerView.m index 76bdf39e7..15532f8da 100644 --- a/Signal/src/ViewControllers/ConversationView/Cells/OWSExpirationTimerView.m +++ b/Signal/src/ViewControllers/ConversationView/Cells/OWSExpirationTimerView.m @@ -161,7 +161,7 @@ const CGFloat kExpirationTimerViewSize = 16.f; } CGFloat ratioRemaining = (CGFloat)timeUntilFlashing / (CGFloat)self.initialDurationSeconds; - CGFloat ratioComplete = Clamp((CGFloat)1.0 - ratioRemaining, 0, 1.0); + CGFloat ratioComplete = CGFloatClamp((CGFloat)1.0 - ratioRemaining, 0, 1.0); CGPoint startPosition = CGPointMake(0, self.fullHourglassImageView.height * ratioComplete); // We offset the bottom slightly to make sure the duration of the perceived animation is correct. diff --git a/Signal/src/ViewControllers/ConversationView/ConversationInputToolbar.m b/Signal/src/ViewControllers/ConversationView/ConversationInputToolbar.m index 578d5c3f8..0571a1e42 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationInputToolbar.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationInputToolbar.m @@ -316,7 +316,7 @@ static const CGFloat ConversationInputToolbarBorderViewHeight = 0.5; const CGFloat kMaxTextViewHeight = 98.f; const CGFloat textViewDesiredHeight = (self.inputTextView.contentSize.height + self.inputTextView.contentInset.top + self.inputTextView.contentInset.bottom); - const CGFloat textViewHeight = ceil(Clamp(textViewDesiredHeight, kMinTextViewHeight, kMaxTextViewHeight)); + const CGFloat textViewHeight = ceil(CGFloatClamp(textViewDesiredHeight, kMinTextViewHeight, kMaxTextViewHeight)); const CGFloat kMinContentHeight = kMinTextViewHeight + textViewVInset * 2; self.textViewHeight = textViewHeight; diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index 8db321fdf..cea1d46c2 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -3960,7 +3960,7 @@ typedef enum : NSUInteger { // keyboard, up to the limits of the content bounds. CGFloat insetChange = newInsets.bottom - oldInsets.bottom; CGFloat oldYOffset = self.collectionView.contentOffset.y; - CGFloat newYOffset = Clamp(oldYOffset + insetChange, 0, self.safeContentHeight); + CGFloat newYOffset = CGFloatClamp(oldYOffset + insetChange, 0, self.safeContentHeight); CGPoint newOffset = CGPointMake(0, newYOffset); // If the user is dismissing the keyboard via interactive scrolling, any additional conset offset feels @@ -4739,7 +4739,7 @@ typedef enum : NSUInteger { const CGFloat swipeTranslation = ([gestureRecognizer translationInView:self.view].x * (self.view.isRTL ? +1.f : -1.f)); - const CGFloat ratioComplete = Clamp(swipeTranslation / self.view.frame.size.width, 0, 1); + const CGFloat ratioComplete = CGFloatClamp(swipeTranslation / self.view.frame.size.width, 0, 1); switch (gestureRecognizer.state) { case UIGestureRecognizerStateBegan: { diff --git a/Signal/src/ViewControllers/HomeView/HomeViewCell.h b/Signal/src/ViewControllers/HomeView/HomeViewCell.h index 2063fdc14..4e51d15fc 100644 --- a/Signal/src/ViewControllers/HomeView/HomeViewCell.h +++ b/Signal/src/ViewControllers/HomeView/HomeViewCell.h @@ -14,9 +14,8 @@ NS_ASSUME_NONNULL_BEGIN + (NSString *)cellReuseIdentifier; - (void)configureWithThread:(TSThread *)thread - contactsManager:(OWSContactsManager *)contactsManager - blockedPhoneNumberSet:(NSSet *)blockedPhoneNumberSet - shouldHaveBottomSeparator:(BOOL)shouldHaveBottomSeparator; + contactsManager:(OWSContactsManager *)contactsManager + blockedPhoneNumberSet:(NSSet *)blockedPhoneNumberSet; @end diff --git a/Signal/src/ViewControllers/HomeView/HomeViewCell.m b/Signal/src/ViewControllers/HomeView/HomeViewCell.m index fbdefd232..371e6393b 100644 --- a/Signal/src/ViewControllers/HomeView/HomeViewCell.m +++ b/Signal/src/ViewControllers/HomeView/HomeViewCell.m @@ -6,6 +6,7 @@ #import "OWSAvatarBuilder.h" #import "Signal-Swift.h" #import +#import #import #import #import @@ -15,16 +16,11 @@ NS_ASSUME_NONNULL_BEGIN -const NSUInteger kHomeViewCellHeight = 72; -const NSUInteger kHomeViewCellHMargin = 16; -const NSUInteger kHomeViewCellVMargin = 12; -const NSUInteger kHomeViewAvatarSize = kHomeViewCellHeight - kHomeViewCellVMargin * 2; -const NSUInteger kHomeViewAvatarHSpacing = 12; - @interface HomeViewCell () @property (nonatomic) AvatarImageView *avatarView; -@property (nonatomic) UIView *payloadView; +@property (nonatomic) UIStackView *payloadView; +@property (nonatomic) UIStackView *topRowView; @property (nonatomic) UILabel *nameLabel; @property (nonatomic) UILabel *snippetLabel; @property (nonatomic) UILabel *dateTimeLabel; @@ -76,17 +72,22 @@ const NSUInteger kHomeViewAvatarHSpacing = 12; self.avatarView = [[AvatarImageView alloc] init]; [self.contentView addSubview:self.avatarView]; - [self.avatarView autoSetDimension:ALDimensionWidth toSize:kHomeViewAvatarSize]; - [self.avatarView autoSetDimension:ALDimensionHeight toSize:kHomeViewAvatarSize]; - [self.avatarView autoPinLeadingToSuperviewMarginWithInset:kHomeViewCellHMargin]; + [self.avatarView autoSetDimension:ALDimensionWidth toSize:self.avatarSize]; + [self.avatarView autoSetDimension:ALDimensionHeight toSize:self.avatarSize]; + [self.avatarView autoPinLeadingToSuperviewMarginWithInset:self.cellHMargin]; [self.avatarView autoVCenterInSuperview]; [self.avatarView setContentHuggingHigh]; [self.avatarView setCompressionResistanceHigh]; - self.payloadView = [UIView containerView]; + self.payloadView = [UIStackView new]; + self.payloadView.axis = UILayoutConstraintAxisVertical; [self.contentView addSubview:self.payloadView]; - [self.payloadView autoPinLeadingToTrailingEdgeOfView:self.avatarView offset:kHomeViewAvatarHSpacing]; + [self.payloadView autoPinLeadingToTrailingEdgeOfView:self.avatarView offset:self.avatarHSpacing]; + [self.payloadView autoPinTrailingToSuperviewMarginWithInset:self.cellHMargin]; [self.payloadView autoVCenterInSuperview]; + // Ensure that the cell's contents never overflow the cell bounds. + [self.payloadView autoPinEdgeToSuperviewMargin:ALEdgeTop relation:NSLayoutRelationGreaterThanOrEqual]; + [self.payloadView autoPinEdgeToSuperviewMargin:ALEdgeBottom relation:NSLayoutRelationGreaterThanOrEqual]; self.nameLabel = [UILabel new]; self.nameLabel.lineBreakMode = NSLineBreakByTruncatingTail; @@ -98,26 +99,19 @@ const NSUInteger kHomeViewAvatarHSpacing = 12; [self.dateTimeLabel setContentHuggingHorizontalHigh]; [self.dateTimeLabel setCompressionResistanceHorizontalHigh]; - UIStackView *topRowView = [[UIStackView alloc] initWithArrangedSubviews:@[ + self.topRowView = [[UIStackView alloc] initWithArrangedSubviews:@[ self.nameLabel, self.dateTimeLabel, ]]; - topRowView.axis = UILayoutConstraintAxisHorizontal; - topRowView.spacing = 4; - [self.payloadView addSubview:topRowView]; - [topRowView autoPinLeadingToSuperviewMargin]; - [topRowView autoPinTrailingToSuperviewMargin]; - [topRowView autoPinTopToSuperviewMargin]; + self.topRowView.axis = UILayoutConstraintAxisHorizontal; + self.topRowView.alignment = UIStackViewAlignmentCenter; + [self.payloadView addArrangedSubview:self.topRowView]; self.snippetLabel = [UILabel new]; self.snippetLabel.font = [self snippetFont]; self.snippetLabel.numberOfLines = 1; self.snippetLabel.lineBreakMode = NSLineBreakByTruncatingTail; - [self.payloadView addSubview:self.snippetLabel]; - [self.snippetLabel autoPinLeadingToSuperviewMargin]; - [self.snippetLabel autoPinTrailingToSuperviewMargin]; - [self.snippetLabel autoPinBottomToSuperviewMargin]; - [self.snippetLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:topRowView withOffset:5.f]; + [self.payloadView addArrangedSubview:self.snippetLabel]; [self.snippetLabel setContentHuggingHorizontalLow]; [self.snippetLabel setCompressionResistanceHorizontalLow]; @@ -129,9 +123,6 @@ const NSUInteger kHomeViewAvatarHSpacing = 12; self.unreadBadge = [NeverClearView new]; self.unreadBadge.backgroundColor = [UIColor ows_materialBlueColor]; - [self.contentView addSubview:self.unreadBadge]; - [self.unreadBadge autoPinTrailingToSuperviewMarginWithInset:kHomeViewCellHMargin]; - [self.unreadBadge autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self.dateTimeLabel]; [self.unreadBadge setContentHuggingHigh]; [self.unreadBadge setCompressionResistanceHigh]; @@ -145,11 +136,6 @@ const NSUInteger kHomeViewAvatarHSpacing = 12; return NSStringFromClass([self class]); } -+ (CGFloat)rowHeight -{ - return kHomeViewCellHeight; -} - - (void)initializeLayout { self.selectionStyle = UITableViewCellSelectionStyleDefault; @@ -163,15 +149,12 @@ const NSUInteger kHomeViewAvatarHSpacing = 12; - (void)configureWithThread:(TSThread *)thread contactsManager:(OWSContactsManager *)contactsManager blockedPhoneNumberSet:(NSSet *)blockedPhoneNumberSet - shouldHaveBottomSeparator:(BOOL)shouldHaveBottomSeparator { OWSAssertIsOnMainThread(); OWSAssert(thread); OWSAssert(contactsManager); OWSAssert(blockedPhoneNumberSet); - // TODO: Honor shouldHaveBottomSeparator. - self.thread = thread; self.contactsManager = contactsManager; @@ -184,22 +167,22 @@ const NSUInteger kHomeViewAvatarHSpacing = 12; [self updateNameLabel]; [self updateAvatarView]; + self.payloadView.spacing = 0.f; + self.topRowView.spacing = ceil([HomeViewCell scaleValueWithDynamicType:5]); + // We update the fonts every time this cell is configured to ensure that // changes to the dynamic type settings are reflected. self.snippetLabel.font = [self snippetFont]; self.snippetLabel.attributedText = [self attributedSnippetForThread:thread blockedPhoneNumberSet:blockedPhoneNumberSet]; - self.dateTimeLabel.attributedText = [self attributedStringForDate:thread.lastMessageDate]; - - self.separatorInset - = UIEdgeInsetsMake(0, kHomeViewAvatarSize + kHomeViewCellHMargin + kHomeViewAvatarHSpacing, 0, 0); + self.dateTimeLabel.attributedText = [self attributedStringForDate:thread.lastMessageDate]; self.dateTimeLabel.textColor = hasUnreadMessages ? [UIColor ows_materialBlueColor] : [UIColor ows_darkGrayColor]; NSUInteger unreadCount = [[OWSMessageUtils sharedManager] unreadMessagesInThread:thread]; if (unreadCount > 0) { + [self.topRowView addArrangedSubview:self.unreadBadge]; - self.unreadBadge.hidden = NO; self.unreadLabel.font = [UIFont ows_dynamicTypeCaption1Font]; self.unreadLabel.text = [OWSFormat formatInt:MIN(99, (int)unreadCount)]; @@ -210,15 +193,8 @@ const NSUInteger kHomeViewAvatarHSpacing = 12; self.unreadBadge.layer.cornerRadius = unreadBadgeSize / 2; [self.viewConstraints addObjectsFromArray:@[ - [self.unreadBadge autoSetDimension:ALDimensionWidth toSize:unreadBadgeSize], - [self.unreadBadge autoSetDimension:ALDimensionHeight toSize:unreadBadgeSize], - [self.unreadBadge autoPinLeadingToTrailingEdgeOfView:self.payloadView offset:4.f], - ]]; - } else { - self.unreadBadge.hidden = YES; - - [self.viewConstraints addObjectsFromArray:@[ - [self.payloadView autoPinTrailingToSuperviewMarginWithInset:kHomeViewCellHMargin], + [self.unreadBadge autoSetDimension:ALDimensionWidth toSize:unreadBadgeSize], + [self.unreadBadge autoSetDimension:ALDimensionHeight toSize:unreadBadgeSize], ]]; } } @@ -240,7 +216,7 @@ const NSUInteger kHomeViewAvatarHSpacing = 12; } self.avatarView.image = - [OWSAvatarBuilder buildImageForThread:thread diameter:kHomeViewAvatarSize contactsManager:contactsManager]; + [OWSAvatarBuilder buildImageForThread:thread diameter:self.avatarSize contactsManager:contactsManager]; } - (NSAttributedString *)attributedSnippetForThread:(TSThread *)thread @@ -331,7 +307,7 @@ const NSUInteger kHomeViewAvatarHSpacing = 12; - (UIFont *)snippetFont { - return [UIFont ows_dynamicTypeBodyFont]; + return [UIFont ows_dynamicTypeFootnoteFont]; } - (UIFont *)nameFont @@ -345,6 +321,42 @@ const NSUInteger kHomeViewAvatarHSpacing = 12; return [UIFont ows_dynamicTypeFootnoteFont]; } +// A simple function to scale dimensions to reflect dynamic type. Given a value +// we lerp it larger linearly to reflect size of dynamic type relative to a +// reference value for default dynamic type sizes. +// +// * We _NEVER_ scale values down. +// * We cap scaling. ++ (CGFloat)scaleValueWithDynamicType:(CGFloat)minValue +{ + // The default size of dynamic "body" type. + const NSUInteger kReferenceFontSizeMin = 17.f; + + CGFloat referenceFontSize = UIFont.ows_dynamicTypeBodyFont.pointSize; + CGFloat alpha = CGFloatClamp(referenceFontSize / kReferenceFontSizeMin, 1.f, 1.3f); + return minValue * alpha; +} + ++ (CGFloat)rowHeight +{ + return 72; +} + +- (NSUInteger)cellHMargin +{ + return 16; +} + +- (NSUInteger)avatarSize +{ + return 48.f; +} + +- (NSUInteger)avatarHSpacing +{ + return 12.f; +} + #pragma mark - Reuse - (void)prepareForReuse @@ -357,6 +369,8 @@ const NSUInteger kHomeViewAvatarHSpacing = 12; self.thread = nil; self.contactsManager = nil; + [self.unreadBadge removeFromSuperview]; + [[NSNotificationCenter defaultCenter] removeObserver:self]; } diff --git a/Signal/src/ViewControllers/HomeView/HomeViewController.m b/Signal/src/ViewControllers/HomeView/HomeViewController.m index 9d4ba834f..fec465118 100644 --- a/Signal/src/ViewControllers/HomeView/HomeViewController.m +++ b/Signal/src/ViewControllers/HomeView/HomeViewController.m @@ -210,6 +210,7 @@ typedef NS_ENUM(NSInteger, CellState) { kArchiveState, kInboxState }; self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; self.tableView.delegate = self; self.tableView.dataSource = self; + self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; [self.tableView registerClass:[HomeViewCell class] forCellReuseIdentifier:HomeViewCell.cellReuseIdentifier]; [self.view addSubview:self.tableView]; [self.tableView autoPinWidthToSuperview]; @@ -583,13 +584,9 @@ typedef NS_ENUM(NSInteger, CellState) { kArchiveState, kInboxState }; TSThread *thread = [self threadForIndexPath:indexPath]; - BOOL isLastCell = (indexPath.row == [self tableView:tableView numberOfRowsInSection:indexPath.section] - 1); - BOOL shouldHaveBottomSeparator = !isLastCell; - [cell configureWithThread:thread - contactsManager:self.contactsManager - blockedPhoneNumberSet:self.blockedPhoneNumberSet - shouldHaveBottomSeparator:shouldHaveBottomSeparator]; + contactsManager:self.contactsManager + blockedPhoneNumberSet:self.blockedPhoneNumberSet]; if ((unsigned long)indexPath.row == [self.threadMappings numberOfItemsInSection:0] - 1) { cell.separatorInset = UIEdgeInsetsMake(0.f, cell.bounds.size.width, 0.f, 0.f); diff --git a/Signal/src/ViewControllers/ThreadSettings/UpdateGroupViewController.m b/Signal/src/ViewControllers/ThreadSettings/UpdateGroupViewController.m index fcea5ff6f..a67d5e7ae 100644 --- a/Signal/src/ViewControllers/ThreadSettings/UpdateGroupViewController.m +++ b/Signal/src/ViewControllers/ThreadSettings/UpdateGroupViewController.m @@ -276,7 +276,7 @@ NS_ASSUME_NONNULL_BEGIN cell.accessoryMessage = NSLocalizedString( @"CONTACT_CELL_IS_BLOCKED", @"An indicator that a contact has been blocked."); } else { - cell.selectionStyle = UITableViewCellSeparatorStyleNone; + cell.selectionStyle = UITableViewCellSelectionStyleNone; } } else { // In the "members" section, we label "new" members as such when editing an existing group. diff --git a/SignalMessaging/attachments/AttachmentApprovalViewController.swift b/SignalMessaging/attachments/AttachmentApprovalViewController.swift index db24d8c87..ccaa6464d 100644 --- a/SignalMessaging/attachments/AttachmentApprovalViewController.swift +++ b/SignalMessaging/attachments/AttachmentApprovalViewController.swift @@ -686,6 +686,6 @@ class CaptioningToolbar: UIView, UITextViewDelegate { private func clampedTextViewHeight(fixedWidth: CGFloat) -> CGFloat { let contentSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude)) - return Clamp(contentSize.height, kMinTextViewHeight, maxTextViewHeight) + return CGFloatClamp(contentSize.height, kMinTextViewHeight, maxTextViewHeight) } } diff --git a/SignalMessaging/categories/UIView+OWS.m b/SignalMessaging/categories/UIView+OWS.m index 4fcdaee9c..4ca3d2741 100644 --- a/SignalMessaging/categories/UIView+OWS.m +++ b/SignalMessaging/categories/UIView+OWS.m @@ -124,7 +124,7 @@ CGFloat ScaleFromIPhone5(CGFloat iPhone5Value) - (NSLayoutConstraint *)autoPinToAspectRatio:(CGFloat)ratio { // Clamp to ensure view has reasonable aspect ratio. - CGFloat clampedRatio = Clamp(ratio, 0.05, 95.0); + CGFloat clampedRatio = CGFloatClamp(ratio, 0.05, 95.0); if (clampedRatio != ratio) { OWSFail(@"Invalid aspect ratio: %f for view: %@", ratio, self); } diff --git a/SignalMessaging/utils/OWSMath.h b/SignalMessaging/utils/OWSMath.h index 82d82968e..4d4826017 100644 --- a/SignalMessaging/utils/OWSMath.h +++ b/SignalMessaging/utils/OWSMath.h @@ -1,23 +1,23 @@ // -// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // NS_ASSUME_NONNULL_BEGIN // TODO: We'll eventually want to promote these into an OWSMath.h header. -static inline CGFloat Clamp(CGFloat value, CGFloat minValue, CGFloat maxValue) +static inline CGFloat CGFloatClamp(CGFloat value, CGFloat minValue, CGFloat maxValue) { return MAX(minValue, MIN(maxValue, value)); } -static inline CGFloat Clamp01(CGFloat value) +static inline CGFloat CGFloatClamp01(CGFloat value) { - return Clamp(value, 0.f, 1.f); + return CGFloatClamp(value, 0.f, 1.f); } static inline CGFloat CGFloatLerp(CGFloat left, CGFloat right, CGFloat alpha) { - alpha = Clamp01(alpha); + alpha = CGFloatClamp01(alpha); return (left * (1.f - alpha)) + (right * alpha); } @@ -27,6 +27,12 @@ static inline CGFloat CGFloatInverseLerp(CGFloat value, CGFloat minValue, CGFloa return (value - minValue) / (maxValue - minValue); } +// Ceil to an even number +static inline CGFloat CeilEven(CGFloat value) +{ + return 2.f * ceil(value * 0.5f); +} + void SetRandFunctionSeed(void); NS_ASSUME_NONNULL_END