Skip to content

Commit

Permalink
Implement showAllRoomsInHomeSpace
Browse files Browse the repository at this point in the history
  • Loading branch information
ismailgulek committed Oct 19, 2021
1 parent d5387f7 commit 02e6af0
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 16 deletions.
5 changes: 5 additions & 0 deletions MatrixSDK/Data/MXRoomSummary.h
Expand Up @@ -340,6 +340,11 @@ FOUNDATION_EXPORT NSUInteger const MXRoomSummaryPaginationChunkSize;
*/
@property (nonatomic, readonly) MXRoomSummarySentStatus sentStatus;

/**
Parent space identifiers.
*/
@property (nonatomic) NSSet<NSString*> *parentSpaceIds;

/**
Mark all messages as read.
*/
Expand Down
13 changes: 13 additions & 0 deletions MatrixSDK/Data/MXRoomSummary.m
Expand Up @@ -195,6 +195,16 @@ - (void)setMembership:(MXMembership)membership
}
}

- (void)setParentSpaceIds:(NSSet<NSString *> *)parentSpaceIds
{
if (![_parentSpaceIds isEqualToSet:parentSpaceIds])
{
_parentSpaceIds = parentSpaceIds;

[self save:YES];
}
}

- (void)updateWith:(id<MXRoomSummaryProtocol>)summary
{
if (!summary)
Expand Down Expand Up @@ -227,6 +237,7 @@ - (void)updateWith:(id<MXRoomSummaryProtocol>)summary
_favoriteTagOrder = summary.favoriteTagOrder;
_dataTypes = summary.dataTypes;
_sentStatus = summary.sentStatus;
_parentSpaceIds = summary.parentSpaceIds;

if (!_others)
{
Expand Down Expand Up @@ -909,6 +920,7 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder
_joinRule = [aDecoder decodeObjectForKey:@"joinRule"];
_sentStatus = (MXRoomSummarySentStatus)[aDecoder decodeIntegerForKey:@"sentStatus"];
_favoriteTagOrder = [aDecoder decodeObjectForKey:@"favoriteTagOrder"];
_parentSpaceIds = [aDecoder decodeObjectForKey:@"parentSpaceIds"];

// Compute the trust if asked to do it automatically
// or maintain its computation it has been already calcutated
Expand Down Expand Up @@ -962,6 +974,7 @@ - (void)encodeWithCoder:(NSCoder *)aCoder
[aCoder encodeObject:_joinRule forKey:@"joinRule"];
[aCoder encodeInteger:_sentStatus forKey:@"sentStatus"];
[aCoder encodeObject:_favoriteTagOrder forKey:@"favoriteTagOrder"];
[aCoder encodeObject:_parentSpaceIds forKey:@"parentSpaceIds"];
}

- (NSString *)description
Expand Down
3 changes: 3 additions & 0 deletions MatrixSDK/Data/MXRoomSummaryProtocol.h
Expand Up @@ -129,6 +129,9 @@ NS_ASSUME_NONNULL_BEGIN
/// In case of suggested rooms we store the `MXSpaceChildInfo` instance for the room
@property (nonatomic, readonly) MXSpaceChildInfo * _Nullable spaceChildInfo;

/// Parent space identifiers of whom the room is a descendant
@property (nonatomic, readonly) NSSet<NSString*> *parentSpaceIds;

#pragma mark - Optional

@optional
Expand Down
50 changes: 37 additions & 13 deletions MatrixSDK/Data/RoomList/MXRoomListDataFilterOptions.swift
Expand Up @@ -66,6 +66,14 @@ public final class MXRoomListDataFilterOptions: NSObject {
}
}
}
/// Show all rooms when `space` is not provided. Related fetcher will be refreshed automatically when updated.
public var showAllRoomsInHomeSpace: Bool {
didSet {
if showAllRoomsInHomeSpace != oldValue {
refreshFetcher()
}
}
}
/// Flag to filter only suggested rooms, if set to `true`, `dataTypes` and `notDataTypes` are not valid.
public let onlySuggested: Bool

