Merge branch 'charlesmchen/viewItemAttachmentsVsDBConnection'

pull/1/head
Matthew Chen 8 years ago
commit 114de70f31

@ -4007,7 +4007,9 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) {
if (viewItem) { if (viewItem) {
viewItem.previousRow = viewItem.row; viewItem.previousRow = viewItem.row;
} else { } else {
viewItem = [[ConversationViewItem alloc] initWithTSInteraction:interaction isGroupThread:isGroupThread]; viewItem = [[ConversationViewItem alloc] initWithInteraction:interaction
isGroupThread:isGroupThread
transaction:transaction];
} }
viewItem.row = (NSInteger)row; viewItem.row = (NSInteger)row;
[viewItems addObject:viewItem]; [viewItems addObject:viewItem];
@ -4125,7 +4127,7 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) {
if (!interaction) { if (!interaction) {
OWSFail(@"%@ could not reload interaction", self.logTag); OWSFail(@"%@ could not reload interaction", self.logTag);
} else { } else {
[viewItem replaceInteraction:interaction]; [viewItem replaceInteraction:interaction transaction:transaction];
} }
}]; }];
} }

@ -30,6 +30,7 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType);
@class TSAttachmentPointer; @class TSAttachmentPointer;
@class TSAttachmentStream; @class TSAttachmentStream;
@class TSInteraction; @class TSInteraction;
@class YapDatabaseReadTransaction;
// This is a ViewModel for cells in the conversation view. // This is a ViewModel for cells in the conversation view.
// //
@ -55,15 +56,15 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType);
// previous update. // previous update.
@property (nonatomic) NSInteger previousRow; @property (nonatomic) NSInteger previousRow;
//@property (nonatomic, weak) ConversationViewCell *lastCell;
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithTSInteraction:(TSInteraction *)interaction isGroupThread:(BOOL)isGroupThread; - (instancetype)initWithInteraction:(TSInteraction *)interaction
isGroupThread:(BOOL)isGroupThread
transaction:(YapDatabaseReadTransaction *)transaction;
- (ConversationViewCell *)dequeueCellForCollectionView:(UICollectionView *)collectionView - (ConversationViewCell *)dequeueCellForCollectionView:(UICollectionView *)collectionView
indexPath:(NSIndexPath *)indexPath; indexPath:(NSIndexPath *)indexPath;
- (void)replaceInteraction:(TSInteraction *)interaction; - (void)replaceInteraction:(TSInteraction *)interaction transaction:(YapDatabaseReadTransaction *)transaction;
- (void)clearCachedLayoutState; - (void)clearCachedLayoutState;

