Skip to content

Commit 9328f2b

Browse files
committed
Add NSUserActivity to rooms to appear in spotlight search
Signed-off-by: Finn Behrens <me@kloenk.dev>
1 parent f339db2 commit 9328f2b

File tree

6 files changed

+125
-1
lines changed

6 files changed

+125
-1
lines changed

Riot/Generated/InfoPlist.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ internal enum InfoPlist {
3535
internal static let nsMicrophoneUsageDescription: String = _document["NSMicrophoneUsageDescription"]
3636
internal static let nsPhotoLibraryUsageDescription: String = _document["NSPhotoLibraryUsageDescription"]
3737
internal static let nsSiriUsageDescription: String = _document["NSSiriUsageDescription"]
38+
internal static let nsUserActivityTypes: [String] = _document["NSUserActivityTypes"]
3839
internal static let uiBackgroundModes: [String] = _document["UIBackgroundModes"]
3940
internal static let uiLaunchStoryboardName: String = _document["UILaunchStoryboardName"]
4041
internal static let uiRequiredDeviceCapabilities: [String] = _document["UIRequiredDeviceCapabilities"]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// Copyright 2021 New Vector Ltd
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
#ifndef UserActivities_h
18+
#define UserActivities_h
19+
20+
#import <Foundation/Foundation.h>
21+
22+
/**
23+
NSUserActivity types for rooms
24+
*/
25+
FOUNDATION_EXPORT NSString *const kUserActivityTypeMatrixRoom;
26+
27+
/**
28+
UserInfo field for the room id
29+
*/
30+
FOUNDATION_EXPORT NSString *const kUserActivityInfoRoomId;
31+
32+
/**
33+
UserInfo field for the user id
34+
*/
35+
FOUNDATION_EXPORT NSString *const kUserActivityInfoUserId;
36+
37+
#endif /* UserActivities_h */
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// Copyright 2021 New Vector Ltd
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
#import "UserActivities.h"
18+
19+
NSString *const kUserActivityTypeMatrixRoom = @"org.matrix.room";
20+
NSString *const kUserActivityInfoRoomId = @"roomID";
21+
NSString *const kUserActivityInfoUserId = @"userID";

Riot/Modules/Application/LegacyAppDelegate.m

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
#import "Riot-Swift.h"
5959
#import "PushNotificationService.h"
6060

61+
#import "UserActivities.h"
62+
6163
//#define MX_CALL_STACK_OPENWEBRTC
6264
#ifdef MX_CALL_STACK_OPENWEBRTC
6365
#import <MatrixOpenWebRTCWrapper/MatrixOpenWebRTCWrapper.h>
@@ -748,13 +750,22 @@ - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserAct
748750
{
749751
continueUserActivity = [self handleUniversalLink:userActivity];
750752
}
753+
else if ([userActivity.activityType isEqualToString:kUserActivityTypeMatrixRoom])
754+
{
755+
NSString *roomID = userActivity.userInfo[kUserActivityInfoRoomId];
756+
if (!roomID)
757+
return continueUserActivity;
758+
759+
[self navigateToRoomById:roomID];
760+
continueUserActivity = YES;
761+
}
751762
else if ([userActivity.activityType isEqualToString:INStartAudioCallIntentIdentifier] ||
752763
[userActivity.activityType isEqualToString:INStartVideoCallIntentIdentifier])
753764
{
754765
INInteraction *interaction = userActivity.interaction;
755766

756767
// roomID provided by Siri intent
757-
NSString *roomID = userActivity.userInfo[@"roomID"];
768+
NSString *roomID = userActivity.userInfo[kUserActivityInfoRoomId];
758769

759770
// We've launched from calls history list
760771
if (!roomID)

Riot/Modules/Room/RoomViewController.m

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
limitations under the License.
1717
*/
1818

19+
@import CoreSpotlight;
20+
1921
#import "RoomViewController.h"
2022

2123
#import "RoomDataSource.h"
@@ -128,6 +130,8 @@
128130

129131
#import "MXSDKOptions.h"
130132

133+
#import "UserActivities.h"
134+
131135
#import "Riot-Swift.h"
132136

133137
NSNotificationName const RoomCallTileTappedNotification = @"RoomCallTileTappedNotification";
@@ -588,6 +592,8 @@ - (void)viewWillAppear:(BOOL)animated
588592
notificationTaskProfile = [MXSDKOptions.sharedInstance.profiler startMeasuringTaskWithName:AnalyticsNoficationsTimeToDisplayContent
589593
category:AnalyticsNoficationsCategory];
590594
}
595+
596+
[self becomeCurrentActivity];
591597
}
592598