Expand All @@ -78,12 +86,14 @@ public final class MXRoomListDataFilterOptions: NSObject {
notDataTypes: MXRoomSummaryDataTypes = [.hidden, .conferenceUser, .space],
onlySuggested: Bool = false,
query: String? = nil,
space: MXSpace? = nil) {
space: MXSpace? = nil,
showAllRoomsInHomeSpace: Bool) {
self.dataTypes = dataTypes
self.notDataTypes = notDataTypes
self.onlySuggested = onlySuggested
self.query = query
self.space = space
self.showAllRoomsInHomeSpace = showAllRoomsInHomeSpace
super.init()
}

Expand All @@ -100,23 +110,13 @@ public final class MXRoomListDataFilterOptions: NSObject {
internal var predicate: NSPredicate? {
var subpredicates: [NSPredicate] = []

if let space = space {
// TODO: Block based predicates won't work for CoreData, find another way when time comes.
let subpredicate = NSPredicate { object, bindings in
guard let summary = object as? MXRoomSummaryProtocol else {
return false
}
return space.isRoomAChild(roomId: summary.roomId)
}
subpredicates.append(subpredicate)
}

if let query = query, !query.isEmpty {
let subpredicate1 = NSPredicate(format: "%K CONTAINS[cd] %@",
#keyPath(MXRoomSummaryProtocol.displayname), query)
let subpredicate2 = NSPredicate(format: "%K CONTAINS[cd] %@",
#keyPath(MXRoomSummaryProtocol.spaceChildInfo.displayName), query)
let subpredicate = NSCompoundPredicate(orPredicateWithSubpredicates: [subpredicate1, subpredicate2])
let subpredicate = NSCompoundPredicate(type: .or,
subpredicates: [subpredicate1, subpredicate2])
subpredicates.append(subpredicate)
}

Expand All @@ -132,6 +132,30 @@ public final class MXRoomListDataFilterOptions: NSObject {
#keyPath(MXRoomSummaryProtocol.dataTypes), notDataTypes.rawValue)
subpredicates.append(subpredicate)
}

if let space = space {
let subpredicate = NSPredicate(format: "%@ IN %K", space.spaceId,
#keyPath(MXRoomSummaryProtocol.parentSpaceIds))
subpredicates.append(subpredicate)
} else {
// home space
let subpredicate1 = NSPredicate(value: showAllRoomsInHomeSpace)

let directDataTypes: MXRoomSummaryDataTypes = .direct
let subpredicate2 = NSPredicate(format: "(%K & %d) != 0",
#keyPath(MXRoomSummaryProtocol.dataTypes), directDataTypes.rawValue)

let favoritedDataTypes: MXRoomSummaryDataTypes = .favorited
let subpredicate3 = NSPredicate(format: "(%K & %d) != 0",
#keyPath(MXRoomSummaryProtocol.dataTypes), favoritedDataTypes.rawValue)

let subpredicate4 = NSPredicate(format: "%K.@count == 0",
#keyPath(MXRoomSummaryProtocol.parentSpaceIds))

let subpredicate = NSCompoundPredicate(type: .or,
subpredicates: [subpredicate1, subpredicate2, subpredicate3, subpredicate4])
subpredicates.append(subpredicate)
}
}

guard !subpredicates.isEmpty else {
Expand Down
24 changes: 24 additions & 0 deletions MatrixSDK/MXSession.m
Expand Up @@ -235,6 +235,11 @@ - (id)initWithMatrixRestClient:(MXRestClient*)mxRestClient
nativeToVirtualRoomIds = [NSMutableDictionary dictionary];
asyncTaskQueue = [[MXAsyncTaskQueue alloc] initWithDispatchQueue:dispatch_get_main_queue() label:@"MXAsyncTaskQueue-MXSession"];
_spaceService = [[MXSpaceService alloc] initWithSession:self];
// add did build graph notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(spaceServiceDidBuildSpaceGraph:)
name:MXSpaceService.didBuildSpaceGraph
object:_spaceService];

[self setIdentityServer:mxRestClient.identityServer andAccessToken:mxRestClient.credentials.identityServerAccessToken];

Expand Down Expand Up @@ -1176,6 +1181,9 @@ - (void)close
}

// Clear spaces
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MXSpaceService.didBuildSpaceGraph
object:self.spaceService];
[self.spaceService close];

_myUser = nil;
Expand Down Expand Up @@ -4758,4 +4766,20 @@ - (NSString *)virtualRoomOf:(NSString *)nativeRoomId
return nativeToVirtualRoomIds[nativeRoomId];
}

#pragma mark - Spaces

- (void)spaceServiceDidBuildSpaceGraph:(NSNotification *)notification
{
if (!self.spaceService.isInitialised)
{
// may also be notified when the space service is closed
return;
}

[self.spaceService.ancestorsPerRoomId enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull roomId, NSSet<NSString *> * _Nonnull parentIds, BOOL * _Nonnull stop)
{
self->roomsSummaries[roomId].parentSpaceIds = parentIds;
}];
}

@end
1 change: 1 addition & 0 deletions MatrixSDK/Space/MXSpaceService.swift
Expand Up @@ -132,6 +132,7 @@ public class MXSpaceService: NSObject {
self.graph = MXSpaceGraphData()
self.notificationCounter.close()
self.isGraphBuilding = false
self.isInitialised = false
self.completionQueue.async {
NotificationCenter.default.post(name: MXSpaceService.didBuildSpaceGraph, object: self)
}
Expand Down
2 changes: 1 addition & 1 deletion MatrixSDKTests/MXRoomListDataManagerTests.swift
Expand Up @@ -35,7 +35,7 @@ class MXRoomListDataManagerTests: XCTestCase {
}

private var basicFetchOptions: MXRoomListDataFetchOptions {
let filterOptions = MXRoomListDataFilterOptions()
let filterOptions = MXRoomListDataFilterOptions(showAllRoomsInHomeSpace: false)
let sortOptions = MXRoomListDataSortOptions(missedNotificationsFirst: false, unreadMessagesFirst: false)
return MXRoomListDataFetchOptions(filterOptions: filterOptions,
sortOptions: sortOptions,
Expand Down
4 changes: 2 additions & 2 deletions MatrixSDKTests/MXRoomListDataManagerUnitTests.swift
Expand Up @@ -36,7 +36,7 @@ class MXRoomListDataManagerUnitTests: XCTestCase {
}

private var basicFetchOptions: MXRoomListDataFetchOptions {
let filterOptions = MXRoomListDataFilterOptions()
let filterOptions = MXRoomListDataFilterOptions(showAllRoomsInHomeSpace: false)
let sortOptions = MXRoomListDataSortOptions(missedNotificationsFirst: false, unreadMessagesFirst: false)
return MXRoomListDataFetchOptions(filterOptions: filterOptions,
sortOptions: sortOptions,
Expand All @@ -60,7 +60,7 @@ class MXRoomListDataManagerUnitTests: XCTestCase {
}

func testFilterOptionsInit() {
let filterOptions = MXRoomListDataFilterOptions()
let filterOptions = MXRoomListDataFilterOptions(showAllRoomsInHomeSpace: false)
XCTAssertTrue(filterOptions.dataTypes.isEmpty, "Default data types should be empty")
XCTAssertEqual(filterOptions.notDataTypes, [.hidden, .conferenceUser, .space], "Default not data types should be provided")
XCTAssertFalse(filterOptions.onlySuggested, "Default filter options should not include onlySuggested")
Expand Down
2 changes: 2 additions & 0 deletions MatrixSDKTests/Mocks/MockRoomSummary.swift
Expand Up @@ -79,6 +79,8 @@ internal class MockRoomSummary: NSObject, MXRoomSummaryProtocol {

var spaceChildInfo: MXSpaceChildInfo?

var parentSpaceIds: Set<String> = []

init(withRoomId roomId: String) {
self.roomId = roomId
super.init()
Expand Down

0 comments on commit 02e6af0

Please sign in to comment.