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 0c5218fa59
commit e53e7bc10b
3 changed files with 108 additions and 89 deletions
@@ -24,12 +24,6 @@
@interface ShareViewController ()
// The current user account
@property (nonatomic) MXKAccount *userAccount;
@property (nonatomic) id removedAccountObserver;
@property (nonatomic) NSArray <MXRoom *> *rooms;
@property (weak, nonatomic) IBOutlet UIView *masterContainerView;
@property (weak, nonatomic) IBOutlet UILabel *tittleLabel;
@property (weak, nonatomic) IBOutlet UIView *contentView;
@@ -48,92 +42,17 @@
{
[super viewDidLoad];
[self prepareSession];
[[NSNotificationCenter defaultCenter] addObserverForName:kShareExtensionManagerDidChangeMXSessionNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notif) {
[self configureViews];
}];
[self configureViews];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// Add observer to handle removed accounts
self.removedAccountObserver = [[NSNotificationCenter defaultCenter] addObserverForName:kMXKAccountManagerDidRemoveAccountNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notif) {
[self checkUserAccount];
}];
[self checkUserAccount];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// Remove listener
if (self.removedAccountObserver)
{
[[NSNotificationCenter defaultCenter] removeObserver:self.removedAccountObserver];
self.removedAccountObserver = nil;
}
[self.userAccount pauseInBackgroundTask];
}
#pragma mark - Private
- (void)prepareSession
{
// Apply the application group
[MXKAppSettings standardAppSettings].applicationGroup = @"group.im.vector";
// We consider for now the first enabled account.
// TODO: Handle multiple accounts
self.userAccount = [MXKAccountManager sharedManager].activeAccounts.firstObject;
if (self.userAccount)
{
NSLog(@"[ShareViewController] openSession for %@ account", self.userAccount.mxCredentials.userId);
// Use MXFileStore as MXStore to permanently store events.
[self.userAccount openSessionWithStore:[[MXFileStore alloc] init]];
[self addMatrixSession:self.userAccount.mxSession];
}
}
- (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 removeMatrixSession:self.userAccount.mxSession];
[self.userAccount closeSession:YES];
self.userAccount = nil;
}
}
if (self.userAccount)
{
// Resume the matrix session
[self.userAccount resume];
}
else
{
// Prepare a new session if a new account is available.
[self prepareSession];
[self configureViews];
}
}
- (void)configureViews
{
self.masterContainerView.layer.cornerRadius = 7;
@@ -153,7 +72,7 @@
self.segmentedViewController = nil;
}
if (self.mainSession)
if ([ShareExtensionManager sharedManager].mxSession)
{
self.tittleLabel.text = [NSString stringWithFormat:NSLocalizedStringFromTable(@"send_to", @"Vector", nil), @""];
[self configureSegmentedViewController];
@@ -179,12 +98,12 @@
}];
};
ShareRecentsDataSource *roomsDataSource = [[ShareRecentsDataSource alloc] initWithMatrixSession:self.mainSession dataSourceMode:RecentsDataSourceModeRooms];
ShareRecentsDataSource *roomsDataSource = [[ShareRecentsDataSource alloc] initWithMatrixSession:[ShareExtensionManager sharedManager].mxSession dataSourceMode:RecentsDataSourceModeRooms];
RoomsListViewController *roomsViewController = [RoomsListViewController recentListViewController];
roomsViewController.failureBlock = failureBlock;
[roomsViewController displayList:roomsDataSource];
ShareRecentsDataSource *peopleDataSource = [[ShareRecentsDataSource alloc] initWithMatrixSession:self.mainSession dataSourceMode:RecentsDataSourceModePeople];
ShareRecentsDataSource *peopleDataSource = [[ShareRecentsDataSource alloc] initWithMatrixSession:[ShareExtensionManager sharedManager].mxSession dataSourceMode:RecentsDataSourceModePeople];
RoomsListViewController *peopleViewController = [RoomsListViewController recentListViewController];
peopleViewController.failureBlock = failureBlock;
[peopleViewController displayList:peopleDataSource];