From e70a483d6b165bca881f701b2b9c7f1c45b0e052 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 28 May 2019 21:12:09 +0200 Subject: [PATCH] Introduce Navigator --- .../vector/riotredesign/core/di/AppModule.kt | 7 +++ .../core/platform/VectorBaseActivity.kt | 2 +- .../core/platform/VectorBaseFragment.kt | 10 +++- .../features/home/HomeDrawerFragment.kt | 2 +- .../riotredesign/features/home/HomeModule.kt | 2 +- .../features/home/HomeNavigator.kt | 10 ++-- .../features/home/HomePermalinkHandler.kt | 12 +++-- .../home/room/list/RoomListFragment.kt | 7 +-- .../features/navigation/DefaultNavigator.kt | 53 +++++++++++++++++++ .../features/navigation/Navigator.kt | 31 +++++++++++ .../roomdirectory/PublicRoomsFragment.kt | 8 +-- .../RoomPreviewNoPreviewFragment.kt | 6 ++- 12 files changed, 122 insertions(+), 28 deletions(-) create mode 100644 vector/src/main/java/im/vector/riotredesign/features/navigation/DefaultNavigator.kt create mode 100644 vector/src/main/java/im/vector/riotredesign/features/navigation/Navigator.kt diff --git a/vector/src/main/java/im/vector/riotredesign/core/di/AppModule.kt b/vector/src/main/java/im/vector/riotredesign/core/di/AppModule.kt index c0a4f9cff0..dc7cf99c58 100644 --- a/vector/src/main/java/im/vector/riotredesign/core/di/AppModule.kt +++ b/vector/src/main/java/im/vector/riotredesign/core/di/AppModule.kt @@ -18,6 +18,7 @@ package im.vector.riotredesign.core.di import android.content.Context import android.content.Context.MODE_PRIVATE +import androidx.fragment.app.Fragment import im.vector.matrix.android.api.Matrix import im.vector.riotredesign.core.error.ErrorFormatter import im.vector.riotredesign.core.resources.LocaleProvider @@ -27,6 +28,8 @@ import im.vector.riotredesign.features.home.HomeRoomListObservableStore import im.vector.riotredesign.features.home.group.SelectedGroupStore import im.vector.riotredesign.features.home.room.list.AlphabeticalRoomComparator import im.vector.riotredesign.features.home.room.list.ChronologicalRoomComparator +import im.vector.riotredesign.features.navigation.DefaultNavigator +import im.vector.riotredesign.features.navigation.Navigator import im.vector.riotredesign.features.notifications.NotificationDrawerManager import org.koin.dsl.module.module @@ -78,5 +81,9 @@ class AppModule(private val context: Context) { Matrix.getInstance().currentSession!! } + factory { (fragment: Fragment) -> + DefaultNavigator(fragment) as Navigator + } + } } \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/core/platform/VectorBaseActivity.kt b/vector/src/main/java/im/vector/riotredesign/core/platform/VectorBaseActivity.kt index 1629d7b7ed..29d49f967e 100644 --- a/vector/src/main/java/im/vector/riotredesign/core/platform/VectorBaseActivity.kt +++ b/vector/src/main/java/im/vector/riotredesign/core/platform/VectorBaseActivity.kt @@ -294,7 +294,7 @@ abstract class VectorBaseActivity : BaseMvRxActivity() { * ========================================================================================== */ fun notImplemented(message: String = "") { - toast(getString(R.string.not_implemented) + message.takeIf { message.isNotBlank() }.run { ": $message" }) + toast(getString(R.string.not_implemented) + message.takeIf { message.isNotBlank() }?.let { ": $it" }) } } \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/core/platform/VectorBaseFragment.kt b/vector/src/main/java/im/vector/riotredesign/core/platform/VectorBaseFragment.kt index 68a0a643b9..ddfad8cc1f 100644 --- a/vector/src/main/java/im/vector/riotredesign/core/platform/VectorBaseFragment.kt +++ b/vector/src/main/java/im/vector/riotredesign/core/platform/VectorBaseFragment.kt @@ -28,9 +28,11 @@ import butterknife.Unbinder import com.airbnb.mvrx.BaseMvRxFragment import com.airbnb.mvrx.MvRx import com.bumptech.glide.util.Util.assertMainThread -import com.google.android.material.snackbar.Snackbar +import im.vector.riotredesign.features.navigation.Navigator import io.reactivex.disposables.CompositeDisposable import io.reactivex.disposables.Disposable +import org.koin.android.ext.android.inject +import org.koin.core.parameter.parametersOf import timber.log.Timber abstract class VectorBaseFragment : BaseMvRxFragment(), OnBackPressed { @@ -42,6 +44,12 @@ abstract class VectorBaseFragment : BaseMvRxFragment(), OnBackPressed { activity as VectorBaseActivity } + /* ========================================================================================== + * Navigator + * ========================================================================================== */ + + protected val navigator: Navigator by inject { parametersOf(this) } + /* ========================================================================================== * Life cycle * ========================================================================================== */ diff --git a/vector/src/main/java/im/vector/riotredesign/features/home/HomeDrawerFragment.kt b/vector/src/main/java/im/vector/riotredesign/features/home/HomeDrawerFragment.kt index f32216db27..658dba4315 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/home/HomeDrawerFragment.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/home/HomeDrawerFragment.kt @@ -52,7 +52,7 @@ class HomeDrawerFragment : VectorBaseFragment() { } } homeDrawerHeaderSettingsView.setOnClickListener { - startActivity(VectorSettingsActivity.getIntent(requireContext(), "TODO")) + navigator.openSettings() } } } \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/features/home/HomeModule.kt b/vector/src/main/java/im/vector/riotredesign/features/home/HomeModule.kt index 8e06637ff3..cd57800e4e 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/home/HomeModule.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/home/HomeModule.kt @@ -53,7 +53,7 @@ class HomeModule { } scope(HOME_SCOPE) { - HomePermalinkHandler(get()) + HomePermalinkHandler(get(), get()) } // Fragment scopes diff --git a/vector/src/main/java/im/vector/riotredesign/features/home/HomeNavigator.kt b/vector/src/main/java/im/vector/riotredesign/features/home/HomeNavigator.kt index 05a2bd2511..8d29c0ab8e 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/home/HomeNavigator.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/home/HomeNavigator.kt @@ -21,8 +21,7 @@ import androidx.fragment.app.FragmentManager import im.vector.matrix.android.api.session.group.model.GroupSummary import im.vector.riotredesign.R import im.vector.riotredesign.core.extensions.replaceFragment -import im.vector.riotredesign.features.home.room.detail.RoomDetailActivity -import im.vector.riotredesign.features.home.room.detail.RoomDetailArgs +import im.vector.riotredesign.features.navigation.Navigator import kotlinx.android.synthetic.main.activity_home.* import timber.log.Timber @@ -43,14 +42,13 @@ class HomeNavigator { } fun openRoomDetail(roomId: String, - eventId: String?) { + eventId: String?, + navigator: Navigator) { Timber.v("Open room detail $roomId - $eventId") activity?.let { //TODO enable eventId permalink. It doesn't work enough at the moment. it.drawerLayout?.closeDrawer(GravityCompat.START) - val args = RoomDetailArgs(roomId) - val roomDetailIntent = RoomDetailActivity.newIntent(it, args) - it.startActivity(roomDetailIntent) + navigator.openRoom(roomId) } } diff --git a/vector/src/main/java/im/vector/riotredesign/features/home/HomePermalinkHandler.kt b/vector/src/main/java/im/vector/riotredesign/features/home/HomePermalinkHandler.kt index f2a42a3310..dc387aa5b0 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/home/HomePermalinkHandler.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/home/HomePermalinkHandler.kt @@ -19,8 +19,10 @@ 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.navigation.Navigator -class HomePermalinkHandler(private val navigator: HomeNavigator) { +class HomePermalinkHandler(private val homeNavigator: HomeNavigator, + private val navigator: Navigator) { fun launch(deepLink: String?) { val uri = deepLink?.let { Uri.parse(it) } @@ -34,16 +36,16 @@ class HomePermalinkHandler(private val navigator: HomeNavigator) { val permalinkData = PermalinkParser.parse(deepLink) when (permalinkData) { is PermalinkData.EventLink -> { - navigator.openRoomDetail(permalinkData.roomIdOrAlias, permalinkData.eventId) + homeNavigator.openRoomDetail(permalinkData.roomIdOrAlias, permalinkData.eventId, navigator) } is PermalinkData.RoomLink -> { - navigator.openRoomDetail(permalinkData.roomIdOrAlias, null) + homeNavigator.openRoomDetail(permalinkData.roomIdOrAlias, null, navigator) } is PermalinkData.GroupLink -> { - navigator.openGroupDetail(permalinkData.groupId) + homeNavigator.openGroupDetail(permalinkData.groupId) } is PermalinkData.UserLink -> { - navigator.openUserDetail(permalinkData.userId) + homeNavigator.openUserDetail(permalinkData.userId) } is PermalinkData.FallbackLink -> { diff --git a/vector/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListFragment.kt b/vector/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListFragment.kt index 0ac135d80b..88b048b758 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListFragment.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListFragment.kt @@ -29,7 +29,6 @@ import im.vector.riotredesign.core.epoxy.LayoutManagerStateRestorer import im.vector.riotredesign.core.extensions.observeEvent import im.vector.riotredesign.core.platform.StateView import im.vector.riotredesign.core.platform.VectorBaseFragment -import im.vector.riotredesign.features.home.HomeNavigator import im.vector.riotredesign.features.roomdirectory.RoomDirectoryActivity import kotlinx.android.parcel.Parcelize import kotlinx.android.synthetic.main.fragment_room_list.* @@ -59,7 +58,6 @@ class RoomListFragment : VectorBaseFragment(), RoomSummaryController.Callback { private val roomListParams: RoomListParams by args() private val roomController by inject() - private val homeNavigator by inject() private val roomListViewModel: RoomListViewModel by fragmentViewModel() override fun getLayoutResId() = R.layout.fragment_room_list @@ -70,15 +68,14 @@ class RoomListFragment : VectorBaseFragment(), RoomSummaryController.Callback { setupCreateRoomButton() roomListViewModel.subscribe { renderState(it) } roomListViewModel.openRoomLiveData.observeEvent(this) { - homeNavigator.openRoomDetail(it, null) + navigator.openRoom(it) } } private fun setupCreateRoomButton() { createRoomButton.setImageResource(R.drawable.ic_add_white) createRoomButton.setOnClickListener { - // Start Activity for now - startActivity(Intent(requireActivity(), RoomDirectoryActivity::class.java)) + navigator.openRoomDirectory() } } diff --git a/vector/src/main/java/im/vector/riotredesign/features/navigation/DefaultNavigator.kt b/vector/src/main/java/im/vector/riotredesign/features/navigation/DefaultNavigator.kt new file mode 100644 index 0000000000..34c774a37a --- /dev/null +++ b/vector/src/main/java/im/vector/riotredesign/features/navigation/DefaultNavigator.kt @@ -0,0 +1,53 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.riotredesign.features.navigation + +import android.app.Activity +import android.content.Intent +import androidx.fragment.app.Fragment +import im.vector.matrix.android.api.session.room.model.roomdirectory.PublicRoom +import im.vector.riotredesign.features.home.room.detail.RoomDetailActivity +import im.vector.riotredesign.features.home.room.detail.RoomDetailArgs +import im.vector.riotredesign.features.roomdirectory.RoomDirectoryActivity +import im.vector.riotredesign.features.roomdirectory.roompreview.RoomPreviewActivity +import im.vector.riotredesign.features.settings.VectorSettingsActivity + +class DefaultNavigator(private val fraqment: Fragment) : Navigator { + + val activity: Activity = fraqment.requireActivity() + + override fun openRoom(roomId: String) { + val args = RoomDetailArgs(roomId) + val intent = RoomDetailActivity.newIntent(activity, args) + activity.startActivity(intent) + } + + override fun openRoomPreview(publicRoom: PublicRoom) { + val intent = RoomPreviewActivity.getIntent(activity, publicRoom) + activity.startActivity(intent) + } + + override fun openRoomDirectory() { + val intent = Intent(activity, RoomDirectoryActivity::class.java) + activity.startActivity(intent) + } + + override fun openSettings() { + val intent = VectorSettingsActivity.getIntent(activity, "TODO") + activity.startActivity(intent) + } +} \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/features/navigation/Navigator.kt b/vector/src/main/java/im/vector/riotredesign/features/navigation/Navigator.kt new file mode 100644 index 0000000000..a47bbaa3d4 --- /dev/null +++ b/vector/src/main/java/im/vector/riotredesign/features/navigation/Navigator.kt @@ -0,0 +1,31 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.riotredesign.features.navigation + +import im.vector.matrix.android.api.session.room.model.roomdirectory.PublicRoom + +interface Navigator { + + fun openRoom(roomId: String) + + fun openRoomPreview(publicRoom: PublicRoom) + + fun openRoomDirectory() + + fun openSettings() + +} \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/features/roomdirectory/PublicRoomsFragment.kt b/vector/src/main/java/im/vector/riotredesign/features/roomdirectory/PublicRoomsFragment.kt index d92af320c8..fea9a5a534 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/roomdirectory/PublicRoomsFragment.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/roomdirectory/PublicRoomsFragment.kt @@ -30,8 +30,6 @@ import im.vector.riotredesign.R import im.vector.riotredesign.core.error.ErrorFormatter import im.vector.riotredesign.core.extensions.addFragmentToBackstack import im.vector.riotredesign.core.platform.VectorBaseFragment -import im.vector.riotredesign.features.home.room.detail.RoomDetailActivity -import im.vector.riotredesign.features.home.room.detail.RoomDetailArgs import im.vector.riotredesign.features.roomdirectory.picker.RoomDirectoryPickerFragment import im.vector.riotredesign.features.roomdirectory.roompreview.RoomPreviewActivity import io.reactivex.rxkotlin.subscribeBy @@ -122,14 +120,12 @@ class PublicRoomsFragment : VectorBaseFragment(), PublicRoomsController.Callback when (joinState) { JoinState.JOINED -> { - val args = RoomDetailArgs(publicRoom.roomId) - val roomDetailIntent = RoomDetailActivity.newIntent(requireActivity(), args) - requireActivity().startActivity(roomDetailIntent) + navigator.openRoom(publicRoom.roomId) } JoinState.NOT_JOINED, JoinState.JOINING_ERROR -> { // ROOM PREVIEW - requireActivity().startActivity(RoomPreviewActivity.getIntent(requireActivity(), publicRoom)) + navigator.openRoomPreview(publicRoom) } else -> { Snackbar.make(publicRoomsCoordinator, getString(R.string.please_wait), Snackbar.LENGTH_SHORT) diff --git a/vector/src/main/java/im/vector/riotredesign/features/roomdirectory/roompreview/RoomPreviewNoPreviewFragment.kt b/vector/src/main/java/im/vector/riotredesign/features/roomdirectory/roompreview/RoomPreviewNoPreviewFragment.kt index bc119dcae1..55030df08e 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/roomdirectory/roompreview/RoomPreviewNoPreviewFragment.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/roomdirectory/roompreview/RoomPreviewNoPreviewFragment.kt @@ -93,8 +93,10 @@ class RoomPreviewNoPreviewFragment : VectorBaseFragment() { roomPreviewNoPreviewError.setTextOrHide(errorFormatter.toHumanReadable(state.lastError)) if (state.roomJoinState == JoinState.JOINED) { - // TODO Quit this screen and open the room - vectorBaseActivity.notImplemented("Open newly join room") + // Quit this screen + requireActivity().finish() + // Open room + navigator.openRoom(roomPreviewData.roomId) } } } \ No newline at end of file