replacing inverted takeIf chain with positive when conditions

This commit is contained in:
Adam Brown 2022-07-28 11:04:35 +01:00
parent 2d8ef9925d
commit 09d840506e
3 changed files with 16 additions and 25 deletions

View File

@ -142,9 +142,10 @@ class UnreadMessagesSharedViewModel @AssistedInject constructor(
val totalCount = roomService.getNotificationCountForRooms(
roomSummaryQueryParams {
this.memberships = listOf(Membership.JOIN)
this.spaceFilter = SpaceFilter.OrphanRooms.takeIf {
!vectorPreferences.prefSpacesShowAllRoomInHome()
} ?: SpaceFilter.NoFilter
this.spaceFilter = when {
vectorPreferences.prefSpacesShowAllRoomInHome() -> SpaceFilter.NoFilter
else -> SpaceFilter.OrphanRooms
}
}
)

View File

@ -27,7 +27,6 @@ import im.vector.app.core.di.MavericksAssistedViewModelFactory
import im.vector.app.core.di.hiltMavericksViewModelFactory
import im.vector.app.core.platform.StateView
import im.vector.app.core.platform.VectorViewModel
import im.vector.app.features.home.room.list.RoomListViewModel
import im.vector.app.features.settings.VectorPreferences
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
@ -107,13 +106,8 @@ class HomeRoomListViewModel @AssistedInject constructor(
}
.onEach { selectedSpaceOption ->
val selectedSpace = selectedSpaceOption.orNull()
val strategy = if (!vectorPreferences.prefSpacesShowAllRoomInHome()) {
RoomListViewModel.SpaceFilterStrategy.ALL_IF_SPACE_NULL
} else {
RoomListViewModel.SpaceFilterStrategy.ORPHANS_IF_SPACE_NULL
}
filteredPagedRoomSummariesLive.queryParams = filteredPagedRoomSummariesLive.queryParams.copy(
spaceFilter = getSpaceFilter(selectedSpaceId = selectedSpace?.roomId, strategy)
spaceFilter = getSpaceFilter(selectedSpaceId = selectedSpace?.roomId)
)
}.launchIn(viewModelScope)
@ -122,15 +116,10 @@ class HomeRoomListViewModel @AssistedInject constructor(
)
}
private fun getSpaceFilter(selectedSpaceId: String?, strategy: RoomListViewModel.SpaceFilterStrategy): SpaceFilter {
return when (strategy) {
RoomListViewModel.SpaceFilterStrategy.ORPHANS_IF_SPACE_NULL -> {
selectedSpaceId.toActiveSpaceOrOrphanRooms()
}
RoomListViewModel.SpaceFilterStrategy.ALL_IF_SPACE_NULL -> {
selectedSpaceId.toActiveSpaceOrNoFilter()
}
RoomListViewModel.SpaceFilterStrategy.NONE -> SpaceFilter.NoFilter
private fun getSpaceFilter(selectedSpaceId: String?): SpaceFilter {
return when {
vectorPreferences.prefSpacesShowAllRoomInHome() -> selectedSpaceId.toActiveSpaceOrNoFilter()
else -> selectedSpaceId.toActiveSpaceOrOrphanRooms()
}
}

View File

@ -98,9 +98,7 @@ class SpaceListViewModel @AssistedInject constructor(
session.roomService().getPagedRoomSummariesLive(
roomSummaryQueryParams {
this.memberships = listOf(Membership.JOIN)
this.spaceFilter = SpaceFilter.OrphanRooms.takeIf {
!vectorPreferences.prefSpacesShowAllRoomInHome()
} ?: SpaceFilter.NoFilter
this.spaceFilter = roomsInSpaceFilter()
}, sortOrder = RoomSortOrder.NONE
).asFlow()
.sample(300)
@ -115,9 +113,7 @@ class SpaceListViewModel @AssistedInject constructor(
val totalCount = session.roomService().getNotificationCountForRooms(
roomSummaryQueryParams {
this.memberships = listOf(Membership.JOIN)
this.spaceFilter = SpaceFilter.OrphanRooms.takeIf {
!vectorPreferences.prefSpacesShowAllRoomInHome()
} ?: SpaceFilter.NoFilter
this.spaceFilter = roomsInSpaceFilter()
}
)
val counts = RoomAggregateNotificationCount(
@ -134,6 +130,11 @@ class SpaceListViewModel @AssistedInject constructor(
.launchIn(viewModelScope)
}
private fun roomsInSpaceFilter() = when {
vectorPreferences.prefSpacesShowAllRoomInHome() -> SpaceFilter.NoFilter
else -> SpaceFilter.OrphanRooms
}
override fun handle(action: SpaceListAction) {
when (action) {
is SpaceListAction.SelectSpace -> handleSelectSpace(action)