2015-11-20 17:14:15 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 OpenMarket Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RecentsDataSource.h"
|
|
|
|
|
|
|
|
#import "EventFormatter.h"
|
|
|
|
|
2015-12-04 16:45:56 +00:00
|
|
|
#import "VectorDesignValues.h"
|
|
|
|
|
2015-12-02 16:18:55 +00:00
|
|
|
@interface RecentsDataSource()
|
|
|
|
{
|
2015-12-04 08:02:02 +00:00
|
|
|
NSMutableArray* favoriteCellDataArray;
|
|
|
|
NSMutableArray* conversationCellDataArray;
|
|
|
|
NSMutableArray* lowPriorityCellDataArray;
|
2015-12-02 16:18:55 +00:00
|
|
|
|
2015-12-04 08:02:02 +00:00
|
|
|
NSInteger favoritesSection;
|
|
|
|
NSInteger conversationSection;
|
|
|
|
NSInteger lowPrioritySection;
|
2015-12-02 16:18:55 +00:00
|
|
|
NSInteger sectionsCount;
|
|
|
|
|
|
|
|
NSMutableDictionary<NSString*, id> *roomTagsListenerByUserId;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2015-11-20 17:14:15 +00:00
|
|
|
@implementation RecentsDataSource
|
|
|
|
|
|
|
|
- (instancetype)init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self)
|
|
|
|
{
|
|
|
|
// Replace event formatter
|
|
|
|
self.eventFormatter = [[EventFormatter alloc] initWithMatrixSession:self.mxSession];
|
2015-12-02 16:18:55 +00:00
|
|
|
|
2015-12-04 08:02:02 +00:00
|
|
|
favoriteCellDataArray = [[NSMutableArray alloc] init];
|
|
|
|
conversationCellDataArray = [[NSMutableArray alloc] init];
|
|
|
|
lowPriorityCellDataArray = [[NSMutableArray alloc] init];
|
2015-12-02 16:18:55 +00:00
|
|
|
|
2015-12-04 08:02:02 +00:00
|
|
|
favoritesSection = -1;
|
|
|
|
conversationSection = -1;
|
|
|
|
lowPrioritySection = -1;
|
|
|
|
sectionsCount = 0;
|
2015-12-02 16:18:55 +00:00
|
|
|
|
|
|
|
roomTagsListenerByUserId = [[NSMutableDictionary alloc] init];
|
2015-11-20 17:14:15 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-12-02 16:18:55 +00:00
|
|
|
- (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)
|
|
|
|
{
|
2015-12-02 16:38:10 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
|
|
|
|
[self refreshRoomsSections];
|
|
|
|
|
|
|
|
// And inform the delegate about the update
|
|
|
|
[self.delegate dataSource:self didCellChange:nil];
|
|
|
|
});
|
2015-12-02 16:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
[roomTagsListenerByUserId setObject:roomTagsListener forKey:self.mxSession.myUser.userId];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UITableViewDataSource
|
|
|
|
|
|
|
|
/**
|
|
|
|
Return the header height from the section.
|
|
|
|
*/
|
|
|
|
- (CGFloat)heightForHeaderInSection:(NSInteger)section
|
|
|
|
{
|
2015-12-04 08:02:02 +00:00
|
|
|
if ((section == favoritesSection) || (section == conversationSection) || (section == lowPrioritySection))
|
2015-12-02 16:18:55 +00:00
|
|
|
{
|
2015-12-04 16:45:56 +00:00
|
|
|
return 30.0f;
|
2015-12-02 16:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2015-12-04 08:02:02 +00:00
|
|
|
if (section == favoritesSection)
|
2015-12-02 16:18:55 +00:00
|
|
|
{
|
2015-12-04 08:02:02 +00:00
|
|
|
count = favoriteCellDataArray.count;
|
2015-12-02 16:18:55 +00:00
|
|
|
}
|
2015-12-04 08:02:02 +00:00
|
|
|
else if (section == conversationSection)
|
2015-12-02 16:18:55 +00:00
|
|
|
{
|
2015-12-04 08:02:02 +00:00
|
|
|
count = conversationCellDataArray.count;
|
2015-12-02 16:18:55 +00:00
|
|
|
}
|
2015-12-04 08:02:02 +00:00
|
|
|
else if (section == lowPrioritySection)
|
2015-12-02 16:18:55 +00:00
|
|
|
{
|
2015-12-04 08:02:02 +00:00
|
|
|
count = lowPriorityCellDataArray.count;
|
2015-12-02 16:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UIView *)viewForHeaderInSection:(NSInteger)section withFrame:(CGRect)frame
|
|
|
|
{
|
|
|
|
// add multi accounts section management
|
|
|
|
|
2015-12-04 08:02:02 +00:00
|
|
|
if ((section == favoritesSection) || (section == conversationSection) || (section == lowPrioritySection))
|
2015-12-02 16:18:55 +00:00
|
|
|
{
|
|
|
|
UILabel* label = [[UILabel alloc] initWithFrame:frame];
|
|
|
|
|
|
|
|
NSString* text = @"";
|
|
|
|
|
2015-12-04 08:02:02 +00:00
|
|
|
if (section == favoritesSection)
|
2015-12-02 16:18:55 +00:00
|
|
|
{
|
|
|
|
text = NSLocalizedStringFromTable(@"room_recents_favourites", @"Vector", nil);
|
|
|
|
}
|
2015-12-04 08:02:02 +00:00
|
|
|
else if (section == conversationSection)
|
2015-12-02 16:18:55 +00:00
|
|
|
{
|
|
|
|
text = NSLocalizedStringFromTable(@"room_recents_conversations", @"Vector", nil);
|
|
|
|
}
|
2015-12-04 08:02:02 +00:00
|
|
|
else if (section == lowPrioritySection)
|
2015-12-02 16:18:55 +00:00
|
|
|
{
|
|
|
|
text = NSLocalizedStringFromTable(@"room_recents_low_priority", @"Vector", nil);
|
|
|
|
}
|
|
|
|
|
|
|
|
label.text = [NSString stringWithFormat:@" %@", text];
|
2015-12-04 16:45:56 +00:00
|
|
|
label.font = [UIFont boldSystemFontOfSize:15.0];
|
|
|
|
label.backgroundColor = VECTOR_LIGHT_GRAY_COLOR;
|
2015-12-02 16:18:55 +00:00
|
|
|
|
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [super viewForHeaderInSection:section withFrame:frame];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id<MXKRecentCellDataStoring>)cellDataAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
|
|
|
id<MXKRecentCellDataStoring> cellData = nil;
|
|
|
|
|
2015-12-04 08:02:02 +00:00
|
|
|
if (indexPath.section == favoritesSection)
|
2015-12-02 16:18:55 +00:00
|
|
|
{
|
2015-12-04 08:02:02 +00:00
|
|
|
cellData = [favoriteCellDataArray objectAtIndex:indexPath.row];
|
2015-12-02 16:18:55 +00:00
|
|
|
}
|
2015-12-04 08:02:02 +00:00
|
|
|
else if (indexPath.section == conversationSection)
|
2015-12-02 16:18:55 +00:00
|
|
|
{
|
2015-12-04 08:02:02 +00:00
|
|
|
cellData = [conversationCellDataArray objectAtIndex:indexPath.row];
|
2015-12-02 16:18:55 +00:00
|
|
|
}
|
2015-12-04 08:02:02 +00:00
|
|
|
else if (indexPath.section == lowPrioritySection)
|
2015-12-02 16:18:55 +00:00
|
|
|
{
|
2015-12-04 08:02:02 +00:00
|
|
|
cellData = [lowPriorityCellDataArray objectAtIndex:indexPath.row];
|
2015-12-02 16:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cellData;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CGFloat)cellHeightAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
2015-12-07 10:50:13 +00:00
|
|
|
// Override this method here to use our own cellDataAtIndexPath
|
2015-12-02 16:18:55 +00:00
|
|
|
id<MXKRecentCellDataStoring> cellData = [self cellDataAtIndexPath:indexPath];
|
|
|
|
|
2015-12-07 10:50:13 +00:00
|
|
|
if (cellData && self.delegate)
|
2015-12-02 16:18:55 +00:00
|
|
|
{
|
2015-12-07 10:50:13 +00:00
|
|
|
Class<MXKCellRendering> class = [self.delegate cellViewClassForCellData:cellData];
|
2015-12-02 16:18:55 +00:00
|
|
|
return [class heightForCellData:cellData withMaximumWidth:0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - MXKDataSourceDelegate
|
|
|
|
|
2015-12-02 17:10:22 +00:00
|
|
|
// create an array filled with NSNull and with the same size as sourceArray
|
2015-12-04 08:02:02 +00:00
|
|
|
- (NSMutableArray*)createEmptyArray:(NSUInteger)count
|
2015-12-02 17:10:22 +00:00
|
|
|
{
|
|
|
|
NSMutableArray* array = [[NSMutableArray alloc] init];
|
|
|
|
|
2015-12-04 08:02:02 +00:00
|
|
|
for(NSUInteger i = 0; i < count; i++)
|
2015-12-02 17:10:22 +00:00
|
|
|
{
|
2015-12-04 08:02:02 +00:00
|
|
|
[array addObject:[NSNull null]];
|
2015-12-02 17:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2015-12-02 16:38:10 +00:00
|
|
|
- (void)refreshRoomsSections
|
2015-12-02 16:18:55 +00:00
|
|
|
{
|
2015-12-04 08:02:02 +00:00
|
|
|
// FIXME manage multi accounts
|
2015-12-03 08:37:45 +00:00
|
|
|
|
2015-12-04 08:02:02 +00:00
|
|
|
favoriteCellDataArray = [[NSMutableArray alloc] init];
|
|
|
|
conversationCellDataArray = [[NSMutableArray alloc] init];
|
|
|
|
lowPriorityCellDataArray = [[NSMutableArray alloc] init];
|
2015-12-03 08:37:45 +00:00
|
|
|
|
2015-12-04 08:02:02 +00:00
|
|
|
favoritesSection = conversationSection = lowPrioritySection = -1;
|
2015-12-02 16:18:55 +00:00
|
|
|
sectionsCount = 0;
|
2015-12-02 17:10:22 +00:00
|
|
|
|
2015-12-02 16:18:55 +00:00
|
|
|
if (displayedRecentsDataSourceArray.count > 0)
|
|
|
|
{
|
|
|
|
MXKSessionRecentsDataSource *recentsDataSource = [displayedRecentsDataSourceArray objectAtIndex:0];
|
2015-12-02 17:10:22 +00:00
|
|
|
MXSession* session = recentsDataSource.mxSession;
|
|
|
|
|
|
|
|
NSArray* sortedFavRooms = [session roomsWithTag:kMXRoomTagFavourite];
|
|
|
|
NSArray* sortedLowPriorRooms = [session roomsWithTag:kMXRoomTagLowPriority];
|
|
|
|
|
2015-12-04 08:02:02 +00:00
|
|
|
favoriteCellDataArray = [self createEmptyArray:sortedFavRooms.count];
|
|
|
|
lowPriorityCellDataArray = [self createEmptyArray:sortedLowPriorRooms.count];
|
2015-12-02 17:10:22 +00:00
|
|
|
|
2015-12-02 16:18:55 +00:00
|
|
|
NSInteger count = recentsDataSource.numberOfCells;
|
|
|
|
|
|
|
|
for(int index = 0; index < count; index++)
|
|
|
|
{
|
2015-12-02 17:10:22 +00:00
|
|
|
NSUInteger pos;
|
2015-12-02 16:18:55 +00:00
|
|
|
id<MXKRecentCellDataStoring> recentCellDataStoring = [recentsDataSource cellDataAtIndex:index];
|
|
|
|
MXRoom* room = recentCellDataStoring.roomDataSource.room;
|
2015-12-02 17:10:22 +00:00
|
|
|
|
|
|
|
if ((pos = [sortedFavRooms indexOfObject:room]) != NSNotFound)
|
2015-12-02 16:18:55 +00:00
|
|
|
{
|
2015-12-04 08:02:02 +00:00
|
|
|
if (pos < favoriteCellDataArray.count)
|
2015-12-02 17:10:22 +00:00
|
|
|
{
|
2015-12-04 08:02:02 +00:00
|
|
|
[favoriteCellDataArray replaceObjectAtIndex:pos withObject:recentCellDataStoring];
|
2015-12-02 17:10:22 +00:00
|
|
|
}
|
2015-12-02 16:18:55 +00:00
|
|
|
}
|
2015-12-02 17:10:22 +00:00
|
|
|
else if ((pos = [sortedLowPriorRooms indexOfObject:room]) != NSNotFound)
|
2015-12-02 16:18:55 +00:00
|
|
|
{
|
2015-12-04 08:02:02 +00:00
|
|
|
if (pos < lowPriorityCellDataArray.count)
|
2015-12-02 17:10:22 +00:00
|
|
|
{
|
2015-12-04 08:02:02 +00:00
|
|
|
[lowPriorityCellDataArray replaceObjectAtIndex:pos withObject:recentCellDataStoring];
|
2015-12-02 17:10:22 +00:00
|
|
|
}
|
2015-12-02 16:18:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-04 08:02:02 +00:00
|
|
|
[conversationCellDataArray addObject:recentCellDataStoring];
|
2015-12-02 16:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-04 08:02:02 +00:00
|
|
|
int sectionIndex = 0;
|
2015-12-02 16:18:55 +00:00
|
|
|
|
2015-12-04 08:02:02 +00:00
|
|
|
[favoriteCellDataArray removeObject:[NSNull null]];
|
|
|
|
if (favoriteCellDataArray.count > 0)
|
2015-12-02 16:18:55 +00:00
|
|
|
{
|
2015-12-04 08:02:02 +00:00
|
|
|
favoritesSection = sectionIndex;
|
|
|
|
sectionIndex++;
|
2015-12-02 16:18:55 +00:00
|
|
|
}
|
|
|
|
|
2015-12-04 08:02:02 +00:00
|
|
|
[conversationCellDataArray removeObject:[NSNull null]];
|
|
|
|
if (conversationCellDataArray.count > 0)
|
2015-12-02 16:18:55 +00:00
|
|
|
{
|
2015-12-04 08:02:02 +00:00
|
|
|
conversationSection = sectionIndex;
|
|
|
|
sectionIndex++;
|
2015-12-02 16:18:55 +00:00
|
|
|
}
|
|
|
|
|
2015-12-04 08:02:02 +00:00
|
|
|
[lowPriorityCellDataArray removeObject:[NSNull null]];
|
|
|
|
if (lowPriorityCellDataArray.count > 0)
|
2015-12-02 16:18:55 +00:00
|
|
|
{
|
2015-12-04 08:02:02 +00:00
|
|
|
lowPrioritySection = sectionIndex;
|
|
|
|
sectionIndex++;
|
2015-12-02 16:18:55 +00:00
|
|
|
}
|
|
|
|
|
2015-12-04 08:02:02 +00:00
|
|
|
sectionsCount = sectionIndex;
|
2015-12-02 16:18:55 +00:00
|
|
|
}
|
2015-12-02 16:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dataSource:(MXKDataSource*)dataSource didCellChange:(id)changes
|
|
|
|
{
|
2015-12-04 08:02:02 +00:00
|
|
|
// FIXME : manage multi accounts
|
|
|
|
// to manage multi accounts
|
|
|
|
// this method in MXKInterleavedRecentsDataSource must be split in two parts
|
|
|
|
// 1 - the intervealing cells method
|
|
|
|
// 2 - [super dataSource:dataSource didCellChange:changes] call.
|
|
|
|
// the [self refreshRoomsSections] call should be done at the end of the 1- method
|
|
|
|
// so a dedicated method must be implemented in MXKInterleavedRecentsDataSource
|
|
|
|
// this class will inherit of this new method
|
|
|
|
// 1 - call [super thisNewMethod]
|
|
|
|
// 2 - call [self refreshRoomsSections]
|
|
|
|
|
2015-12-03 08:06:40 +00:00
|
|
|
// refresh the
|
|
|
|
[self refreshRoomsSections];
|
|
|
|
|
2015-12-02 16:18:55 +00:00
|
|
|
// Call super to keep update readyRecentsDataSourceArray.
|
|
|
|
[super dataSource:dataSource didCellChange:changes];
|
|
|
|
}
|
|
|
|
|
2015-11-20 17:14:15 +00:00
|
|
|
#pragma mark - Override MXKDataSource
|
|
|
|
|
|
|
|
- (void)destroy
|
|
|
|
{
|
|
|
|
[super destroy];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|