element-ios/RiotShareExtension/Shared/ShareDataSource.m

199 lines
5.8 KiB
Mathematica
Raw Normal View History

2017-08-26 08:58:17 +00:00
/*
Copyright 2017 Aram Sargsyan
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 "ShareDataSource.h"
#import "RecentRoomTableViewCell.h"
2017-08-26 08:58:17 +00:00
@interface ShareDataSource ()
@property (nonatomic, strong, readonly) MXFileStore *fileStore;
@property (nonatomic, strong, readonly) MXCredentials *credentials;
2017-08-26 08:58:17 +00:00
@property NSArray <MXKRecentCellData *> *recentCellDatas;
@property NSMutableArray <MXKRecentCellData *> *visibleRoomCellDatas;
2017-08-26 08:58:17 +00:00
@property (nonatomic, strong) NSMutableSet<NSString *> *internalSelectedRoomIdentifiers;
2017-08-26 08:58:17 +00:00
@end
@implementation ShareDataSource
- (instancetype)initWithFileStore:(MXFileStore *)fileStore
credentials:(MXCredentials *)credentials
2017-08-26 08:58:17 +00:00
{
if (self = [super init])
2017-08-26 08:58:17 +00:00
{
_fileStore = fileStore;
_credentials = credentials;
_internalSelectedRoomIdentifiers = [NSMutableSet set];
[self loadCellData];
2017-08-26 08:58:17 +00:00
}
return self;
}
- (void)destroy
2017-08-26 08:58:17 +00:00
{
[super destroy];
2017-08-26 08:58:17 +00:00
_recentCellDatas = nil;
_visibleRoomCellDatas = nil;
}
- (NSSet<NSString *> *)selectedRoomIdentifiers
{
return self.internalSelectedRoomIdentifiers.copy;
}
- (void)selectRoomWithIdentifier:(NSString *)roomIdentifier animated:(BOOL)animated
{
[self.internalSelectedRoomIdentifiers addObject:roomIdentifier];
[self.shareDelegate shareDataSourceDidChangeSelectedRoomIdentifiers:self];
}
- (void)deselectRoomWithIdentifier:(NSString *)roomIdentifier animated:(BOOL)animated
{
[self.internalSelectedRoomIdentifiers removeObject:roomIdentifier];
[self.shareDelegate shareDataSourceDidChangeSelectedRoomIdentifiers:self];
}
#pragma mark - Private
- (void)loadCellData
{
[self.fileStore.summariesModule fetchAllSummaries:^(NSArray<id<MXRoomSummaryProtocol>> *summaries) {
NSMutableArray *cellData = [NSMutableArray array];
// Add a fake matrix session to each room summary to provide it a REST client (used to handle correctly the room avatar).
MXSession *session = [[MXSession alloc] initWithMatrixRestClient:[[MXRestClient alloc] initWithCredentials:self.credentials andOnUnrecognizedCertificateBlock:nil]];
for (id<MXRoomSummaryProtocol> summary in summaries)
2017-08-26 08:58:17 +00:00
{
if (!summary.hiddenFromUser && summary.roomType == MXRoomTypeRoom)
{
if ([summary respondsToSelector:@selector(setMatrixSession:)])
{
[summary setMatrixSession:session];
}
MXKRecentCellData *recentCellData = [[MXKRecentCellData alloc] initWithRoomSummary:summary dataSource:nil];
[cellData addObject:recentCellData];
}
}
// Sort rooms according to their last messages (most recent first)
NSComparator comparator = ^NSComparisonResult(MXKRecentCellData *recentCellData1, MXKRecentCellData *recentCellData2) {
2021-06-02 11:31:34 +00:00
return [recentCellData1.roomSummary.lastMessage compareOriginServerTs:recentCellData2.roomSummary.lastMessage];
};
[cellData sortUsingComparator:comparator];
self.recentCellDatas = cellData;
dispatch_async(dispatch_get_main_queue(), ^{
[self.delegate dataSource:self didCellChange:nil];
});
2017-08-26 08:58:17 +00:00
}];
}
#pragma mark - MXKRecentsDataSource
2017-08-26 08:58:17 +00:00
- (MXKRecentCellData *)cellDataAtIndexPath:(NSIndexPath *)indexPath
{
if (self.visibleRoomCellDatas)
2017-08-26 08:58:17 +00:00
{
return self.visibleRoomCellDatas[indexPath.row];
}
return self.recentCellDatas[indexPath.row];
}
- (void)searchWithPatterns:(NSArray *)patternsList
{
if (self.visibleRoomCellDatas)
{
[self.visibleRoomCellDatas removeAllObjects];
2017-08-26 08:58:17 +00:00
}
else
{
self.visibleRoomCellDatas = [NSMutableArray arrayWithCapacity:self.recentCellDatas.count];
2017-08-26 08:58:17 +00:00
}
if (patternsList.count)
{
for (MXKRecentCellData *cellData in self.recentCellDatas)
{
for (NSString* pattern in patternsList)
{
if (cellData.roomSummary.displayname && [cellData.roomSummary.displayname rangeOfString:pattern options:NSCaseInsensitiveSearch].location != NSNotFound)
{
[self.visibleRoomCellDatas addObject:cellData];
break;
}
}
}
}
else
{
self.visibleRoomCellDatas = nil;
}
[self.delegate dataSource:self didCellChange:nil];
2017-08-26 08:58:17 +00:00
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.visibleRoomCellDatas)
2017-08-26 08:58:17 +00:00
{
return self.visibleRoomCellDatas.count;
2017-08-26 08:58:17 +00:00
}
return self.recentCellDatas.count;
2017-08-26 08:58:17 +00:00
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
RecentRoomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[RecentRoomTableViewCell defaultReuseIdentifier]];
2017-08-26 08:58:17 +00:00
MXKRecentCellData *data = [self cellDataAtIndexPath:indexPath];
[cell render:data];
[cell setCustomSelected:[self.selectedRoomIdentifiers containsObject:data.roomSummary.roomId] animated:NO];
2017-08-26 08:58:17 +00:00
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
2017-08-26 08:58:17 +00:00
@end