Recents: trigger back pagination when last event description is empty in order to display the actual last event.

This commit is contained in:
giomfo 2014-12-17 09:56:25 +01:00
parent 38156c0873
commit 68ef5bcf19
3 changed files with 83 additions and 24 deletions

View file

@ -18,11 +18,14 @@
@interface RecentRoom : NSObject
@property (nonatomic, readonly) NSString *roomId;
@property (nonatomic, readonly) MXEvent *lastEvent;
@property (nonatomic, readonly) NSString *lastEventDescription;
@property (nonatomic, readonly) uint64_t lastEventOriginServerTs;
@property (nonatomic, readonly) NSUInteger unreadCount;
- (id)initWithLastEvent:(MXEvent*)event andMarkAsUnread:(BOOL)isUnread;
- (void)updateWithLastEvent:(MXEvent*)event andMarkAsUnread:(BOOL)isUnread;
- (id)initWithLastEvent:(MXEvent*)event andRoomState:(MXRoomState*)roomState markAsUnread:(BOOL)isUnread;
// Update the current last event description with the provided event, except if this description is empty (see unsupported/unexpected events).
// Return true when the provided event is considered as new last event
- (BOOL)updateWithLastEvent:(MXEvent*)event andRoomState:(MXRoomState*)roomState markAsUnread:(BOOL)isUnread;
- (void)resetUnreadCount;
@end

View file

