mirror of
https://github.com/element-hq/element-android
synced 2024-11-27 11:59:12 +03:00
Clean a bit code of Room list
This commit is contained in:
parent
9c48eba26c
commit
6d9ec2789b
6 changed files with 63 additions and 66 deletions
|
@ -6,5 +6,6 @@ sealed class RoomListActions {
|
|||
|
||||
data class SelectRoom(val roomSummary: RoomSummary) : RoomListActions()
|
||||
|
||||
object RoomDisplayed : RoomListActions()
|
||||
|
||||
}
|
|
@ -8,7 +8,6 @@ import com.airbnb.mvrx.Fail
|
|||
import com.airbnb.mvrx.Incomplete
|
||||
import com.airbnb.mvrx.Success
|
||||
import com.airbnb.mvrx.fragmentViewModel
|
||||
import com.airbnb.mvrx.withState
|
||||
import im.vector.matrix.android.api.failure.Failure
|
||||
import im.vector.matrix.android.api.session.room.model.RoomSummary
|
||||
import im.vector.riotredesign.R
|
||||
|
@ -39,30 +38,28 @@ class RoomListFragment : RiotFragment(), RoomSummaryController.Callback {
|
|||
roomController = RoomSummaryController(this)
|
||||
stateView.contentView = epoxyRecyclerView
|
||||
epoxyRecyclerView.setController(roomController)
|
||||
|
||||
viewModel.subscribe { renderState(it) }
|
||||
}
|
||||
|
||||
private fun renderState(state: RoomListViewState) {
|
||||
when (state.roomSummaries) {
|
||||
when (state.async) {
|
||||
is Incomplete -> renderLoading()
|
||||
is Success -> renderSuccess(state.roomSummaries(), state.selectedRoom)
|
||||
is Fail -> renderFailure(state.roomSummaries.error)
|
||||
is Success -> renderSuccess(state)
|
||||
is Fail -> renderFailure(state.async.error)
|
||||
}
|
||||
if (state.selectedRoom != null && state.showLastSelectedRoom) {
|
||||
val position = state.roomSummaries()?.indexOf(state.selectedRoom) ?: 0
|
||||
epoxyRecyclerView.scrollToPosition(position)
|
||||
if (state.shouldOpenRoomDetail && state.selectedRoom != null) {
|
||||
homeNavigator.openRoomDetail(state.selectedRoom.roomId)
|
||||
viewModel.accept(RoomListActions.RoomDisplayed)
|
||||
}
|
||||
}
|
||||
|
||||
private fun renderSuccess(roomSummaries: List<RoomSummary>?, selectedRoom: RoomSummary?) {
|
||||
if (roomSummaries.isNullOrEmpty()) {
|
||||
stateView.state = StateView.State.Empty("Rejoignez une room pour commencer à utiliser l'application")
|
||||
private fun renderSuccess(state: RoomListViewState) {
|
||||
if (state.async().isNullOrEmpty()) {
|
||||
stateView.state = StateView.State.Empty(getString(R.string.room_list_empty))
|
||||
} else {
|
||||
stateView.state = StateView.State.Content
|
||||
}
|
||||
roomController.setData(roomSummaries, selectedRoom)
|
||||
roomController.setData(state)
|
||||
}
|
||||
|
||||
private fun renderLoading() {
|
||||
|
@ -71,19 +68,14 @@ class RoomListFragment : RiotFragment(), RoomSummaryController.Callback {
|
|||
|
||||
private fun renderFailure(error: Throwable) {
|
||||
val message = when (error) {
|
||||
is Failure.NetworkConnection -> "Pas de connexion internet"
|
||||
else -> "Une erreur est survenue"
|
||||
is Failure.NetworkConnection -> getString(R.string.error_no_network)
|
||||
else -> getString(R.string.error_common)
|
||||
}
|
||||
stateView.state = StateView.State.Error(message)
|
||||
}
|
||||
|
||||
override fun onRoomSelected(room: RoomSummary) {
|
||||
withState(viewModel) {
|
||||
if (it.selectedRoom?.roomId != room.roomId) {
|
||||
viewModel.accept(RoomListActions.SelectRoom(room))
|
||||
homeNavigator.openRoomDetail(room.roomId)
|
||||
}
|
||||
}
|
||||
viewModel.accept(RoomListActions.SelectRoom(room))
|
||||
}
|
||||
|
||||
}
|
|
@ -29,25 +29,40 @@ class RoomListViewModel(initialState: RoomListViewState,
|
|||
fun accept(action: RoomListActions) {
|
||||
when (action) {
|
||||
is RoomListActions.SelectRoom -> handleSelectRoom(action)
|
||||
is RoomListActions.RoomDisplayed -> setState { copy(shouldOpenRoomDetail = false) }
|
||||
}
|
||||
}
|
||||
|
||||
// PRIVATE METHODS *****************************************************************************
|
||||
|
||||
private fun handleSelectRoom(action: RoomListActions.SelectRoom) {
|
||||
session.saveLastSelectedRoom(action.roomSummary)
|
||||
setState { copy(selectedRoom = action.roomSummary) }
|
||||
withState { state ->
|
||||
if (state.selectedRoom?.roomId != action.roomSummary.roomId) {
|
||||
session.saveLastSelectedRoom(action.roomSummary)
|
||||
setState { copy(selectedRoom = action.roomSummary, shouldOpenRoomDetail = true) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun observeRoomSummaries() {
|
||||
session
|
||||
.rx().liveRoomSummaries()
|
||||
.execute {
|
||||
.execute { async ->
|
||||
val summaries = async()
|
||||
val directRooms = summaries?.filter { it.isDirect } ?: emptyList()
|
||||
val groupRooms = summaries?.filter { !it.isDirect } ?: emptyList()
|
||||
|
||||
val selectedRoom = selectedRoom
|
||||
?: session.lastSelectedRoom()
|
||||
?: it.invoke()?.firstOrNull()
|
||||
?: directRooms.firstOrNull()
|
||||
?: groupRooms.firstOrNull()
|
||||
|
||||
copy(roomSummaries = it, selectedRoom = selectedRoom)
|
||||
copy(
|
||||
async = async,
|
||||
directRooms = directRooms,
|
||||
groupRooms = groupRooms,
|
||||
selectedRoom = selectedRoom
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,20 +6,9 @@ import com.airbnb.mvrx.Uninitialized
|
|||
import im.vector.matrix.android.api.session.room.model.RoomSummary
|
||||
|
||||
data class RoomListViewState(
|
||||
val roomSummaries: Async<List<RoomSummary>> = Uninitialized,
|
||||
val async: Async<List<RoomSummary>> = Uninitialized,
|
||||
val directRooms: List<RoomSummary> = emptyList(),
|
||||
val groupRooms: List<RoomSummary> = emptyList(),
|
||||
val selectedRoom: RoomSummary? = null,
|
||||
private var _showLastSelectedRoom: Boolean = true
|
||||
) : MvRxState {
|
||||
|
||||
var showLastSelectedRoom: Boolean = _showLastSelectedRoom
|
||||
private set
|
||||
get() {
|
||||
if (_showLastSelectedRoom) {
|
||||
_showLastSelectedRoom = false
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
val shouldOpenRoomDetail: Boolean = true
|
||||
) : MvRxState
|
|
@ -1,62 +1,59 @@
|
|||
package im.vector.riotredesign.features.home.room.list
|
||||
|
||||
import com.airbnb.epoxy.Typed2EpoxyController
|
||||
import com.airbnb.epoxy.TypedEpoxyController
|
||||
import im.vector.matrix.android.api.session.room.model.RoomSummary
|
||||
|
||||
class RoomSummaryController(private val callback: Callback? = null
|
||||
) : Typed2EpoxyController<List<RoomSummary>, RoomSummary>() {
|
||||
) : TypedEpoxyController<RoomListViewState>() {
|
||||
|
||||
|
||||
private var directRoomsExpanded = true
|
||||
private var groupRoomsExpanded = true
|
||||
private var isDirectRoomsExpanded = true
|
||||
private var isGroupRoomsExpanded = true
|
||||
|
||||
override fun buildModels(summaries: List<RoomSummary>?, selected: RoomSummary?) {
|
||||
|
||||
val directRooms = summaries?.filter { it.isDirect } ?: emptyList()
|
||||
val groupRooms = summaries?.filter { !it.isDirect } ?: emptyList()
|
||||
override fun buildModels(viewState: RoomListViewState) {
|
||||
|
||||
RoomCategoryItem(
|
||||
title = "DIRECT MESSAGES",
|
||||
isExpanded = directRoomsExpanded,
|
||||
isExpanded = isDirectRoomsExpanded,
|
||||
listener = {
|
||||
directRoomsExpanded = !directRoomsExpanded
|
||||
setData(summaries, selected)
|
||||
isDirectRoomsExpanded = !isDirectRoomsExpanded
|
||||
setData(viewState)
|
||||
}
|
||||
)
|
||||
.id("direct_messages")
|
||||
.addTo(this)
|
||||
|
||||
if (directRoomsExpanded) {
|
||||
buildRoomModels(directRooms, selected)
|
||||
if (isDirectRoomsExpanded) {
|
||||
buildRoomModels(viewState.directRooms, viewState.selectedRoom)
|
||||
}
|
||||
|
||||
RoomCategoryItem(
|
||||
title = "GROUPS",
|
||||
isExpanded = groupRoomsExpanded,
|
||||
isExpanded = isGroupRoomsExpanded,
|
||||
listener = {
|
||||
groupRoomsExpanded = !groupRoomsExpanded
|
||||
setData(summaries, selected)
|
||||
isGroupRoomsExpanded = !isGroupRoomsExpanded
|
||||
setData(viewState)
|
||||
}
|
||||
)
|
||||
.id("group_messages")
|
||||
.addTo(this)
|
||||
|
||||
if (groupRoomsExpanded) {
|
||||
buildRoomModels(groupRooms, selected)
|
||||
if (isGroupRoomsExpanded) {
|
||||
buildRoomModels(viewState.groupRooms, viewState.selectedRoom)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private fun buildRoomModels(summaries: List<RoomSummary>, selected: RoomSummary?) {
|
||||
|
||||
summaries.forEach {
|
||||
summaries.forEach { roomSummary ->
|
||||
val isSelected = roomSummary.roomId == selected?.roomId
|
||||
RoomSummaryItem(
|
||||
roomName = it.displayName,
|
||||
avatarUrl = it.avatarUrl,
|
||||
isSelected = it.roomId == selected?.roomId,
|
||||
listener = { callback?.onRoomSelected(it) }
|
||||
roomName = roomSummary.displayName,
|
||||
avatarUrl = roomSummary.avatarUrl,
|
||||
isSelected = isSelected,
|
||||
listener = { callback?.onRoomSelected(roomSummary) }
|
||||
)
|
||||
.id(it.roomId)
|
||||
.id(roomSummary.roomId)
|
||||
.addTo(this)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,5 +2,8 @@
|
|||
<string name="app_name">Riot Redesign</string>
|
||||
|
||||
<string name="global_retry">Réessayer</string>
|
||||
<string name="error_no_network">Pas de connexion internet</string>
|
||||
<string name="error_common">Une erreur est survenue</string>
|
||||
<string name="room_list_empty">Rejoignez une room pour commencer à utiliser l\'application</string>
|
||||
|
||||
</resources>
|
||||
|
|
Loading…
Reference in a new issue