diff --git a/Riot/Modules/Spaces/SpaceList/SpaceListViewModel.swift b/Riot/Modules/Spaces/SpaceList/SpaceListViewModel.swift index 911529a45..ef1cf2534 100644 --- a/Riot/Modules/Spaces/SpaceList/SpaceListViewModel.swift +++ b/Riot/Modules/Spaces/SpaceList/SpaceListViewModel.swift @@ -37,7 +37,7 @@ final class SpaceListViewModel: SpaceListViewModelType { private var sections: [SpaceListSection] = [] private var selectedIndexPath: IndexPath = IndexPath(row: 0, section: 0) { didSet { - self.selectedItemId = self.itemId(with: self.selectedIndexPath) + self.selectedItemId = self.itemId(with: self.selectedIndexPath) ?? Constants.homeSpaceId } } private var homeIndexPath: IndexPath = IndexPath(row: 0, section: 0) @@ -270,7 +270,7 @@ final class SpaceListViewModel: SpaceListViewModelType { self.currentOperation?.cancel() } - private func itemId(with indexPath: IndexPath) -> String { + private func itemId(with indexPath: IndexPath) -> String? { guard self.selectedIndexPath.section < self.sections.count else { return Constants.homeSpaceId } @@ -279,6 +279,9 @@ final class SpaceListViewModel: SpaceListViewModelType { case .home: return Constants.homeSpaceId case .spaces(let viewDataList): + guard self.selectedIndexPath.row < viewDataList.count else { + return nil + } let spaceViewData = viewDataList[self.selectedIndexPath.row] return spaceViewData.spaceId case .addSpace: diff --git a/changelog.d/5825.bugfix b/changelog.d/5825.bugfix new file mode 100644 index 000000000..dd5050379 --- /dev/null +++ b/changelog.d/5825.bugfix @@ -0,0 +1 @@ +Crash after leaving last space. \ No newline at end of file