From 7823ec24494ccf193205b5b866a86aae29ebcf09 Mon Sep 17 00:00:00 2001 From: Finn Behrens Date: Sat, 18 Sep 2021 16:23:04 +0200 Subject: [PATCH 1/5] Add NSUserActivity to rooms to appear in spotlight search Signed-off-by: Finn Behrens --- Riot/Managers/Activities/UserActivities.h | 37 +++++++++++++++ Riot/Managers/Activities/UserActivities.m | 21 +++++++++ Riot/Modules/Application/LegacyAppDelegate.m | 13 +++++- Riot/Modules/Room/RoomViewController.m | 49 ++++++++++++++++++++ Riot/SupportingFiles/Info.plist | 4 ++ changelog.d/4865.feature | 1 + 6 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 Riot/Managers/Activities/UserActivities.h create mode 100644 Riot/Managers/Activities/UserActivities.m create mode 100644 changelog.d/4865.feature diff --git a/Riot/Managers/Activities/UserActivities.h b/Riot/Managers/Activities/UserActivities.h new file mode 100644 index 0000000000..f283af7e1f --- /dev/null +++ b/Riot/Managers/Activities/UserActivities.h @@ -0,0 +1,37 @@ +// +// Copyright 2021 New Vector Ltd +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#ifndef UserActivities_h +#define UserActivities_h + +#import + +/** + NSUserActivity types for rooms + */ +FOUNDATION_EXPORT NSString *const kUserActivityTypeMatrixRoom; + +/** + UserInfo field for the room id + */ +FOUNDATION_EXPORT NSString *const kUserActivityInfoRoomId; + +/** + UserInfo field for the user id + */ +FOUNDATION_EXPORT NSString *const kUserActivityInfoUserId; + +#endif /* UserActivities_h */ diff --git a/Riot/Managers/Activities/UserActivities.m b/Riot/Managers/Activities/UserActivities.m new file mode 100644 index 0000000000..1c86f8b5d1 --- /dev/null +++ b/Riot/Managers/Activities/UserActivities.m @@ -0,0 +1,21 @@ +// +// Copyright 2021 New Vector Ltd +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#import "UserActivities.h" + +NSString *const kUserActivityTypeMatrixRoom = @"org.matrix.room"; +NSString *const kUserActivityInfoRoomId = @"roomID"; +NSString *const kUserActivityInfoUserId = @"userID"; diff --git a/Riot/Modules/Application/LegacyAppDelegate.m b/Riot/Modules/Application/LegacyAppDelegate.m index 6b9816643b..7a0ac769aa 100644 --- a/Riot/Modules/Application/LegacyAppDelegate.m +++ b/Riot/Modules/Application/LegacyAppDelegate.m @@ -58,6 +58,8 @@ #import "Riot-Swift.h" #import "PushNotificationService.h" +#import "UserActivities.h" + //#define MX_CALL_STACK_OPENWEBRTC #ifdef MX_CALL_STACK_OPENWEBRTC #import @@ -745,13 +747,22 @@ - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserAct { continueUserActivity = [self handleUniversalLink:userActivity]; } + else if ([userActivity.activityType isEqualToString:kUserActivityTypeMatrixRoom]) + { + NSString *roomID = userActivity.userInfo[kUserActivityInfoRoomId]; + if (!roomID) + return continueUserActivity; + + [self navigateToRoomById:roomID]; + continueUserActivity = YES; + } else if ([userActivity.activityType isEqualToString:INStartAudioCallIntentIdentifier] || [userActivity.activityType isEqualToString:INStartVideoCallIntentIdentifier]) { INInteraction *interaction = userActivity.interaction; // roomID provided by Siri intent - NSString *roomID = userActivity.userInfo[@"roomID"]; + NSString *roomID = userActivity.userInfo[kUserActivityInfoRoomId]; // We've launched from calls history list if (!roomID) diff --git a/Riot/Modules/Room/RoomViewController.m b/Riot/Modules/Room/RoomViewController.m index 6d989d669b..dfda898f10 100644 --- a/Riot/Modules/Room/RoomViewController.m +++ b/Riot/Modules/Room/RoomViewController.m @@ -17,6 +17,7 @@ */ @import MobileCoreServices; +@import CoreSpotlight; #import "RoomViewController.h" @@ -131,6 +132,8 @@ #import "MXSDKOptions.h" +#import "UserActivities.h" + #import "Riot-Swift.h" NSNotificationName const RoomCallTileTappedNotification = @"RoomCallTileTappedNotification"; @@ -610,6 +613,8 @@ - (void)viewWillAppear:(BOOL)animated notificationTaskProfile = [MXSDKOptions.sharedInstance.profiler startMeasuringTaskWithName:AnalyticsNoficationsTimeToDisplayContent category:AnalyticsNoficationsCategory]; } + + [self becomeCurrentActivity]; } - (void)viewWillDisappear:(BOOL)animated @@ -2030,6 +2035,50 @@ - (void)setupActions { roomInputView.actionsBar.actionItems = actionItems; } +- (void)becomeCurrentActivity +{ + if (!self.userActivity) { + self.userActivity = [[NSUserActivity alloc] initWithActivityType:kUserActivityTypeMatrixRoom]; + } + + self.userActivity.title = self.roomDataSource.room.summary.displayname; + self.userActivity.requiredUserInfoKeys = [[NSSet alloc] initWithObjects:kUserActivityInfoRoomId, nil]; + + // user info + NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init]; + [userInfo setObject:self.roomDataSource.roomId forKey:kUserActivityInfoRoomId]; + if ([self.roomDataSource.room isDirect]) { + [userInfo setObject:self.roomDataSource.room.directUserId forKey:kUserActivityInfoUserId]; + } + self.userActivity.userInfo = userInfo; + + // TODO: add a NSUserActivityDelegate to save the current text in the userinfo of the activity + // self.userActivity.delegate = self; + // self.userActivity.needsSave = true; + self.userActivity.persistentIdentifier = self.roomDataSource.roomId; + + self.userActivity.eligibleForHandoff = true; + self.userActivity.eligibleForSearch = true; + self.userActivity.eligibleForPrediction = true; + + CSSearchableItemAttributeSet *contentAttribute; + if (@available(iOS 14.0, *)) { + contentAttribute = [[CSSearchableItemAttributeSet alloc] initWithContentType:UTTypeItem]; + } else { + contentAttribute = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"public.item"]; + } + + contentAttribute.title = self.roomDataSource.room.summary.displayname; + contentAttribute.displayName = self.roomDataSource.room.summary.displayname; + contentAttribute.contentDescription = self.roomDataSource.room.summary.lastMessage.text; + + // TODO: contentAttribute.thumbnailURL = + // TODO: accountHandles of everyone in the room + contentAttribute.instantMessageAddresses = [[NSArray alloc] initWithObjects:self.roomDataSource.roomId, nil]; + + self.userActivity.contentAttributeSet = contentAttribute; +} + - (void)roomInputToolbarViewPresentStickerPicker { // Search for the sticker picker widget in the user account diff --git a/Riot/SupportingFiles/Info.plist b/Riot/SupportingFiles/Info.plist index 5bcda43e69..7accfad9e3 100644 --- a/Riot/SupportingFiles/Info.plist +++ b/Riot/SupportingFiles/Info.plist @@ -65,6 +65,10 @@ The photo library is used to send photos and videos. NSSiriUsageDescription Siri is used to perform calls even from the lock screen. + NSUserActivityTypes + + org.matrix.room + UIBackgroundModes audio diff --git a/changelog.d/4865.feature b/changelog.d/4865.feature new file mode 100644 index 0000000000..cac334709c --- /dev/null +++ b/changelog.d/4865.feature @@ -0,0 +1 @@ +Add NSUserActivity to rooms to appear in spotlight search From 3685d80d9746cfbaf57eda9b50fb004e8062f02c Mon Sep 17 00:00:00 2001 From: Doug Date: Fri, 24 Sep 2021 17:52:48 +0100 Subject: [PATCH 2/5] Add skeleton UserActivityService. --- .../Activities/UserActivityService.swift | 50 +++++++++++++++++++ Riot/Modules/Room/RoomViewController.m | 3 ++ 2 files changed, 53 insertions(+) create mode 100644 Riot/Managers/Activities/UserActivityService.swift diff --git a/Riot/Managers/Activities/UserActivityService.swift b/Riot/Managers/Activities/UserActivityService.swift new file mode 100644 index 0000000000..f90443d77d --- /dev/null +++ b/Riot/Managers/Activities/UserActivityService.swift @@ -0,0 +1,50 @@ +// +// Copyright 2021 New Vector Ltd +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +import Foundation +import MatrixSDK + +@objcMembers +class UserActivityService: NSObject { + + // MARK: - Constants + + // TODO: Move constants in here from UserActivities.m + + // MARK: - Properties + + #warning("This is initialised lazily so currently only observes left rooms if RoomViewController has been presented.") + static let shared = UserActivityService() + + // MARK: - Setup + + private override init() { + super.init() + + NotificationCenter.default.addObserver(self, selector: #selector(didLeaveRoom(_:)), name: .mxSessionDidLeaveRoom, object: nil) + } + + // MARK: - Public + + func update(_ activity: NSUserActivity, from room: MXRoom) { + // TODO: Convert objc code into here. + } + + func didLeaveRoom(_ notification: Notification) { + guard let roomId = notification.userInfo?[kMXSessionNotificationRoomIdKey] as? String else { return } + // TODO: Remove the room from spotlight + } +} diff --git a/Riot/Modules/Room/RoomViewController.m b/Riot/Modules/Room/RoomViewController.m index dfda898f10..b647d37ec4 100644 --- a/Riot/Modules/Room/RoomViewController.m +++ b/Riot/Modules/Room/RoomViewController.m @@ -2041,6 +2041,9 @@ - (void)becomeCurrentActivity self.userActivity = [[NSUserActivity alloc] initWithActivityType:kUserActivityTypeMatrixRoom]; } + // TODO: Move everything else into the method called below + [UserActivityService.shared update:self.userActivity from:self.roomDataSource.room]; + self.userActivity.title = self.roomDataSource.room.summary.displayname; self.userActivity.requiredUserInfoKeys = [[NSSet alloc] initWithObjects:kUserActivityInfoRoomId, nil]; From 61079efea478825d8266d6cacaf77f06fcbbdc3a Mon Sep 17 00:00:00 2001 From: Finn Behrens Date: Fri, 24 Sep 2021 19:57:05 +0200 Subject: [PATCH 3/5] move NSUserActitivy update code into UserActivityService Signed-off-by: Finn Behrens --- Riot/Managers/Activities/UserActivities.m | 21 --------- ...UserActivities.h => UserActivityService.h} | 26 +++++------ .../Activities/UserActivityService.swift | 45 ++++++++++++++++--- Riot/Modules/Application/LegacyAppDelegate.m | 10 ++--- Riot/Modules/Room/RoomViewController.m | 43 ++---------------- Riot/SupportingFiles/Riot-Bridging-Header.h | 1 + 6 files changed, 58 insertions(+), 88 deletions(-) delete mode 100644 Riot/Managers/Activities/UserActivities.m rename Riot/Managers/Activities/{UserActivities.h => UserActivityService.h} (51%) diff --git a/Riot/Managers/Activities/UserActivities.m b/Riot/Managers/Activities/UserActivities.m deleted file mode 100644 index 1c86f8b5d1..0000000000 --- a/Riot/Managers/Activities/UserActivities.m +++ /dev/null @@ -1,21 +0,0 @@ -// -// Copyright 2021 New Vector Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -#import "UserActivities.h" - -NSString *const kUserActivityTypeMatrixRoom = @"org.matrix.room"; -NSString *const kUserActivityInfoRoomId = @"roomID"; -NSString *const kUserActivityInfoUserId = @"userID"; diff --git a/Riot/Managers/Activities/UserActivities.h b/Riot/Managers/Activities/UserActivityService.h similarity index 51% rename from Riot/Managers/Activities/UserActivities.h rename to Riot/Managers/Activities/UserActivityService.h index f283af7e1f..c480e966d6 100644 --- a/Riot/Managers/Activities/UserActivities.h +++ b/Riot/Managers/Activities/UserActivityService.h @@ -14,24 +14,18 @@ // limitations under the License. // -#ifndef UserActivities_h -#define UserActivities_h +#ifndef UserActivityService_h +#define UserActivityService_h -#import +/// MXUserActivityTypes identifes user activities +typedef NSString *const UserActivityType NS_TYPED_EXTENSIBLE_ENUM; -/** - NSUserActivity types for rooms - */ -FOUNDATION_EXPORT NSString *const kUserActivityTypeMatrixRoom; +static UserActivityType const MXUserActivityTypeRoom = @"org.matrix.room"; -/** - UserInfo field for the room id - */ -FOUNDATION_EXPORT NSString *const kUserActivityInfoRoomId; +/// MXUserActivityFields identifies fields in the userInfo of a UserActivity +typedef NSString *const UserActivityField NS_TYPED_EXTENSIBLE_ENUM; -/** - UserInfo field for the user id - */ -FOUNDATION_EXPORT NSString *const kUserActivityInfoUserId; +static UserActivityField const UserActivityFieldRoom = @"roomID"; +static UserActivityField const UserActivityFieldUser = @"userID"; -#endif /* UserActivities_h */ +#endif /* UserActivityService_h */ diff --git a/Riot/Managers/Activities/UserActivityService.swift b/Riot/Managers/Activities/UserActivityService.swift index f90443d77d..b40cb43a0b 100644 --- a/Riot/Managers/Activities/UserActivityService.swift +++ b/Riot/Managers/Activities/UserActivityService.swift @@ -15,15 +15,12 @@ // import Foundation +import CoreSpotlight import MatrixSDK @objcMembers class UserActivityService: NSObject { - // MARK: - Constants - - // TODO: Move constants in here from UserActivities.m - // MARK: - Properties #warning("This is initialised lazily so currently only observes left rooms if RoomViewController has been presented.") @@ -39,12 +36,46 @@ class UserActivityService: NSObject { // MARK: - Public - func update(_ activity: NSUserActivity, from room: MXRoom) { - // TODO: Convert objc code into here. + func update(_ userActivity: NSUserActivity, from room: MXRoom) { + userActivity.title = room.summary.displayname + + userActivity.requiredUserInfoKeys = [ UserActivityField.room.rawValue ] + var userInfo = [String: Any]() + userInfo[UserActivityField.room.rawValue] = room.roomId + if room.isDirect { + userInfo[UserActivityField.user.rawValue] = room.directUserId + } + userActivity.userInfo = userInfo + + // TODO: if we add more userActivities, a `org.matrix.room` prefix should probably be added + userActivity.persistentIdentifier = room.roomId + + userActivity.isEligibleForHandoff = true + userActivity.isEligibleForSearch = true + userActivity.isEligibleForPrediction = true + + var contentAttributes: CSSearchableItemAttributeSet + if #available(iOS 14.0, *) { + contentAttributes = CSSearchableItemAttributeSet(contentType: UTType.item) + } else { + contentAttributes = CSSearchableItemAttributeSet(itemContentType: "public.item") + } + + contentAttributes.title = room.summary.displayname + contentAttributes.displayName = room.summary.displayname + contentAttributes.contentDescription = room.summary.lastMessage.text + // TODO: contentAttributes.thumbnailURL + contentAttributes.domainIdentifier = room.roomId + contentAttributes.relatedUniqueIdentifier = room.summary.lastMessage.eventId + // TODO: contentAttributes.weakRelatedUniqueIdentifier (is this needed? does it break anything else?) + contentAttributes.instantMessageAddresses = [ room.roomId ] + + userActivity.contentAttributeSet = contentAttributes } func didLeaveRoom(_ notification: Notification) { guard let roomId = notification.userInfo?[kMXSessionNotificationRoomIdKey] as? String else { return } - // TODO: Remove the room from spotlight + NSUserActivity.deleteSavedUserActivities(withPersistentIdentifiers: [roomId], completionHandler: { }) + CSSearchableIndex.default().deleteSearchableItems(withDomainIdentifiers: [roomId], completionHandler: nil) } } diff --git a/Riot/Modules/Application/LegacyAppDelegate.m b/Riot/Modules/Application/LegacyAppDelegate.m index 7a0ac769aa..447140f184 100644 --- a/Riot/Modules/Application/LegacyAppDelegate.m +++ b/Riot/Modules/Application/LegacyAppDelegate.m @@ -55,11 +55,11 @@ #import "MXSession+Riot.h" #import "MXRoom+Riot.h" +#import "UserActivityService.h" + #import "Riot-Swift.h" #import "PushNotificationService.h" -#import "UserActivities.h" - //#define MX_CALL_STACK_OPENWEBRTC #ifdef MX_CALL_STACK_OPENWEBRTC #import @@ -747,9 +747,9 @@ - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserAct { continueUserActivity = [self handleUniversalLink:userActivity]; } - else if ([userActivity.activityType isEqualToString:kUserActivityTypeMatrixRoom]) + else if ([userActivity.activityType isEqualToString:MXUserActivityTypeRoom]) { - NSString *roomID = userActivity.userInfo[kUserActivityInfoRoomId]; + NSString *roomID = userActivity.userInfo[UserActivityFieldRoom]; if (!roomID) return continueUserActivity; @@ -762,7 +762,7 @@ - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserAct INInteraction *interaction = userActivity.interaction; // roomID provided by Siri intent - NSString *roomID = userActivity.userInfo[kUserActivityInfoRoomId]; + NSString *roomID = userActivity.userInfo[UserActivityFieldRoom]; // We've launched from calls history list if (!roomID) diff --git a/Riot/Modules/Room/RoomViewController.m b/Riot/Modules/Room/RoomViewController.m index b647d37ec4..dd25d113a2 100644 --- a/Riot/Modules/Room/RoomViewController.m +++ b/Riot/Modules/Room/RoomViewController.m @@ -129,11 +129,10 @@ #import "SecurityViewController.h" #import "TypingUserInfo.h" +#import "UserActivityService.h" #import "MXSDKOptions.h" -#import "UserActivities.h" - #import "Riot-Swift.h" NSNotificationName const RoomCallTileTappedNotification = @"RoomCallTileTappedNotification"; @@ -614,7 +613,7 @@ - (void)viewWillAppear:(BOOL)animated category:AnalyticsNoficationsCategory]; } - [self becomeCurrentActivity]; + [self updateUserActivity]; } - (void)viewWillDisappear:(BOOL)animated @@ -2035,51 +2034,17 @@ - (void)setupActions { roomInputView.actionsBar.actionItems = actionItems; } -- (void)becomeCurrentActivity +- (void)updateUserActivity { if (!self.userActivity) { - self.userActivity = [[NSUserActivity alloc] initWithActivityType:kUserActivityTypeMatrixRoom]; + self.userActivity = [[NSUserActivity alloc] initWithActivityType:MXUserActivityTypeRoom]; } - // TODO: Move everything else into the method called below [UserActivityService.shared update:self.userActivity from:self.roomDataSource.room]; - self.userActivity.title = self.roomDataSource.room.summary.displayname; - self.userActivity.requiredUserInfoKeys = [[NSSet alloc] initWithObjects:kUserActivityInfoRoomId, nil]; - - // user info - NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init]; - [userInfo setObject:self.roomDataSource.roomId forKey:kUserActivityInfoRoomId]; - if ([self.roomDataSource.room isDirect]) { - [userInfo setObject:self.roomDataSource.room.directUserId forKey:kUserActivityInfoUserId]; - } - self.userActivity.userInfo = userInfo; - // TODO: add a NSUserActivityDelegate to save the current text in the userinfo of the activity // self.userActivity.delegate = self; // self.userActivity.needsSave = true; - self.userActivity.persistentIdentifier = self.roomDataSource.roomId; - - self.userActivity.eligibleForHandoff = true; - self.userActivity.eligibleForSearch = true; - self.userActivity.eligibleForPrediction = true; - - CSSearchableItemAttributeSet *contentAttribute; - if (@available(iOS 14.0, *)) { - contentAttribute = [[CSSearchableItemAttributeSet alloc] initWithContentType:UTTypeItem]; - } else { - contentAttribute = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"public.item"]; - } - - contentAttribute.title = self.roomDataSource.room.summary.displayname; - contentAttribute.displayName = self.roomDataSource.room.summary.displayname; - contentAttribute.contentDescription = self.roomDataSource.room.summary.lastMessage.text; - - // TODO: contentAttribute.thumbnailURL = - // TODO: accountHandles of everyone in the room - contentAttribute.instantMessageAddresses = [[NSArray alloc] initWithObjects:self.roomDataSource.roomId, nil]; - - self.userActivity.contentAttributeSet = contentAttribute; } - (void)roomInputToolbarViewPresentStickerPicker diff --git a/Riot/SupportingFiles/Riot-Bridging-Header.h b/Riot/SupportingFiles/Riot-Bridging-Header.h index 68dff29865..669dfdda7c 100644 --- a/Riot/SupportingFiles/Riot-Bridging-Header.h +++ b/Riot/SupportingFiles/Riot-Bridging-Header.h @@ -45,3 +45,4 @@ #import "GroupDetailsViewController.h" #import "RoomInputToolbarView.h" #import "NSArray+Element.h" +#import "UserActivityService.h" From 448ed1a4e69b5150e371a660ab20f4f12f221744 Mon Sep 17 00:00:00 2001 From: Finn Behrens Date: Tue, 28 Sep 2021 10:16:08 +0200 Subject: [PATCH 4/5] Delete all NSUserActivities when account is logging out Signed-off-by: Finn Behrens --- Riot/Managers/Activities/UserActivityService.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Riot/Managers/Activities/UserActivityService.swift b/Riot/Managers/Activities/UserActivityService.swift index b40cb43a0b..773845b9bc 100644 --- a/Riot/Managers/Activities/UserActivityService.swift +++ b/Riot/Managers/Activities/UserActivityService.swift @@ -32,6 +32,7 @@ class UserActivityService: NSObject { super.init() NotificationCenter.default.addObserver(self, selector: #selector(didLeaveRoom(_:)), name: .mxSessionDidLeaveRoom, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(didLogOut(_:)), name: .mxkAccountManagerDidRemoveAccount, object: nil) } // MARK: - Public @@ -78,4 +79,9 @@ class UserActivityService: NSObject { NSUserActivity.deleteSavedUserActivities(withPersistentIdentifiers: [roomId], completionHandler: { }) CSSearchableIndex.default().deleteSearchableItems(withDomainIdentifiers: [roomId], completionHandler: nil) } + + func didLogOut(_ notification: Notification) { + NSUserActivity.deleteAllSavedUserActivities(completionHandler: { }) + CSSearchableIndex.default().deleteAllSearchableItems(completionHandler: nil) + } } From a971424f5c6db9a636fc6acf1ee9c5886f18b46a Mon Sep 17 00:00:00 2001 From: Finn Behrens Date: Wed, 29 Sep 2021 09:31:19 +0200 Subject: [PATCH 5/5] UserActivityService: make sure roomID exists Signed-off-by: Finn Behrens --- .../Activities/UserActivityService.swift | 27 ++++--------------- Riot/Modules/Application/AppCoordinator.swift | 3 +++ Riot/Modules/Application/LegacyAppDelegate.m | 7 ++++- 3 files changed, 14 insertions(+), 23 deletions(-) diff --git a/Riot/Managers/Activities/UserActivityService.swift b/Riot/Managers/Activities/UserActivityService.swift index 773845b9bc..96d195fd39 100644 --- a/Riot/Managers/Activities/UserActivityService.swift +++ b/Riot/Managers/Activities/UserActivityService.swift @@ -15,7 +15,6 @@ // import Foundation -import CoreSpotlight import MatrixSDK @objcMembers @@ -23,7 +22,6 @@ class UserActivityService: NSObject { // MARK: - Properties - #warning("This is initialised lazily so currently only observes left rooms if RoomViewController has been presented.") static let shared = UserActivityService() // MARK: - Setup @@ -38,50 +36,35 @@ class UserActivityService: NSObject { // MARK: - Public func update(_ userActivity: NSUserActivity, from room: MXRoom) { + guard let roomId = room.roomId else { + return + } userActivity.title = room.summary.displayname userActivity.requiredUserInfoKeys = [ UserActivityField.room.rawValue ] var userInfo = [String: Any]() - userInfo[UserActivityField.room.rawValue] = room.roomId + userInfo[UserActivityField.room.rawValue] = roomId if room.isDirect { userInfo[UserActivityField.user.rawValue] = room.directUserId } userActivity.userInfo = userInfo // TODO: if we add more userActivities, a `org.matrix.room` prefix should probably be added - userActivity.persistentIdentifier = room.roomId + userActivity.persistentIdentifier = roomId userActivity.isEligibleForHandoff = true userActivity.isEligibleForSearch = true userActivity.isEligibleForPrediction = true - var contentAttributes: CSSearchableItemAttributeSet - if #available(iOS 14.0, *) { - contentAttributes = CSSearchableItemAttributeSet(contentType: UTType.item) - } else { - contentAttributes = CSSearchableItemAttributeSet(itemContentType: "public.item") - } - - contentAttributes.title = room.summary.displayname - contentAttributes.displayName = room.summary.displayname - contentAttributes.contentDescription = room.summary.lastMessage.text - // TODO: contentAttributes.thumbnailURL - contentAttributes.domainIdentifier = room.roomId - contentAttributes.relatedUniqueIdentifier = room.summary.lastMessage.eventId - // TODO: contentAttributes.weakRelatedUniqueIdentifier (is this needed? does it break anything else?) - contentAttributes.instantMessageAddresses = [ room.roomId ] - userActivity.contentAttributeSet = contentAttributes } func didLeaveRoom(_ notification: Notification) { guard let roomId = notification.userInfo?[kMXSessionNotificationRoomIdKey] as? String else { return } NSUserActivity.deleteSavedUserActivities(withPersistentIdentifiers: [roomId], completionHandler: { }) - CSSearchableIndex.default().deleteSearchableItems(withDomainIdentifiers: [roomId], completionHandler: nil) } func didLogOut(_ notification: Notification) { NSUserActivity.deleteAllSavedUserActivities(completionHandler: { }) - CSSearchableIndex.default().deleteAllSearchableItems(completionHandler: nil) } } diff --git a/Riot/Modules/Application/AppCoordinator.swift b/Riot/Modules/Application/AppCoordinator.swift index 32121a0f6b..80d313b325 100755 --- a/Riot/Modules/Application/AppCoordinator.swift +++ b/Riot/Modules/Application/AppCoordinator.swift @@ -79,6 +79,9 @@ final class AppCoordinator: NSObject, AppCoordinatorType { func start() { self.setupLogger() self.setupTheme() + + // Make sure the UserActivityService is loaded + let _ = UserActivityService.shared // Setup navigation router store _ = NavigationRouterStore.shared diff --git a/Riot/Modules/Application/LegacyAppDelegate.m b/Riot/Modules/Application/LegacyAppDelegate.m index 447140f184..264f993508 100644 --- a/Riot/Modules/Application/LegacyAppDelegate.m +++ b/Riot/Modules/Application/LegacyAppDelegate.m @@ -2790,6 +2790,11 @@ - (void)selectMatrixAccount:(void (^)(MXKAccount *selectedAccount))onSelection #pragma mark - Matrix Rooms handling - (void)navigateToRoomById:(NSString *)roomId +{ + [self navigateToRoomById:roomId andEventId:nil]; +} + +- (void)navigateToRoomById:(NSString *)roomId andEventId:(NSString *)eventId { if (roomId.length) { @@ -2823,7 +2828,7 @@ - (void)navigateToRoomById:(NSString *)roomId { MXLogDebug(@"[AppDelegate][Push] navigateToRoomById: open the roomViewController %@", roomId); - [self showRoom:roomId andEventId:nil withMatrixSession:dedicatedAccount.mxSession]; + [self showRoom:roomId andEventId:eventId withMatrixSession:dedicatedAccount.mxSession]; } else {