Merge branch 'charlesmchen/conversationScrollRefinements'

pull/1/head
Matthew Chen 6 years ago
commit 8dddea3c6e

@ -101,6 +101,11 @@ typedef enum : NSUInteger {
kMediaTypeVideo, kMediaTypeVideo,
} kMediaTypes; } kMediaTypes;
typedef enum : NSUInteger {
kScrollContinuityBottom = 0,
kScrollContinuityTop,
} ScrollContinuity;
#pragma mark - #pragma mark -
@interface ConversationViewController () <AttachmentApprovalViewControllerDelegate, @interface ConversationViewController () <AttachmentApprovalViewControllerDelegate,
@ -197,6 +202,8 @@ typedef enum : NSUInteger {
@property (nonatomic) CGFloat scrollDistanceToBottomSnapshot; @property (nonatomic) CGFloat scrollDistanceToBottomSnapshot;
@property (nonatomic, nullable) NSNumber *lastKnownDistanceFromBottom; @property (nonatomic, nullable) NSNumber *lastKnownDistanceFromBottom;
@property (nonatomic) ScrollContinuity scrollContinuity;
@property (nonatomic, nullable) NSTimer *autoLoadMoreTimer;
@end @end
@ -239,6 +246,8 @@ typedef enum : NSUInteger {
NSString *audioActivityDescription = [NSString stringWithFormat:@"%@ voice note", self.logTag]; NSString *audioActivityDescription = [NSString stringWithFormat:@"%@ voice note", self.logTag];
_recordVoiceNoteAudioActivity = [[OWSAudioActivity alloc] initWithAudioDescription:audioActivityDescription behavior:OWSAudioBehavior_PlayAndRecord]; _recordVoiceNoteAudioActivity = [[OWSAudioActivity alloc] initWithAudioDescription:audioActivityDescription behavior:OWSAudioBehavior_PlayAndRecord];
self.scrollContinuity = kScrollContinuityBottom;
} }
#pragma mark - Dependencies #pragma mark - Dependencies
@ -467,6 +476,7 @@ typedef enum : NSUInteger {
- (void)dealloc - (void)dealloc
{ {
[self.reloadTimer invalidate]; [self.reloadTimer invalidate];
[self.autoLoadMoreTimer invalidate];
} }
- (void)reloadTimerDidFire - (void)reloadTimerDidFire
@ -800,11 +810,17 @@ typedef enum : NSUInteger {
- (void)resetContentAndLayout - (void)resetContentAndLayout
{ {
self.scrollContinuity = kScrollContinuityBottom;
// Avoid layout corrupt issues and out-of-date message subtitles. // Avoid layout corrupt issues and out-of-date message subtitles.
self.lastReloadDate = [NSDate new]; self.lastReloadDate = [NSDate new];
[self.conversationViewModel viewDidResetContentAndLayout]; [self.conversationViewModel viewDidResetContentAndLayout];
[self.collectionView.collectionViewLayout invalidateLayout]; [self.collectionView.collectionViewLayout invalidateLayout];
[self.collectionView reloadData]; [self.collectionView reloadData];
if (self.viewHasEverAppeared) {
// Try to update the lastKnownDistanceFromBottom; the content size may have changed.
[self updateLastKnownDistanceFromBottom];
}
} }
- (void)setUserHasScrolled:(BOOL)userHasScrolled - (void)setUserHasScrolled:(BOOL)userHasScrolled
@ -3743,21 +3759,34 @@ typedef enum : NSUInteger {
#pragma mark - UIScrollViewDelegate #pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView - (void)updateLastKnownDistanceFromBottom
{ {
// Constantly update the lastKnownDistanceFromBottom, // Never update the lastKnownDistanceFromBottom,
// unless we're presenting the menu actions which // if we're presenting the menu actions which
// temporarily meddles with the content insets. // temporarily meddles with the content insets.
if (!OWSWindowManager.sharedManager.isPresentingMenuActions) { if (!OWSWindowManager.sharedManager.isPresentingMenuActions) {
self.lastKnownDistanceFromBottom = @(self.safeDistanceFromBottom); self.lastKnownDistanceFromBottom = @(self.safeDistanceFromBottom);
} }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// Constantly try to update the lastKnownDistanceFromBottom.
[self updateLastKnownDistanceFromBottom];
[self updateLastVisibleSortId]; [self updateLastVisibleSortId];
__weak ConversationViewController *weakSelf = self; [self.autoLoadMoreTimer invalidate];
dispatch_async(dispatch_get_main_queue(), ^{ self.autoLoadMoreTimer = [NSTimer weakScheduledTimerWithTimeInterval:0.1f
[weakSelf autoLoadMoreIfNecessary]; target:self
}); selector:@selector(autoLoadMoreTimerDidFire)
userInfo:nil
repeats:NO];
}
- (void)autoLoadMoreTimerDidFire
{
[self autoLoadMoreIfNecessary];
} }
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
@ -4253,11 +4282,18 @@ typedef enum : NSUInteger {
- (CGPoint)collectionView:(UICollectionView *)collectionView - (CGPoint)collectionView:(UICollectionView *)collectionView
targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset
{ {
if (self.lastKnownDistanceFromBottom) { if (self.scrollContinuity == kScrollContinuityBottom && self.lastKnownDistanceFromBottom) {
// Adjust the content offset to reflect the "last known" distance // Adjust the content offset to reflect the "last known" distance
// from the bottom of the content. // from the bottom of the content.
CGFloat contentOffsetYBottom = self.maxContentOffsetY; CGFloat contentOffsetYBottom = self.maxContentOffsetY;
CGFloat contentOffsetY = contentOffsetYBottom - self.lastKnownDistanceFromBottom.floatValue; CGFloat contentOffsetY = contentOffsetYBottom - MAX(0, self.lastKnownDistanceFromBottom.floatValue);
CGFloat minContentOffsetY;
if (@available(iOS 11, *)) {
minContentOffsetY = -self.collectionView.safeAreaInsets.top;
} else {
minContentOffsetY = 0.f;
}
contentOffsetY = MAX(minContentOffsetY, contentOffsetY);
proposedContentOffset.y = contentOffsetY; proposedContentOffset.y = contentOffsetY;
} }
@ -4290,8 +4326,8 @@ typedef enum : NSUInteger {
// To determine that, we find the appropriate "content offset y" if // To determine that, we find the appropriate "content offset y" if
// the scroll view were scrolled down as far as possible. IFF the // the scroll view were scrolled down as far as possible. IFF the
// actual "content offset y" is "near" that value, we return YES. // actual "content offset y" is "near" that value, we return YES.
CGFloat contentOffsetYBottom = self.maxContentOffsetY; CGFloat maxContentOffsetY = self.maxContentOffsetY;
CGFloat distanceFromBottom = contentOffsetYBottom - self.collectionView.contentOffset.y; CGFloat distanceFromBottom = maxContentOffsetY - self.collectionView.contentOffset.y;
return distanceFromBottom; return distanceFromBottom;
} }
@ -4299,11 +4335,16 @@ typedef enum : NSUInteger {
{ {
CGFloat contentHeight = self.safeContentHeight; CGFloat contentHeight = self.safeContentHeight;
UIEdgeInsets adjustedContentInset;
if (@available(iOS 11, *)) {
adjustedContentInset = self.collectionView.adjustedContentInset;
} else {
adjustedContentInset = self.collectionView.contentInset;
}
// Note the usage of MAX() to handle the case where there isn't enough // Note the usage of MAX() to handle the case where there isn't enough
// content to fill the collection view at its current size. // content to fill the collection view at its current size.
CGFloat contentOffsetYBottom CGFloat maxContentOffsetY = contentHeight + adjustedContentInset.bottom - self.collectionView.bounds.size.height;
= MAX(0.f, contentHeight + self.collectionView.contentInset.bottom - self.collectionView.bounds.size.height); return maxContentOffsetY;
return contentOffsetYBottom;
} }
#pragma mark - ContactsPickerDelegate #pragma mark - ContactsPickerDelegate
@ -4517,6 +4558,8 @@ typedef enum : NSUInteger {
// if the user is inserting new interactions. // if the user is inserting new interactions.
__block BOOL scrollToBottom = NO; __block BOOL scrollToBottom = NO;
self.scrollContinuity = ([self isScrolledToBottom] ? kScrollContinuityBottom : kScrollContinuityTop);
void (^batchUpdates)(void) = ^{ void (^batchUpdates)(void) = ^{
OWSAssertIsOnMainThread(); OWSAssertIsOnMainThread();
@ -4575,6 +4618,9 @@ typedef enum : NSUInteger {
if (scrollToBottom) { if (scrollToBottom) {
[self scrollToBottomAnimated:NO]; [self scrollToBottomAnimated:NO];
} }
// Try to update the lastKnownDistanceFromBottom; the content size may have changed.
[self updateLastKnownDistanceFromBottom];
}; };
@try { @try {

Loading…
Cancel
Save