mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 21:33:26 +00:00
Flescio/create room action (#925)
* fix invites users screen * add create room action * add a task runner with timeout, add fetch room summary after creation * rename Runner, add Unit Test, separate UserProfile form client proxy * change to all room summary publisher
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// Copyright 2023 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 ExpiringTaskRunnerError: Error {
|
||||
case timeout
|
||||
}
|
||||
|
||||
actor ExpiringTaskRunner<T> {
|
||||
private var continuation: CheckedContinuation<T, Error>?
|
||||
|
||||
private var task: () async throws -> T
|
||||
|
||||
init(_ task: @escaping () async throws -> T) {
|
||||
self.task = task
|
||||
}
|
||||
|
||||
func run(timeout: Duration) async throws -> T {
|
||||
try await withCheckedThrowingContinuation {
|
||||
continuation = $0
|
||||
|
||||
Task {
|
||||
try await Task.sleep(for: timeout)
|
||||
continuation?.resume(with: .failure(ExpiringTaskRunnerError.timeout))
|
||||
continuation = nil
|
||||
}
|
||||
|
||||
Task {
|
||||
do {
|
||||
let result = try await task()
|
||||
continuation?.resume(with: .success(result))
|
||||
} catch {
|
||||
continuation?.resume(with: .failure(error))
|
||||
}
|
||||
continuation = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,12 +19,13 @@ import SwiftUI
|
||||
|
||||
struct CreateRoomCoordinatorParameters {
|
||||
let userSession: UserSessionProtocol
|
||||
weak var userIndicatorController: UserIndicatorControllerProtocol?
|
||||
let createRoomParameters: CurrentValuePublisher<CreateRoomFlowParameters, Never>
|
||||
let selectedUsers: CurrentValuePublisher<[UserProfile], Never>
|
||||
}
|
||||
|
||||
enum CreateRoomCoordinatorAction {
|
||||
case createRoom
|
||||
case openRoom(withIdentifier: String)
|
||||
case deselectUser(UserProfile)
|
||||
case updateDetails(CreateRoomFlowParameters)
|
||||
}
|
||||
@@ -42,6 +43,7 @@ final class CreateRoomCoordinator: CoordinatorProtocol {
|
||||
init(parameters: CreateRoomCoordinatorParameters) {
|
||||
self.parameters = parameters
|
||||
viewModel = CreateRoomViewModel(userSession: parameters.userSession,
|
||||
userIndicatorController: parameters.userIndicatorController,
|
||||
createRoomParameters: parameters.createRoomParameters,
|
||||
selectedUsers: parameters.selectedUsers)
|
||||
}
|
||||
@@ -52,8 +54,8 @@ final class CreateRoomCoordinator: CoordinatorProtocol {
|
||||
switch action {
|
||||
case .deselectUser(let user):
|
||||
self.actionsSubject.send(.deselectUser(user))
|
||||
case .createRoom:
|
||||
self.actionsSubject.send(.createRoom)
|
||||
case .openRoom(let identifier):
|
||||
self.actionsSubject.send(.openRoom(withIdentifier: identifier))
|
||||
case .updateDetails(let details):
|
||||
self.actionsSubject.send(.updateDetails(details))
|
||||
}
|
||||
|
||||
@@ -16,8 +16,13 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
enum CreateRoomScreenErrorType: Error {
|
||||
case failedCreatingRoom
|
||||
case unknown
|
||||
}
|
||||
|
||||
enum CreateRoomViewModelAction {
|
||||
case createRoom
|
||||
case openRoom(withIdentifier: String)
|
||||
case deselectUser(UserProfile)
|
||||
case updateDetails(CreateRoomFlowParameters)
|
||||
}
|
||||
@@ -35,6 +40,9 @@ struct CreateRoomViewStateBindings {
|
||||
var roomName: String
|
||||
var roomTopic: String
|
||||
var isRoomPrivate: Bool
|
||||
|
||||
/// Information describing the currently displayed alert.
|
||||
var alertInfo: AlertInfo<CreateRoomScreenErrorType>?
|
||||
}
|
||||
|
||||
enum CreateRoomViewAction {
|
||||
|
||||
@@ -20,17 +20,22 @@ import SwiftUI
|
||||
typealias CreateRoomViewModelType = StateStoreViewModel<CreateRoomViewState, CreateRoomViewAction>
|
||||
|
||||
class CreateRoomViewModel: CreateRoomViewModelType, CreateRoomViewModelProtocol {
|
||||
private let userSession: UserSessionProtocol
|
||||
private var actionsSubject: PassthroughSubject<CreateRoomViewModelAction, Never> = .init()
|
||||
private var createRoomParameters: CreateRoomFlowParameters
|
||||
|
||||
private weak var userIndicatorController: UserIndicatorControllerProtocol?
|
||||
|
||||
var actions: AnyPublisher<CreateRoomViewModelAction, Never> {
|
||||
actionsSubject.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
init(userSession: UserSessionProtocol,
|
||||
userIndicatorController: UserIndicatorControllerProtocol?,
|
||||
createRoomParameters: CurrentValuePublisher<CreateRoomFlowParameters, Never>,
|
||||
selectedUsers: CurrentValuePublisher<[UserProfile], Never>) {
|
||||
let parameters = createRoomParameters.value
|
||||
self.userSession = userSession
|
||||
self.userIndicatorController = userIndicatorController
|
||||
self.createRoomParameters = parameters
|
||||
let bindings = CreateRoomViewStateBindings(roomName: parameters.name, roomTopic: parameters.topic, isRoomPrivate: parameters.isRoomPrivate)
|
||||
|
||||
@@ -50,7 +55,9 @@ class CreateRoomViewModel: CreateRoomViewModelType, CreateRoomViewModelProtocol
|
||||
override func process(viewAction: CreateRoomViewAction) {
|
||||
switch viewAction {
|
||||
case .createRoom:
|
||||
actionsSubject.send(.createRoom)
|
||||
Task {
|
||||
await createRoom()
|
||||
}
|
||||
case .deselectUser(let user):
|
||||
actionsSubject.send(.deselectUser(user))
|
||||
}
|
||||
@@ -71,4 +78,49 @@ class CreateRoomViewModel: CreateRoomViewModelType, CreateRoomViewModelProtocol
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
}
|
||||
|
||||
private func displayError(_ type: ClientProxyError) {
|
||||
switch type {
|
||||
case .failedCreatingRoom:
|
||||
state.bindings.alertInfo = AlertInfo(id: .failedCreatingRoom,
|
||||
title: L10n.commonError,
|
||||
message: L10n.screenStartChatErrorStartingChat)
|
||||
case .failedSearchingUsers:
|
||||
state.bindings.alertInfo = AlertInfo(id: .unknown)
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
private var clientProxy: ClientProxyProtocol {
|
||||
userSession.clientProxy
|
||||
}
|
||||
|
||||
private func createRoom() async {
|
||||
defer {
|
||||
hideLoadingIndicator()
|
||||
}
|
||||
showLoadingIndicator()
|
||||
switch await clientProxy.createRoom(with: createRoomParameters, userIDs: state.selectedUsers.map(\.userID)) {
|
||||
case .success(let roomId):
|
||||
actionsSubject.send(.openRoom(withIdentifier: roomId))
|
||||
case .failure(let failure):
|
||||
displayError(failure)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Loading indicator
|
||||
|
||||
private static let loadingIndicatorIdentifier = "CreateRoomLoading"
|
||||
|
||||
private func showLoadingIndicator() {
|
||||
userIndicatorController?.submitIndicator(UserIndicator(id: Self.loadingIndicatorIdentifier,
|
||||
type: .modal,
|
||||
title: L10n.commonLoading,
|
||||
persistent: true))
|
||||
}
|
||||
|
||||
private func hideLoadingIndicator() {
|
||||
userIndicatorController?.retractIndicatorWithId(Self.loadingIndicatorIdentifier)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,14 +166,14 @@ struct CreateRoom_Previews: PreviewProvider {
|
||||
let parameters = CreateRoomFlowParameters()
|
||||
let selectedUsers: [UserProfile] = [.mockAlice, .mockBob, .mockCharlie]
|
||||
|
||||
return CreateRoomViewModel(userSession: userSession, createRoomParameters: .init(parameters), selectedUsers: .init(selectedUsers))
|
||||
return CreateRoomViewModel(userSession: userSession, userIndicatorController: nil, createRoomParameters: .init(parameters), selectedUsers: .init(selectedUsers))
|
||||
}()
|
||||
|
||||
static let emtpyViewModel = {
|
||||
let userSession = MockUserSession(clientProxy: MockClientProxy(userID: "@userid:example.com"),
|
||||
mediaProvider: MockMediaProvider())
|
||||
let parameters = CreateRoomFlowParameters()
|
||||
return CreateRoomViewModel(userSession: userSession, createRoomParameters: .init(parameters), selectedUsers: .init([]))
|
||||
return CreateRoomViewModel(userSession: userSession, userIndicatorController: nil, createRoomParameters: .init(parameters), selectedUsers: .init([]))
|
||||
}()
|
||||
|
||||
static var previews: some View {
|
||||
|
||||
@@ -21,32 +21,35 @@ struct InviteUsersScreen: View {
|
||||
@ObservedObject var context: InviteUsersScreenViewModel.Context
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
if !context.viewState.selectedUsers.isEmpty {
|
||||
selectedUsersSection
|
||||
mainContent
|
||||
.scrollContentBackground(.hidden)
|
||||
.background(Color.element.formBackground.ignoresSafeArea())
|
||||
.navigationTitle(L10n.screenCreateRoomActionInvitePeople)
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .confirmationAction) {
|
||||
nextButton
|
||||
}
|
||||
}
|
||||
searchContent
|
||||
}
|
||||
.scrollContentBackground(.hidden)
|
||||
.background(Color.element.formBackground.ignoresSafeArea())
|
||||
.navigationTitle(L10n.screenCreateRoomActionInvitePeople)
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .confirmationAction) {
|
||||
nextButton
|
||||
}
|
||||
}
|
||||
.searchable(text: $context.searchQuery, placement: .navigationBarDrawer(displayMode: .always), prompt: L10n.commonSearchForSomeone)
|
||||
.compoundSearchField()
|
||||
.alert(item: $context.alertInfo) { $0.alert }
|
||||
.searchable(text: $context.searchQuery, placement: .navigationBarDrawer(displayMode: .always), prompt: L10n.commonSearchForSomeone)
|
||||
.compoundSearchField()
|
||||
.alert(item: $context.alertInfo) { $0.alert }
|
||||
.background(ViewFrameReader(frame: $frame))
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
/// The content shown in the form when a search query has been entered.
|
||||
@ViewBuilder
|
||||
private var searchContent: some View {
|
||||
private var mainContent: some View {
|
||||
Form {
|
||||
if !context.viewState.selectedUsers.isEmpty {
|
||||
// this is a fix for having the carousel not clipped, and inside the form, so when the search is dismissed, it wont break the design
|
||||
Section {
|
||||
EmptyView()
|
||||
} header: {
|
||||
selectedUsersSection
|
||||
.textCase(.none)
|
||||
}
|
||||
}
|
||||
if context.viewState.hasEmptySearchResults {
|
||||
noResultsContent
|
||||
} else {
|
||||
@@ -84,7 +87,9 @@ struct InviteUsersScreen: View {
|
||||
.formSectionStyle()
|
||||
}
|
||||
|
||||
@State private var frame: CGRect = .zero
|
||||
@ScaledMetric private var cellWidth: CGFloat = 64
|
||||
|
||||
private var selectedUsersSection: some View {
|
||||
ScrollView(.horizontal, showsIndicators: false) {
|
||||
ScrollViewReader { scrollView in
|
||||
@@ -105,6 +110,7 @@ struct InviteUsersScreen: View {
|
||||
.padding(.horizontal, 18)
|
||||
}
|
||||
}
|
||||
.frame(width: frame.width)
|
||||
}
|
||||
|
||||
private var nextButton: some View {
|
||||
|
||||
@@ -107,6 +107,7 @@ final class StartChatScreenCoordinator: CoordinatorProtocol {
|
||||
|
||||
private func openCreateRoomScreen() {
|
||||
let createParameters = CreateRoomCoordinatorParameters(userSession: parameters.userSession,
|
||||
userIndicatorController: parameters.userIndicatorController,
|
||||
createRoomParameters: createRoomParametersPublisher,
|
||||
selectedUsers: selectedUsersPublisher)
|
||||
let coordinator = CreateRoomCoordinator(parameters: createParameters)
|
||||
@@ -116,8 +117,8 @@ final class StartChatScreenCoordinator: CoordinatorProtocol {
|
||||
self?.toggleUser(user)
|
||||
case .updateDetails(let details):
|
||||
self?.createRoomParameters.send(details)
|
||||
case .createRoom:
|
||||
break
|
||||
case .openRoom(let identifier):
|
||||
self?.actionsSubject.send(.openRoom(withIdentifier: identifier))
|
||||
}
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
@@ -127,10 +127,11 @@ class StartChatScreenViewModel: StartChatScreenViewModelType, StartChatScreenVie
|
||||
}
|
||||
|
||||
private func createDirectRoom(with user: UserProfile) async {
|
||||
defer {
|
||||
hideLoadingIndicator()
|
||||
}
|
||||
showLoadingIndicator()
|
||||
let result = await clientProxy.createDirectRoom(with: user.userID)
|
||||
hideLoadingIndicator()
|
||||
switch result {
|
||||
switch await clientProxy.createDirectRoom(with: user.userID, expectedRoomName: user.displayName) {
|
||||
case .success(let roomId):
|
||||
actionsSubject.send(.openRoom(withIdentifier: roomId))
|
||||
case .failure(let failure):
|
||||
|
||||
@@ -192,9 +192,9 @@ class ClientProxy: ClientProxyProtocol {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func createDirectRoom(with userID: String) async -> Result<String, ClientProxyError> {
|
||||
await Task.dispatch(on: clientQueue) {
|
||||
|
||||
func createDirectRoom(with userID: String, expectedRoomName: String?) async -> Result<String, ClientProxyError> {
|
||||
let result: Result<String, ClientProxyError> = await Task.dispatch(on: clientQueue) {
|
||||
do {
|
||||
let parameters = CreateRoomParameters(name: nil, topic: nil, isEncrypted: true, isDirect: true, visibility: .private, preset: .trustedPrivateChat, invite: [userID], avatar: nil)
|
||||
let result = try self.client.createRoom(request: parameters)
|
||||
@@ -203,6 +203,52 @@ class ClientProxy: ClientProxyProtocol {
|
||||
return .failure(.failedCreatingRoom)
|
||||
}
|
||||
}
|
||||
|
||||
return await waitForRoomSummary(with: result, name: expectedRoomName)
|
||||
}
|
||||
|
||||
func createRoom(with parameters: CreateRoomFlowParameters, userIDs: [String]) async -> Result<String, ClientProxyError> {
|
||||
let result: Result<String, ClientProxyError> = await Task.dispatch(on: clientQueue) {
|
||||
do {
|
||||
let parameters = CreateRoomParameters(name: parameters.name,
|
||||
topic: parameters.topic,
|
||||
isEncrypted: parameters.isRoomPrivate,
|
||||
isDirect: false,
|
||||
visibility: parameters.isRoomPrivate ? .private : .public,
|
||||
preset: parameters.isRoomPrivate ? .privateChat : .publicChat,
|
||||
invite: userIDs,
|
||||
avatar: nil)
|
||||
let roomId = try self.client.createRoom(request: parameters)
|
||||
return .success(roomId)
|
||||
} catch {
|
||||
return .failure(.failedCreatingRoom)
|
||||
}
|
||||
}
|
||||
|
||||
return await waitForRoomSummary(with: result, name: parameters.name)
|
||||
}
|
||||
|
||||
/// Await the room to be available in the room summary list
|
||||
/// - Parameter result: the result of a room creation Task with the roomId
|
||||
private func waitForRoomSummary(with result: Result<String, ClientProxyError>, name: String?) async -> Result<String, ClientProxyError> {
|
||||
guard case .success(let roomId) = result else { return result }
|
||||
let runner = ExpiringTaskRunner { [weak self] in
|
||||
guard let roomLists = self?.allRoomsSummaryProvider?.roomListPublisher.values else {
|
||||
return
|
||||
}
|
||||
// for every list of summaries, we check if we have a room summary with matching ID and name (if present)
|
||||
for await roomList in roomLists {
|
||||
guard let summary = roomList.first(where: { $0.id == roomId }) else { continue }
|
||||
guard let name else { break }
|
||||
if summary.name == name {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// we want to ignore the timeout error, and return the .success case because the room it was properly created already, we are only waiting for it to appear
|
||||
try? await runner.run(timeout: .seconds(10))
|
||||
return result
|
||||
}
|
||||
|
||||
func roomForIdentifier(_ identifier: String) async -> RoomProxyProtocol? {
|
||||
|
||||
@@ -94,7 +94,9 @@ protocol ClientProxyProtocol: AnyObject, MediaLoaderProtocol {
|
||||
|
||||
func directRoomForUserID(_ userID: String) async -> Result<String?, ClientProxyError>
|
||||
|
||||
func createDirectRoom(with userID: String) async -> Result<String, ClientProxyError>
|
||||
func createDirectRoom(with userID: String, expectedRoomName: String?) async -> Result<String, ClientProxyError>
|
||||
|
||||
func createRoom(with parameters: CreateRoomFlowParameters, userIDs: [String]) async -> Result<String, ClientProxyError>
|
||||
|
||||
func roomForIdentifier(_ identifier: String) async -> RoomProxyProtocol?
|
||||
|
||||
|
||||
@@ -53,7 +53,11 @@ class MockClientProxy: ClientProxyProtocol {
|
||||
.failure(.failedRetrievingDirectRoom)
|
||||
}
|
||||
|
||||
func createDirectRoom(with userID: String) async -> Result<String, ClientProxyError> {
|
||||
func createDirectRoom(with userID: String, expectedRoomName: String?) async -> Result<String, ClientProxyError> {
|
||||
.failure(.failedCreatingRoom)
|
||||
}
|
||||
|
||||
func createRoom(with parameters: CreateRoomFlowParameters, userIDs: [String]) async -> Result<String, ClientProxyError> {
|
||||
.failure(.failedCreatingRoom)
|
||||
}
|
||||
|
||||
|
||||
@@ -39,6 +39,15 @@ enum RoomSummary: CustomStringConvertible {
|
||||
}
|
||||
}
|
||||
|
||||
var name: String? {
|
||||
switch self {
|
||||
case .empty:
|
||||
return nil
|
||||
case .invalidated(let details), .filled(let details):
|
||||
return details.name
|
||||
}
|
||||
}
|
||||
|
||||
var description: String {
|
||||
switch self {
|
||||
case .empty:
|
||||
|
||||
@@ -473,7 +473,7 @@ class MockScreen: Identifiable {
|
||||
let mockUserSession = MockUserSession(clientProxy: clientProxy, mediaProvider: MockMediaProvider())
|
||||
let createRoomParameters = CreateRoomFlowParameters()
|
||||
let selectedUsers: [UserProfile] = [.mockAlice, .mockBob, .mockCharlie]
|
||||
let parameters = CreateRoomCoordinatorParameters(userSession: mockUserSession, createRoomParameters: .init(createRoomParameters), selectedUsers: .init(selectedUsers))
|
||||
let parameters = CreateRoomCoordinatorParameters(userSession: mockUserSession, userIndicatorController: MockUserIndicatorController(), createRoomParameters: .init(createRoomParameters), selectedUsers: .init(selectedUsers))
|
||||
let coordinator = CreateRoomCoordinator(parameters: parameters)
|
||||
navigationStackCoordinator.setRootCoordinator(coordinator)
|
||||
return navigationStackCoordinator
|
||||
@@ -482,7 +482,7 @@ class MockScreen: Identifiable {
|
||||
let clientProxy = MockClientProxy(userID: "@mock:client.com")
|
||||
let mockUserSession = MockUserSession(clientProxy: clientProxy, mediaProvider: MockMediaProvider())
|
||||
let createRoomParameters = CreateRoomFlowParameters()
|
||||
let parameters = CreateRoomCoordinatorParameters(userSession: mockUserSession, createRoomParameters: .init(createRoomParameters), selectedUsers: .init([]))
|
||||
let parameters = CreateRoomCoordinatorParameters(userSession: mockUserSession, userIndicatorController: MockUserIndicatorController(), createRoomParameters: .init(createRoomParameters), selectedUsers: .init([]))
|
||||
let coordinator = CreateRoomCoordinator(parameters: parameters)
|
||||
navigationStackCoordinator.setRootCoordinator(coordinator)
|
||||
return navigationStackCoordinator
|
||||
|
||||
Reference in New Issue
Block a user