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 4effc7e2a0
commit 56475bbec1
9 changed files with 74 additions and 46 deletions

View file

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

View file

@ -1372,9 +1372,9 @@ NSString *const AppDelegateUniversalLinkDidChangeNotification = @"AppDelegateUni
else
{
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];
@ -1651,11 +1651,13 @@ NSString *const AppDelegateUniversalLinkDidChangeNotification = @"AppDelegateUni
// Try to get more information about the room before opening its preview
[roomPreviewData peekInRoom:^(BOOL succeeded) {
MXStrongifyAndReturnIfNil(self);
MXKViewController *homeViewController = (MXKViewController*)self.masterTabBarController.selectedViewController;
// Note: the activity indicator will not disappear if the session is not ready
[homeViewController stopActivityIndicator];
if ([self.masterTabBarController.selectedViewController conformsToProtocol:@protocol(MXKViewControllerActivityHandling)])
{
UIViewController<MXKViewControllerActivityHandling> *homeViewController = (UIViewController<MXKViewControllerActivityHandling>*)self.masterTabBarController.selectedViewController;
// 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 (!succeeded && self->universalLinkFragmentPendingRoomAlias[roomIdOrAlias])

View file

@ -16,8 +16,8 @@
import Foundation
class HomeViewControllerWithBannerWrapperViewController: MXKActivityHandlingViewController, BannerPresentationProtocol {
class HomeViewControllerWithBannerWrapperViewController: UIViewController, MXKViewControllerActivityHandling, BannerPresentationProtocol {
@objc let homeViewController: HomeViewController
private var bannerContainerView: UIView!
private var stackView: UIStackView!
@ -85,4 +85,22 @@ class HomeViewControllerWithBannerWrapperViewController: MXKActivityHandlingView
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;
} andPersistentTokenDataHandler:^(void (^handler)(NSArray<MXCredentials *> *credentials, void (^completion)(BOOL didUpdateCredentials))) {
[[MXKAccountManager sharedManager] readAndWriteCredentials:handler];
}];
} andUnauthenticatedHandler: nil];
MXWeakify(self);
[[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
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

View file

@ -959,9 +959,6 @@ static NSArray<NSNumber*> *initialSyncSilentErrorsHTTPStatusCodes;
}
}
- (void)deletePusher
{
if (self.pushNotificationServiceIsActive)
@ -1670,16 +1667,6 @@ static NSArray<NSNumber*> *initialSyncSilentErrorsHTTPStatusCodes;
{
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
@ -1688,9 +1675,9 @@ static NSArray<NSNumber*> *initialSyncSilentErrorsHTTPStatusCodes;
{
return;
}
MXWeakify(self);
mxRestClient = [[MXRestClient alloc] initWithCredentials:self.mxCredentials andOnUnrecognizedCertificateBlock:^BOOL(NSData *certificate) {
MXStrongifyAndReturnValueIfNil(self, NO);
if (_onCertificateChangeBlock)
{
if (_onCertificateChangeBlock (self, certificate))
@ -1713,9 +1700,29 @@ static NSArray<NSNumber*> *initialSyncSilentErrorsHTTPStatusCodes;
} andPersistentTokenDataHandler:^(void (^handler)(NSArray<MXCredentials *> *credentials, void (^completion)(BOOL didUpdateCredentials))) {
[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
{
if ([mxSession.roomSummaryUpdateDelegate isKindOfClass:MXKEventFormatter.class])

View file

@ -56,22 +56,11 @@ class NotificationService: UNNotificationServiceExtension {
guard let userAccount = userAccount else {
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)
}
restClient.refreshTokensFailedHandler = { mxError in
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)
}
}
}, unauthenticatedHandler: { error, completion in
userAccount.handleUnauthenticated(error, andCompletion: completion)
})
return restClient
}()
@ -191,6 +180,8 @@ class NotificationService: UNNotificationServiceExtension {
self.logMemory()
NotificationService.backgroundSyncService = MXBackgroundSyncService(withCredentials: userAccount.mxCredentials, persistTokenDataHandler: { persistTokenDataHandler in
MXKAccountManager.shared().readAndWriteCredentials(persistTokenDataHandler)
}, unauthenticatedHandler: { error, completion in
userAccount.handleUnauthenticated(error, andCompletion: completion)
})
MXLog.debug("[NotificationService] setup: MXBackgroundSyncService init: AFTER")
self.logMemory()

View file

@ -81,10 +81,11 @@
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).
MXSession *session = [[MXSession alloc] initWithMatrixRestClient:[[MXRestClient alloc] initWithCredentials:self.credentials andOnUnrecognizedCertificateBlock:nil andPersistentTokenDataHandler:^(void (^handler)(NSArray<MXCredentials *> *credentials, void (^completion)(BOOL didUpdateCredentials))) {
MXRestClient *mxRestClient = [[MXRestClient alloc] initWithCredentials:self.credentials andOnUnrecognizedCertificateBlock:nil andPersistentTokenDataHandler:^(void (^handler)(NSArray<MXCredentials *> *credentials, void (^completion)(BOOL didUpdateCredentials))) {
[[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)
{

View file

@ -78,9 +78,14 @@
- (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];
}]];
} andUnauthenticatedHandler:^(MXError *error, void (^completion)(void)) {
MXStrongifyAndReturnIfNil(self);
[self.userAccount handleUnauthenticated:error andCompletion:completion];
}];
MXSession *session = [[MXSession alloc] initWithMatrixRestClient:restClient];
[MXFileStore setPreloadOptions:0];
MXWeakify(session);