Skip to content

Commit

Permalink
Merge pull request #823 from stigger/master
Browse files Browse the repository at this point in the history
Fixing a crash and a couple of other issues introduced in a recent OTRMessagesViewController rework (#817)
  • Loading branch information
chrisballinger authored Aug 1, 2017
2 parents 168defb + 52fe316 commit 59a2847
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions ChatSecure/Classes/View Controllers/OTRMessagesViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ @interface OTRMessagesViewController () <UITextViewDelegate, OTRAttachmentPicker
@property (nonatomic, strong) UIView *jidForwardingHeaderView;

@property (nonatomic) BOOL loadingMessages;
@property (nonatomic) BOOL messageRangeExtended;
@property (nonatomic, strong) NSIndexPath *currentIndexPath;
@property (nonatomic, strong) id currentMessage;
@property (nonatomic, strong) NSCache *messageSizeCache;
Expand All @@ -113,6 +114,7 @@ - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibB
_state = [[MessagesViewControllerState alloc] init];
self.messageSizeCache = [NSCache new];
self.messageSizeCache.countLimit = kOTRMessagePageSize;
self.messageRangeExtended = NO;
}
return self;
}
Expand Down Expand Up @@ -354,6 +356,7 @@ - (nullable OTRAccount *)accountWithTransaction:(nonnull YapDatabaseReadTransact

- (void)setThreadKey:(NSString *)key collection:(NSString *)collection
{
self.currentIndexPath = nil;
NSString *oldKey = self.threadKey;
NSString *oldCollection = self.threadCollection;

Expand Down Expand Up @@ -1070,18 +1073,18 @@ - (void)updateRangeOptions:(BOOL)reset
{
YapDatabaseViewRangeOptions *options = [self.viewHandler.mappings rangeOptionsForGroup:self.threadKey];
if (reset) {
if (options != nil && options.length <= kOTRMessagePageSize) {
if (options != nil && !self.messageRangeExtended) {
return;
}
options = [YapDatabaseViewRangeOptions flexibleRangeWithLength:kOTRMessagePageSize
offset:0
from:YapDatabaseViewEnd];
self.messageSizeCache.countLimit = kOTRMessagePageSize;
self.messageRangeExtended = NO;
} else {
options = [YapDatabaseViewRangeOptions flexibleRangeWithLength:options.length + kOTRMessagePageSize
offset:0
from:YapDatabaseViewEnd];
options = [options copyWithNewLength:options.length + kOTRMessagePageSize];
self.messageSizeCache.countLimit += kOTRMessagePageSize;
self.messageRangeExtended = YES;
}
[self.viewHandler.mappings setRangeOptions:options forGroup:self.threadKey];

Expand All @@ -1099,6 +1102,7 @@ - (void)updateRangeOptions:(BOOL)reset
}];

if (!reset) {
[self.collectionView.collectionViewLayout invalidateLayout];
[self.collectionView layoutSubviews];
self.collectionView.contentOffset = CGPointMake(0, self.collectionView.contentSize.height - distanceToBottom);
}
Expand Down Expand Up @@ -1443,7 +1447,10 @@ - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollection

// Although JSQMessagesBubblesSizeCalculator has its own cache, its size is fixed and quite small, so it quickly chokes on scrolling into the past
CGSize size = [super collectionView:collectionView layout:collectionViewLayout sizeForItemAtIndexPath:indexPath];
[self.messageSizeCache setObject:[NSValue valueWithCGSize:size] forKey:key];
// The height of the first cell might change: on loading additional messages the date label most likely will disappear
if (indexPath.row > 0) {
[self.messageSizeCache setObject:[NSValue valueWithCGSize:size] forKey:key];
}
return size;
}

Expand Down

0 comments on commit 59a2847

Please sign in to comment.