From fa297a7b6a025616f25e24bad6e28f4b63b66475 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 10 Jun 2019 16:53:21 +0200 Subject: [PATCH] Ganfra's review: use NavigationViewModel to open the drawer --- .../features/home/HomeActivity.kt | 22 ++++++++++++++----- .../features/home/HomeDetailFragment.kt | 7 +++++- .../features/home/HomeNavigationViewModel.kt | 21 ++++++++++++++++++ 3 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 vector/src/main/java/im/vector/riotredesign/features/home/HomeNavigationViewModel.kt diff --git a/vector/src/main/java/im/vector/riotredesign/features/home/HomeActivity.kt b/vector/src/main/java/im/vector/riotredesign/features/home/HomeActivity.kt index 9ec42de8d4..379b3373f8 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/home/HomeActivity.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/home/HomeActivity.kt @@ -27,10 +27,12 @@ import androidx.core.view.GravityCompat import androidx.drawerlayout.widget.DrawerLayout import androidx.fragment.app.FragmentManager import androidx.lifecycle.Observer +import androidx.lifecycle.ViewModelProviders import com.airbnb.mvrx.viewModel import im.vector.matrix.android.api.Matrix import im.vector.riotredesign.R import im.vector.riotredesign.core.extensions.hideKeyboard +import im.vector.riotredesign.core.extensions.observeEvent import im.vector.riotredesign.core.extensions.replaceFragment import im.vector.riotredesign.core.platform.OnBackPressed import im.vector.riotredesign.core.platform.ToolbarConfigurable @@ -46,7 +48,13 @@ import org.koin.android.scope.ext.android.getOrCreateScope class HomeActivity : VectorBaseActivity(), ToolbarConfigurable { + // Supported navigation actions for this Activity + sealed class Navigation { + object OpenDrawer : Navigation() + } + private val homeActivityViewModel: HomeActivityViewModel by viewModel() + private lateinit var navigationViewModel: HomeNavigationViewModel private val homeNavigator by inject() private var progress: ProgressDialog? = null @@ -63,6 +71,9 @@ class HomeActivity : VectorBaseActivity(), ToolbarConfigurable { super.onCreate(savedInstanceState) bindScope(getOrCreateScope(HomeModule.HOME_SCOPE)) homeNavigator.activity = this + + navigationViewModel = ViewModelProviders.of(this).get(HomeNavigationViewModel::class.java) + drawerLayout.addDrawerListener(drawerListener) if (isFirstCreation()) { val homeDrawerFragment = HomeDrawerFragment.newInstance() @@ -82,6 +93,12 @@ class HomeActivity : VectorBaseActivity(), ToolbarConfigurable { progress?.dismiss() } }) + + navigationViewModel.navigateTo.observeEvent(this) { navigation -> + when (navigation) { + is Navigation.OpenDrawer -> drawerLayout.openDrawer(GravityCompat.START) + } + } } override fun onDestroy() { @@ -122,11 +139,6 @@ class HomeActivity : VectorBaseActivity(), ToolbarConfigurable { return true } - // Called by HomeDetailFragment - fun openDrawer() { - drawerLayout.openDrawer(GravityCompat.START) - } - override fun onBackPressed() { if (drawerLayout.isDrawerOpen(GravityCompat.START)) { drawerLayout.closeDrawer(GravityCompat.START) diff --git a/vector/src/main/java/im/vector/riotredesign/features/home/HomeDetailFragment.kt b/vector/src/main/java/im/vector/riotredesign/features/home/HomeDetailFragment.kt index 25c38c233c..cd47df2d38 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/home/HomeDetailFragment.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/home/HomeDetailFragment.kt @@ -20,6 +20,7 @@ import android.os.Bundle import android.os.Parcelable import android.view.LayoutInflater import androidx.core.view.forEachIndexed +import androidx.lifecycle.ViewModelProviders import com.airbnb.mvrx.args import com.airbnb.mvrx.fragmentViewModel import com.airbnb.mvrx.withState @@ -56,6 +57,7 @@ class HomeDetailFragment : VectorBaseFragment() { private lateinit var currentDisplayMode: RoomListFragment.DisplayMode private val viewModel: HomeDetailViewModel by fragmentViewModel() + private lateinit var navigationViewModel: HomeNavigationViewModel override fun getLayoutResId(): Int { return R.layout.fragment_home_detail @@ -65,6 +67,9 @@ class HomeDetailFragment : VectorBaseFragment() { super.onActivityCreated(savedInstanceState) currentDisplayMode = savedInstanceState?.getSerializable(CURRENT_DISPLAY_MODE) as? RoomListFragment.DisplayMode ?: RoomListFragment.DisplayMode.HOME + + navigationViewModel = ViewModelProviders.of(requireActivity()).get(HomeNavigationViewModel::class.java) + switchDisplayMode(currentDisplayMode) setupBottomNavigationView() setupToolbar() @@ -89,7 +94,7 @@ class HomeDetailFragment : VectorBaseFragment() { groupToolbarAvatarImageView ) groupToolbarAvatarImageView.setOnClickListener { - (vectorBaseActivity as? HomeActivity)?.openDrawer() + navigationViewModel.goTo(HomeActivity.Navigation.OpenDrawer) } } diff --git a/vector/src/main/java/im/vector/riotredesign/features/home/HomeNavigationViewModel.kt b/vector/src/main/java/im/vector/riotredesign/features/home/HomeNavigationViewModel.kt new file mode 100644 index 0000000000..d922f3ae14 --- /dev/null +++ b/vector/src/main/java/im/vector/riotredesign/features/home/HomeNavigationViewModel.kt @@ -0,0 +1,21 @@ +/* + * 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.home + +import im.vector.riotredesign.core.mvrx.NavigationViewModel + +class HomeNavigationViewModel : NavigationViewModel() \ No newline at end of file