App Extension - Fix matrix session handling

The matrix session should be handled by the ShareExtensionManager. This manager is able to prepare, suspend and resume the session.
Note: The ShareViewController is reloaded each time the extension is launched
This commit is contained in:
Giom Foret
2017-08-21 22:33:06 +02:00
parent 490c022ed5
commit 36d080400c
3 changed files with 108 additions and 89 deletions
@@ -18,6 +18,8 @@
#import "MXKPieChartHUD.h"
@import MobileCoreServices;
NSString *const kShareExtensionManagerDidChangeMXSessionNotification = @"kShareExtensionManagerDidChangeMXSessionNotification";
typedef NS_ENUM(NSInteger, ImageCompressionMode)
{
ImageCompressionModeNone,
@@ -28,6 +30,9 @@ typedef NS_ENUM(NSInteger, ImageCompressionMode)
@interface ShareExtensionManager ()
// The current user account
@property (nonatomic) MXKAccount *userAccount;
@property ImageCompressionMode imageCompressionMode;
@property CGFloat actualLargeSize;
@@ -43,14 +48,92 @@ typedef NS_ENUM(NSInteger, ImageCompressionMode)
static ShareExtensionManager *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:sharedInstance selector:@selector(onMediaUploadProgress:) name:kMXMediaUploadProgressNotification object:nil];
// Add observer to handle logout
[[NSNotificationCenter defaultCenter] addObserver:sharedInstance selector:@selector(checkUserAccount) name:kMXKAccountManagerDidRemoveAccountNotification object:nil];
// Add observer on the Extension host
[[NSNotificationCenter defaultCenter] addObserver:sharedInstance selector:@selector(checkUserAccount) name:NSExtensionHostWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:sharedInstance selector:@selector(suspendSession) name:NSExtensionHostDidEnterBackgroundNotification object:nil];
// Apply the application group
[MXKAppSettings standardAppSettings].applicationGroup = @"group.im.vector";
});
return sharedInstance;
}
- (void)checkUserAccount
{
// Force account manager to reload account from the local storage.
[[MXKAccountManager sharedManager] forceReloadAccounts];
if (self.userAccount)
{
// 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 closeSession:YES];
self.userAccount = nil;
_mxSession = nil;
// Post notification
[[NSNotificationCenter defaultCenter] postNotificationName:kShareExtensionManagerDidChangeMXSessionNotification object:_mxSession userInfo:nil];
}
}
if (self.userAccount)
{
// Resume the matrix session
[self.userAccount resume];
}
else
{
// Prepare a new session if a new account is available.
[self prepareSession];
}
}
- (void)prepareSession
{
// We consider the first enabled account.
// TODO: Handle multiple accounts
self.userAccount = [MXKAccountManager sharedManager].activeAccounts.firstObject;
if (self.userAccount)
{
NSLog(@"[ShareExtensionManager] openSession for %@ account", self.userAccount.mxCredentials.userId);
// Use MXFileStore as MXStore to permanently store events.
[self.userAccount openSessionWithStore:[[MXFileStore alloc] init]];
_mxSession = self.userAccount.mxSession;
// Post notification
[[NSNotificationCenter defaultCenter] postNotificationName:kShareExtensionManagerDidChangeMXSessionNotification object:_mxSession userInfo:nil];
}
}
- (void)suspendSession
{
[self.userAccount pauseInBackgroundTask];
}
#pragma mark - Public
- (void)setShareExtensionContext:(NSExtensionContext *)shareExtensionContext
{
_shareExtensionContext = shareExtensionContext;
// Prepare or resume the matrix session.
[self checkUserAccount];
}
- (void)sendContentToRoom:(MXRoom *)room failureBlock:(void(^)())failureBlock
{
NSString *UTTypeText = (__bridge NSString *)kUTTypeText;
@@ -154,6 +237,8 @@ typedef NS_ENUM(NSInteger, ImageCompressionMode)
- (void)terminateExtensionCanceled:(BOOL)canceled
{
[self suspendSession];
if (canceled)
{
[self.shareExtensionContext cancelRequestWithError:[NSError errorWithDomain:@"MXUserCancelErrorDomain" code:4201 userInfo:nil]];
@@ -341,6 +426,7 @@ typedef NS_ENUM(NSInteger, ImageCompressionMode)
if (weakSelf)
{
typeof(self) self = weakSelf;
[self suspendSession];
[self.shareExtensionContext completeRequestReturningItems:@[extensionItem] completionHandler:nil];
}
} failure:^(NSError *error) {
@@ -372,6 +458,7 @@ typedef NS_ENUM(NSInteger, ImageCompressionMode)
if (weakSelf)
{
typeof(self) self = weakSelf;
[self suspendSession];
[self.shareExtensionContext completeRequestReturningItems:@[extensionItem] completionHandler:nil];
}
} failure:^(NSError *error) {
@@ -465,6 +552,7 @@ typedef NS_ENUM(NSInteger, ImageCompressionMode)
if (weakSelf)
{
typeof(self) self = weakSelf;
[self suspendSession];
[self.shareExtensionContext completeRequestReturningItems:@[extensionItem] completionHandler:nil];
}
} failure:^(NSError *error) {
@@ -505,6 +593,7 @@ typedef NS_ENUM(NSInteger, ImageCompressionMode)
if (weakSelf)
{
typeof(self) self = weakSelf;
[self suspendSession];
[self.shareExtensionContext completeRequestReturningItems:@[extensionItem] completionHandler:nil];
}
} failure:^(NSError *error) {