Merge MatrixKit develop with commit hash: b85b736313bec0592bd1cabc68035d97f5331137

This commit is contained in:
SBiOSoftWhare
2021-12-03 11:47:24 +01:00
parent 475fe5552f
commit 1d9f01b5e3
475 changed files with 87437 additions and 0 deletions
@@ -0,0 +1,27 @@
/*
Copyright 2017 Vector Creations 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 <Foundation/Foundation.h>
#import <MatrixSDK/MatrixSDK.h>
#import "MXKDirectoryServerCellDataStoring.h"
/**
`MXKRoomMemberCellData` modelised the data for a `MXKRoomMemberTableViewCell` cell.
*/
@interface MXKDirectoryServerCellData : MXKCellData <MXKDirectoryServerCellDataStoring>
@end
@@ -0,0 +1,66 @@
/*
Copyright 2017 Vector Creations Ltd
Copyright 2018 New Vector 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 "MXKDirectoryServerCellData.h"
#import "NSBundle+MatrixKit.h"
#import "MXKSwiftHeader.h"
@implementation MXKDirectoryServerCellData;
@synthesize desc, icon;
@synthesize homeserver, includeAllNetworks;
@synthesize thirdPartyProtocolInstance, thirdPartyProtocol;
@synthesize mediaManager;
- (id)initWithHomeserver:(NSString *)theHomeserver includeAllNetworks:(BOOL)theIncludeAllNetworks
{
self = [super init];
if (self)
{
homeserver = theHomeserver;
includeAllNetworks = theIncludeAllNetworks;
if (theIncludeAllNetworks)
{
desc = homeserver;
icon = nil;
}
else
{
// Use the Matrix name and logo when looking for Matrix rooms only
desc = [MatrixKitL10n matrix];
icon = [NSBundle mxk_imageFromMXKAssetsBundleWithName:@"network_matrix"];
}
}
return self;
}
- (id)initWithProtocolInstance:(MXThirdPartyProtocolInstance *)instance protocol:(MXThirdPartyProtocol *)protocol
{
self = [super init];
if (self)
{
thirdPartyProtocolInstance = instance;
thirdPartyProtocol = protocol;
desc = thirdPartyProtocolInstance.desc;
icon = nil;
}
return self;
}
@end
@@ -0,0 +1,75 @@
/*
Copyright 2017 Vector Creations Ltd
Copyright 2018 New Vector 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 <Foundation/Foundation.h>
#import <MatrixSDK/MatrixSDK.h>
#import "MXKCellData.h"
/**
`MXKDirectoryServerCellDataStoring` defines a protocol a class must conform in order to
store directory cell data managed by `MXKDirectoryServersDataSource`.
*/
@protocol MXKDirectoryServerCellDataStoring <NSObject>
#pragma mark - Data displayed by a server cell
/**
The name of the directory server.
*/
@property (nonatomic) NSString *desc;
/**
The icon of the server.
*/
@property (nonatomic) UIImage *icon;
/**
The optional media manager used to download the icon of the server.
*/
@property (nonatomic) MXMediaManager *mediaManager;
/**
In case the cell data represents a homeserver, its description.
*/
@property (nonatomic, readonly) NSString *homeserver;
@property (nonatomic, readonly) BOOL includeAllNetworks;
/**
In case the cell data represents a third-party protocol instance, its description.
*/
@property (nonatomic, readonly) MXThirdPartyProtocolInstance *thirdPartyProtocolInstance;
@property (nonatomic, readonly) MXThirdPartyProtocol *thirdPartyProtocol;
/**
Define a MXKDirectoryServerCellData that will store a homeserver.
@param homeserver the homeserver name (ex: "matrix.org).
@param includeAllNetworks YES to list all public rooms on the homeserver whatever their protocol.
NO to list only matrix rooms.
*/
- (id)initWithHomeserver:(NSString*)homeserver includeAllNetworks:(BOOL)includeAllNetworks;
/**
Define a MXKDirectoryServerCellData that will store a third-party protocol instance.
@param instance the instance of the protocol.
@param protocol the protocol description.
*/
- (id)initWithProtocolInstance:(MXThirdPartyProtocolInstance*)instance protocol:(MXThirdPartyProtocol*)protocol;
@end
@@ -0,0 +1,82 @@
/*
Copyright 2017 Vector Creations 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 <Foundation/Foundation.h>
#import "MXKDataSource.h"
#import "MXKDirectoryServerCellDataStoring.h"
/**
Identifier to use for cells that display a server in the servers list.
*/
FOUNDATION_EXPORT NSString *const kMXKDirectorServerCellIdentifier;
/**
`DirectoryServersDataSource` is a base class to list servers and third-party protocols
instances available on the user homeserver.
We can then list public rooms from the directory of these servers. This is done
with `PublicRoomsDirectoryDataSource`.
As a `MXKDataSource` child class, the class has a state where values have the following meanings:
- MXKDataSourceStatePreparing: the data source is not yet ready or it is fetching data from the homeserver.
- MXKDataSourceStateReady: the data source data is ready.
- MXKDataSourceStateFailed: the data source failed to fetch data.
There is no way in Matrix to be notified when there is a change.
*/
@interface MXKDirectoryServersDataSource : MXKDataSource <UITableViewDataSource>
{
@protected
/**
The data for the cells served by `DirectoryServersDataSource`.
*/
NSMutableArray<id<MXKDirectoryServerCellDataStoring>> *cellDataArray;
/**
The filtered servers: sub-list of `cellDataArray` defined by `searchWithPatterns:`.
*/
NSMutableArray<id<MXKDirectoryServerCellDataStoring>> *filteredCellDataArray;
}
/**
Additional room directory servers the datasource will list.
*/
@property (nonatomic) NSArray<NSString*> *roomDirectoryServers;
/**
Fetch the data source data.
*/
- (void)loadData;
/**
Filter the current recents list according to the provided patterns.
When patterns are not empty, the search result is stored in `filteredCellDataArray`,
this array provides then data for the cells served by `MXKDirectoryServersDataSource`.
@param patternsList the list of patterns to match with. Set nil to cancel search.
*/
- (void)searchWithPatterns:(NSArray<NSString*> *)patternsList;
/**
Get the data for the cell at the given index path.
@param indexPath the index of the cell.
@return the cell data.
*/
- (id<MXKDirectoryServerCellDataStoring>)cellDataAtIndexPath:(NSIndexPath*)indexPath;
@end
@@ -0,0 +1,230 @@
/*
Copyright 2017 Vector Creations Ltd
Copyright 2018 New Vector 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 "MXKDirectoryServersDataSource.h"
#import "MXKDirectoryServerCellData.h"
NSString *const kMXKDirectorServerCellIdentifier = @"kMXKDirectorServerCellIdentifier";
#pragma mark - DirectoryServersDataSource
@interface MXKDirectoryServersDataSource ()
{
// The pending request to load third-party protocols.
MXHTTPOperation *request;
}
@end
@implementation MXKDirectoryServersDataSource
- (instancetype)init
{
self = [super init];
if (self)
{
cellDataArray = [NSMutableArray array];
filteredCellDataArray = nil;
// Set default data w classes
[self registerCellDataClass:MXKDirectoryServerCellData.class forCellIdentifier:kMXKDirectorServerCellIdentifier];
}
return self;
}
- (void)destroy
{
cellDataArray = nil;
filteredCellDataArray = nil;
}
- (void)cancelAllRequests
{
[super cancelAllRequests];
[request cancel];
request = nil;
}
- (void)loadData
{
// Cancel the previous request
if (request)
{
[request cancel];
}
// Reset all vars
[cellDataArray removeAllObjects];
[self setState:MXKDataSourceStatePreparing];
Class class = [self cellDataClassForCellIdentifier:kMXKDirectorServerCellIdentifier];
// Add user's HS
NSString *userHomeserver = self.mxSession.matrixRestClient.credentials.homeServerName;
id<MXKDirectoryServerCellDataStoring> cellData = [[class alloc] initWithHomeserver:userHomeserver includeAllNetworks:YES];
[cellDataArray addObject:cellData];
// Add user's HS but for Matrix public rooms only
cellData = [[class alloc] initWithHomeserver:userHomeserver includeAllNetworks:NO];
[cellDataArray addObject:cellData];
// Add custom directory servers
for (NSString *homeserver in _roomDirectoryServers)
{
if (![homeserver isEqualToString:userHomeserver])
{
cellData = [[class alloc] initWithHomeserver:homeserver includeAllNetworks:YES];
[cellDataArray addObject:cellData];
}
}
MXWeakify(self);
request = [self.mxSession.matrixRestClient thirdpartyProtocols:^(MXThirdpartyProtocolsResponse *thirdpartyProtocolsResponse) {
MXStrongifyAndReturnIfNil(self);
for (NSString *protocolName in thirdpartyProtocolsResponse.protocols)
{
MXThirdPartyProtocol *protocol = thirdpartyProtocolsResponse.protocols[protocolName];
for (MXThirdPartyProtocolInstance *instance in protocol.instances)
{
id<MXKDirectoryServerCellDataStoring> cellData = [[class alloc] initWithProtocolInstance:instance protocol:protocol];
cellData.mediaManager = self.mxSession.mediaManager;
[self->cellDataArray addObject:cellData];
}
}
[self setState:MXKDataSourceStateReady];
} failure:^(NSError *error) {
MXStrongifyAndReturnIfNil(self);
if (!self->request || self->request.isCancelled)
{
// Do not take into account error coming from a cancellation
return;
}
self->request = nil;
MXLogDebug(@"[MXKDirectoryServersDataSource] Failed to fecth third-party protocols. The HS may be too old to support third party networks");
[self setState:MXKDataSourceStateReady];
}];
}
- (void)searchWithPatterns:(NSArray*)patternsList
{
if (patternsList.count)
{
if (filteredCellDataArray)
{
[filteredCellDataArray removeAllObjects];
}
else
{
filteredCellDataArray = [NSMutableArray arrayWithCapacity:cellDataArray.count];
}
for (id<MXKDirectoryServerCellDataStoring> cellData in cellDataArray)
{
for (NSString* pattern in patternsList)
{
if ([cellData.desc rangeOfString:pattern options:NSCaseInsensitiveSearch].location != NSNotFound)
{
[filteredCellDataArray addObject:cellData];
break;
}
}
}
}
else
{
filteredCellDataArray = nil;
}
if (self.delegate)
{
[self.delegate dataSource:self didCellChange:nil];
}
}
/**
Get the data for the cell at the given index path.
@param indexPath the index of the cell.
@return the cell data.
*/
- (id<MXKDirectoryServerCellDataStoring>)cellDataAtIndexPath:(NSIndexPath*)indexPath;
{
if (filteredCellDataArray)
{
return filteredCellDataArray[indexPath.row];
}
return cellDataArray[indexPath.row];
}
#pragma mark - Private methods
// Update the MXKDataSource state and the delegate
- (void)setState:(MXKDataSourceState)newState
{
state = newState;
if (self.delegate && [self.delegate respondsToSelector:@selector(dataSource:didStateChange:)])
{
[self.delegate dataSource:self didStateChange:state];
}
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (filteredCellDataArray)
{
return filteredCellDataArray.count;
}
return cellDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
id<MXKDirectoryServerCellDataStoring> cellData = [self cellDataAtIndexPath:indexPath];
if (cellData && self.delegate)
{
NSString *identifier = [self.delegate cellReuseIdentifierForCellData:cellData];
if (identifier)
{
UITableViewCell<MXKCellRendering> *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
// Make the cell display the data
[cell render:cellData];
return cell;
}
}
// Return a fake cell to prevent app from crashing.
return [[UITableViewCell alloc] init];
}
@end