From 5cc617168e1f24ec7bd534614a90307a1f050627 Mon Sep 17 00:00:00 2001 From: ganfra Date: Mon, 5 Nov 2018 21:58:03 +0100 Subject: [PATCH] Home : use only one ViewModel and one state --- .idea/caches/build_file_checksums.ser | Bin 659 -> 661 bytes .../riotredesign/features/home/HomeActions.kt | 14 +++++ .../RoomListViewModel.kt => HomeViewModel.kt} | 42 +++++++++----- .../features/home/HomeViewState.kt | 17 ++++++ .../features/home/group/GroupListActions.kt | 9 --- .../features/home/group/GroupListFragment.kt | 28 +++++---- .../features/home/group/GroupListViewModel.kt | 53 ------------------ .../features/home/group/GroupListViewState.kt | 12 ---- .../home/group/GroupSummaryController.kt | 7 ++- .../features/home/group/GroupSummaryItem.kt | 1 + .../home/room/list/RoomListActions.kt | 12 ---- .../home/room/list/RoomListFragment.kt | 25 +++++---- .../home/room/list/RoomListViewState.kt | 14 ----- .../home/room/list/RoomSummaryController.kt | 7 +-- 14 files changed, 96 insertions(+), 145 deletions(-) create mode 100644 app/src/main/java/im/vector/riotredesign/features/home/HomeActions.kt rename app/src/main/java/im/vector/riotredesign/features/home/{room/list/RoomListViewModel.kt => HomeViewModel.kt} (57%) create mode 100644 app/src/main/java/im/vector/riotredesign/features/home/HomeViewState.kt delete mode 100644 app/src/main/java/im/vector/riotredesign/features/home/group/GroupListActions.kt delete mode 100644 app/src/main/java/im/vector/riotredesign/features/home/group/GroupListViewModel.kt delete mode 100644 app/src/main/java/im/vector/riotredesign/features/home/group/GroupListViewState.kt delete mode 100644 app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListActions.kt delete mode 100644 app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListViewState.kt diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index d04576cac8db44fa39154a4e05bc03b178281c5c..d4c39de25406e077818d761a58916cc0c7bf4cdc 100644 GIT binary patch delta 67 zcmV-J0KET`1(gMmm;~jF$}y3gh?8gmzapJ8-4f!BFNqE8lw$EA2~KnXDKAxXWpZ;b ZW^!R}V{d75lQID|0TGis0UMK50zS43865xs delta 65 zcmV-H0KWf~1(OAkm;~fW+!>Lahz=(&RdZ!>b1!FMZf0^}lLZ1clOzGZ6Mxpspp(initialState) { +class HomeViewModel(initialState: HomeViewState, private val session: Session) : BaseMvRxViewModel(initialState) { - companion object : MvRxViewModelFactory { + companion object : MvRxViewModelFactory { @JvmStatic - override fun create(activity: FragmentActivity, state: RoomListViewState): RoomListViewModel { + override fun create(activity: FragmentActivity, state: HomeViewState): HomeViewModel { val matrix = activity.get() val currentSession = matrix.currentSession - return RoomListViewModel(state, currentSession) + return HomeViewModel(state, currentSession) } } init { observeRoomSummaries() + observeGroupSummaries() } - fun accept(action: RoomListActions) { + fun accept(action: HomeActions) { when (action) { - is RoomListActions.SelectRoom -> handleSelectRoom(action) - is RoomListActions.RoomDisplayed -> setState { copy(shouldOpenRoomDetail = false) } + is HomeActions.SelectRoom -> handleSelectRoom(action) + is HomeActions.SelectGroup -> handleSelectGroup(action) + is HomeActions.RoomDisplayed -> setState { copy(shouldOpenRoomDetail = false) } } } // PRIVATE METHODS ***************************************************************************** - private fun handleSelectRoom(action: RoomListActions.SelectRoom) { + private fun handleSelectRoom(action: HomeActions.SelectRoom) { withState { state -> if (state.selectedRoom?.roomId != action.roomSummary.roomId) { session.saveLastSelectedRoom(action.roomSummary) @@ -47,6 +44,14 @@ class RoomListViewModel(initialState: RoomListViewState, } } + private fun handleSelectGroup(action: HomeActions.SelectGroup) { + withState { state -> + if (state.selectedGroup?.groupId != action.groupSummary.groupId) { + setState { copy(selectedGroup = action.groupSummary) } + } + } + } + private fun observeRoomSummaries() { session .rx().liveRoomSummaries() @@ -61,7 +66,7 @@ class RoomListViewModel(initialState: RoomListViewState, ?: groupRooms.firstOrNull() copy( - async = async, + asyncRooms = async, directRooms = directRooms, groupRooms = groupRooms, selectedRoom = selectedRoom @@ -69,4 +74,11 @@ class RoomListViewModel(initialState: RoomListViewState, } } + private fun observeGroupSummaries() { + session + .rx().liveGroupSummaries() + .execute { async -> + copy(asyncGroups = async) + } + } } \ No newline at end of file diff --git a/app/src/main/java/im/vector/riotredesign/features/home/HomeViewState.kt b/app/src/main/java/im/vector/riotredesign/features/home/HomeViewState.kt new file mode 100644 index 0000000000..01c3758e8c --- /dev/null +++ b/app/src/main/java/im/vector/riotredesign/features/home/HomeViewState.kt @@ -0,0 +1,17 @@ +package im.vector.riotredesign.features.home + +import com.airbnb.mvrx.Async +import com.airbnb.mvrx.MvRxState +import com.airbnb.mvrx.Uninitialized +import im.vector.matrix.android.api.session.group.model.GroupSummary +import im.vector.matrix.android.api.session.room.model.RoomSummary + +data class HomeViewState( + val asyncRooms: Async> = Uninitialized, + val directRooms: List = emptyList(), + val groupRooms: List = emptyList(), + val selectedRoom: RoomSummary? = null, + val shouldOpenRoomDetail: Boolean = true, + val asyncGroups: Async> = Uninitialized, + val selectedGroup: GroupSummary? = null +) : MvRxState \ No newline at end of file diff --git a/app/src/main/java/im/vector/riotredesign/features/home/group/GroupListActions.kt b/app/src/main/java/im/vector/riotredesign/features/home/group/GroupListActions.kt deleted file mode 100644 index 92f758a759..0000000000 --- a/app/src/main/java/im/vector/riotredesign/features/home/group/GroupListActions.kt +++ /dev/null @@ -1,9 +0,0 @@ -package im.vector.riotredesign.features.home.group - -import im.vector.matrix.android.api.session.group.model.GroupSummary - -sealed class GroupListActions { - - data class SelectGroup(val groupSummary: GroupSummary) : GroupListActions() - -} \ No newline at end of file diff --git a/app/src/main/java/im/vector/riotredesign/features/home/group/GroupListFragment.kt b/app/src/main/java/im/vector/riotredesign/features/home/group/GroupListFragment.kt index 131a82e54d..16e4bbd817 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/group/GroupListFragment.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/group/GroupListFragment.kt @@ -6,12 +6,15 @@ import android.view.View import android.view.ViewGroup import com.airbnb.mvrx.Incomplete import com.airbnb.mvrx.Success -import com.airbnb.mvrx.fragmentViewModel +import com.airbnb.mvrx.activityViewModel import im.vector.matrix.android.api.session.group.model.GroupSummary import im.vector.riotredesign.R import im.vector.riotredesign.core.platform.RiotFragment import im.vector.riotredesign.core.platform.StateView -import kotlinx.android.synthetic.main.fragment_room_list.* +import im.vector.riotredesign.features.home.HomeActions +import im.vector.riotredesign.features.home.HomeViewModel +import im.vector.riotredesign.features.home.HomeViewState +import kotlinx.android.synthetic.main.fragment_group_list.* class GroupListFragment : RiotFragment(), GroupSummaryController.Callback { @@ -21,8 +24,9 @@ class GroupListFragment : RiotFragment(), GroupSummaryController.Callback { } } - private val viewModel: GroupListViewModel by fragmentViewModel() - private lateinit var roomController: GroupSummaryController + private val viewModel: HomeViewModel by activityViewModel() + + private lateinit var groupController: GroupSummaryController override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { return inflater.inflate(R.layout.fragment_group_list, container, false) @@ -30,22 +34,22 @@ class GroupListFragment : RiotFragment(), GroupSummaryController.Callback { override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) - roomController = GroupSummaryController(this) + groupController = GroupSummaryController(this) stateView.contentView = epoxyRecyclerView - epoxyRecyclerView.setController(roomController) + epoxyRecyclerView.setController(groupController) viewModel.subscribe { renderState(it) } } - private fun renderState(state: GroupListViewState) { - when (state.async) { + private fun renderState(state: HomeViewState) { + when (state.asyncGroups) { is Incomplete -> renderLoading() - is Success -> renderSuccess(state) + is Success -> renderSuccess(state) } } - private fun renderSuccess(state: GroupListViewState) { + private fun renderSuccess(state: HomeViewState) { stateView.state = StateView.State.Content - roomController.setData(state) + groupController.setData(state) } private fun renderLoading() { @@ -53,7 +57,7 @@ class GroupListFragment : RiotFragment(), GroupSummaryController.Callback { } override fun onGroupSelected(groupSummary: GroupSummary) { - viewModel.accept(GroupListActions.SelectGroup(groupSummary)) + viewModel.accept(HomeActions.SelectGroup(groupSummary)) } } \ No newline at end of file diff --git a/app/src/main/java/im/vector/riotredesign/features/home/group/GroupListViewModel.kt b/app/src/main/java/im/vector/riotredesign/features/home/group/GroupListViewModel.kt deleted file mode 100644 index 1cfb98c46f..0000000000 --- a/app/src/main/java/im/vector/riotredesign/features/home/group/GroupListViewModel.kt +++ /dev/null @@ -1,53 +0,0 @@ -package im.vector.riotredesign.features.home.group - -import android.support.v4.app.FragmentActivity -import com.airbnb.mvrx.BaseMvRxViewModel -import com.airbnb.mvrx.MvRxViewModelFactory -import im.vector.matrix.android.api.Matrix -import im.vector.matrix.android.api.session.Session -import im.vector.matrix.rx.rx -import org.koin.android.ext.android.get - -class GroupListViewModel(initialState: GroupListViewState, - private val session: Session -) : BaseMvRxViewModel(initialState) { - - companion object : MvRxViewModelFactory { - - @JvmStatic - override fun create(activity: FragmentActivity, state: GroupListViewState): GroupListViewModel { - val matrix = activity.get() - val currentSession = matrix.currentSession - return GroupListViewModel(state, currentSession) - } - } - - init { - observeGroupSummaries() - } - - fun accept(action: GroupListActions) { - when (action) { - is GroupListActions.SelectGroup -> handleSelectGroup(action) - } - } - - // PRIVATE METHODS ***************************************************************************** - - private fun handleSelectGroup(action: GroupListActions.SelectGroup) { - withState { state -> - if (state.selectedGroup?.groupId != action.groupSummary.groupId) { - setState { copy(selectedGroup = action.groupSummary) } - } - } - } - - private fun observeGroupSummaries() { - session - .rx().liveGroupSummaries() - .execute { async -> - copy(async = async) - } - } - -} \ No newline at end of file diff --git a/app/src/main/java/im/vector/riotredesign/features/home/group/GroupListViewState.kt b/app/src/main/java/im/vector/riotredesign/features/home/group/GroupListViewState.kt deleted file mode 100644 index 5922f0738a..0000000000 --- a/app/src/main/java/im/vector/riotredesign/features/home/group/GroupListViewState.kt +++ /dev/null @@ -1,12 +0,0 @@ -package im.vector.riotredesign.features.home.group - -import com.airbnb.mvrx.Async -import com.airbnb.mvrx.MvRxState -import com.airbnb.mvrx.Uninitialized -import im.vector.matrix.android.api.session.group.model.GroupSummary -import im.vector.matrix.android.api.session.room.model.RoomSummary - -data class GroupListViewState( - val async: Async> = Uninitialized, - val selectedGroup: GroupSummary? = null -) : MvRxState \ No newline at end of file diff --git a/app/src/main/java/im/vector/riotredesign/features/home/group/GroupSummaryController.kt b/app/src/main/java/im/vector/riotredesign/features/home/group/GroupSummaryController.kt index d8e114ce46..edd7c19e9f 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/group/GroupSummaryController.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/group/GroupSummaryController.kt @@ -2,12 +2,13 @@ package im.vector.riotredesign.features.home.group import com.airbnb.epoxy.TypedEpoxyController import im.vector.matrix.android.api.session.group.model.GroupSummary +import im.vector.riotredesign.features.home.HomeViewState class GroupSummaryController(private val callback: Callback? = null -) : TypedEpoxyController() { +) : TypedEpoxyController() { - override fun buildModels(viewState: GroupListViewState) { - buildGroupModels(viewState.async(), viewState.selectedGroup) + override fun buildModels(viewState: HomeViewState) { + buildGroupModels(viewState.asyncGroups(), viewState.selectedGroup) } private fun buildGroupModels(summaries: List?, selected: GroupSummary?) { diff --git a/app/src/main/java/im/vector/riotredesign/features/home/group/GroupSummaryItem.kt b/app/src/main/java/im/vector/riotredesign/features/home/group/GroupSummaryItem.kt index 3eff417379..75e7463ea0 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/group/GroupSummaryItem.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/group/GroupSummaryItem.kt @@ -16,6 +16,7 @@ data class GroupSummaryItem( private val avatarImageView by bind(R.id.groupAvatarImageView) override fun bind() { + avatarImageView.setOnClickListener { listener?.invoke() } AvatarRenderer.render(avatarUrl, groupName.toString(), avatarImageView) } } \ No newline at end of file diff --git a/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListActions.kt b/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListActions.kt deleted file mode 100644 index 5556034b6d..0000000000 --- a/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListActions.kt +++ /dev/null @@ -1,12 +0,0 @@ -package im.vector.riotredesign.features.home.room.list - -import im.vector.matrix.android.api.session.room.model.RoomSummary -import im.vector.riotredesign.features.home.group.GroupListActions - -sealed class RoomListActions { - - data class SelectRoom(val roomSummary: RoomSummary) : RoomListActions() - - object RoomDisplayed : RoomListActions() - -} \ No newline at end of file diff --git a/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListFragment.kt b/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListFragment.kt index eb51c532c2..774841e340 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListFragment.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListFragment.kt @@ -7,13 +7,16 @@ import android.view.ViewGroup import com.airbnb.mvrx.Fail import com.airbnb.mvrx.Incomplete import com.airbnb.mvrx.Success -import com.airbnb.mvrx.fragmentViewModel +import com.airbnb.mvrx.activityViewModel import im.vector.matrix.android.api.failure.Failure import im.vector.matrix.android.api.session.room.model.RoomSummary import im.vector.riotredesign.R import im.vector.riotredesign.core.platform.RiotFragment import im.vector.riotredesign.core.platform.StateView +import im.vector.riotredesign.features.home.HomeActions import im.vector.riotredesign.features.home.HomeNavigator +import im.vector.riotredesign.features.home.HomeViewModel +import im.vector.riotredesign.features.home.HomeViewState import kotlinx.android.synthetic.main.fragment_room_list.* import org.koin.android.ext.android.inject @@ -26,7 +29,7 @@ class RoomListFragment : RiotFragment(), RoomSummaryController.Callback { } private val homeNavigator by inject() - private val viewModel: RoomListViewModel by fragmentViewModel() + private val viewModel: HomeViewModel by activityViewModel() private lateinit var roomController: RoomSummaryController override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { @@ -41,20 +44,20 @@ class RoomListFragment : RiotFragment(), RoomSummaryController.Callback { viewModel.subscribe { renderState(it) } } - private fun renderState(state: RoomListViewState) { - when (state.async) { + private fun renderState(state: HomeViewState) { + when (state.asyncRooms) { is Incomplete -> renderLoading() - is Success -> renderSuccess(state) - is Fail -> renderFailure(state.async.error) + is Success -> renderSuccess(state) + is Fail -> renderFailure(state.asyncRooms.error) } if (state.shouldOpenRoomDetail && state.selectedRoom != null) { homeNavigator.openRoomDetail(state.selectedRoom.roomId) - viewModel.accept(RoomListActions.RoomDisplayed) + viewModel.accept(HomeActions.RoomDisplayed) } } - private fun renderSuccess(state: RoomListViewState) { - if (state.async().isNullOrEmpty()) { + private fun renderSuccess(state: HomeViewState) { + if (state.asyncRooms().isNullOrEmpty()) { stateView.state = StateView.State.Empty(getString(R.string.room_list_empty)) } else { stateView.state = StateView.State.Content @@ -69,13 +72,13 @@ class RoomListFragment : RiotFragment(), RoomSummaryController.Callback { private fun renderFailure(error: Throwable) { val message = when (error) { is Failure.NetworkConnection -> getString(R.string.error_no_network) - else -> getString(R.string.error_common) + else -> getString(R.string.error_common) } stateView.state = StateView.State.Error(message) } override fun onRoomSelected(room: RoomSummary) { - viewModel.accept(RoomListActions.SelectRoom(room)) + viewModel.accept(HomeActions.SelectRoom(room)) } } \ No newline at end of file diff --git a/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListViewState.kt b/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListViewState.kt deleted file mode 100644 index 2e90da5134..0000000000 --- a/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListViewState.kt +++ /dev/null @@ -1,14 +0,0 @@ -package im.vector.riotredesign.features.home.room.list - -import com.airbnb.mvrx.Async -import com.airbnb.mvrx.MvRxState -import com.airbnb.mvrx.Uninitialized -import im.vector.matrix.android.api.session.room.model.RoomSummary - -data class RoomListViewState( - val async: Async> = Uninitialized, - val directRooms: List = emptyList(), - val groupRooms: List = emptyList(), - val selectedRoom: RoomSummary? = null, - val shouldOpenRoomDetail: Boolean = true -) : MvRxState \ No newline at end of file diff --git a/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomSummaryController.kt b/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomSummaryController.kt index fc9e425075..485fc1b3b4 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomSummaryController.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/room/list/RoomSummaryController.kt @@ -2,17 +2,16 @@ package im.vector.riotredesign.features.home.room.list import com.airbnb.epoxy.TypedEpoxyController import im.vector.matrix.android.api.session.room.model.RoomSummary -import im.vector.riotredesign.features.home.group.GroupListViewState -import im.vector.riotredesign.features.home.group.GroupSummaryItem +import im.vector.riotredesign.features.home.HomeViewState class RoomSummaryController(private val callback: Callback? = null -) : TypedEpoxyController() { +) : TypedEpoxyController() { private var isDirectRoomsExpanded = true private var isGroupRoomsExpanded = true - override fun buildModels(viewState: RoomListViewState) { + override fun buildModels(viewState: HomeViewState) { RoomCategoryItem( title = "DIRECT MESSAGES",