diff --git a/Riot/Modules/Room/RoomViewController.m b/Riot/Modules/Room/RoomViewController.m index ac1ab9931..71b2e8107 100644 --- a/Riot/Modules/Room/RoomViewController.m +++ b/Riot/Modules/Room/RoomViewController.m @@ -4577,7 +4577,8 @@ static CGSize kThreadListBarButtonItemImageSize; { ForwardingShareItemSender *shareItemSender = [[ForwardingShareItemSender alloc] initWithEvent:selectedEvent]; self.shareManager = [[ShareManager alloc] initWithShareItemSender:shareItemSender - type:ShareManagerTypeForward]; + type:ShareManagerTypeForward + session:self.mainSession]; MXWeakify(self); [self.shareManager setCompletionCallback:^(ShareManagerResult result) { diff --git a/RiotShareExtension/Shared/ForwardingShareItemSender.swift b/RiotShareExtension/Shared/ForwardingShareItemSender.swift index 3909812db..e75b6b3fd 100644 --- a/RiotShareExtension/Shared/ForwardingShareItemSender.swift +++ b/RiotShareExtension/Shared/ForwardingShareItemSender.swift @@ -56,6 +56,7 @@ class ForwardingShareItemSender: NSObject, ShareItemSenderProtocol { case .failure(let innerError): errors.append(innerError) default: + room.summary.resetLastMessage(nil, failure: nil, commit: false) break } diff --git a/RiotShareExtension/Shared/ShareManager.h b/RiotShareExtension/Shared/ShareManager.h index c2b110a05..5b7edb90f 100644 --- a/RiotShareExtension/Shared/ShareManager.h +++ b/RiotShareExtension/Shared/ShareManager.h @@ -36,7 +36,9 @@ typedef NS_ENUM(NSUInteger, ShareManagerResult) { @property (nonatomic, copy) void (^completionCallback)(ShareManagerResult); - (instancetype)initWithShareItemSender:(id)itemSender - type:(ShareManagerType)type; + type:(ShareManagerType)type + session:(nullable MXSession*)session; + - (UIViewController *)mainViewController; diff --git a/RiotShareExtension/Shared/ShareManager.m b/RiotShareExtension/Shared/ShareManager.m index 0e1c74cf7..7e2b0172e 100644 --- a/RiotShareExtension/Shared/ShareManager.m +++ b/RiotShareExtension/Shared/ShareManager.m @@ -31,6 +31,8 @@ @property (nonatomic, strong, readonly) ShareViewController *shareViewController; +@property (nonatomic) BOOL useCustomSession; +@property (nonatomic, strong) MXSession* session; @property (nonatomic, strong) MXKAccount *userAccount; @property (nonatomic, strong) MXFileStore *fileStore; @@ -51,11 +53,14 @@ static MXSession *fakeSession; - (instancetype)initWithShareItemSender:(id)itemSender type:(ShareManagerType)type + session:(MXSession*)session { if (self = [super init]) { _shareItemSender = itemSender; _shareItemSender.delegate = self; + _session = session; + _useCustomSession = _session == nil; [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(checkUserAccount) name:kMXKAccountManagerDidRemoveAccountNotification object:nil]; [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(checkUserAccount) name:NSExtensionHostWillEnterForegroundNotification object:nil]; @@ -71,7 +76,20 @@ static MXSession *fakeSession; [NSBundle mxk_setLanguage:language]; [NSBundle mxk_setFallbackLanguage:@"en"]; - [self checkUserAccount]; + if (!_useCustomSession) + { + // If we don't use a custom session, we can initialize the shareViewController with our existing session + self.userAccount = [MXKAccountManager sharedManager].activeAccounts.firstObject; + ShareDataSource *roomDataSource = [[ShareDataSource alloc] initWithFileStore:_session.store + session:_session]; + + [self.shareViewController configureWithState:ShareViewControllerAccountStateConfigured + roomDataSource:roomDataSource]; + } + else + { + [self checkUserAccount]; + } } return self; @@ -95,32 +113,23 @@ static MXSession *fakeSession; MXStrongifyAndReturnIfNil(self); [self.userAccount handleUnauthenticatedWithError:error isSoftLogout:isSoftLogout isRefreshTokenAuth:isRefreshTokenAuth andCompletion:completion]; }]; - MXSession *session = [[MXSession alloc] initWithMatrixRestClient:restClient]; - [MXFileStore setPreloadOptions:0]; - - MXWeakify(session); - [session setStore:self.fileStore success:^{ - MXStrongifyAndReturnIfNil(session); - - self.selectedRooms = [NSMutableArray array]; - for (NSString *roomIdentifier in roomIdentifiers) { - MXRoom *room = [MXRoom loadRoomFromStore:self.fileStore withRoomId:roomIdentifier matrixSession:session]; - if (room) { - [self.selectedRooms addObject:room]; - } - } - - [self.shareItemSender sendItemsToRooms:self.selectedRooms success:^{ - self.selectedRooms = nil; - self.completionCallback(ShareManagerResultFinished); - } failure:^(NSArray *errors) { - self.selectedRooms = nil; - [self showFailureAlert:[VectorL10n roomEventFailedToSend]]; + if (self.useCustomSession || !self.session) + { + MXSession* session = [[MXSession alloc] initWithMatrixRestClient:restClient]; + [MXFileStore setPreloadOptions:0]; + + MXWeakify(session); + [session setStore:self.fileStore success:^{ + MXStrongifyAndReturnIfNil(session); + [self shareForRoomIdentifiers:roomIdentifiers usingSession:session]; + } failure:^(NSError *error) { + MXLogError(@"[ShareManager] Failed preparing matrix session"); }]; - - } failure:^(NSError *error) { - MXLogError(@"[ShareManager] Failed preparing matrix session"); - }]; + } + else + { + [self shareForRoomIdentifiers:roomIdentifiers usingSession:self.session]; + } } - (void)shareViewControllerDidRequestDismissal:(ShareViewController *)shareViewController @@ -142,6 +151,25 @@ static MXSession *fakeSession; #pragma mark - Private +- (void)shareForRoomIdentifiers:(NSSet *)roomIdentifiers usingSession:(MXSession*)session +{ + self.selectedRooms = [NSMutableArray array]; + for (NSString *roomIdentifier in roomIdentifiers) { + MXRoom *room = [MXRoom loadRoomFromStore:session.store withRoomId:roomIdentifier matrixSession:session]; + if (room) { + [self.selectedRooms addObject:room]; + } + } + + [self.shareItemSender sendItemsToRooms:self.selectedRooms success:^{ + self.selectedRooms = nil; + self.completionCallback(ShareManagerResultFinished); + } failure:^(NSArray *errors) { + self.selectedRooms = nil; + [self showFailureAlert:[VectorL10n roomEventFailedToSend]]; + }]; +} + - (void)showFailureAlert:(NSString *)title { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert]; diff --git a/changelog.d/7641.bugfix b/changelog.d/7641.bugfix new file mode 100644 index 000000000..99af6e75a --- /dev/null +++ b/changelog.d/7641.bugfix @@ -0,0 +1 @@ +Fix an issue where rooms were not correctly sorted after forwarding a message.