From c42be8141ea61552b49e2dca8202e6a5b1394121 Mon Sep 17 00:00:00 2001 From: ganfra Date: Thu, 27 Dec 2018 19:32:52 +0100 Subject: [PATCH] Iterate on navigation... We have to think about backstack, maybe using a finite state machine. WIP --- .../features/RootFlowCoordinator.kt | 4 ++ .../features/home/HomeFlowCoordinator.kt | 4 ++ .../home/HomePermalinkHandler.kt} | 7 ++- .../features/home/HomeViewModel.kt | 45 ++++++++++++++----- .../features/home/HomeViewState.kt | 3 +- .../home/room/detail/RoomDetailFragment.kt | 7 ++- .../home/room/list/RoomListFragment.kt | 4 +- .../home/room/list/RoomSummaryController.kt | 8 ++-- .../features/login/LoginFlowCoordinator.kt | 2 + 9 files changed, 60 insertions(+), 24 deletions(-) create mode 100644 app/src/main/java/im/vector/riotredesign/features/RootFlowCoordinator.kt create mode 100644 app/src/main/java/im/vector/riotredesign/features/home/HomeFlowCoordinator.kt rename app/src/main/java/im/vector/riotredesign/{core/HomePermalinkNavigator.kt => features/home/HomePermalinkHandler.kt} (85%) create mode 100644 app/src/main/java/im/vector/riotredesign/features/login/LoginFlowCoordinator.kt diff --git a/app/src/main/java/im/vector/riotredesign/features/RootFlowCoordinator.kt b/app/src/main/java/im/vector/riotredesign/features/RootFlowCoordinator.kt new file mode 100644 index 00000000..2c5fc206 --- /dev/null +++ b/app/src/main/java/im/vector/riotredesign/features/RootFlowCoordinator.kt @@ -0,0 +1,4 @@ +package im.vector.riotredesign.features + +class RootFlowCoordinator { +} \ No newline at end of file diff --git a/app/src/main/java/im/vector/riotredesign/features/home/HomeFlowCoordinator.kt b/app/src/main/java/im/vector/riotredesign/features/home/HomeFlowCoordinator.kt new file mode 100644 index 00000000..331b22fb --- /dev/null +++ b/app/src/main/java/im/vector/riotredesign/features/home/HomeFlowCoordinator.kt @@ -0,0 +1,4 @@ +package im.vector.riotredesign.features.home + +class HomeFlowCoordinator { +} \ No newline at end of file diff --git a/app/src/main/java/im/vector/riotredesign/core/HomePermalinkNavigator.kt b/app/src/main/java/im/vector/riotredesign/features/home/HomePermalinkHandler.kt similarity index 85% rename from app/src/main/java/im/vector/riotredesign/core/HomePermalinkNavigator.kt rename to app/src/main/java/im/vector/riotredesign/features/home/HomePermalinkHandler.kt index dfa8b977..b4809b16 100644 --- a/app/src/main/java/im/vector/riotredesign/core/HomePermalinkNavigator.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/HomePermalinkHandler.kt @@ -1,11 +1,10 @@ -package im.vector.riotredesign.core +package im.vector.riotredesign.features.home import android.net.Uri import im.vector.matrix.android.api.permalinks.PermalinkData import im.vector.matrix.android.api.permalinks.PermalinkParser -import im.vector.riotredesign.features.home.HomeNavigator -class HomePermalinkNavigator(private val navigator: HomeNavigator) { +class HomePermalinkHandler(private val navigator: HomeNavigator) { fun launch(deepLink: String?) { val uri = deepLink?.let { Uri.parse(it) } @@ -22,7 +21,7 @@ class HomePermalinkNavigator(private val navigator: HomeNavigator) { navigator.openRoomDetail(permalinkData.roomIdOrAlias, permalinkData.eventId) } is PermalinkData.RoomLink -> { - navigator.openRoomDetail(permalinkData.roomIdOrAlias, null ) + navigator.openRoomDetail(permalinkData.roomIdOrAlias, null) } is PermalinkData.GroupLink -> { navigator.openGroupDetail(permalinkData.groupId) diff --git a/app/src/main/java/im/vector/riotredesign/features/home/HomeViewModel.kt b/app/src/main/java/im/vector/riotredesign/features/home/HomeViewModel.kt index 6d16d1d8..3e7d437d 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/HomeViewModel.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/HomeViewModel.kt @@ -4,6 +4,7 @@ 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.permalinks.PermalinkData import im.vector.matrix.android.api.session.Session import im.vector.matrix.rx.rx @@ -25,19 +26,42 @@ class HomeViewModel(initialState: HomeViewState, private val session: Session) : fun accept(action: HomeActions) { when (action) { - is HomeActions.SelectRoom -> handleSelectRoom(action) - is HomeActions.SelectGroup -> handleSelectGroup(action) - is HomeActions.RoomDisplayed -> setState { copy(shouldOpenRoomDetail = false) } + is HomeActions.SelectRoom -> handleSelectRoom(action) + is HomeActions.SelectGroup -> handleSelectGroup(action) + is HomeActions.RoomDisplayed -> setState { copy(shouldOpenRoomDetail = false) } + is HomeActions.PermalinkClicked -> handlePermalinkClicked(action) } } // PRIVATE METHODS ***************************************************************************** + private fun handlePermalinkClicked(action: HomeActions.PermalinkClicked) { + withState { state -> + when (action.permalinkData) { + is PermalinkData.EventLink -> { + + } + is PermalinkData.RoomLink -> { + + } + is PermalinkData.GroupLink -> { + + } + is PermalinkData.UserLink -> { + + } + is PermalinkData.FallbackLink -> { + + } + } + } + } + private fun handleSelectRoom(action: HomeActions.SelectRoom) { withState { state -> - if (state.selectedRoom?.roomId != action.roomSummary.roomId) { + if (state.selectedRoomId != action.roomSummary.roomId) { session.saveLastSelectedRoom(action.roomSummary) - setState { copy(selectedRoom = action.roomSummary, shouldOpenRoomDetail = true) } + setState { copy(selectedRoomId = action.roomSummary.roomId, shouldOpenRoomDetail = true) } } } } @@ -56,21 +80,20 @@ class HomeViewModel(initialState: HomeViewState, private val session: Session) : session .rx().liveRoomSummaries() .execute { async -> - val summaries = async() val directRooms = summaries?.filter { it.isDirect } ?: emptyList() val groupRooms = summaries?.filter { !it.isDirect } ?: emptyList() - val selectedRoom = selectedRoom - ?: session.lastSelectedRoom() - ?: directRooms.firstOrNull() - ?: groupRooms.firstOrNull() + val selectedRoomId = selectedRoomId + ?: session.lastSelectedRoom()?.roomId + ?: directRooms.firstOrNull()?.roomId + ?: groupRooms.firstOrNull()?.roomId copy( asyncRooms = async, directRooms = directRooms, groupRooms = groupRooms, - selectedRoom = selectedRoom + selectedRoomId = selectedRoomId ) } } 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 index 01c3758e..49211b3d 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/HomeViewState.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/HomeViewState.kt @@ -10,7 +10,8 @@ data class HomeViewState( val asyncRooms: Async> = Uninitialized, val directRooms: List = emptyList(), val groupRooms: List = emptyList(), - val selectedRoom: RoomSummary? = null, + val selectedRoomId: String? = null, + val selectedEventId: String? = null, val shouldOpenRoomDetail: Boolean = true, val asyncGroups: Async> = Uninitialized, val selectedGroup: GroupSummary? = null diff --git a/app/src/main/java/im/vector/riotredesign/features/home/room/detail/RoomDetailFragment.kt b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/RoomDetailFragment.kt index 7410c13c..a9acc877 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/room/detail/RoomDetailFragment.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/RoomDetailFragment.kt @@ -7,6 +7,7 @@ import android.support.v7.widget.LinearLayoutManager import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import com.airbnb.mvrx.activityViewModel import im.vector.matrix.android.api.Matrix import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.permalinks.PermalinkParser @@ -20,11 +21,12 @@ import im.vector.riotredesign.core.platform.ToolbarConfigurable import im.vector.riotredesign.core.utils.FragmentArgumentDelegate import im.vector.riotredesign.core.utils.UnsafeFragmentArgumentDelegate import im.vector.riotredesign.features.home.AvatarRenderer +import im.vector.riotredesign.features.home.HomeActions +import im.vector.riotredesign.features.home.HomeViewModel import im.vector.riotredesign.features.home.room.detail.timeline.TimelineEventController import kotlinx.android.synthetic.main.fragment_room_detail.* import org.koin.android.ext.android.inject import org.koin.core.parameter.parametersOf -import timber.log.Timber class RoomDetailFragment : RiotFragment(), TimelineEventController.Callback { @@ -38,6 +40,7 @@ class RoomDetailFragment : RiotFragment(), TimelineEventController.Callback { } } + private val viewModel: HomeViewModel by activityViewModel() private val currentSession = Matrix.getInstance().currentSession private var roomId: String by UnsafeFragmentArgumentDelegate() private var eventId: String? by FragmentArgumentDelegate() @@ -106,7 +109,7 @@ class RoomDetailFragment : RiotFragment(), TimelineEventController.Callback { override fun onUrlClicked(url: String) { val permalinkData = PermalinkParser.parse(url) - Timber.v("Permalink data : $permalinkData") + viewModel.accept(HomeActions.PermalinkClicked(permalinkData)) } } 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 11cbd7d6..4830ba8d 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 @@ -50,8 +50,8 @@ class RoomListFragment : RiotFragment(), RoomSummaryController.Callback { is Success -> renderSuccess(state) is Fail -> renderFailure(state.asyncRooms.error) } - if (state.shouldOpenRoomDetail && state.selectedRoom != null) { - homeNavigator.openRoomDetail(state.selectedRoom.roomId, null) + if (state.shouldOpenRoomDetail && state.selectedRoomId != null) { + homeNavigator.openRoomDetail(state.selectedRoomId, null) viewModel.accept(HomeActions.RoomDisplayed) } } 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 6aeb893c..998e59fd 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 @@ -34,7 +34,7 @@ class RoomSummaryController(private val callback: Callback? = null .isNotEmpty() } } - buildRoomModels(filteredDirectRooms, viewState.selectedRoom) + buildRoomModels(filteredDirectRooms, viewState.selectedRoomId) } RoomCategoryItem( @@ -52,14 +52,14 @@ class RoomSummaryController(private val callback: Callback? = null val filteredGroupRooms = viewState.groupRooms.filter { viewState.selectedGroup?.roomIds?.contains(it.roomId) ?: true } - buildRoomModels(filteredGroupRooms, viewState.selectedRoom) + buildRoomModels(filteredGroupRooms, viewState.selectedRoomId) } } - private fun buildRoomModels(summaries: List, selected: RoomSummary?) { + private fun buildRoomModels(summaries: List, selectedRoomId: String?) { summaries.forEach { roomSummary -> - val isSelected = roomSummary.roomId == selected?.roomId + val isSelected = roomSummary.roomId == selectedRoomId RoomSummaryItem( roomName = roomSummary.displayName, avatarUrl = roomSummary.avatarUrl, diff --git a/app/src/main/java/im/vector/riotredesign/features/login/LoginFlowCoordinator.kt b/app/src/main/java/im/vector/riotredesign/features/login/LoginFlowCoordinator.kt new file mode 100644 index 00000000..147879d1 --- /dev/null +++ b/app/src/main/java/im/vector/riotredesign/features/login/LoginFlowCoordinator.kt @@ -0,0 +1,2 @@ +package im.vector.riotredesign.features.login +