SP3.1: Update room settings for Spaces element-ios#5231

- Changed the Room Settings screen according to the new design
- Implemented the room access flow
- Added room upgrade support
- Implemented the room suggestion screen
This commit is contained in:
Gil Eluard
2022-01-13 15:53:45 +01:00
parent 069e79e24e
commit 086afb1835
78 changed files with 3755 additions and 196 deletions
@@ -0,0 +1,47 @@
//
// 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
class MatrixItemChooserRoomAncestorsDataSource: MatrixItemChooserDataSource {
private let roomId: String
var preselectedItemIds: Set<String>? { nil }
init(roomId: String) {
self.roomId = roomId
}
func sections(with session: MXSession, completion: @escaping (Result<[MatrixListItemSectionData], Error>) -> Void) {
let ancestorsId = session.spaceService.ancestorsPerRoomId[roomId] ?? []
completion(Result(catching: {
return [
MatrixListItemSectionData(title: VectorL10n.roomAccessSpaceChooserKnownSpacesSection(session.room(withRoomId: roomId)?.displayName ?? ""), infoText: nil, items: ancestorsId.compactMap { spaceId in
guard let space = session.spaceService.getSpace(withId: spaceId) else {
return nil
}
guard let room = space.room else {
return nil
}
return MatrixListItemData(mxRoom: room, spaceService: session.spaceService)
}
.sorted { $0.displayName ?? "" < $1.displayName ?? "" })
]
}))
}
}
@@ -0,0 +1,63 @@
//
// 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
enum MatrixItemChooserRoomDirectParentsDataSourcePreselectionMode {
case none
case suggestedRoom
}
class MatrixItemChooserRoomDirectParentsDataSource: MatrixItemChooserDataSource {
private let roomId: String
private let preselectionMode: MatrixItemChooserRoomDirectParentsDataSourcePreselectionMode
private(set) var preselectedItemIds: Set<String>?
init(roomId: String, preselectionMode: MatrixItemChooserRoomDirectParentsDataSourcePreselectionMode = .none) {
self.roomId = roomId
self.preselectionMode = preselectionMode
}
func sections(with session: MXSession, completion: @escaping (Result<[MatrixListItemSectionData], Error>) -> Void) {
let ancestorsId = session.spaceService.directParentIds(ofRoomWithId: roomId)
switch preselectionMode {
case .none:
preselectedItemIds = nil
case .suggestedRoom:
preselectedItemIds = session.spaceService.directParentIds(ofRoomWithId: roomId, isRoomSuggested: true)
}
completion(Result(catching: {
return [
MatrixListItemSectionData(title: VectorL10n.roomAccessSpaceChooserKnownSpacesSection(session.room(withRoomId: roomId)?.displayName ?? ""), infoText: nil, items: ancestorsId.compactMap { spaceId in
guard let space = session.spaceService.getSpace(withId: spaceId) else {
return nil
}
guard let room = space.room else {
return nil
}
return MatrixListItemData(mxRoom: room, spaceService: session.spaceService)
}
.sorted { $0.displayName ?? "" < $1.displayName ?? "" })
]
}))
}
}
@@ -0,0 +1,94 @@
//
// 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
class MatrixItemChooserRoomRestrictedAllowedParentsDataSource: MatrixItemChooserDataSource {
private let roomId: String
private var allowedParentIds: [String] = []
var preselectedItemIds: Set<String>? {
Set(allowedParentIds)
}
init(roomId: String) {
self.roomId = roomId
}
func sections(with session: MXSession, completion: @escaping (Result<[MatrixListItemSectionData], Error>) -> Void) {
guard let room = session.room(withRoomId: roomId) else {
return
}
room.state { [weak self] state in
guard let self = self else { return }
let joinRuleEvent = state?.stateEvents(with: .roomJoinRules)?.last
let allowContent: [[String:String]] = joinRuleEvent?.wireContent["allow"] as? [[String:String]] ?? []
self.allowedParentIds = allowContent.compactMap { allowDictionnary in
guard let type = allowDictionnary["type"], type == "m.room_membership" else {
return nil
}
return allowDictionnary["room_id"]
}
let ancestorsId = session.spaceService.ancestorsPerRoomId[self.roomId] ?? []
var sections = [
MatrixListItemSectionData(
title: VectorL10n.roomAccessSpaceChooserKnownSpacesSection(room.displayName ?? ""),
infoText: nil,
items: ancestorsId.compactMap { spaceId in
guard let space = session.spaceService.getSpace(withId: spaceId) else {
return nil
}
guard let room = space.room else {
return nil
}
return MatrixListItemData(mxRoom: room, spaceService: session.spaceService)
}.sorted { $0.displayName ?? "" < $1.displayName ?? "" }
)
]
var unknownParents = self.allowedParentIds
for roomId in ancestorsId {
if let index = unknownParents.firstIndex(of: roomId) {
unknownParents.remove(at: index)
}
}
if !unknownParents.isEmpty {
sections.append(MatrixListItemSectionData(
title: VectorL10n.roomAccessSpaceChooserOtherSpacesSection,
infoText: VectorL10n.roomAccessSpaceChooserOtherSpacesSectionInfo(room.displayName ?? ""),
items: unknownParents.compactMap({ roomId in
MatrixListItemData(
id: roomId,
type: .space,
avatar: AvatarInput(mxContentUri: roomId, matrixItemId: roomId, displayName: roomId),
displayName: roomId,
detailText: nil)
})
))
}
completion(Result(catching: {
return sections
}))
}
}
}
@@ -0,0 +1,35 @@
//
// 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
class MatrixItemChooserRoomsDataSource: MatrixItemChooserDataSource {
var preselectedItemIds: Set<String>? { nil }
func sections(with session: MXSession, completion: @escaping (Result<[MatrixListItemSectionData], Error>) -> Void) {
completion(Result(catching: {
[
MatrixListItemSectionData(title: nil, infoText: nil, items: session.rooms.compactMap { room in
if room.summary.roomType == .space || room.isDirect {
return nil
}
return MatrixListItemData(mxRoom: room, spaceService: session.spaceService)
})
]
}))
}
}
@@ -17,8 +17,14 @@
import Foundation
import Combine
protocol MatrixItemChooserDataSource {
func sections(with session: MXSession, completion: @escaping (Result<[MatrixListItemSectionData], Error>) -> Void)
var preselectedItemIds: Set<String>? { get }
}
protocol MatrixItemChooserProcessorProtocol {
var dataType: MatrixItemChooserType { get }
var loadingText: String? { get }
var dataSource: MatrixItemChooserDataSource { get }
func computeSelection(withIds itemsIds:[String], completion: @escaping (Result<Void, Error>) -> Void)
func isItemIncluded(_ item: (MatrixListItemData)) -> Bool
}
@@ -30,56 +36,60 @@ class MatrixItemChooserService: MatrixItemChooserServiceProtocol {
// MARK: Private
private let processingQueue = DispatchQueue(label: "org.matrix.element.MatrixItemChooserService.processingQueue")
private let processingQueue = DispatchQueue(label: "io.matrix.element.MatrixItemChooserService.processingQueue")
private let completionQueue = DispatchQueue.main
private let session: MXSession
private let items: [MatrixListItemData]
private var filteredItems: [MatrixListItemData] {
private var sections: [MatrixListItemSectionData] = []
private var filteredSections: [MatrixListItemSectionData] = [] {
didSet {
itemsSubject.send(filteredItems)
sectionsSubject.send(filteredSections)
}
}
private var selectedItemIds: Set<String>
private let itemsProcessor: MatrixItemChooserProcessorProtocol?
private let itemsProcessor: MatrixItemChooserProcessorProtocol
// MARK: Public
private(set) var type: MatrixItemChooserType
private(set) var itemsSubject: CurrentValueSubject<[MatrixListItemData], Never>
private(set) var sectionsSubject: CurrentValueSubject<[MatrixListItemSectionData], Never>
private(set) var selectedItemIdsSubject: CurrentValueSubject<Set<String>, Never>
var searchText: String = "" {
didSet {
refresh()
}
}
var loadingText: String? {
itemsProcessor.loadingText
}
// MARK: - Setup
init(session: MXSession, selectedItemIds: [String], itemsProcessor: MatrixItemChooserProcessorProtocol?) {
init(session: MXSession, selectedItemIds: [String], itemsProcessor: MatrixItemChooserProcessorProtocol) {
self.session = session
self.type = itemsProcessor?.dataType ?? .room
switch type {
case .people:
self.items = session.users().map { user in
MatrixListItemData(mxUser: user)
}
case .room:
self.items = session.rooms.compactMap { room in
if room.summary.roomType == .space || room.isDirect {
return nil
}
return MatrixListItemData(mxRoom: room, spaceService: session.spaceService)
}
}
self.itemsSubject = CurrentValueSubject(self.items)
self.filteredItems = []
self.sectionsSubject = CurrentValueSubject(self.sections)
self.selectedItemIds = Set(selectedItemIds)
self.selectedItemIdsSubject = CurrentValueSubject(self.selectedItemIds)
self.itemsProcessor = itemsProcessor
itemsProcessor.dataSource.sections(with: session) { [weak self] result in
guard let self = self else { return }
switch result {
case .success(let sections):
self.sections = sections
self.refresh()
if let preselectedItemIds = itemsProcessor.dataSource.preselectedItemIds {
for itemId in preselectedItemIds {
self.selectedItemIds.insert(itemId)
}
self.selectedItemIdsSubject.send(self.selectedItemIds)
}
case .failure(let error):
break
}
}
refresh()
}
@@ -95,82 +105,40 @@ class MatrixItemChooserService: MatrixItemChooserServiceProtocol {
}
func processSelection(completion: @escaping (Result<Void, Error>) -> Void) {
guard let selectionProcessor = self.itemsProcessor else {
completion(Result.success(()))
return
}
selectionProcessor.computeSelection(withIds: Array(selectedItemIds), completion: completion)
itemsProcessor.computeSelection(withIds: Array(selectedItemIds), completion: completion)
}
func refresh() {
self.processingQueue.async { [weak self] in
guard let self = self else { return }
let filteredItems = self.filter(items: self.items)
let filteredSections = self.filter(sections: self.sections)
self.completionQueue.async {
self.filteredItems = filteredItems
self.filteredSections = filteredSections
}
}
}
// MARK: - Private
private func filter(items: [MatrixListItemData]) -> [MatrixListItemData] {
if searchText.isEmpty {
if let selectionProcessor = self.itemsProcessor {
return items.filter {
selectionProcessor.isItemIncluded($0)
private func filter(sections: [MatrixListItemSectionData]) -> [MatrixListItemSectionData] {
var newSections: [MatrixListItemSectionData] = []
for section in sections {
let items: [MatrixListItemData]
if searchText.isEmpty {
items = section.items.filter {
itemsProcessor.isItemIncluded($0)
}
} else {
return items
}
} else {
let lowercasedSearchText = self.searchText.lowercased()
if let selectionProcessor = self.itemsProcessor {
return items.filter {
selectionProcessor.isItemIncluded($0) && ($0.id.lowercased().contains(lowercasedSearchText) || ($0.displayName ?? "").lowercased().contains(lowercasedSearchText))
}
} else {
return items.filter {
$0.id.lowercased().contains(lowercasedSearchText) || ($0.displayName ?? "").lowercased().contains(lowercasedSearchText)
let lowercasedSearchText = self.searchText.lowercased()
items = section.items.filter {
itemsProcessor.isItemIncluded($0) && ($0.id.lowercased().contains(lowercasedSearchText) || ($0.displayName ?? "").lowercased().contains(lowercasedSearchText))
}
}
newSections.append(MatrixListItemSectionData(title: section.title, infoText: section.infoText, items: items))
}
return newSections
}
}
fileprivate extension MatrixListItemData {
init(mxUser: MXUser) {
self.init(id: mxUser.userId, avatar: mxUser.avatarData, displayName: mxUser.displayname, detailText: mxUser.userId)
}
init(mxRoom: MXRoom, spaceService: MXSpaceService) {
let parentSapceIds = mxRoom.summary.parentSpaceIds ?? Set()
let detailText: String?
if parentSapceIds.isEmpty {
detailText = nil
} else {
if let spaceName = spaceService.getSpace(withId: parentSapceIds.first ?? "")?.summary?.displayname {
let count = parentSapceIds.count - 1
switch count {
case 0:
detailText = VectorL10n.spacesCreationInSpacename(spaceName)
case 1:
detailText = VectorL10n.spacesCreationInSpacenamePlusOne(spaceName)
default:
detailText = VectorL10n.spacesCreationInSpacenamePlusMany(spaceName, "\(count)")
}
} else {
if parentSapceIds.count > 1 {
detailText = VectorL10n.spacesCreationInManySpaces("\(parentSapceIds.count)")
} else {
detailText = VectorL10n.spacesCreationInOneSpace
}
}
}
self.init(id: mxRoom.roomId, avatar: mxRoom.avatarData, displayName: mxRoom.summary.displayname, detailText: detailText)
}
}
@@ -0,0 +1,31 @@
//
// 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
class MatrixItemChooserUsersDataSource: MatrixItemChooserDataSource {
var preselectedItemIds: Set<String>? { nil }
func sections(with session: MXSession, completion: @escaping (Result<[MatrixListItemSectionData], Error>) -> Void) {
completion(Result(catching: {
[
MatrixListItemSectionData(title: nil, infoText: nil, items: session.users().map { user in
MatrixListItemData(mxUser: user)
})
]
}))
}
}
@@ -0,0 +1,58 @@
//
// 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
extension MatrixListItemData {
init(mxUser: MXUser) {
self.init(id: mxUser.userId, type: .user, avatar: mxUser.avatarData, displayName: mxUser.displayname, detailText: mxUser.userId)
}
init(mxRoom: MXRoom, spaceService: MXSpaceService) {
let parentSapceIds = mxRoom.summary.parentSpaceIds ?? Set()
let detailText: String?
if parentSapceIds.isEmpty {
detailText = nil
} else {
if let spaceName = spaceService.getSpace(withId: parentSapceIds.first ?? "")?.summary?.displayname {
let count = parentSapceIds.count - 1
switch count {
case 0:
detailText = VectorL10n.spacesCreationInSpacename(spaceName)
case 1:
detailText = VectorL10n.spacesCreationInSpacenamePlusOne(spaceName)
default:
detailText = VectorL10n.spacesCreationInSpacenamePlusMany(spaceName, "\(count)")
}
} else {
if parentSapceIds.count > 1 {
detailText = VectorL10n.spacesCreationInManySpaces("\(parentSapceIds.count)")
} else {
detailText = VectorL10n.spacesCreationInOneSpace
}
}
}
let type: MatrixListItemDataType
if let summary = mxRoom.summary, summary.roomType == .space {
type = .space
} else {
type = .room
}
self.init(id: mxRoom.roomId, type: type, avatar: mxRoom.avatarData, displayName: mxRoom.summary.displayname, detailText: detailText)
}
}