->Display the recents sorted by tags (favorites, converstations, lower priority)

-> the tag event is listened but seems bugged.
This commit is contained in:
yannick
2015-12-02 17:18:55 +01:00
parent fb99a80d46
commit 66f523a243
4 changed files with 263 additions and 0 deletions
+244
View File
@@ -20,6 +20,21 @@
#import "EventFormatter.h"
@interface RecentsDataSource()
{
NSMutableArray* favoritesCells;
NSMutableArray* conversationCells;
NSMutableArray* lowPriorityCells;
NSInteger favoritesPos;
NSInteger conversationPos;
NSInteger lowPriorityPos;
NSInteger sectionsCount;
NSMutableDictionary<NSString*, id> *roomTagsListenerByUserId;
}
@end
@implementation RecentsDataSource
- (instancetype)init
@@ -32,10 +47,239 @@
// Replace event formatter
self.eventFormatter = [[EventFormatter alloc] initWithMatrixSession:self.mxSession];
favoritesCells = [[NSMutableArray alloc] init];
conversationCells = [[NSMutableArray alloc] init];
lowPriorityCells = [[NSMutableArray alloc] init];
favoritesPos = 0;
conversationPos = 1;
lowPriorityPos = 2;
sectionsCount = 3;
roomTagsListenerByUserId = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void)removeMatrixSession:(MXSession*)matrixSession
{
[super removeMatrixSession:matrixSession];
// sanity check
if (matrixSession && matrixSession.myUser && matrixSession.myUser.userId)
{
id roomTagListener = [roomTagsListenerByUserId objectForKey:matrixSession.myUser.userId];
if (roomTagListener)
{
[self.mxSession removeListener:roomTagListener];
[roomTagsListenerByUserId removeObjectForKey:matrixSession.myUser.userId];
}
}
}
- (void)dataSource:(MXKDataSource*)dataSource didStateChange:(MXKDataSourceState)aState
{
[super dataSource:dataSource didStateChange:aState];
if ((aState == MXKDataSourceStateReady) && self.mxSession && self.mxSession.myUser && self.mxSession.myUser.userId)
{
// Register the room tags updates to refresh the favorites order
id roomTagsListener = [self.mxSession listenToEventsOfTypes:@[kMXEventTypeStringRoomTag]
onEvent:^(MXEvent *event, MXEventDirection direction, id customObject) {
// Consider only live event
if (direction == MXEventDirectionForwards)
{
// And inform the delegate about the update
[self.delegate dataSource:self didCellChange:nil];
}
}];
[roomTagsListenerByUserId setObject:roomTagsListener forKey:self.mxSession.myUser.userId];
}
}
#pragma mark - UITableViewDataSource
/**
Return the header height from the section.
*/
- (CGFloat)heightForHeaderInSection:(NSInteger)section
{
if ((section == favoritesPos) || (section == conversationPos) || (section == lowPriorityPos))
{
return 44.0f;
}
return 0.0f;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Check whether all data sources are ready before rendering recents
if (self.state == MXKDataSourceStateReady)
{
// Only one section is handled by this data source.
return sectionsCount;
}
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSUInteger count = 0;
if (section == favoritesPos)
{
count = favoritesCells.count;
}
else if (section == conversationPos)
{
count = conversationCells.count;
}
else if (section == lowPriorityPos)
{
count = lowPriorityCells.count;
}
return count;
}
- (UIView *)viewForHeaderInSection:(NSInteger)section withFrame:(CGRect)frame
{
// add multi accounts section management
if ((section == favoritesPos) || (section == conversationPos) || (section == lowPriorityPos))
{
UILabel* label = [[UILabel alloc] initWithFrame:frame];
NSString* text = @"";
if (section == favoritesPos)
{
text = NSLocalizedStringFromTable(@"room_recents_favourites", @"Vector", nil);
}
else if (section == conversationPos)
{
text = NSLocalizedStringFromTable(@"room_recents_conversations", @"Vector", nil);
}
else if (section == lowPriorityPos)
{
text = NSLocalizedStringFromTable(@"room_recents_low_priority", @"Vector", nil);
}
label.text = [NSString stringWithFormat:@" %@", text];
label.backgroundColor = [UIColor lightGrayColor];
return label;
}
return [super viewForHeaderInSection:section withFrame:frame];
}
- (id<MXKRecentCellDataStoring>)cellDataAtIndexPath:(NSIndexPath *)indexPath
{
id<MXKRecentCellDataStoring> cellData = nil;
if (indexPath.section == favoritesPos)
{
cellData = [favoritesCells objectAtIndex:indexPath.row];
}
else if (indexPath.section == conversationPos)
{
cellData = [conversationCells objectAtIndex:indexPath.row];
}
else if (indexPath.section == lowPriorityPos)
{
cellData = [lowPriorityCells objectAtIndex:indexPath.row];
}
return cellData;
}
- (CGFloat)cellHeightAtIndexPath:(NSIndexPath *)indexPath
{
id<MXKRecentCellDataStoring> cellData = [self cellDataAtIndexPath:indexPath];
if (cellData)
{
Class<MXKCellRendering> class = [self cellViewClassForCellIdentifier:kMXKRecentCellIdentifier];
return [class heightForCellData:cellData withMaximumWidth:0];
}
return 0;
}
#pragma mark - MXKDataSourceDelegate
- (void)dataSource:(MXKDataSource*)dataSource didCellChange:(id)changes
{
// displayedRecentsDataSourceArray.count
// TODO manage multi accounts
favoritesCells = [[NSMutableArray alloc] init];
conversationCells = [[NSMutableArray alloc] init];
lowPriorityCells = [[NSMutableArray alloc] init];
favoritesPos = conversationPos = lowPriorityPos = -1;
sectionsCount = 0;
if (displayedRecentsDataSourceArray.count > 0)
{
MXKSessionRecentsDataSource *recentsDataSource = [displayedRecentsDataSourceArray objectAtIndex:0];
NSInteger count = recentsDataSource.numberOfCells;
for(int index = 0; index < count; index++)
{
id<MXKRecentCellDataStoring> recentCellDataStoring = [recentsDataSource cellDataAtIndex:index];
MXRoom* room = recentCellDataStoring.roomDataSource.room;
NSDictionary* tags = room.accountData.tags;
if (tags && [tags objectForKey:kMXRoomTagFavourite])
{
[favoritesCells addObject:recentCellDataStoring];
}
else if (tags && [tags objectForKey:kMXRoomTagLowPriority])
{
[lowPriorityCells addObject:recentCellDataStoring];
}
else
{
[conversationCells addObject:recentCellDataStoring];
}
}
int pos = 0;
if (favoritesCells.count > 0)
{
favoritesPos = pos;
pos++;
}
if (conversationCells.count > 0)
{
conversationPos = pos;
pos++;
}
if (lowPriorityCells.count > 0)
{
lowPriorityPos = pos;
pos++;
}
sectionsCount = pos;
}
// Call super to keep update readyRecentsDataSourceArray.
[super dataSource:dataSource didCellChange:changes];
}
#pragma mark - Override MXKDataSource
- (void)destroy