mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-23 18:12:44 +02:00
+110
@@ -0,0 +1,110 @@
|
||||
//
|
||||
// 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 Combine
|
||||
|
||||
@available(iOS 14.0, *)
|
||||
class SpaceCreationMatrixItemChooserService: SpaceCreationMatrixItemChooserServiceProtocol {
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
// MARK: Private
|
||||
|
||||
private let processingQueue = DispatchQueue(label: "org.matrix.element.SpaceCreationMatrixItemChooserService.processingQueue")
|
||||
private let completionQueue = DispatchQueue.main
|
||||
|
||||
private let session: MXSession
|
||||
private let items: [SpaceCreationMatrixItem]
|
||||
private var filteredItems: [SpaceCreationMatrixItem] {
|
||||
didSet {
|
||||
itemsSubject.send(filteredItems)
|
||||
}
|
||||
}
|
||||
private var selectedItemIds: Set<String>
|
||||
|
||||
// MARK: Public
|
||||
|
||||
private(set) var type: SpaceCreationMatrixItemType
|
||||
private(set) var itemsSubject: CurrentValueSubject<[SpaceCreationMatrixItem], Never>
|
||||
private(set) var selectedItemIdsSubject: CurrentValueSubject<Set<String>, Never>
|
||||
var searchText: String = "" {
|
||||
didSet {
|
||||
if searchText.isEmpty {
|
||||
filteredItems = items
|
||||
} else {
|
||||
self.processingQueue.async {
|
||||
let lowercasedSearchText = self.searchText.lowercased()
|
||||
let filteredItems = self.items.filter { $0.id.lowercased().contains(lowercasedSearchText) || ($0.displayName ?? "").lowercased().contains(lowercasedSearchText) }
|
||||
|
||||
self.completionQueue.async {
|
||||
self.filteredItems = filteredItems
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Setup
|
||||
|
||||
init(session: MXSession, type: SpaceCreationMatrixItemType, selectedItemIds: [String]) {
|
||||
self.session = session
|
||||
self.type = type
|
||||
switch type {
|
||||
case .people:
|
||||
self.items = session.users().map { user in
|
||||
SpaceCreationMatrixItem(mxUser: user)
|
||||
}
|
||||
case .room:
|
||||
self.items = session.rooms.compactMap { room in
|
||||
if room.summary.roomType == .space || room.isDirect {
|
||||
return nil
|
||||
}
|
||||
|
||||
return SpaceCreationMatrixItem(mxRoom: room)
|
||||
}
|
||||
}
|
||||
self.itemsSubject = CurrentValueSubject(self.items)
|
||||
self.filteredItems = self.items
|
||||
|
||||
self.selectedItemIds = Set(selectedItemIds)
|
||||
self.selectedItemIdsSubject = CurrentValueSubject(self.selectedItemIds)
|
||||
}
|
||||
|
||||
// MARK: - Public
|
||||
|
||||
func reverseSelectionForItem(withId itemId: String) {
|
||||
if selectedItemIds.contains(itemId) {
|
||||
selectedItemIds.remove(itemId)
|
||||
} else {
|
||||
selectedItemIds.insert(itemId)
|
||||
}
|
||||
selectedItemIdsSubject.send(selectedItemIds)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fileprivate extension SpaceCreationMatrixItem {
|
||||
|
||||
init(mxUser: MXUser) {
|
||||
self.init(id: mxUser.userId, avatar: mxUser.avatarData, displayName: mxUser.displayname, detailText: mxUser.userId)
|
||||
}
|
||||
|
||||
init(mxRoom: MXRoom) {
|
||||
self.init(id: mxRoom.roomId, avatar: mxRoom.avatarData, displayName: mxRoom.summary.displayname, detailText: mxRoom.summary.roomId)
|
||||
}
|
||||
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
// File created from SimpleUserProfileExample
|
||||
// $ createScreen.sh Spaces/SpaceCreation/SpaceCreationMatrixItemChooser SpaceCreationMatrixItemChooser
|
||||
//
|
||||
// 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 SwiftUI
|
||||
|
||||
/// Using an enum for the screen allows you define the different state cases with
|
||||
/// the relevant associated data for each case.
|
||||
@available(iOS 14.0, *)
|
||||
enum MockSpaceCreationMatrixItemChooserScreenState: MockScreenState, CaseIterable {
|
||||
// A case for each state you want to represent
|
||||
// with specific, minimal associated data that will allow you
|
||||
// mock that screen.
|
||||
case noItems
|
||||
case items
|
||||
case selectedItems
|
||||
|
||||
/// The associated screen
|
||||
var screenType: Any.Type {
|
||||
SpaceCreationMatrixItem.self
|
||||
}
|
||||
|
||||
/// Generate the view struct for the screen state.
|
||||
var screenView: ([Any], AnyView) {
|
||||
let service: MockSpaceCreationMatrixItemChooserService
|
||||
switch self {
|
||||
case .noItems:
|
||||
service = MockSpaceCreationMatrixItemChooserService(type: .room, items: [])
|
||||
case .items:
|
||||
service = MockSpaceCreationMatrixItemChooserService()
|
||||
case .selectedItems:
|
||||
service = MockSpaceCreationMatrixItemChooserService(type: .room, items: MockSpaceCreationMatrixItemChooserService.mockItems, selectedItemIndexes: [0, 2])
|
||||
}
|
||||
let viewModel = SpaceCreationMatrixItemChooserViewModel.makeSpaceCreationMatrixItemChooserViewModel(spaceCreationMatrixItemChooserService: service, creationParams: SpaceCreationParameters())
|
||||
|
||||
// can simulate service and viewModel actions here if needs be.
|
||||
|
||||
return (
|
||||
[service, viewModel],
|
||||
AnyView(SpaceCreationMatrixItemChooser(viewModel: viewModel.context)
|
||||
.addDependency(MockAvatarService.example))
|
||||
)
|
||||
}
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
// File created from SimpleUserProfileExample
|
||||
// $ createScreen.sh Spaces/SpaceCreation/SpaceCreationMatrixItemChooser SpaceCreationMatrixItemChooser
|
||||
//
|
||||
// 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 Combine
|
||||
|
||||
@available(iOS 14.0, *)
|
||||
class MockSpaceCreationMatrixItemChooserService: SpaceCreationMatrixItemChooserServiceProtocol {
|
||||
|
||||
static let mockItems = [
|
||||
SpaceCreationMatrixItem(id: "!aaabaa:matrix.org", avatar: MockAvatarInput.example, displayName: "Matrix Discussion", detailText: "Descripton of this room"),
|
||||
SpaceCreationMatrixItem(id: "!zzasds:matrix.org", avatar: MockAvatarInput.example, displayName: "Element Mobile", detailText: "Descripton of this room"),
|
||||
SpaceCreationMatrixItem(id: "!scthve:matrix.org", avatar: MockAvatarInput.example, displayName: "Alice Personal", detailText: "Descripton of this room")
|
||||
]
|
||||
var itemsSubject: CurrentValueSubject<[SpaceCreationMatrixItem], Never>
|
||||
var selectedItemIdsSubject: CurrentValueSubject<Set<String>, Never>
|
||||
var searchText: String = ""
|
||||
var type: SpaceCreationMatrixItemType = .room
|
||||
var selectedItemIds: Set<String> = Set()
|
||||
|
||||
init(type: SpaceCreationMatrixItemType = .room, items: [SpaceCreationMatrixItem] = mockItems, selectedItemIndexes: [Int] = []) {
|
||||
itemsSubject = CurrentValueSubject(items)
|
||||
var selectedItemIds = Set<String>()
|
||||
for index in selectedItemIndexes {
|
||||
if index >= items.count {
|
||||
continue
|
||||
}
|
||||
|
||||
selectedItemIds.insert(items[index].id)
|
||||
}
|
||||
selectedItemIdsSubject = CurrentValueSubject(selectedItemIds)
|
||||
self.selectedItemIds = selectedItemIds
|
||||
}
|
||||
|
||||
func simulateSelectionForItem(at index: Int) {
|
||||
guard index < itemsSubject.value.count else {
|
||||
return
|
||||
}
|
||||
|
||||
reverseSelectionForItem(withId: itemsSubject.value[index].id)
|
||||
}
|
||||
|
||||
func reverseSelectionForItem(withId itemId: String) {
|
||||
if selectedItemIds.contains(itemId) {
|
||||
selectedItemIds.remove(itemId)
|
||||
} else {
|
||||
selectedItemIds.insert(itemId)
|
||||
}
|
||||
selectedItemIdsSubject.send(selectedItemIds)
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
// File created from SimpleUserProfileExample
|
||||
// $ createScreen.sh Spaces/SpaceCreation/SpaceCreationMatrixItemChooser SpaceCreationMatrixItemChooser
|
||||
//
|
||||
// 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 Combine
|
||||
|
||||
@available(iOS 14.0, *)
|
||||
protocol SpaceCreationMatrixItemChooserServiceProtocol {
|
||||
var type: SpaceCreationMatrixItemType { get }
|
||||
var itemsSubject: CurrentValueSubject<[SpaceCreationMatrixItem], Never> { get }
|
||||
var selectedItemIdsSubject: CurrentValueSubject<Set<String>, Never> { get }
|
||||
var searchText: String { get set }
|
||||
|
||||
func reverseSelectionForItem(withId itemId: String)
|
||||
}
|
||||
Reference in New Issue
Block a user