Remove duplicated logout code and fix spinner.

- Remove some duplication of logout behaviour
- Fix additional spinner on homeViewController
This commit is contained in:
David Langley
2022-01-10 21:38:24 +00:00
parent 6df4713eba
commit bc08428f4c
9 changed files with 74 additions and 46 deletions

View File

@@ -155,7 +155,7 @@ class UserSessionsService: NSObject {
let isSessionStateValid: Bool let isSessionStateValid: Bool
switch mxSession.state { switch mxSession.state {
case .closed, .unauthenticated: case .closed:
isSessionStateValid = false isSessionStateValid = false
default: default:
isSessionStateValid = true isSessionStateValid = true

View File

@@ -1372,9 +1372,9 @@ NSString *const AppDelegateUniversalLinkDidChangeNotification = @"AppDelegateUni
else else
{ {
void(^findRoom)(void) = ^{ void(^findRoom)(void) = ^{
if ([_masterTabBarController.selectedViewController isKindOfClass:MXKActivityHandlingViewController.class]) if ([_masterTabBarController.selectedViewController conformsToProtocol:@protocol(MXKViewControllerActivityHandling)])
{ {
MXKActivityHandlingViewController *homeViewController = (MXKActivityHandlingViewController*)_masterTabBarController.selectedViewController; UIViewController<MXKViewControllerActivityHandling> *homeViewController = (UIViewController<MXKViewControllerActivityHandling>*)_masterTabBarController.selectedViewController;
[homeViewController startActivityIndicator]; [homeViewController startActivityIndicator];
@@ -1651,11 +1651,13 @@ NSString *const AppDelegateUniversalLinkDidChangeNotification = @"AppDelegateUni
// Try to get more information about the room before opening its preview // Try to get more information about the room before opening its preview
[roomPreviewData peekInRoom:^(BOOL succeeded) { [roomPreviewData peekInRoom:^(BOOL succeeded) {
MXStrongifyAndReturnIfNil(self); MXStrongifyAndReturnIfNil(self);
if ([self.masterTabBarController.selectedViewController conformsToProtocol:@protocol(MXKViewControllerActivityHandling)])
MXKViewController *homeViewController = (MXKViewController*)self.masterTabBarController.selectedViewController; {
UIViewController<MXKViewControllerActivityHandling> *homeViewController = (UIViewController<MXKViewControllerActivityHandling>*)self.masterTabBarController.selectedViewController;
// Note: the activity indicator will not disappear if the session is not ready
[homeViewController stopActivityIndicator]; // Note: the activity indicator will not disappear if the session is not ready
[homeViewController stopActivityIndicator];
}
// If no data is available for this room, we name it with the known room alias (if any). // If no data is available for this room, we name it with the known room alias (if any).
if (!succeeded && self->universalLinkFragmentPendingRoomAlias[roomIdOrAlias]) if (!succeeded && self->universalLinkFragmentPendingRoomAlias[roomIdOrAlias])

View File

@@ -16,8 +16,8 @@
import Foundation import Foundation
class HomeViewControllerWithBannerWrapperViewController: MXKActivityHandlingViewController, BannerPresentationProtocol { class HomeViewControllerWithBannerWrapperViewController: UIViewController, MXKViewControllerActivityHandling, BannerPresentationProtocol {
@objc let homeViewController: HomeViewController @objc let homeViewController: HomeViewController
private var bannerContainerView: UIView! private var bannerContainerView: UIView!
private var stackView: UIStackView! private var stackView: UIStackView!
@@ -85,4 +85,22 @@ class HomeViewControllerWithBannerWrapperViewController: MXKActivityHandlingView
bannerView.removeFromSuperview() bannerView.removeFromSuperview()
} }
} }
// MARK: - MXKViewControllerActivityHandling
var activityIndicator: UIActivityIndicatorView! {
get {
return homeViewController.activityIndicator
}
set {
homeViewController.activityIndicator = newValue
}
}
func startActivityIndicator() {
homeViewController.startActivityIndicator()
}
func stopActivityIndicator() {
homeViewController.stopActivityIndicator()
}
} }

View File

@@ -1453,7 +1453,7 @@
return NO; return NO;
} andPersistentTokenDataHandler:^(void (^handler)(NSArray<MXCredentials *> *credentials, void (^completion)(BOOL didUpdateCredentials))) { } andPersistentTokenDataHandler:^(void (^handler)(NSArray<MXCredentials *> *credentials, void (^completion)(BOOL didUpdateCredentials))) {
[[MXKAccountManager sharedManager] readAndWriteCredentials:handler]; [[MXKAccountManager sharedManager] readAndWriteCredentials:handler];
}]; } andUnauthenticatedHandler: nil];
MXWeakify(self); MXWeakify(self);
[[MXKAccountManager sharedManager].dehydrationService rehydrateDeviceWithMatrixRestClient:mxRestClient dehydrationKey:keyData success:^(NSString * deviceId) { [[MXKAccountManager sharedManager].dehydrationService rehydrateDeviceWithMatrixRestClient:mxRestClient dehydrationKey:keyData success:^(NSString * deviceId) {

View File

@@ -359,4 +359,8 @@ typedef BOOL (^MXKAccountOnCertificateChange)(MXKAccount *mxAccount, NSData *cer
success:(void (^)(void))success success:(void (^)(void))success
failure:(void (^)(NSError *error))failure; failure:(void (^)(NSError *error))failure;
/**
Handle unauthenticated errors from the server triggering hard/soft logouts as appropriate.
*/
- (void)handleUnauthenticated:(MXError *)error andCompletion:(void (^)(void))completion;
@end @end

View File

@@ -959,9 +959,6 @@ static NSArray<NSNumber*> *initialSyncSilentErrorsHTTPStatusCodes;
} }
} }
- (void)deletePusher - (void)deletePusher
{ {
if (self.pushNotificationServiceIsActive) if (self.pushNotificationServiceIsActive)
@@ -1670,16 +1667,6 @@ static NSArray<NSNumber*> *initialSyncSilentErrorsHTTPStatusCodes;
{ {
isPauseRequested = NO; isPauseRequested = NO;
} }
else if (mxSession.state == MXSessionStateUnauthenticated)
{
// Logout this account
[[MXKAccountManager sharedManager] removeAccount:self sendLogoutRequest:NO completion:nil];
}
else if (mxSession.state == MXSessionStateSoftLogout)
{
// Soft logout this account
[[MXKAccountManager sharedManager] softLogout:self];
}
} }
- (void)prepareRESTClient - (void)prepareRESTClient
@@ -1688,9 +1675,9 @@ static NSArray<NSNumber*> *initialSyncSilentErrorsHTTPStatusCodes;
{ {
return; return;
} }
MXWeakify(self);
mxRestClient = [[MXRestClient alloc] initWithCredentials:self.mxCredentials andOnUnrecognizedCertificateBlock:^BOOL(NSData *certificate) { mxRestClient = [[MXRestClient alloc] initWithCredentials:self.mxCredentials andOnUnrecognizedCertificateBlock:^BOOL(NSData *certificate) {
MXStrongifyAndReturnValueIfNil(self, NO);
if (_onCertificateChangeBlock) if (_onCertificateChangeBlock)
{ {
if (_onCertificateChangeBlock (self, certificate)) if (_onCertificateChangeBlock (self, certificate))
@@ -1713,9 +1700,29 @@ static NSArray<NSNumber*> *initialSyncSilentErrorsHTTPStatusCodes;
} andPersistentTokenDataHandler:^(void (^handler)(NSArray<MXCredentials *> *credentials, void (^completion)(BOOL didUpdateCredentials))) { } andPersistentTokenDataHandler:^(void (^handler)(NSArray<MXCredentials *> *credentials, void (^completion)(BOOL didUpdateCredentials))) {
[MXKAccountManager.sharedManager readAndWriteCredentials:handler]; [MXKAccountManager.sharedManager readAndWriteCredentials:handler];
} andUnauthenticatedHandler:^(MXError *error, void (^completion)(void)) {
MXStrongifyAndReturnIfNil(self);
[self handleUnauthenticated:error andCompletion:completion];
}]; }];
} }
- (void)handleUnauthenticated:(MXError *)error andCompletion:(void (^)(void))completion
{
if (error.httpResponse.statusCode == 401
&& [error.userInfo[kMXErrorSoftLogoutKey] isEqual:@(YES)])
{
MXLogDebug(@"[MXKAccountManager] handleUnauthenticated: soft logout.");
[[MXKAccountManager sharedManager] softLogout:self];
completion();
}
else
{
MXLogDebug(@"[MXKAccountManager] handleUnauthenticated: hard logout.");
[[MXKAccountManager sharedManager] removeAccount:self sendLogoutRequest:NO completion:completion];
}
}
- (void)onDateTimeFormatUpdate - (void)onDateTimeFormatUpdate
{ {
if ([mxSession.roomSummaryUpdateDelegate isKindOfClass:MXKEventFormatter.class]) if ([mxSession.roomSummaryUpdateDelegate isKindOfClass:MXKEventFormatter.class])

View File

@@ -56,22 +56,11 @@ class NotificationService: UNNotificationServiceExtension {
guard let userAccount = userAccount else { guard let userAccount = userAccount else {
return nil return nil
} }
let restClient = MXRestClient(credentials: userAccount.mxCredentials, unrecognizedCertificateHandler: nil) { persistTokenDataHandler in let restClient = MXRestClient(credentials: userAccount.mxCredentials, unrecognizedCertificateHandler: nil, persistentTokenDataHandler: { persistTokenDataHandler in
MXKAccountManager.shared().readAndWriteCredentials(persistTokenDataHandler) MXKAccountManager.shared().readAndWriteCredentials(persistTokenDataHandler)
} }, unauthenticatedHandler: { error, completion in
restClient.refreshTokensFailedHandler = { mxError in userAccount.handleUnauthenticated(error, andCompletion: completion)
MXLog.debug("[NotificationService] mxRestClient: The rest client is no longer authenticated.") })
if let mxError = mxError,
mxError.httpResponse.statusCode == 401,
let softLogout = mxError.userInfo[kMXErrorSoftLogoutKey] as? Bool,
softLogout {
MXLog.debug("[NotificationService] mxRestClient: soft logout");
userAccount.softLogout()
} else {
MXLog.debug("[NotificationService] mxRestClient: full logout");
MXKAccountManager.shared().removeAccount(userAccount, completion: nil)
}
}
return restClient return restClient
}() }()
@@ -191,6 +180,8 @@ class NotificationService: UNNotificationServiceExtension {
self.logMemory() self.logMemory()
NotificationService.backgroundSyncService = MXBackgroundSyncService(withCredentials: userAccount.mxCredentials, persistTokenDataHandler: { persistTokenDataHandler in NotificationService.backgroundSyncService = MXBackgroundSyncService(withCredentials: userAccount.mxCredentials, persistTokenDataHandler: { persistTokenDataHandler in
MXKAccountManager.shared().readAndWriteCredentials(persistTokenDataHandler) MXKAccountManager.shared().readAndWriteCredentials(persistTokenDataHandler)
}, unauthenticatedHandler: { error, completion in
userAccount.handleUnauthenticated(error, andCompletion: completion)
}) })
MXLog.debug("[NotificationService] setup: MXBackgroundSyncService init: AFTER") MXLog.debug("[NotificationService] setup: MXBackgroundSyncService init: AFTER")
self.logMemory() self.logMemory()

View File

@@ -81,10 +81,11 @@
NSMutableArray *cellData = [NSMutableArray array]; NSMutableArray *cellData = [NSMutableArray array];
// Add a fake matrix session to each room summary to provide it a REST client (used to handle correctly the room avatar). MXRestClient *mxRestClient = [[MXRestClient alloc] initWithCredentials:self.credentials andOnUnrecognizedCertificateBlock:nil andPersistentTokenDataHandler:^(void (^handler)(NSArray<MXCredentials *> *credentials, void (^completion)(BOOL didUpdateCredentials))) {
MXSession *session = [[MXSession alloc] initWithMatrixRestClient:[[MXRestClient alloc] initWithCredentials:self.credentials andOnUnrecognizedCertificateBlock:nil andPersistentTokenDataHandler:^(void (^handler)(NSArray<MXCredentials *> *credentials, void (^completion)(BOOL didUpdateCredentials))) {
[[MXKAccountManager sharedManager] readAndWriteCredentials:handler]; [[MXKAccountManager sharedManager] readAndWriteCredentials:handler];
}]]; } andUnauthenticatedHandler:nil];
// Add a fake matrix session to each room summary to provide it a REST client (used to handle correctly the room avatar).
MXSession *session = [[MXSession alloc] initWithMatrixRestClient:mxRestClient];
for (MXRoomSummary *roomSummary in roomsSummaries) for (MXRoomSummary *roomSummary in roomsSummaries)
{ {

View File

@@ -78,9 +78,14 @@
- (void)shareViewController:(ShareViewController *)shareViewController didRequestShareForRoomIdentifiers:(NSSet<NSString *> *)roomIdentifiers - (void)shareViewController:(ShareViewController *)shareViewController didRequestShareForRoomIdentifiers:(NSSet<NSString *> *)roomIdentifiers
{ {
MXSession *session = [[MXSession alloc] initWithMatrixRestClient:[[MXRestClient alloc] initWithCredentials:self.userAccount.mxCredentials andOnUnrecognizedCertificateBlock:nil andPersistentTokenDataHandler:^(void (^handler)(NSArray<MXCredentials *> *credentials, void (^completion)(BOOL didUpdateCredentials))) { MXWeakify(self);
MXRestClient *restClient = [[MXRestClient alloc] initWithCredentials:self.userAccount.mxCredentials andOnUnrecognizedCertificateBlock:nil andPersistentTokenDataHandler:^(void (^handler)(NSArray<MXCredentials *> *credentials, void (^completion)(BOOL didUpdateCredentials))) {
[[MXKAccountManager sharedManager] readAndWriteCredentials:handler]; [[MXKAccountManager sharedManager] readAndWriteCredentials:handler];
}]]; } andUnauthenticatedHandler:^(MXError *error, void (^completion)(void)) {
MXStrongifyAndReturnIfNil(self);
[self.userAccount handleUnauthenticated:error andCompletion:completion];
}];
MXSession *session = [[MXSession alloc] initWithMatrixRestClient:restClient];
[MXFileStore setPreloadOptions:0]; [MXFileStore setPreloadOptions:0];
MXWeakify(session); MXWeakify(session);