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"
|
2017-08-26 11:54:25 +00:00
|
|
|
#import "ShareExtensionManager.h"
|
2017-09-15 15:17:03 +00:00
|
|
|
#import "RecentRoomTableViewCell.h"
|
|
|
|
//#import "MXRoom+Riot.h"
|
2017-08-26 08:58:17 +00:00
|
|
|
|
|
|
|
@interface ShareDataSource ()
|
|
|
|
|
|
|
|
@property (nonatomic, readwrite) ShareDataSourceMode dataSourceMode;
|
|
|
|
|
2017-09-15 15:17:03 +00:00
|
|
|
@property NSArray <MXKRecentCellData *> *recentCellDatas;
|
|
|
|
@property NSMutableArray <MXKRecentCellData *> *visibleRoomCellDatas;
|
2017-08-26 08:58:17 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation ShareDataSource
|
|
|
|
|
|
|
|
- (instancetype)initWithMode:(ShareDataSourceMode)dataSourceMode
|
|
|
|
{
|
2017-08-31 13:51:49 +00:00
|
|
|
self = [super init];
|
2017-08-26 08:58:17 +00:00
|
|
|
if (self)
|
|
|
|
{
|
|
|
|
self.dataSourceMode = dataSourceMode;
|
2017-09-15 15:17:03 +00:00
|
|
|
_recentCellDatas = [NSMutableArray array];
|
2017-08-26 08:58:17 +00:00
|
|
|
[self updateRooms];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark - Private
|
|
|
|
|
|
|
|
- (void)updateRooms
|
|
|
|
{
|
2017-09-08 15:02:45 +00:00
|
|
|
MXFileStore *fileStore = [[MXFileStore alloc] initWithCredentials:[ShareExtensionManager sharedManager].userAccount.mxCredentials];
|
|
|
|
MXSession *session = [[MXSession alloc] initWithMatrixRestClient:[[MXRestClient alloc] initWithCredentials:[ShareExtensionManager sharedManager].userAccount.mxCredentials andOnUnrecognizedCertificateBlock:nil]];
|
2017-08-26 08:58:17 +00:00
|
|
|
|
2017-08-31 13:51:49 +00:00
|
|
|
__weak MXSession *weakSession = session;
|
|
|
|
[session setStore:fileStore success:^{
|
|
|
|
if (weakSession)
|
|
|
|
{
|
2017-09-15 15:17:03 +00:00
|
|
|
//__strong MXSession *session = weakSession;
|
|
|
|
[self getCellDatasFromStore:fileStore];
|
2017-08-31 13:51:49 +00:00
|
|
|
}
|
|
|
|
} failure:^(NSError *error) {
|
2017-09-15 15:17:03 +00:00
|
|
|
NSLog(@"[ShareDataSource failed to set store]");
|
2017-08-31 13:51:49 +00:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2017-09-15 15:17:03 +00:00
|
|
|
- (void)getCellDatasFromStore:(MXFileStore *)fileStore
|
2017-08-31 13:51:49 +00:00
|
|
|
{
|
2017-09-15 15:17:03 +00:00
|
|
|
NSMutableArray *cellDatas = [NSMutableArray array];
|
2017-08-26 08:58:17 +00:00
|
|
|
[fileStore asyncRoomsSummaries:^(NSArray<MXRoomSummary *> * _Nonnull roomsSummaries) {
|
|
|
|
for (MXRoomSummary *roomSummary in roomsSummaries)
|
|
|
|
{
|
2017-09-15 15:17:03 +00:00
|
|
|
MXKRecentCellData *recentCellData = [[MXKRecentCellData alloc] initWithRoomSummary:roomSummary andRecentListDataSource:nil];
|
|
|
|
|
|
|
|
|
|
|
|
if ((self.dataSourceMode == DataSourceModeRooms) ^ roomSummary.isDirect)
|
|
|
|
{
|
|
|
|
[cellDatas addObject:recentCellData];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([roomsSummaries indexOfObject:roomSummary] == roomsSummaries.count - 1)
|
|
|
|
{
|
|
|
|
self.recentCellDatas = cellDatas;
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
[self.delegate dataSource:self didCellChange:nil];
|
|
|
|
});
|
|
|
|
}
|
2017-08-31 13:51:49 +00:00
|
|
|
|
2017-08-26 08:58:17 +00:00
|
|
|
}
|
|
|
|
} failure:^(NSError * _Nonnull error) {
|
2017-09-15 15:17:03 +00:00
|
|
|
NSLog(@"[ShareDataSource failed to get room summaries]");
|
2017-08-26 08:58:17 +00:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2017-08-31 13:51:49 +00:00
|
|
|
#pragma mark - MXKRecentsDataSource
|
2017-08-26 08:58:17 +00:00
|
|
|
|
2017-08-31 13:51:49 +00:00
|
|
|
- (void)dataSource:(MXKDataSource *)dataSource didCellChange:(id)changes
|
2017-08-26 08:58:17 +00:00
|
|
|
{
|
2017-08-31 13:51:49 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
[self.delegate dataSource:dataSource didCellChange:nil];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
- (MXRoom *)getRoomAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
2017-09-15 15:17:03 +00:00
|
|
|
NSLog(@"vori");
|
|
|
|
/*if (self.visibleRoomCellDatas)
|
|
|
|
{
|
|
|
|
return self.visibleRoomCellDatas[indexPath.row];
|
|
|
|
}
|
|
|
|
return self.visibleRoomCellDatas[indexPath.row];*/
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id<MXKRecentCellDataStoring>)cellDataAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
|
if (self.visibleRoomCellDatas)
|
2017-08-26 08:58:17 +00:00
|
|
|
{
|
2017-09-15 15:17:03 +00:00
|
|
|
return self.visibleRoomCellDatas[indexPath.row];
|
2017-08-31 13:51:49 +00:00
|
|
|
}
|
2017-09-15 15:17:03 +00:00
|
|
|
return self.recentCellDatas[indexPath.row];
|
2017-08-31 13:51:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)searchWithPatterns:(NSArray *)patternsList
|
|
|
|
{
|
2017-09-15 15:17:03 +00:00
|
|
|
if (self.visibleRoomCellDatas)
|
2017-08-31 13:51:49 +00:00
|
|
|
{
|
2017-09-15 15:17:03 +00:00
|
|
|
[self.visibleRoomCellDatas removeAllObjects];
|
2017-08-26 08:58:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-09-15 15:17:03 +00:00
|
|
|
self.visibleRoomCellDatas = [NSMutableArray arrayWithCapacity:self.recentCellDatas.count];
|
2017-08-26 08:58:17 +00:00
|
|
|
}
|
2017-08-31 13:51:49 +00:00
|
|
|
if (patternsList.count)
|
|
|
|
{
|
2017-09-15 15:17:03 +00:00
|
|
|
for (MXKRecentCellData *cellData in self.recentCellDatas)
|
2017-08-31 13:51:49 +00:00
|
|
|
{
|
|
|
|
for (NSString* pattern in patternsList)
|
|
|
|
{
|
2017-09-15 15:17:03 +00:00
|
|
|
if (cellData.roomSummary.displayname && [cellData.roomSummary.displayname rangeOfString:pattern options:NSCaseInsensitiveSearch].location != NSNotFound)
|
2017-08-31 13:51:49 +00:00
|
|
|
{
|
2017-09-15 15:17:03 +00:00
|
|
|
[self.visibleRoomCellDatas addObject:cellData];
|
2017-08-31 13:51:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-09-15 15:17:03 +00:00
|
|
|
self.visibleRoomCellDatas = nil;
|
2017-08-31 13:51:49 +00:00
|
|
|
}
|
|
|
|
[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
|
|
|
|
{
|
2017-09-15 15:17:03 +00:00
|
|
|
if (self.visibleRoomCellDatas)
|
2017-08-26 08:58:17 +00:00
|
|
|
{
|
2017-09-15 15:17:03 +00:00
|
|
|
return self.visibleRoomCellDatas.count;
|
2017-08-26 08:58:17 +00:00
|
|
|
}
|
2017-09-15 15:17:03 +00:00
|
|
|
return self.recentCellDatas.count;
|
2017-08-26 08:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
2017-09-15 15:17:03 +00:00
|
|
|
RecentRoomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[RecentRoomTableViewCell defaultReuseIdentifier]];
|
2017-08-26 08:58:17 +00:00
|
|
|
|
2017-09-15 15:17:03 +00:00
|
|
|
[cell render:[self cellDataAtIndexPath:indexPath]];
|
2017-08-26 08:58:17 +00:00
|
|
|
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@end
|