Make sure the selected/available rooms are updated when adding space children fails part way through. (#4997)

* Make sure the available rooms are updated if adding space children fails part way through.

* Additionally handle the case where removing rooms fails part way through.
This commit is contained in:
Doug
2026-01-23 17:36:26 +00:00
committed by GitHub
parent a3683ea996
commit 3b231339cd
6 changed files with 125 additions and 12 deletions
@@ -17,6 +17,9 @@ class SpaceAddRoomsScreenViewModel: SpaceAddRoomsScreenViewModelType, SpaceAddRo
private let userIndicatorController: UserIndicatorControllerProtocol
private var suggestedRooms: [SpaceAddRoomsScreenRoom] = []
/// A place to track rooms that were sucessfully added to the space in order to filter them out from
/// the roomsSection when a failure occurs part way through the array.
private var addedRoomIDs: Set<String> = []
private var actionsSubject: PassthroughSubject<SpaceAddRoomsScreenViewModelAction, Never> = .init()
var actions: AnyPublisher<SpaceAddRoomsScreenViewModelAction, Never> {
@@ -89,15 +92,15 @@ class SpaceAddRoomsScreenViewModel: SpaceAddRoomsScreenViewModelType, SpaceAddRo
}
let existingRooms = spaceRoomListProxy.spaceRoomsPublisher.value
var rooms = [SpaceAddRoomsScreenRoom]()
for summary in roomSummaryProvider.roomListPublisher.value where !summary.isDirect && !existingRooms.contains(where: { $0.id == summary.id }) {
rooms.append(.init(summary: summary))
}
let rooms = roomSummaryProvider.roomListPublisher.value
.lazy
.filter { [addedRoomIDs] summary in
!summary.isDirect && !existingRooms.contains { $0.id == summary.id } && !addedRoomIDs.contains(summary.id)
}
.map(SpaceAddRoomsScreenRoom.init)
state.roomsSection = .init(type: .searchResults, rooms: rooms)
// rooms crap
state.roomsSection = .init(type: .searchResults, rooms: Array(rooms))
}
/// The actual range values don't matter as long as they contain the lower
@@ -131,15 +134,26 @@ class SpaceAddRoomsScreenViewModel: SpaceAddRoomsScreenViewModelType, SpaceAddRo
showSavingIndicator()
defer { hideSavingIndicator() }
MXLog.info("Adding \(state.selectedRooms.count) rooms to space \(spaceRoomListProxy.id)")
for room in state.selectedRooms {
if case .failure(let error) = await spaceServiceProxy.addChild(room.id, to: spaceRoomListProxy.id) {
switch await spaceServiceProxy.addChild(room.id, to: spaceRoomListProxy.id) {
case .success:
addedRoomIDs.insert(room.id)
case .failure(let error):
MXLog.error("Failed adding room to space: \(error)")
showErrorIndicator()
updateRooms() // Hide any rooms that were already added.
// Hide rooms that were successfully added.
state.selectedRooms.removeAll { addedRoomIDs.contains($0.id) }
updateRooms()
return
}
}
MXLog.info("\(state.selectedRooms.count) rooms added to space \(spaceRoomListProxy.id)")
await spaceRoomListProxy.resetAndWaitForFullReload(timeout: .seconds(10))
actionsSubject.send(.dismiss)
@@ -39,6 +39,7 @@ struct SpaceScreenViewState: BindableState {
var editMode: EditMode = .inactive
var editModeSelectedIDs: Set<String> = []
var editModeRemovedIDs: Set<String> = []
var bindings = SpaceScreenViewStateBindings()
@@ -50,7 +51,7 @@ struct SpaceScreenViewState: BindableState {
if editMode == .inactive {
rooms
} else {
rooms.filter { !$0.isSpace }
rooms.filter { !$0.isSpace && !editModeRemovedIDs.contains($0.id) }
}
}
@@ -156,6 +156,7 @@ class SpaceScreenViewModel: SpaceScreenViewModelType, SpaceScreenViewModelProtoc
case .finishManagingChildren:
withAnimation(.easeOut(duration: 0.25).disabledDuringTests()) {
state.editMode = .inactive
state.editModeRemovedIDs = []
} completion: {
self.state.editModeSelectedIDs.removeAll()
}
@@ -209,16 +210,27 @@ class SpaceScreenViewModel: SpaceScreenViewModelType, SpaceScreenViewModelProtoc
state.bindings.isPresentingRemoveChildrenConfirmation = false
MXLog.info("Removing \(state.editModeSelectedIDs.count) children from space \(spaceRoomListProxy.id)")
var removedIDs: [String] = [] // Using an intermediate array so the screen doesn't change until the operation finishes.
for childID in state.editModeSelectedIDs {
switch await spaceServiceProxy.removeChild(childID, from: spaceRoomListProxy.id) {
case .success:
MXLog.info("Successfully removed \(childID) from \(spaceRoomListProxy.id)")
case .failure:
removedIDs.append(childID)
case .failure(let error):
MXLog.error("Failed removing room from space: \(error)")
showFailureIndicator()
// Hide rooms that were successfully removed.
state.editModeSelectedIDs = state.editModeSelectedIDs.filter { !removedIDs.contains($0) }
state.editModeRemovedIDs.formUnion(removedIDs)
return
}
}
MXLog.info("\(state.editModeSelectedIDs.count) children removed from space \(spaceRoomListProxy.id)")
await spaceRoomListProxy.resetAndWaitForFullReload(timeout: .seconds(10))
process(viewAction: .finishManagingChildren)