593599
- (void)viewWillDisappear:(BOOL)animated
@@ -1991,6 +1997,50 @@ - (void)setupActions {
19911997
roomInputView.actionsBar.actionItems = actionItems;
19921998
}
19931999

2000+
- (void)becomeCurrentActivity
2001+
{
2002+
if (!self.userActivity) {
2003+
self.userActivity = [[NSUserActivity alloc] initWithActivityType:kUserActivityTypeMatrixRoom];
2004+
}
2005+
2006+
self.userActivity.title = self.roomDataSource.room.summary.displayname;
2007+
self.userActivity.requiredUserInfoKeys = [[NSSet alloc] initWithObjects:kUserActivityInfoRoomId, nil];
2008+
2009+
// user info
2010+
NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
2011+
[userInfo setObject:self.roomDataSource.roomId forKey:kUserActivityInfoRoomId];
2012+
if ([self.roomDataSource.room isDirect]) {
2013+
[userInfo setObject:self.roomDataSource.room.directUserId forKey:kUserActivityInfoUserId];
2014+
}
2015+
self.userActivity.userInfo = userInfo;
2016+
2017+
// TODO: add a NSUserActivityDelegate to save the current text in the userinfo of the activity
2018+
// self.userActivity.delegate = self;
2019+
// self.userActivity.needsSave = true;
2020+
self.userActivity.persistentIdentifier = self.roomDataSource.roomId;
2021+
2022+
self.userActivity.eligibleForHandoff = true;
2023+
self.userActivity.eligibleForSearch = true;
2024+
self.userActivity.eligibleForPrediction = true;
2025+
2026+
CSSearchableItemAttributeSet *contentAttribute;
2027+
if (@available(iOS 14.0, *)) {
2028+
contentAttribute = [[CSSearchableItemAttributeSet alloc] initWithContentType:UTTypeItem];
2029+
} else {
2030+
contentAttribute = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"public.item"];
2031+
}
2032+
2033+
contentAttribute.title = self.roomDataSource.room.summary.displayname;
2034+
contentAttribute.displayName = self.roomDataSource.room.summary.displayname;
2035+
contentAttribute.contentDescription = self.roomDataSource.room.summary.lastMessage.text;
2036+
2037+
// TODO: contentAttribute.thumbnailURL =
2038+
// TODO: accountHandles of everyone in the room
2039+
contentAttribute.instantMessageAddresses = [[NSArray alloc] initWithObjects:self.roomDataSource.roomId, nil];
2040+
2041+
self.userActivity.contentAttributeSet = contentAttribute;
2042+
}
2043+
19942044
- (void)roomInputToolbarViewPresentStickerPicker
19952045
{
19962046
// Search for the sticker picker widget in the user account

Riot/SupportingFiles/Info.plist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@
6565
<string>The photo library is used to send photos and videos.</string>
6666
<key>NSSiriUsageDescription</key>
6767
<string>Siri is used to perform calls even from the lock screen.</string>
68+
<key>NSUserActivityTypes</key>
69+
<array>
70+
<string>org.matrix.room</string>
71+
</array>
6872
<key>UIBackgroundModes</key>
6973
<array>
7074
<string>audio</string>

0 commit comments

Comments
 (0)