@ -63,7 +63,9 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType)
@implementation ConversationViewItem @implementation ConversationViewItem
- (instancetype)initWithTSInteraction:(TSInteraction *)interaction isGroupThread:(BOOL)isGroupThread - (instancetype)initWithInteraction:(TSInteraction *)interaction
isGroupThread:(BOOL)isGroupThread
transaction:(YapDatabaseReadTransaction *)transaction
{ {
self = [super init]; self = [super init];
@ -76,10 +78,12 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType)
self.row = NSNotFound; self.row = NSNotFound;
self.previousRow = NSNotFound; self.previousRow = NSNotFound;
[self ensureViewState:transaction];
return self; return self;
} }
- (void)replaceInteraction:(TSInteraction *)interaction - (void)replaceInteraction:(TSInteraction *)interaction transaction:(YapDatabaseReadTransaction *)transaction
{ {
OWSAssert(interaction); OWSAssert(interaction);
@ -93,6 +97,8 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType)
self.contentSize = CGSizeZero; self.contentSize = CGSizeZero;
[self clearCachedLayoutState]; [self clearCachedLayoutState];
[self ensureViewState:transaction];
} }
- (void)setShouldShowDate:(BOOL)shouldShowDate - (void)setShouldShowDate:(BOOL)shouldShowDate
@ -332,7 +338,10 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType)
} }
- (nullable TSAttachment *)firstAttachmentIfAnyOfMessage:(TSMessage *)message - (nullable TSAttachment *)firstAttachmentIfAnyOfMessage:(TSMessage *)message
transaction:(YapDatabaseReadTransaction *)transaction
{ {
OWSAssert(transaction);
if (message.attachmentIds.count == 0) { if (message.attachmentIds.count == 0) {
return nil; return nil;
} }
@ -340,21 +349,22 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType)
if (attachmentId.length == 0) { if (attachmentId.length == 0) {
return nil; return nil;
} }
return [TSAttachment fetchObjectWithUniqueID:attachmentId]; return [TSAttachment fetchObjectWithUniqueID:attachmentId transaction:transaction];
} }
- (void)ensureViewState - (void)ensureViewState:(YapDatabaseReadTransaction *)transaction
{ {
OWSAssert([NSThread isMainThread]);
OWSAssert(transaction);
OWSAssert(!self.hasViewState);
OWSAssert([self.interaction isKindOfClass:[TSOutgoingMessage class]] || OWSAssert([self.interaction isKindOfClass:[TSOutgoingMessage class]] ||
[self.interaction isKindOfClass:[TSIncomingMessage class]]); [self.interaction isKindOfClass:[TSIncomingMessage class]]);
if (self.hasViewState) {
return;
}
self.hasViewState = YES; self.hasViewState = YES;
TSMessage *message = (TSMessage *)self.interaction; TSMessage *message = (TSMessage *)self.interaction;
TSAttachment *_Nullable attachment = [self firstAttachmentIfAnyOfMessage:message]; TSAttachment *_Nullable attachment = [self firstAttachmentIfAnyOfMessage:message transaction:transaction];
if (attachment) { if (attachment) {
if ([attachment isKindOfClass:[TSAttachmentStream class]]) { if ([attachment isKindOfClass:[TSAttachmentStream class]]) {
self.attachmentStream = (TSAttachmentStream *)attachment; self.attachmentStream = (TSAttachmentStream *)attachment;
@ -418,16 +428,13 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType)
{ {
OWSAssert([NSThread isMainThread]); OWSAssert([NSThread isMainThread]);
[self ensureViewState];
return _messageCellType; return _messageCellType;
} }
- (nullable DisplayableText *)displayableText - (nullable DisplayableText *)displayableText
{ {
OWSAssert([NSThread isMainThread]); OWSAssert([NSThread isMainThread]);
OWSAssert(self.hasViewState);
[self ensureViewState];
OWSAssert(_displayableText); OWSAssert(_displayableText);
OWSAssert(_displayableText.displayText); OWSAssert(_displayableText.displayText);
@ -439,8 +446,7 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType)
- (nullable TSAttachmentStream *)attachmentStream - (nullable TSAttachmentStream *)attachmentStream
{ {
OWSAssert([NSThread isMainThread]); OWSAssert([NSThread isMainThread]);
OWSAssert(self.hasViewState);
[self ensureViewState];
return _attachmentStream; return _attachmentStream;
} }
@ -448,8 +454,7 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType)
- (nullable TSAttachmentPointer *)attachmentPointer - (nullable TSAttachmentPointer *)attachmentPointer
{ {
OWSAssert([NSThread isMainThread]); OWSAssert([NSThread isMainThread]);
OWSAssert(self.hasViewState);
[self ensureViewState];
return _attachmentPointer; return _attachmentPointer;
} }
@ -457,8 +462,7 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType)
- (CGSize)contentSize - (CGSize)contentSize
{ {
OWSAssert([NSThread isMainThread]); OWSAssert([NSThread isMainThread]);
OWSAssert(self.hasViewState);
[self ensureViewState];
return _contentSize; return _contentSize;
} }

@ -48,7 +48,10 @@
TSOutgoingMessage *message = TSOutgoingMessage *message =
[[TSOutgoingMessage alloc] initWithTimestamp:1 inThread:nil messageBody:self.fakeTextMessageText]; [[TSOutgoingMessage alloc] initWithTimestamp:1 inThread:nil messageBody:self.fakeTextMessageText];
[message save]; [message save];
ConversationViewItem *viewItem = [[ConversationViewItem alloc] initWithTSInteraction:message isGroupThread:NO]; __block ConversationViewItem *viewItem = nil;
[TSYapDatabaseObject.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
viewItem = [[ConversationViewItem alloc] initWithInteraction:message isGroupThread:NO transaction:transaction];
}];
return viewItem; return viewItem;
} }
@ -72,7 +75,7 @@
TSOutgoingMessage *message = TSOutgoingMessage *message =
[[TSOutgoingMessage alloc] initWithTimestamp:1 inThread:nil messageBody:nil attachmentIds:attachmentIds]; [[TSOutgoingMessage alloc] initWithTimestamp:1 inThread:nil messageBody:nil attachmentIds:attachmentIds];
[message save]; [message save];
ConversationViewItem *viewItem = [[ConversationViewItem alloc] initWithTSInteraction:message isGroupThread:NO]; ConversationViewItem *viewItem = [[ConversationViewItem alloc] initWithInteraction:message isGroupThread:NO];
return viewItem; return viewItem;
} }

Loading…
Cancel
Save