2017-08-17 15:28:08 +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
|
2017-09-08 15:02:45 +00:00
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
2017-08-17 15:28:08 +00:00
|
|
|
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.
|
|
|
|
*/
|
2017-08-10 22:38:47 +00:00
|
|
|
|
|
|
|
@import MobileCoreServices;
|
2021-10-14 09:57:32 +00:00
|
|
|
|
2019-04-02 12:42:43 +00:00
|
|
|
#import <mach/mach.h>
|
2017-08-10 22:38:47 +00:00
|
|
|
|
2021-10-14 09:57:32 +00:00
|
|
|
#import "ShareManager.h"
|
|
|
|
#import "ShareViewController.h"
|
|
|
|
#import "ShareDataSource.h"
|
2021-10-28 07:16:22 +00:00
|
|
|
#import "ShareItemSenderProtocol.h"
|
2021-10-14 09:57:32 +00:00
|
|
|
|
2021-10-26 15:42:33 +00:00
|
|
|
#import "GeneratedInterface-Swift.h"
|
2017-08-21 20:33:06 +00:00
|
|
|
|
2021-10-28 07:16:22 +00:00
|
|
|
@interface ShareManager () <ShareViewControllerDelegate, ShareItemSenderDelegate>
|
2019-03-21 11:50:02 +00:00
|
|
|
|
2021-10-28 07:16:22 +00:00
|
|
|
@property (nonatomic, strong, readonly) id<ShareItemSenderProtocol> shareItemSender;
|
2021-10-14 09:05:28 +00:00
|
|
|
|
2021-10-14 11:35:36 +00:00
|
|
|
@property (nonatomic, strong, readonly) ShareViewController *shareViewController;
|
2017-09-08 15:02:45 +00:00
|
|
|
|
2021-10-14 09:05:28 +00:00
|
|
|
@property (nonatomic, strong) MXKAccount *userAccount;
|
|
|
|
@property (nonatomic, strong) MXFileStore *fileStore;
|
2017-08-26 11:54:25 +00:00
|
|
|
|
2017-08-16 20:01:54 +00:00
|
|
|
@end
|
|
|
|
|
2017-08-10 22:38:47 +00:00
|
|
|
|
2021-10-14 09:57:32 +00:00
|
|
|
@implementation ShareManager
|
2017-08-10 22:38:47 +00:00
|
|
|
|
2021-10-28 07:16:22 +00:00
|
|
|
- (instancetype)initWithShareItemSender:(id<ShareItemSenderProtocol>)itemSender
|
|
|
|
type:(ShareManagerType)type
|
2017-08-10 22:38:47 +00:00
|
|
|
{
|
2021-10-15 10:09:11 +00:00
|
|
|
if (self = [super init])
|
|
|
|
{
|
2021-10-28 07:16:22 +00:00
|
|
|
_shareItemSender = itemSender;
|
|
|
|
_shareItemSender.delegate = self;
|
2017-08-22 15:56:30 +00:00
|
|
|
|
2021-10-28 07:16:22 +00:00
|
|
|
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(checkUserAccount) name:kMXKAccountManagerDidRemoveAccountNotification object:nil];
|
|
|
|
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(checkUserAccount) name:NSExtensionHostWillEnterForegroundNotification object:nil];
|
2021-10-14 09:05:28 +00:00
|
|
|
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(didReceiveMemoryWarning:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
|
2017-08-21 20:33:06 +00:00
|
|
|
|
2021-10-18 13:30:32 +00:00
|
|
|
_shareViewController = [[ShareViewController alloc] initWithType:(type == ShareManagerTypeForward ? ShareViewControllerTypeForward : ShareViewControllerTypeSend)
|
2021-10-14 09:05:28 +00:00
|
|
|
currentState:ShareViewControllerAccountStateNotConfigured];
|
|
|
|
[_shareViewController setDelegate:self];
|
|
|
|
|
|
|
|
// Set up runtime language on each context update.
|
|
|
|
NSUserDefaults *sharedUserDefaults = [MXKAppSettings standardAppSettings].sharedUserDefaults;
|
|
|
|
NSString *language = [sharedUserDefaults objectForKey:@"appLanguage"];
|
|
|
|
[NSBundle mxk_setLanguage:language];
|
|
|
|
[NSBundle mxk_setFallbackLanguage:@"en"];
|
|
|
|
|
|
|
|
[self checkUserAccount];
|
2017-09-08 15:02:45 +00:00
|
|
|
}
|
2017-09-29 08:37:54 +00:00
|
|
|
|
2021-10-14 09:05:28 +00:00
|
|
|
return self;
|
2017-09-08 15:02:45 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 11:25:02 +00:00
|
|
|
#pragma mark - Public
|
|
|
|
|
2021-10-14 09:05:28 +00:00
|
|
|
- (UIViewController *)mainViewController
|
2017-08-21 20:33:06 +00:00
|
|
|
{
|
2021-10-14 09:05:28 +00:00
|
|
|
return self.shareViewController;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - ShareViewControllerDelegate
|
|
|
|
|
2021-10-18 13:30:32 +00:00
|
|
|
- (void)shareViewController:(ShareViewController *)shareViewController didRequestShareForRoomIdentifiers:(NSSet<NSString *> *)roomIdentifiers
|
2021-10-14 09:05:28 +00:00
|
|
|
{
|
2022-01-10 21:38:24 +00:00
|
|
|
MXWeakify(self);
|
|
|
|
MXRestClient *restClient = [[MXRestClient alloc] initWithCredentials:self.userAccount.mxCredentials andOnUnrecognizedCertificateBlock:nil andPersistentTokenDataHandler:^(void (^handler)(NSArray<MXCredentials *> *credentials, void (^completion)(BOOL didUpdateCredentials))) {
|
2022-01-06 20:35:37 +00:00
|
|
|
[[MXKAccountManager sharedManager] readAndWriteCredentials:handler];
|
2022-01-13 17:33:51 +00:00
|
|
|
} andUnauthenticatedHandler:^(MXError *error, BOOL isSoftLogout, BOOL isRefreshTokenAuth, void (^completion)(void)) {
|
2022-01-10 21:38:24 +00:00
|
|
|
MXStrongifyAndReturnIfNil(self);
|
2022-01-13 17:33:51 +00:00
|
|
|
[self.userAccount handleUnauthenticatedWithError:error isSoftLogout:isSoftLogout isRefreshTokenAuth:isRefreshTokenAuth andCompletion:completion];
|
2022-01-10 21:38:24 +00:00
|
|
|
}];
|
|
|
|
MXSession *session = [[MXSession alloc] initWithMatrixRestClient:restClient];
|
2021-10-14 09:05:28 +00:00
|
|
|
[MXFileStore setPreloadOptions:0];
|
2017-12-27 13:00:16 +00:00
|
|
|
|
2021-10-14 09:05:28 +00:00
|
|
|
MXWeakify(session);
|
|
|
|
[session setStore:self.fileStore success:^{
|
|
|
|
MXStrongifyAndReturnIfNil(session);
|
|
|
|
|
2021-10-14 11:35:36 +00:00
|
|
|
session.crypto.warnOnUnknowDevices = NO; // Do not warn for unknown devices. We have cross-signing now
|
2021-10-14 09:05:28 +00:00
|
|
|
|
2021-10-18 13:30:32 +00:00
|
|
|
NSMutableArray<MXRoom *> *rooms = [NSMutableArray array];
|
|
|
|
for (NSString *roomIdentifier in roomIdentifiers) {
|
|
|
|
MXRoom *room = [MXRoom loadRoomFromStore:self.fileStore withRoomId:roomIdentifier matrixSession:session];
|
|
|
|
if (room) {
|
|
|
|
[rooms addObject:room];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-28 07:16:22 +00:00
|
|
|
[self.shareItemSender sendItemsToRooms:rooms success:^{
|
2021-10-18 13:30:32 +00:00
|
|
|
self.completionCallback(ShareManagerResultFinished);
|
2021-10-28 07:16:22 +00:00
|
|
|
} failure:^(NSArray<NSError *> *errors) {
|
2021-10-14 11:35:36 +00:00
|
|
|
[self showFailureAlert:[VectorL10n roomEventFailedToSend]];
|
2021-10-14 09:05:28 +00:00
|
|
|
}];
|
2021-10-18 13:30:32 +00:00
|
|
|
|
2021-10-14 09:05:28 +00:00
|
|
|
} failure:^(NSError *error) {
|
2021-10-18 13:30:32 +00:00
|
|
|
MXLogError(@"[ShareManager] Failed preparing matrix session");
|
2021-10-14 09:05:28 +00:00
|
|
|
}];
|
2017-08-21 20:33:06 +00:00
|
|
|
}
|
|
|
|
|
2021-10-14 09:05:28 +00:00
|
|
|
- (void)shareViewControllerDidRequestDismissal:(ShareViewController *)shareViewController
|
|
|
|
{
|
2021-10-14 11:35:36 +00:00
|
|
|
self.completionCallback(ShareManagerResultCancelled);
|
2021-10-14 09:05:28 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 07:16:22 +00:00
|
|
|
#pragma mark - ShareItemSenderDelegate
|
|
|
|
|
|
|
|
- (void)shareItemSenderDidStartSending:(id<ShareItemSenderProtocol>)shareItemSender
|
|
|
|
{
|
|
|
|
[self.shareViewController showProgressIndicator];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)shareItemSender:(id<ShareItemSenderProtocol>)shareItemSender didUpdateProgress:(CGFloat)progress
|
|
|
|
{
|
|
|
|
[self.shareViewController setProgress:progress];
|
|
|
|
}
|
|
|
|
|
2021-10-14 09:05:28 +00:00
|
|
|
#pragma mark - Private
|
|
|
|
|
2021-10-18 13:30:32 +00:00
|
|
|
- (void)showFailureAlert:(NSString *)title
|
2017-08-10 22:38:47 +00:00
|
|
|
{
|
2021-10-18 13:30:32 +00:00
|
|
|
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
|
|
|
|
|
|
|
|
MXWeakify(self);
|
2022-03-03 09:32:19 +00:00
|
|
|
UIAlertAction *okAction = [UIAlertAction actionWithTitle:[VectorL10n ok] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
|
2021-10-18 13:30:32 +00:00
|
|
|
MXStrongifyAndReturnIfNil(self);
|
|
|
|
|
|
|
|
if (self.completionCallback)
|
|
|
|
{
|
|
|
|
self.completionCallback(ShareManagerResultFailed);
|
|
|
|
}
|
|
|
|
}];
|
2019-02-08 13:43:21 +00:00
|
|
|
|
2021-10-18 13:30:32 +00:00
|
|
|
[alertController addAction:okAction];
|
|
|
|
|
|
|
|
[self.mainViewController presentViewController:alertController animated:YES completion:nil];
|
|
|
|
}
|
|
|
|
|
2021-10-14 09:05:28 +00:00
|
|
|
- (void)checkUserAccount
|
2017-08-10 22:38:47 +00:00
|
|
|
{
|
2021-10-14 09:05:28 +00:00
|
|
|
// Force account manager to reload account from the local storage.
|
2022-01-06 20:35:37 +00:00
|
|
|
[MXKAccountManager sharedManagerWithReload:YES];
|
2021-10-14 09:05:28 +00:00
|
|
|
|
|
|
|
if (self.userAccount)
|
2017-08-17 22:27:56 +00:00
|
|
|
{
|
2021-10-14 09:05:28 +00:00
|
|
|
// Check whether the used account is still the first active one
|
|
|
|
MXKAccount *firstAccount = [MXKAccountManager sharedManager].activeAccounts.firstObject;
|
|
|
|
|
|
|
|
// Compare the access token
|
|
|
|
if (!firstAccount || ![self.userAccount.mxCredentials.accessToken isEqualToString:firstAccount.mxCredentials.accessToken])
|
|
|
|
{
|
|
|
|
// Remove this account
|
|
|
|
self.userAccount = nil;
|
|
|
|
}
|
2017-08-17 22:27:56 +00:00
|
|
|
}
|
2021-10-14 09:05:28 +00:00
|
|
|
|
|
|
|
if (!self.userAccount)
|
2017-08-17 22:27:56 +00:00
|
|
|
{
|
2021-10-14 09:05:28 +00:00
|
|
|
// We consider the first enabled account.
|
|
|
|
// TODO: Handle multiple accounts
|
|
|
|
self.userAccount = [MXKAccountManager sharedManager].activeAccounts.firstObject;
|
2017-08-17 22:27:56 +00:00
|
|
|
}
|
2017-08-25 09:55:37 +00:00
|
|
|
|
2021-10-14 09:05:28 +00:00
|
|
|
// Reset the file store to reload the room data.
|
|
|
|
if (_fileStore)
|
|
|
|
{
|
|
|
|
[_fileStore close];
|
|
|
|
_fileStore = nil;
|
|
|
|
}
|
2019-01-04 12:57:35 +00:00
|
|
|
|
2021-10-14 09:05:28 +00:00
|
|
|
if (self.userAccount)
|
|
|
|
{
|
|
|
|
_fileStore = [[MXFileStore alloc] initWithCredentials:self.userAccount.mxCredentials];
|
|
|
|
|
2021-10-18 13:30:32 +00:00
|
|
|
ShareDataSource *roomDataSource = [[ShareDataSource alloc] initWithFileStore:_fileStore
|
|
|
|
credentials:self.userAccount.mxCredentials];
|
2021-10-14 09:05:28 +00:00
|
|
|
|
|
|
|
[self.shareViewController configureWithState:ShareViewControllerAccountStateConfigured
|
2021-10-18 13:30:32 +00:00
|
|
|
roomDataSource:roomDataSource];
|
2021-10-14 09:05:28 +00:00
|
|
|
} else {
|
|
|
|
[self.shareViewController configureWithState:ShareViewControllerAccountStateNotConfigured
|
2021-10-18 13:30:32 +00:00
|
|
|
roomDataSource:nil];
|
2021-10-14 09:05:28 +00:00
|
|
|
}
|
2017-08-10 22:38:47 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 13:30:32 +00:00
|
|
|
- (void)didStartSending
|
2017-08-20 21:39:08 +00:00
|
|
|
{
|
2021-10-14 09:05:28 +00:00
|
|
|
[self.shareViewController showProgressIndicator];
|
2017-08-20 21:39:08 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 07:16:22 +00:00
|
|
|
- (void)didReceiveMemoryWarning:(NSNotification*)notification
|
2019-04-02 13:01:50 +00:00
|
|
|
{
|
2021-10-28 07:16:22 +00:00
|
|
|
MXLogDebug(@"[ShareManager] Did receive memory warning");
|
|
|
|
[self logMemoryUsage];
|
2019-04-02 13:01:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 12:42:43 +00:00
|
|
|
// Log memory usage.
|
|
|
|
// NOTE: This result may not be reliable for all iOS versions (see https://forums.developer.apple.com/thread/64665 for more information).
|
|
|
|
- (void)logMemoryUsage
|
|
|
|
{
|
|
|
|
struct task_basic_info basicInfo;
|
|
|
|
mach_msg_type_number_t size = TASK_BASIC_INFO_COUNT;
|
|
|
|
kern_return_t kerr = task_info(mach_task_self(),
|
|
|
|
TASK_BASIC_INFO,
|
|
|
|
(task_info_t)&basicInfo,
|
|
|
|
&size);
|
|
|
|
|
|
|
|
vm_size_t memoryUsedInBytes = basicInfo.resident_size;
|
|
|
|
CGFloat memoryUsedInMegabytes = memoryUsedInBytes / (1024*1024);
|
|
|
|
|
|
|
|
if (kerr == KERN_SUCCESS)
|
|
|
|
{
|
2021-10-14 09:57:32 +00:00
|
|
|
MXLogDebug(@"[ShareManager] Memory in use (in MB): %f", memoryUsedInMegabytes);
|
2019-04-02 12:42:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-14 09:57:32 +00:00
|
|
|
MXLogDebug(@"[ShareManager] Error with task_info(): %s", mach_error_string(kerr));
|
2019-04-02 12:42:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 15:52:32 +00:00
|
|
|
@end
|