Restore "load more messages" functionality.

// FREEBIE
pull/1/head
Matthew Chen 8 years ago
parent 78bf4fb57f
commit 163e66dd4f

@ -100,6 +100,8 @@ static const int kConversationInitialMaxRangeSize = kYapDatabasePageSize * kYapD
static const int kYapDatabaseRangeMaxLength = kYapDatabasePageSize * kYapDatabaseMaxPageCount; static const int kYapDatabaseRangeMaxLength = kYapDatabasePageSize * kYapDatabaseMaxPageCount;
static const int kYapDatabaseRangeMinLength = 0; static const int kYapDatabaseRangeMinLength = 0;
static const CGFloat kLoadMoreHeaderHeight = 60.f;
typedef enum : NSUInteger { typedef enum : NSUInteger {
kMediaTypePicture, kMediaTypePicture,
kMediaTypeVideo, kMediaTypeVideo,
@ -194,6 +196,8 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) {
@property (nonatomic, nullable) ThreadDynamicInteractions *dynamicInteractions; @property (nonatomic, nullable) ThreadDynamicInteractions *dynamicInteractions;
@property (nonatomic) BOOL hasClearedUnreadMessagesIndicator; @property (nonatomic) BOOL hasClearedUnreadMessagesIndicator;
@property (nonatomic) BOOL showLoadMoreHeader;
@property (nonatomic) UIButton *loadMoreHeader;
@property (nonatomic) uint64_t lastVisibleTimestamp; @property (nonatomic) uint64_t lastVisibleTimestamp;
@property (nonatomic, readonly) BOOL isGroupConversation; @property (nonatomic, readonly) BOOL isGroupConversation;
@ -455,7 +459,7 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) {
[self createScrollButtons]; [self createScrollButtons];
[self createHeaderViews]; [self createHeaderViews];
[self addNotificationListeners]; [self addNotificationListeners];
// [self updateLoadEarlierVisible]; [self updateShowLoadMoreHeader];
} }
- (void)createContents - (void)createContents
@ -483,6 +487,18 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) {
[self.inputToolbar autoPinWidthToSuperview]; [self.inputToolbar autoPinWidthToSuperview];
[self.inputToolbar autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.collectionView]; [self.inputToolbar autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.collectionView];
[self autoPinViewToBottomGuideOrKeyboard:self.inputToolbar]; [self autoPinViewToBottomGuideOrKeyboard:self.inputToolbar];
self.loadMoreHeader = [UIButton buttonWithType:UIButtonTypeCustom];
[self.loadMoreHeader setTitle:NSLocalizedString(@"load_earlier_messages", @"") forState:UIControlStateNormal];
[self.loadMoreHeader setTitleColor:[UIColor ows_materialBlueColor] forState:UIControlStateNormal];
self.loadMoreHeader.titleLabel.font = [UIFont ows_mediumFontWithSize:20.f];
[self.loadMoreHeader addTarget:self
action:@selector(loadMoreHeaderTapped:)
forControlEvents:UIControlEventTouchUpInside];
[self.collectionView addSubview:self.loadMoreHeader];
[self.loadMoreHeader autoPinWidthToWidthOfView:self.view];
[self.loadMoreHeader autoPinEdgeToSuperviewEdge:ALEdgeTop];
[self.loadMoreHeader autoSetDimension:ALDimensionHeight toSize:kLoadMoreHeaderHeight];
} }
- (void)registerCellClasses - (void)registerCellClasses
@ -1394,35 +1410,36 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) {
[self showConversationSettings]; [self showConversationSettings];
} }
//- (void)collectionView:(JSQMessagesCollectionView *)collectionView - (void)loadMoreHeaderTapped:(id)sender
// header:(JSQMessagesLoadEarlierHeaderView *)headerView {
// didTapLoadEarlierMessagesButton:(UIButton *)sender if (self.isUserScrolling) {
//{ DDLogError(@"%@ Ignoring load more tap while user is scrolling.", self.tag);
// OWSAssert(!self.isUserScrolling); return;
// }
// BOOL hasEarlierUnseenMessages = self.dynamicInteractions.hasMoreUnseenMessages;
// BOOL hasEarlierUnseenMessages = self.dynamicInteractions.hasMoreUnseenMessages;
// // We want to restore the current scroll state after we update the range, update
// // the dynamic interactions and re-layout. Here we take a "before" snapshot. // We want to restore the current scroll state after we update the range, update
// CGFloat scrollDistanceToBottom = self.safeContentHeight - self.collectionView.contentOffset.y; // the dynamic interactions and re-layout. Here we take a "before" snapshot.
// CGFloat scrollDistanceToBottom = self.safeContentHeight - self.collectionView.contentOffset.y;
// self.page = MIN(self.page + 1, (NSUInteger)kYapDatabaseMaxPageCount - 1);
// self.page = MIN(self.page + 1, (NSUInteger)kYapDatabaseMaxPageCount - 1);
// [self resetMappings];
// [self resetMappings];
// [self.collectionView layoutSubviews];
// [self.layout prepareLayout];
// self.collectionView.contentOffset = CGPointMake(0, self.safeContentHeight - scrollDistanceToBottom);
// self.collectionView.contentOffset = CGPointMake(0, self.safeContentHeight - scrollDistanceToBottom);
// // Dont auto-scroll afterloading more messagesunless we havemore unseen messages.
// // // Dont auto-scroll afterloading more messagesunless we havemore unseen messages.
// // Otherwise, tapping on "load more messages" autoscrolls you downward which is completely wrong. //
// if (hasEarlierUnseenMessages) { // Otherwise, tapping on "load more messages" autoscrolls you downward which is completely wrong.
// [self scrollToUnreadIndicatorAnimated]; if (hasEarlierUnseenMessages) {
// } [self scrollToUnreadIndicatorAnimated];
// }
// [self updateLoadEarlierVisible];
//} [self updateShowLoadMoreHeader];
}
- (BOOL)shouldShowLoadEarlierMessages - (BOOL)shouldShowLoadEarlierMessages
{ {
@ -1440,10 +1457,24 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) {
return show; return show;
} }
- (void)updateLoadEarlierVisible - (void)updateShowLoadMoreHeader
{ {
// TODO: [self setShowLoadMoreHeader:[self shouldShowLoadEarlierMessages]];
// [self setShowLoadEarlierMessagesHeader:[self shouldShowLoadEarlierMessages]]; }
- (void)setShowLoadMoreHeader:(BOOL)showLoadMoreHeader
{
BOOL valueChanged = _showLoadMoreHeader != showLoadMoreHeader;
_showLoadMoreHeader = showLoadMoreHeader;
self.loadMoreHeader.hidden = !showLoadMoreHeader;
self.loadMoreHeader.userInteractionEnabled = showLoadMoreHeader;
if (valueChanged) {
[self.collectionView.collectionViewLayout invalidateLayout];
[self.collectionView reloadData];
}
} }
- (void)updateMessageMappingRangeOptions:(MessagesRangeSizeMode)mode - (void)updateMessageMappingRangeOptions:(MessagesRangeSizeMode)mode
@ -3471,7 +3502,7 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) {
// //
// We can safely call prepareLayout to ensure the layout state is up-to-date // We can safely call prepareLayout to ensure the layout state is up-to-date
// since our layout uses a dirty flag internally to debounce redundant work. // since our layout uses a dirty flag internally to debounce redundant work.
[self.collectionView.collectionViewLayout prepareLayout]; [self.layout prepareLayout];
return [self.collectionView.collectionViewLayout collectionViewContentSize].height; return [self.collectionView.collectionViewLayout collectionViewContentSize].height;
} }
@ -3562,6 +3593,12 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) {
return self.viewItems; return self.viewItems;
} }
- (CGFloat)layoutHeaderHeight
{
return (self.showLoadMoreHeader ? kLoadMoreHeaderHeight : 0.f);
}
#pragma mark - ConversationInputToolbarDelegate #pragma mark - ConversationInputToolbarDelegate
- (void)sendButtonPressed - (void)sendButtonPressed
@ -3786,7 +3823,7 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) {
[self reloadViewItems]; [self reloadViewItems];
[self resetContentAndLayout]; [self resetContentAndLayout];
[self updateLoadEarlierVisible]; [self updateShowLoadMoreHeader];
[self ensureDynamicInteractions]; [self ensureDynamicInteractions];
[self updateBackButtonUnreadCount]; [self updateBackButtonUnreadCount];
[self updateNavigationBarSubtitleLabel]; [self updateNavigationBarSubtitleLabel];

@ -26,6 +26,8 @@ typedef NS_ENUM(NSInteger, ConversationViewLayoutAlignment) {
- (NSArray<id<ConversationViewLayoutItem>> *)layoutItems; - (NSArray<id<ConversationViewLayoutItem>> *)layoutItems;
- (CGFloat)layoutHeaderHeight;
@end @end
#pragma mark - #pragma mark -

@ -91,7 +91,7 @@ NS_ASSUME_NONNULL_BEGIN
NSArray<id<ConversationViewLayoutItem>> *layoutItems = self.delegate.layoutItems; NSArray<id<ConversationViewLayoutItem>> *layoutItems = self.delegate.layoutItems;
CGFloat y = vInset; CGFloat y = vInset + self.delegate.layoutHeaderHeight;
CGFloat contentBottom = y; CGFloat contentBottom = y;
BOOL isRTL = self.collectionView.isRTL; BOOL isRTL = self.collectionView.isRTL;

Loading…
Cancel
Save