element-ios/RiotShareExtension/Model/ShareDataSource.m
2017-08-26 12:58:17 +04:00

139 lines
3.7 KiB
Objective-C

/*
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 "RoomTableViewCell.h"
@interface ShareDataSource ()
@property (nonatomic, readwrite) ShareDataSourceMode dataSourceMode;
@property NSMutableArray *recentRooms;
@property NSMutableArray *recentPeople;
@end
@implementation ShareDataSource
- (instancetype)initWithMode:(ShareDataSourceMode)dataSourceMode
{
if (self)
{
self.dataSourceMode = dataSourceMode;
_recentRooms = [NSMutableArray array];
_recentPeople = [NSMutableArray array];
[self updateRooms];
}
return self;
}
#pragma mark - Private
- (void)updateRooms
{
[self.recentRooms removeAllObjects];
[self.recentPeople removeAllObjects];
// Force account manager to reload account from the local storage.
[[MXKAccountManager sharedManager] forceReloadAccounts];
// Get the first active account
MXKAccount *account = [MXKAccountManager sharedManager].activeAccounts.firstObject;
MXFileStore *fileStore = [[MXFileStore alloc] initWithCredentials:account.mxCredentials];
//NSMutableArray *rooms = [NSMutableArray array];
//NSMutableArray *people = [NSMutableArray array];
[fileStore asyncRoomsSummaries:^(NSArray<MXRoomSummary *> * _Nonnull roomsSummaries) {
for (MXRoomSummary *roomSummary in roomsSummaries)
{
if (self.dataSourceMode == DataSourceModePeople)
{
if (roomSummary.isDirect)
{
[self.recentPeople addObject:roomSummary];
}
}
else
{
if (!roomSummary.isDirect)
{
[self.recentRooms addObject:roomSummary];
}
}
[self.delegate dataSource:self didCellChange:nil];
}
} failure:^(NSError * _Nonnull error) {
NSLog(@"[ShareExtensionManager failed to get room summaries]");
}];
}
- (MXRoomSummary *)getRoomSummaryAtIndexPath:(NSIndexPath *)indexPath
{
MXRoomSummary *room = nil;
if (self.dataSourceMode == DataSourceModePeople)
{
room = self.recentPeople[indexPath.row];
}
else
{
room = self.recentRooms[indexPath.row];
}
return room;
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.dataSourceMode == DataSourceModePeople)
{
return self.recentPeople.count;
}
else
{
return self.recentRooms.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
RoomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[RoomTableViewCell defaultReuseIdentifier]];
MXRoomSummary *roomSummary = [self getRoomSummaryAtIndexPath:indexPath];
[cell renderWithSummary:roomSummary];
if (!roomSummary.displayname.length && !cell.titleLabel.text.length)
{
cell.titleLabel.text = NSLocalizedStringFromTable(@"room_displayname_no_title", @"Vector", nil);
}
return cell;
}
@end