Skip to content

Commit

Permalink
move NSUserActitivy update code into UserActivityService
Browse files Browse the repository at this point in the history
Signed-off-by: Finn Behrens <me@kloenk.dev>
  • Loading branch information
kloenk committed Sep 27, 2021
1 parent c5db1b8 commit f5e88e8
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 103 deletions.
37 changes: 0 additions & 37 deletions Riot/Managers/Activities/UserActivities.h

This file was deleted.

21 changes: 0 additions & 21 deletions Riot/Managers/Activities/UserActivities.m

This file was deleted.

52 changes: 50 additions & 2 deletions Riot/Managers/Activities/UserActivityService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,28 @@
//

import Foundation
import CoreSpotlight
import MatrixSDK

@objcMembers
class UserActivityService: NSObject {

// MARK: - Constants
/// The NSUserActivity type for rooms
public static let roomActivityType = "org.matrix.room"

// TODO: Move constants in here from UserActivities.m
/// Keys to use inside the userInfo dict
public enum UserInfoKeys: String, RawRepresentable {
/// Key for a room id usually the internal id prefix with `!`
case roomId = "roomID"

/// Key to store a user id
case userId = "userID"
}

// objc reexport
public static let userInfoKeyRoomId = UserInfoKeys.roomId.rawValue
public static let userInfoKeyUserId = UserInfoKeys.userId.rawValue

// MARK: - Properties

Expand All @@ -39,12 +53,46 @@ class UserActivityService: NSObject {

// MARK: - Public

func update(_ activity: NSUserActivity, from room: MXRoom) {
func update(_ userActivity: NSUserActivity, from room: MXRoom) {
// TODO: Convert objc code into here.
userActivity.title = room.summary.displayname

userActivity.requiredUserInfoKeys = [ UserInfoKeys.roomId.rawValue ]
var userInfo = [UserInfoKeys: Any]()
userInfo[UserInfoKeys.roomId] = room.roomId
if room.isDirect {
userInfo[UserInfoKeys.userId] = 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
// 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: { })
}
}
8 changes: 3 additions & 5 deletions Riot/Modules/Application/LegacyAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@
#import "Riot-Swift.h"
#import "PushNotificationService.h"

#import "UserActivities.h"

//#define MX_CALL_STACK_OPENWEBRTC
#ifdef MX_CALL_STACK_OPENWEBRTC
#import <MatrixOpenWebRTCWrapper/MatrixOpenWebRTCWrapper.h>
Expand Down Expand Up @@ -751,9 +749,9 @@ - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserAct
{
continueUserActivity = [self handleUniversalLink:userActivity];
}
else if ([userActivity.activityType isEqualToString:kUserActivityTypeMatrixRoom])
else if ([userActivity.activityType isEqualToString:[UserActivityService roomActivityType]])
{
NSString *roomID = userActivity.userInfo[kUserActivityInfoRoomId];
NSString *roomID = userActivity.userInfo[[UserActivityService userInfoKeyRoomId]];
if (!roomID)
return continueUserActivity;

Expand All @@ -766,7 +764,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[[UserActivityService userInfoKeyRoomId]];

// We've launched from calls history list
if (!roomID)
Expand Down
41 changes: 3 additions & 38 deletions Riot/Modules/Room/RoomViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@

#import "MXSDKOptions.h"

#import "UserActivities.h"

#import "Riot-Swift.h"

NSNotificationName const RoomCallTileTappedNotification = @"RoomCallTileTappedNotification";
Expand Down Expand Up @@ -595,7 +593,7 @@ - (void)viewWillAppear:(BOOL)animated
category:AnalyticsNoficationsCategory];
}

[self becomeCurrentActivity];
[self updateUserActivity];
}

- (void)viewWillDisappear:(BOOL)animated
Expand Down Expand Up @@ -2001,51 +1999,18 @@ - (void)setupActions {
roomInputView.actionsBar.actionItems = actionItems;
}

- (void)becomeCurrentActivity
- (void)updateUserActivity
{
if (!self.userActivity) {
self.userActivity = [[NSUserActivity alloc] initWithActivityType:kUserActivityTypeMatrixRoom];
self.userActivity = [[NSUserActivity alloc] initWithActivityType:[UserActivityService roomActivityType]];
}

// 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
Expand Down

0 comments on commit f5e88e8

Please sign in to comment.