@ -15,22 +15,61 @@
*/
#import "RecentRoom.h"
#import "MatrixHandler.h"
@interface RecentRoom() {
MXRoom *mxRoom;
id backPaginationListener;
}
@end
@implementation RecentRoom
- (id)initWithLastEvent:(MXEvent*)event andMarkAsUnread:(BOOL)isUnread {
- (id)initWithLastEvent:(MXEvent*)event andRoomState:(MXRoomState*)roomState markAsUnread:(BOOL)isUnread {
if (self = [super init]) {
_lastEvent = event;
MatrixHandler *mxHandler = [MatrixHandler sharedHandler];
_roomId = event.roomId;
_lastEventDescription = [mxHandler displayTextForEvent:event withRoomState:roomState inSubtitleMode:YES];
_lastEventOriginServerTs = event.originServerTs;
_unreadCount = isUnread ? 1 : 0;
if (!_lastEventDescription.length) {
// Trigger back pagination to get an event with a non empty description
mxRoom = [mxHandler.mxSession roomWithRoomId:event.roomId];
if (mxRoom) {
backPaginationListener = [mxRoom listenToEventsOfTypes:mxHandler.eventsFilterForMessages onEvent:^(MXEvent *event, MXEventDirection direction, MXRoomState *roomState) {
// Handle only backward events (Sanity check: be sure that the description has not been set by an other way)
if (direction == MXEventDirectionBackwards && !_lastEventDescription.length) {
if (![self updateWithLastEvent:event andRoomState:roomState markAsUnread:NO]) {
// get back one more event
[self triggerBackPagination];
}
}
}];
// Trigger a back pagination by reseting first backState to get room history from live
[mxRoom resetBackState];
[self triggerBackPagination];
}
}
}
return self;
}
- (void)updateWithLastEvent:(MXEvent*)event andMarkAsUnread:(BOOL)isUnread {
_lastEvent = event;
if (isUnread) {
_unreadCount ++;
- (BOOL)updateWithLastEvent:(MXEvent*)event andRoomState:(MXRoomState*)roomState markAsUnread:(BOOL)isUnread {
// Check whether the description of the provided event is not empty
NSString *description = [[MatrixHandler sharedHandler] displayTextForEvent:event withRoomState:roomState inSubtitleMode:YES];
if (description.length) {
[self cancelBackPagination];
// Update current last event
_lastEventDescription = description;
_lastEventOriginServerTs = event.originServerTs;
if (isUnread) {
_unreadCount ++;
}
return YES;
}
return NO;
}
- (void)resetUnreadCount {
@ -38,13 +77,28 @@
}
- (void)dealloc {
_lastEvent = nil;
[self cancelBackPagination];
_lastEventDescription = nil;
}
#pragma mark -
- (void)triggerBackPagination {
if (mxRoom.canPaginate) {
[mxRoom paginateBackMessages:1 complete:^{
} failure:^(NSError *error) {
NSLog(@"RecentRoom: Failed to paginate back: %@", error);
[self cancelBackPagination];
}];
} else {
[self cancelBackPagination];
}
}
- (NSString*)roomId {
return _lastEvent.roomId;
- (void)cancelBackPagination {
if (backPaginationListener && mxRoom) {
[mxRoom removeListener:backPaginationListener];
backPaginationListener = nil;
mxRoom = nil;
}
}
@end

View file

@ -194,15 +194,16 @@
NSArray *recentEvents = [NSMutableArray arrayWithArray:[mxHandler.mxSession recentsWithTypeIn:mxHandler.eventsFilterForMessages]];
recents = [NSMutableArray arrayWithCapacity:recentEvents.count];
for (MXEvent *mxEvent in recentEvents) {
RecentRoom *recentRoom = [[RecentRoom alloc] initWithLastEvent:mxEvent andMarkAsUnread:NO];
MXRoom *mxRoom = [mxHandler.mxSession roomWithRoomId:mxEvent.roomId];
RecentRoom *recentRoom = [[RecentRoom alloc] initWithLastEvent:mxEvent andRoomState:mxRoom.state markAsUnread:NO];
if (recentRoom) {
[recents addObject:recentRoom];
}
}
// Register recent listener
recentsListener = [mxHandler.mxSession listenToEventsOfTypes:mxHandler.eventsFilterForMessages onEvent:^(MXEvent *event, MXEventDirection direction, id customObject) {
// Consider only live event
recentsListener = [mxHandler.mxSession listenToEventsOfTypes:mxHandler.eventsFilterForMessages onEvent:^(MXEvent *event, MXEventDirection direction, MXRoomState *roomState) {
// Consider first live event
if (direction == MXEventDirectionForwards) {
// Check user's membership in live room state (We will remove left rooms from recents)
MXRoom *mxRoom = [mxHandler.mxSession roomWithRoomId:event.roomId];
@ -222,17 +223,18 @@
// Remove left room
[recents removeObjectAtIndex:index];
} else {
[recentRoom updateWithLastEvent:event andMarkAsUnread:isUnread];
// Move this room at first position
[recents removeObjectAtIndex:index];
[recents insertObject:recentRoom atIndex:0];
if ([recentRoom updateWithLastEvent:event andRoomState:roomState markAsUnread:isUnread]) {
// Move this room at first position
[recents removeObjectAtIndex:index];
[recents insertObject:recentRoom atIndex:0];
}
}
break;
}
}
if (!isFound && !isLeft) {
// Insert in first position this new room
RecentRoom *recentRoom = [[RecentRoom alloc] initWithLastEvent:event andMarkAsUnread:isUnread];
RecentRoom *recentRoom = [[RecentRoom alloc] initWithLastEvent:event andRoomState:roomState markAsUnread:isUnread];
if (recentRoom) {
[recents insertObject:recentRoom atIndex:0];
}
@ -419,7 +421,7 @@
MXRoom *mxRoom = [mxHandler.mxSession roomWithRoomId:recentRoom.roomId];
cell.roomTitle.text = [mxRoom.state displayname];
cell.lastEventDescription.text = [mxHandler displayTextForEvent:recentRoom.lastEvent withRoomState:mxRoom.state inSubtitleMode:YES];
cell.lastEventDescription.text = recentRoom.lastEventDescription;
// Set in bold public room name
if (mxRoom.state.isPublic) {
@ -428,8 +430,8 @@
cell.roomTitle.font = [UIFont systemFontOfSize:19];
}
if (recentRoom.lastEvent.originServerTs != kMXUndefinedTimestamp) {
NSDate *date = [NSDate dateWithTimeIntervalSince1970:recentRoom.lastEvent.originServerTs/1000];
if (recentRoom.lastEventOriginServerTs != kMXUndefinedTimestamp) {
NSDate *date = [NSDate dateWithTimeIntervalSince1970:recentRoom.lastEventOriginServerTs/1000];
cell.recentDate.text = [dateFormatter stringFromDate:date];
} else {
cell.recentDate.text = nil;